2 * wm8350.c -- WM8350 ALSA SoC audio driver
4 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/wm8350/audio.h>
21 #include <linux/mfd/wm8350/core.h>
22 #include <linux/regulator/consumer.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <trace/events/asoc.h>
33 #define WM8350_OUTn_0dB 0x39
35 #define WM8350_RAMP_NONE 0
36 #define WM8350_RAMP_UP 1
37 #define WM8350_RAMP_DOWN 2
39 /* We only include the analogue supplies here; the digital supplies
40 * need to be available well before this driver can be probed.
42 static const char *supply_names
[] = {
47 struct wm8350_output
{
55 struct wm8350_jack_data
{
56 struct snd_soc_jack
*jack
;
57 struct delayed_work work
;
63 struct snd_soc_codec codec
;
64 struct wm8350_output out1
;
65 struct wm8350_output out2
;
66 struct wm8350_jack_data hpl
;
67 struct wm8350_jack_data hpr
;
68 struct wm8350_jack_data mic
;
69 struct regulator_bulk_data supplies
[ARRAY_SIZE(supply_names
)];
74 static unsigned int wm8350_codec_cache_read(struct snd_soc_codec
*codec
,
77 struct wm8350
*wm8350
= codec
->control_data
;
78 return wm8350
->reg_cache
[reg
];
81 static unsigned int wm8350_codec_read(struct snd_soc_codec
*codec
,
84 struct wm8350
*wm8350
= codec
->control_data
;
85 return wm8350_reg_read(wm8350
, reg
);
88 static int wm8350_codec_write(struct snd_soc_codec
*codec
, unsigned int reg
,
91 struct wm8350
*wm8350
= codec
->control_data
;
92 return wm8350_reg_write(wm8350
, reg
, value
);
96 * Ramp OUT1 PGA volume to minimise pops at stream startup and shutdown.
98 static inline int wm8350_out1_ramp_step(struct snd_soc_codec
*codec
)
100 struct wm8350_data
*wm8350_data
= snd_soc_codec_get_drvdata(codec
);
101 struct wm8350_output
*out1
= &wm8350_data
->out1
;
102 struct wm8350
*wm8350
= codec
->control_data
;
103 int left_complete
= 0, right_complete
= 0;
107 reg
= wm8350_reg_read(wm8350
, WM8350_LOUT1_VOLUME
);
108 val
= (reg
& WM8350_OUT1L_VOL_MASK
) >> WM8350_OUT1L_VOL_SHIFT
;
110 if (out1
->ramp
== WM8350_RAMP_UP
) {
112 if (val
< out1
->left_vol
) {
114 reg
&= ~WM8350_OUT1L_VOL_MASK
;
115 wm8350_reg_write(wm8350
, WM8350_LOUT1_VOLUME
,
116 reg
| (val
<< WM8350_OUT1L_VOL_SHIFT
));
119 } else if (out1
->ramp
== WM8350_RAMP_DOWN
) {
123 reg
&= ~WM8350_OUT1L_VOL_MASK
;
124 wm8350_reg_write(wm8350
, WM8350_LOUT1_VOLUME
,
125 reg
| (val
<< WM8350_OUT1L_VOL_SHIFT
));
132 reg
= wm8350_reg_read(wm8350
, WM8350_ROUT1_VOLUME
);
133 val
= (reg
& WM8350_OUT1R_VOL_MASK
) >> WM8350_OUT1R_VOL_SHIFT
;
134 if (out1
->ramp
== WM8350_RAMP_UP
) {
136 if (val
< out1
->right_vol
) {
138 reg
&= ~WM8350_OUT1R_VOL_MASK
;
139 wm8350_reg_write(wm8350
, WM8350_ROUT1_VOLUME
,
140 reg
| (val
<< WM8350_OUT1R_VOL_SHIFT
));
143 } else if (out1
->ramp
== WM8350_RAMP_DOWN
) {
147 reg
&= ~WM8350_OUT1R_VOL_MASK
;
148 wm8350_reg_write(wm8350
, WM8350_ROUT1_VOLUME
,
149 reg
| (val
<< WM8350_OUT1R_VOL_SHIFT
));
154 /* only hit the update bit if either volume has changed this step */
155 if (!left_complete
|| !right_complete
)
156 wm8350_set_bits(wm8350
, WM8350_LOUT1_VOLUME
, WM8350_OUT1_VU
);
158 return left_complete
& right_complete
;
162 * Ramp OUT2 PGA volume to minimise pops at stream startup and shutdown.
164 static inline int wm8350_out2_ramp_step(struct snd_soc_codec
*codec
)
166 struct wm8350_data
*wm8350_data
= snd_soc_codec_get_drvdata(codec
);
167 struct wm8350_output
*out2
= &wm8350_data
->out2
;
168 struct wm8350
*wm8350
= codec
->control_data
;
169 int left_complete
= 0, right_complete
= 0;
173 reg
= wm8350_reg_read(wm8350
, WM8350_LOUT2_VOLUME
);
174 val
= (reg
& WM8350_OUT2L_VOL_MASK
) >> WM8350_OUT1L_VOL_SHIFT
;
175 if (out2
->ramp
== WM8350_RAMP_UP
) {
177 if (val
< out2
->left_vol
) {
179 reg
&= ~WM8350_OUT2L_VOL_MASK
;
180 wm8350_reg_write(wm8350
, WM8350_LOUT2_VOLUME
,
181 reg
| (val
<< WM8350_OUT1L_VOL_SHIFT
));
184 } else if (out2
->ramp
== WM8350_RAMP_DOWN
) {
188 reg
&= ~WM8350_OUT2L_VOL_MASK
;
189 wm8350_reg_write(wm8350
, WM8350_LOUT2_VOLUME
,
190 reg
| (val
<< WM8350_OUT1L_VOL_SHIFT
));
197 reg
= wm8350_reg_read(wm8350
, WM8350_ROUT2_VOLUME
);
198 val
= (reg
& WM8350_OUT2R_VOL_MASK
) >> WM8350_OUT1R_VOL_SHIFT
;
199 if (out2
->ramp
== WM8350_RAMP_UP
) {
201 if (val
< out2
->right_vol
) {
203 reg
&= ~WM8350_OUT2R_VOL_MASK
;
204 wm8350_reg_write(wm8350
, WM8350_ROUT2_VOLUME
,
205 reg
| (val
<< WM8350_OUT1R_VOL_SHIFT
));
208 } else if (out2
->ramp
== WM8350_RAMP_DOWN
) {
212 reg
&= ~WM8350_OUT2R_VOL_MASK
;
213 wm8350_reg_write(wm8350
, WM8350_ROUT2_VOLUME
,
214 reg
| (val
<< WM8350_OUT1R_VOL_SHIFT
));
219 /* only hit the update bit if either volume has changed this step */
220 if (!left_complete
|| !right_complete
)
221 wm8350_set_bits(wm8350
, WM8350_LOUT2_VOLUME
, WM8350_OUT2_VU
);
223 return left_complete
& right_complete
;
227 * This work ramps both output PGAs at stream start/stop time to
228 * minimise pop associated with DAPM power switching.
229 * It's best to enable Zero Cross when ramping occurs to minimise any
232 static void wm8350_pga_work(struct work_struct
*work
)
234 struct snd_soc_dapm_context
*dapm
=
235 container_of(work
, struct snd_soc_dapm_context
, delayed_work
.work
);
236 struct snd_soc_codec
*codec
= dapm
->codec
;
237 struct wm8350_data
*wm8350_data
= snd_soc_codec_get_drvdata(codec
);
238 struct wm8350_output
*out1
= &wm8350_data
->out1
,
239 *out2
= &wm8350_data
->out2
;
240 int i
, out1_complete
, out2_complete
;
242 /* do we need to ramp at all ? */
243 if (out1
->ramp
== WM8350_RAMP_NONE
&& out2
->ramp
== WM8350_RAMP_NONE
)
246 /* PGA volumes have 6 bits of resolution to ramp */
247 for (i
= 0; i
<= 63; i
++) {
248 out1_complete
= 1, out2_complete
= 1;
249 if (out1
->ramp
!= WM8350_RAMP_NONE
)
250 out1_complete
= wm8350_out1_ramp_step(codec
);
251 if (out2
->ramp
!= WM8350_RAMP_NONE
)
252 out2_complete
= wm8350_out2_ramp_step(codec
);
254 /* ramp finished ? */
255 if (out1_complete
&& out2_complete
)
258 /* we need to delay longer on the up ramp */
259 if (out1
->ramp
== WM8350_RAMP_UP
||
260 out2
->ramp
== WM8350_RAMP_UP
) {
261 /* delay is longer over 0dB as increases are larger */
262 if (i
>= WM8350_OUTn_0dB
)
263 schedule_timeout_interruptible(msecs_to_jiffies
266 schedule_timeout_interruptible(msecs_to_jiffies
269 udelay(50); /* doesn't matter if we delay longer */
272 out1
->ramp
= WM8350_RAMP_NONE
;
273 out2
->ramp
= WM8350_RAMP_NONE
;
280 static int pga_event(struct snd_soc_dapm_widget
*w
,
281 struct snd_kcontrol
*kcontrol
, int event
)
283 struct snd_soc_codec
*codec
= w
->codec
;
284 struct wm8350_data
*wm8350_data
= snd_soc_codec_get_drvdata(codec
);
285 struct wm8350_output
*out
;
290 out
= &wm8350_data
->out1
;
294 out
= &wm8350_data
->out2
;
303 case SND_SOC_DAPM_POST_PMU
:
304 out
->ramp
= WM8350_RAMP_UP
;
307 if (!delayed_work_pending(&codec
->dapm
.delayed_work
))
308 schedule_delayed_work(&codec
->dapm
.delayed_work
,
309 msecs_to_jiffies(1));
312 case SND_SOC_DAPM_PRE_PMD
:
313 out
->ramp
= WM8350_RAMP_DOWN
;
316 if (!delayed_work_pending(&codec
->dapm
.delayed_work
))
317 schedule_delayed_work(&codec
->dapm
.delayed_work
,
318 msecs_to_jiffies(1));
325 static int wm8350_put_volsw_2r_vu(struct snd_kcontrol
*kcontrol
,
326 struct snd_ctl_elem_value
*ucontrol
)
328 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
329 struct wm8350_data
*wm8350_priv
= snd_soc_codec_get_drvdata(codec
);
330 struct wm8350_output
*out
= NULL
;
331 struct soc_mixer_control
*mc
=
332 (struct soc_mixer_control
*)kcontrol
->private_value
;
334 unsigned int reg
= mc
->reg
;
337 /* For OUT1 and OUT2 we shadow the values and only actually write
338 * them out when active in order to ensure the amplifier comes on
339 * as quietly as possible. */
341 case WM8350_LOUT1_VOLUME
:
342 out
= &wm8350_priv
->out1
;
344 case WM8350_LOUT2_VOLUME
:
345 out
= &wm8350_priv
->out2
;
352 out
->left_vol
= ucontrol
->value
.integer
.value
[0];
353 out
->right_vol
= ucontrol
->value
.integer
.value
[1];
358 ret
= snd_soc_put_volsw(kcontrol
, ucontrol
);
362 /* now hit the volume update bits (always bit 8) */
363 val
= wm8350_codec_read(codec
, reg
);
364 wm8350_codec_write(codec
, reg
, val
| WM8350_OUT1_VU
);
368 static int wm8350_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
369 struct snd_ctl_elem_value
*ucontrol
)
371 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
372 struct wm8350_data
*wm8350_priv
= snd_soc_codec_get_drvdata(codec
);
373 struct wm8350_output
*out1
= &wm8350_priv
->out1
;
374 struct wm8350_output
*out2
= &wm8350_priv
->out2
;
375 struct soc_mixer_control
*mc
=
376 (struct soc_mixer_control
*)kcontrol
->private_value
;
377 unsigned int reg
= mc
->reg
;
379 /* If these are cached registers use the cache */
381 case WM8350_LOUT1_VOLUME
:
382 ucontrol
->value
.integer
.value
[0] = out1
->left_vol
;
383 ucontrol
->value
.integer
.value
[1] = out1
->right_vol
;
386 case WM8350_LOUT2_VOLUME
:
387 ucontrol
->value
.integer
.value
[0] = out2
->left_vol
;
388 ucontrol
->value
.integer
.value
[1] = out2
->right_vol
;
395 return snd_soc_get_volsw(kcontrol
, ucontrol
);
398 static const char *wm8350_deemp
[] = { "None", "32kHz", "44.1kHz", "48kHz" };
399 static const char *wm8350_pol
[] = { "Normal", "Inv R", "Inv L", "Inv L & R" };
400 static const char *wm8350_dacmutem
[] = { "Normal", "Soft" };
401 static const char *wm8350_dacmutes
[] = { "Fast", "Slow" };
402 static const char *wm8350_adcfilter
[] = { "None", "High Pass" };
403 static const char *wm8350_adchp
[] = { "44.1kHz", "8kHz", "16kHz", "32kHz" };
404 static const char *wm8350_lr
[] = { "Left", "Right" };
406 static const struct soc_enum wm8350_enum
[] = {
407 SOC_ENUM_SINGLE(WM8350_DAC_CONTROL
, 4, 4, wm8350_deemp
),
408 SOC_ENUM_SINGLE(WM8350_DAC_CONTROL
, 0, 4, wm8350_pol
),
409 SOC_ENUM_SINGLE(WM8350_DAC_MUTE_VOLUME
, 14, 2, wm8350_dacmutem
),
410 SOC_ENUM_SINGLE(WM8350_DAC_MUTE_VOLUME
, 13, 2, wm8350_dacmutes
),
411 SOC_ENUM_SINGLE(WM8350_ADC_CONTROL
, 15, 2, wm8350_adcfilter
),
412 SOC_ENUM_SINGLE(WM8350_ADC_CONTROL
, 8, 4, wm8350_adchp
),
413 SOC_ENUM_SINGLE(WM8350_ADC_CONTROL
, 0, 4, wm8350_pol
),
414 SOC_ENUM_SINGLE(WM8350_INPUT_MIXER_VOLUME
, 15, 2, wm8350_lr
),
417 static DECLARE_TLV_DB_SCALE(pre_amp_tlv
, -1200, 3525, 0);
418 static DECLARE_TLV_DB_SCALE(out_pga_tlv
, -5700, 600, 0);
419 static DECLARE_TLV_DB_SCALE(dac_pcm_tlv
, -7163, 36, 1);
420 static DECLARE_TLV_DB_SCALE(adc_pcm_tlv
, -12700, 50, 1);
421 static DECLARE_TLV_DB_SCALE(out_mix_tlv
, -1500, 300, 1);
423 static const unsigned int capture_sd_tlv
[] = {
424 TLV_DB_RANGE_HEAD(2),
425 0, 12, TLV_DB_SCALE_ITEM(-3600, 300, 1),
426 13, 15, TLV_DB_SCALE_ITEM(0, 0, 0),
429 static const struct snd_kcontrol_new wm8350_snd_controls
[] = {
430 SOC_ENUM("Playback Deemphasis", wm8350_enum
[0]),
431 SOC_ENUM("Playback DAC Inversion", wm8350_enum
[1]),
432 SOC_DOUBLE_R_EXT_TLV("Playback PCM Volume",
433 WM8350_DAC_DIGITAL_VOLUME_L
,
434 WM8350_DAC_DIGITAL_VOLUME_R
,
435 0, 255, 0, wm8350_get_volsw_2r
,
436 wm8350_put_volsw_2r_vu
, dac_pcm_tlv
),
437 SOC_ENUM("Playback PCM Mute Function", wm8350_enum
[2]),
438 SOC_ENUM("Playback PCM Mute Speed", wm8350_enum
[3]),
439 SOC_ENUM("Capture PCM Filter", wm8350_enum
[4]),
440 SOC_ENUM("Capture PCM HP Filter", wm8350_enum
[5]),
441 SOC_ENUM("Capture ADC Inversion", wm8350_enum
[6]),
442 SOC_DOUBLE_R_EXT_TLV("Capture PCM Volume",
443 WM8350_ADC_DIGITAL_VOLUME_L
,
444 WM8350_ADC_DIGITAL_VOLUME_R
,
445 0, 255, 0, wm8350_get_volsw_2r
,
446 wm8350_put_volsw_2r_vu
, adc_pcm_tlv
),
447 SOC_DOUBLE_TLV("Capture Sidetone Volume",
449 8, 4, 15, 1, capture_sd_tlv
),
450 SOC_DOUBLE_R_EXT_TLV("Capture Volume",
451 WM8350_LEFT_INPUT_VOLUME
,
452 WM8350_RIGHT_INPUT_VOLUME
,
453 2, 63, 0, wm8350_get_volsw_2r
,
454 wm8350_put_volsw_2r_vu
, pre_amp_tlv
),
455 SOC_DOUBLE_R("Capture ZC Switch",
456 WM8350_LEFT_INPUT_VOLUME
,
457 WM8350_RIGHT_INPUT_VOLUME
, 13, 1, 0),
458 SOC_SINGLE_TLV("Left Input Left Sidetone Volume",
459 WM8350_OUTPUT_LEFT_MIXER_VOLUME
, 1, 7, 0, out_mix_tlv
),
460 SOC_SINGLE_TLV("Left Input Right Sidetone Volume",
461 WM8350_OUTPUT_LEFT_MIXER_VOLUME
,
462 5, 7, 0, out_mix_tlv
),
463 SOC_SINGLE_TLV("Left Input Bypass Volume",
464 WM8350_OUTPUT_LEFT_MIXER_VOLUME
,
465 9, 7, 0, out_mix_tlv
),
466 SOC_SINGLE_TLV("Right Input Left Sidetone Volume",
467 WM8350_OUTPUT_RIGHT_MIXER_VOLUME
,
468 1, 7, 0, out_mix_tlv
),
469 SOC_SINGLE_TLV("Right Input Right Sidetone Volume",
470 WM8350_OUTPUT_RIGHT_MIXER_VOLUME
,
471 5, 7, 0, out_mix_tlv
),
472 SOC_SINGLE_TLV("Right Input Bypass Volume",
473 WM8350_OUTPUT_RIGHT_MIXER_VOLUME
,
474 13, 7, 0, out_mix_tlv
),
475 SOC_SINGLE("Left Input Mixer +20dB Switch",
476 WM8350_INPUT_MIXER_VOLUME_L
, 0, 1, 0),
477 SOC_SINGLE("Right Input Mixer +20dB Switch",
478 WM8350_INPUT_MIXER_VOLUME_R
, 0, 1, 0),
479 SOC_SINGLE_TLV("Out4 Capture Volume",
480 WM8350_INPUT_MIXER_VOLUME
,
481 1, 7, 0, out_mix_tlv
),
482 SOC_DOUBLE_R_EXT_TLV("Out1 Playback Volume",
485 2, 63, 0, wm8350_get_volsw_2r
,
486 wm8350_put_volsw_2r_vu
, out_pga_tlv
),
487 SOC_DOUBLE_R("Out1 Playback ZC Switch",
489 WM8350_ROUT1_VOLUME
, 13, 1, 0),
490 SOC_DOUBLE_R_EXT_TLV("Out2 Playback Volume",
493 2, 63, 0, wm8350_get_volsw_2r
,
494 wm8350_put_volsw_2r_vu
, out_pga_tlv
),
495 SOC_DOUBLE_R("Out2 Playback ZC Switch", WM8350_LOUT2_VOLUME
,
496 WM8350_ROUT2_VOLUME
, 13, 1, 0),
497 SOC_SINGLE("Out2 Right Invert Switch", WM8350_ROUT2_VOLUME
, 10, 1, 0),
498 SOC_SINGLE_TLV("Out2 Beep Volume", WM8350_BEEP_VOLUME
,
499 5, 7, 0, out_mix_tlv
),
501 SOC_DOUBLE_R("Out1 Playback Switch",
505 SOC_DOUBLE_R("Out2 Playback Switch",
515 /* Left Playback Mixer */
516 static const struct snd_kcontrol_new wm8350_left_play_mixer_controls
[] = {
517 SOC_DAPM_SINGLE("Playback Switch",
518 WM8350_LEFT_MIXER_CONTROL
, 11, 1, 0),
519 SOC_DAPM_SINGLE("Left Bypass Switch",
520 WM8350_LEFT_MIXER_CONTROL
, 2, 1, 0),
521 SOC_DAPM_SINGLE("Right Playback Switch",
522 WM8350_LEFT_MIXER_CONTROL
, 12, 1, 0),
523 SOC_DAPM_SINGLE("Left Sidetone Switch",
524 WM8350_LEFT_MIXER_CONTROL
, 0, 1, 0),
525 SOC_DAPM_SINGLE("Right Sidetone Switch",
526 WM8350_LEFT_MIXER_CONTROL
, 1, 1, 0),
529 /* Right Playback Mixer */
530 static const struct snd_kcontrol_new wm8350_right_play_mixer_controls
[] = {
531 SOC_DAPM_SINGLE("Playback Switch",
532 WM8350_RIGHT_MIXER_CONTROL
, 12, 1, 0),
533 SOC_DAPM_SINGLE("Right Bypass Switch",
534 WM8350_RIGHT_MIXER_CONTROL
, 3, 1, 0),
535 SOC_DAPM_SINGLE("Left Playback Switch",
536 WM8350_RIGHT_MIXER_CONTROL
, 11, 1, 0),
537 SOC_DAPM_SINGLE("Left Sidetone Switch",
538 WM8350_RIGHT_MIXER_CONTROL
, 0, 1, 0),
539 SOC_DAPM_SINGLE("Right Sidetone Switch",
540 WM8350_RIGHT_MIXER_CONTROL
, 1, 1, 0),
544 static const struct snd_kcontrol_new wm8350_out4_mixer_controls
[] = {
545 SOC_DAPM_SINGLE("Right Playback Switch",
546 WM8350_OUT4_MIXER_CONTROL
, 12, 1, 0),
547 SOC_DAPM_SINGLE("Left Playback Switch",
548 WM8350_OUT4_MIXER_CONTROL
, 11, 1, 0),
549 SOC_DAPM_SINGLE("Right Capture Switch",
550 WM8350_OUT4_MIXER_CONTROL
, 9, 1, 0),
551 SOC_DAPM_SINGLE("Out3 Playback Switch",
552 WM8350_OUT4_MIXER_CONTROL
, 2, 1, 0),
553 SOC_DAPM_SINGLE("Right Mixer Switch",
554 WM8350_OUT4_MIXER_CONTROL
, 1, 1, 0),
555 SOC_DAPM_SINGLE("Left Mixer Switch",
556 WM8350_OUT4_MIXER_CONTROL
, 0, 1, 0),
560 static const struct snd_kcontrol_new wm8350_out3_mixer_controls
[] = {
561 SOC_DAPM_SINGLE("Left Playback Switch",
562 WM8350_OUT3_MIXER_CONTROL
, 11, 1, 0),
563 SOC_DAPM_SINGLE("Left Capture Switch",
564 WM8350_OUT3_MIXER_CONTROL
, 8, 1, 0),
565 SOC_DAPM_SINGLE("Out4 Playback Switch",
566 WM8350_OUT3_MIXER_CONTROL
, 3, 1, 0),
567 SOC_DAPM_SINGLE("Left Mixer Switch",
568 WM8350_OUT3_MIXER_CONTROL
, 0, 1, 0),
571 /* Left Input Mixer */
572 static const struct snd_kcontrol_new wm8350_left_capt_mixer_controls
[] = {
573 SOC_DAPM_SINGLE_TLV("L2 Capture Volume",
574 WM8350_INPUT_MIXER_VOLUME_L
, 1, 7, 0, out_mix_tlv
),
575 SOC_DAPM_SINGLE_TLV("L3 Capture Volume",
576 WM8350_INPUT_MIXER_VOLUME_L
, 9, 7, 0, out_mix_tlv
),
577 SOC_DAPM_SINGLE("PGA Capture Switch",
578 WM8350_LEFT_INPUT_VOLUME
, 14, 1, 1),
581 /* Right Input Mixer */
582 static const struct snd_kcontrol_new wm8350_right_capt_mixer_controls
[] = {
583 SOC_DAPM_SINGLE_TLV("L2 Capture Volume",
584 WM8350_INPUT_MIXER_VOLUME_R
, 5, 7, 0, out_mix_tlv
),
585 SOC_DAPM_SINGLE_TLV("L3 Capture Volume",
586 WM8350_INPUT_MIXER_VOLUME_R
, 13, 7, 0, out_mix_tlv
),
587 SOC_DAPM_SINGLE("PGA Capture Switch",
588 WM8350_RIGHT_INPUT_VOLUME
, 14, 1, 1),
592 static const struct snd_kcontrol_new wm8350_left_mic_mixer_controls
[] = {
593 SOC_DAPM_SINGLE("INN Capture Switch", WM8350_INPUT_CONTROL
, 1, 1, 0),
594 SOC_DAPM_SINGLE("INP Capture Switch", WM8350_INPUT_CONTROL
, 0, 1, 0),
595 SOC_DAPM_SINGLE("IN2 Capture Switch", WM8350_INPUT_CONTROL
, 2, 1, 0),
598 /* Right Mic Mixer */
599 static const struct snd_kcontrol_new wm8350_right_mic_mixer_controls
[] = {
600 SOC_DAPM_SINGLE("INN Capture Switch", WM8350_INPUT_CONTROL
, 9, 1, 0),
601 SOC_DAPM_SINGLE("INP Capture Switch", WM8350_INPUT_CONTROL
, 8, 1, 0),
602 SOC_DAPM_SINGLE("IN2 Capture Switch", WM8350_INPUT_CONTROL
, 10, 1, 0),
606 static const struct snd_kcontrol_new wm8350_beep_switch_controls
=
607 SOC_DAPM_SINGLE("Switch", WM8350_BEEP_VOLUME
, 15, 1, 1);
609 /* Out4 Capture Mux */
610 static const struct snd_kcontrol_new wm8350_out4_capture_controls
=
611 SOC_DAPM_ENUM("Route", wm8350_enum
[7]);
613 static const struct snd_soc_dapm_widget wm8350_dapm_widgets
[] = {
615 SND_SOC_DAPM_PGA("IN3R PGA", WM8350_POWER_MGMT_2
, 11, 0, NULL
, 0),
616 SND_SOC_DAPM_PGA("IN3L PGA", WM8350_POWER_MGMT_2
, 10, 0, NULL
, 0),
617 SND_SOC_DAPM_PGA_E("Right Out2 PGA", WM8350_POWER_MGMT_3
, 3, 0, NULL
,
619 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
620 SND_SOC_DAPM_PGA_E("Left Out2 PGA", WM8350_POWER_MGMT_3
, 2, 0, NULL
, 0,
622 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
623 SND_SOC_DAPM_PGA_E("Right Out1 PGA", WM8350_POWER_MGMT_3
, 1, 0, NULL
,
625 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
626 SND_SOC_DAPM_PGA_E("Left Out1 PGA", WM8350_POWER_MGMT_3
, 0, 0, NULL
, 0,
628 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
630 SND_SOC_DAPM_MIXER("Right Capture Mixer", WM8350_POWER_MGMT_2
,
631 7, 0, &wm8350_right_capt_mixer_controls
[0],
632 ARRAY_SIZE(wm8350_right_capt_mixer_controls
)),
634 SND_SOC_DAPM_MIXER("Left Capture Mixer", WM8350_POWER_MGMT_2
,
635 6, 0, &wm8350_left_capt_mixer_controls
[0],
636 ARRAY_SIZE(wm8350_left_capt_mixer_controls
)),
638 SND_SOC_DAPM_MIXER("Out4 Mixer", WM8350_POWER_MGMT_2
, 5, 0,
639 &wm8350_out4_mixer_controls
[0],
640 ARRAY_SIZE(wm8350_out4_mixer_controls
)),
642 SND_SOC_DAPM_MIXER("Out3 Mixer", WM8350_POWER_MGMT_2
, 4, 0,
643 &wm8350_out3_mixer_controls
[0],
644 ARRAY_SIZE(wm8350_out3_mixer_controls
)),
646 SND_SOC_DAPM_MIXER("Right Playback Mixer", WM8350_POWER_MGMT_2
, 1, 0,
647 &wm8350_right_play_mixer_controls
[0],
648 ARRAY_SIZE(wm8350_right_play_mixer_controls
)),
650 SND_SOC_DAPM_MIXER("Left Playback Mixer", WM8350_POWER_MGMT_2
, 0, 0,
651 &wm8350_left_play_mixer_controls
[0],
652 ARRAY_SIZE(wm8350_left_play_mixer_controls
)),
654 SND_SOC_DAPM_MIXER("Left Mic Mixer", WM8350_POWER_MGMT_2
, 8, 0,
655 &wm8350_left_mic_mixer_controls
[0],
656 ARRAY_SIZE(wm8350_left_mic_mixer_controls
)),
658 SND_SOC_DAPM_MIXER("Right Mic Mixer", WM8350_POWER_MGMT_2
, 9, 0,
659 &wm8350_right_mic_mixer_controls
[0],
660 ARRAY_SIZE(wm8350_right_mic_mixer_controls
)),
662 /* virtual mixer for Beep and Out2R */
663 SND_SOC_DAPM_MIXER("Out2 Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
665 SND_SOC_DAPM_SWITCH("Beep", WM8350_POWER_MGMT_3
, 7, 0,
666 &wm8350_beep_switch_controls
),
668 SND_SOC_DAPM_ADC("Right ADC", "Right Capture",
669 WM8350_POWER_MGMT_4
, 3, 0),
670 SND_SOC_DAPM_ADC("Left ADC", "Left Capture",
671 WM8350_POWER_MGMT_4
, 2, 0),
672 SND_SOC_DAPM_DAC("Right DAC", "Right Playback",
673 WM8350_POWER_MGMT_4
, 5, 0),
674 SND_SOC_DAPM_DAC("Left DAC", "Left Playback",
675 WM8350_POWER_MGMT_4
, 4, 0),
677 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8350_POWER_MGMT_1
, 4, 0),
679 SND_SOC_DAPM_MUX("Out4 Capture Channel", SND_SOC_NOPM
, 0, 0,
680 &wm8350_out4_capture_controls
),
682 SND_SOC_DAPM_OUTPUT("OUT1R"),
683 SND_SOC_DAPM_OUTPUT("OUT1L"),
684 SND_SOC_DAPM_OUTPUT("OUT2R"),
685 SND_SOC_DAPM_OUTPUT("OUT2L"),
686 SND_SOC_DAPM_OUTPUT("OUT3"),
687 SND_SOC_DAPM_OUTPUT("OUT4"),
689 SND_SOC_DAPM_INPUT("IN1RN"),
690 SND_SOC_DAPM_INPUT("IN1RP"),
691 SND_SOC_DAPM_INPUT("IN2R"),
692 SND_SOC_DAPM_INPUT("IN1LP"),
693 SND_SOC_DAPM_INPUT("IN1LN"),
694 SND_SOC_DAPM_INPUT("IN2L"),
695 SND_SOC_DAPM_INPUT("IN3R"),
696 SND_SOC_DAPM_INPUT("IN3L"),
699 static const struct snd_soc_dapm_route audio_map
[] = {
701 /* left playback mixer */
702 {"Left Playback Mixer", "Playback Switch", "Left DAC"},
703 {"Left Playback Mixer", "Left Bypass Switch", "IN3L PGA"},
704 {"Left Playback Mixer", "Right Playback Switch", "Right DAC"},
705 {"Left Playback Mixer", "Left Sidetone Switch", "Left Mic Mixer"},
706 {"Left Playback Mixer", "Right Sidetone Switch", "Right Mic Mixer"},
708 /* right playback mixer */
709 {"Right Playback Mixer", "Playback Switch", "Right DAC"},
710 {"Right Playback Mixer", "Right Bypass Switch", "IN3R PGA"},
711 {"Right Playback Mixer", "Left Playback Switch", "Left DAC"},
712 {"Right Playback Mixer", "Left Sidetone Switch", "Left Mic Mixer"},
713 {"Right Playback Mixer", "Right Sidetone Switch", "Right Mic Mixer"},
715 /* out4 playback mixer */
716 {"Out4 Mixer", "Right Playback Switch", "Right DAC"},
717 {"Out4 Mixer", "Left Playback Switch", "Left DAC"},
718 {"Out4 Mixer", "Right Capture Switch", "Right Capture Mixer"},
719 {"Out4 Mixer", "Out3 Playback Switch", "Out3 Mixer"},
720 {"Out4 Mixer", "Right Mixer Switch", "Right Playback Mixer"},
721 {"Out4 Mixer", "Left Mixer Switch", "Left Playback Mixer"},
722 {"OUT4", NULL
, "Out4 Mixer"},
724 /* out3 playback mixer */
725 {"Out3 Mixer", "Left Playback Switch", "Left DAC"},
726 {"Out3 Mixer", "Left Capture Switch", "Left Capture Mixer"},
727 {"Out3 Mixer", "Left Mixer Switch", "Left Playback Mixer"},
728 {"Out3 Mixer", "Out4 Playback Switch", "Out4 Mixer"},
729 {"OUT3", NULL
, "Out3 Mixer"},
732 {"Right Out2 PGA", NULL
, "Right Playback Mixer"},
733 {"Left Out2 PGA", NULL
, "Left Playback Mixer"},
734 {"OUT2L", NULL
, "Left Out2 PGA"},
735 {"OUT2R", NULL
, "Right Out2 PGA"},
738 {"Right Out1 PGA", NULL
, "Right Playback Mixer"},
739 {"Left Out1 PGA", NULL
, "Left Playback Mixer"},
740 {"OUT1L", NULL
, "Left Out1 PGA"},
741 {"OUT1R", NULL
, "Right Out1 PGA"},
744 {"Left ADC", NULL
, "Left Capture Mixer"},
745 {"Right ADC", NULL
, "Right Capture Mixer"},
747 /* Left capture mixer */
748 {"Left Capture Mixer", "L2 Capture Volume", "IN2L"},
749 {"Left Capture Mixer", "L3 Capture Volume", "IN3L PGA"},
750 {"Left Capture Mixer", "PGA Capture Switch", "Left Mic Mixer"},
751 {"Left Capture Mixer", NULL
, "Out4 Capture Channel"},
753 /* Right capture mixer */
754 {"Right Capture Mixer", "L2 Capture Volume", "IN2R"},
755 {"Right Capture Mixer", "L3 Capture Volume", "IN3R PGA"},
756 {"Right Capture Mixer", "PGA Capture Switch", "Right Mic Mixer"},
757 {"Right Capture Mixer", NULL
, "Out4 Capture Channel"},
760 {"IN3L PGA", NULL
, "IN3L"},
761 {"IN3R PGA", NULL
, "IN3R"},
764 {"Left Mic Mixer", "INN Capture Switch", "IN1LN"},
765 {"Left Mic Mixer", "INP Capture Switch", "IN1LP"},
766 {"Left Mic Mixer", "IN2 Capture Switch", "IN2L"},
768 /* Right Mic mixer */
769 {"Right Mic Mixer", "INN Capture Switch", "IN1RN"},
770 {"Right Mic Mixer", "INP Capture Switch", "IN1RP"},
771 {"Right Mic Mixer", "IN2 Capture Switch", "IN2R"},
774 {"Out4 Capture Channel", NULL
, "Out4 Mixer"},
777 {"Beep", NULL
, "IN3R PGA"},
780 static int wm8350_add_widgets(struct snd_soc_codec
*codec
)
782 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
785 ret
= snd_soc_dapm_new_controls(dapm
,
787 ARRAY_SIZE(wm8350_dapm_widgets
));
789 dev_err(codec
->dev
, "dapm control register failed\n");
793 /* set up audio paths */
794 ret
= snd_soc_dapm_add_routes(dapm
, audio_map
, ARRAY_SIZE(audio_map
));
796 dev_err(codec
->dev
, "DAPM route register failed\n");
803 static int wm8350_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
804 int clk_id
, unsigned int freq
, int dir
)
806 struct snd_soc_codec
*codec
= codec_dai
->codec
;
807 struct wm8350
*wm8350
= codec
->control_data
;
811 case WM8350_MCLK_SEL_MCLK
:
812 wm8350_clear_bits(wm8350
, WM8350_CLOCK_CONTROL_1
,
815 case WM8350_MCLK_SEL_PLL_MCLK
:
816 case WM8350_MCLK_SEL_PLL_DAC
:
817 case WM8350_MCLK_SEL_PLL_ADC
:
818 case WM8350_MCLK_SEL_PLL_32K
:
819 wm8350_set_bits(wm8350
, WM8350_CLOCK_CONTROL_1
,
821 fll_4
= wm8350_codec_read(codec
, WM8350_FLL_CONTROL_4
) &
822 ~WM8350_FLL_CLK_SRC_MASK
;
823 wm8350_codec_write(codec
, WM8350_FLL_CONTROL_4
, fll_4
| clk_id
);
828 if (dir
== SND_SOC_CLOCK_OUT
)
829 wm8350_set_bits(wm8350
, WM8350_CLOCK_CONTROL_2
,
832 wm8350_clear_bits(wm8350
, WM8350_CLOCK_CONTROL_2
,
838 static int wm8350_set_clkdiv(struct snd_soc_dai
*codec_dai
, int div_id
, int div
)
840 struct snd_soc_codec
*codec
= codec_dai
->codec
;
844 case WM8350_ADC_CLKDIV
:
845 val
= wm8350_codec_read(codec
, WM8350_ADC_DIVIDER
) &
846 ~WM8350_ADC_CLKDIV_MASK
;
847 wm8350_codec_write(codec
, WM8350_ADC_DIVIDER
, val
| div
);
849 case WM8350_DAC_CLKDIV
:
850 val
= wm8350_codec_read(codec
, WM8350_DAC_CLOCK_CONTROL
) &
851 ~WM8350_DAC_CLKDIV_MASK
;
852 wm8350_codec_write(codec
, WM8350_DAC_CLOCK_CONTROL
, val
| div
);
854 case WM8350_BCLK_CLKDIV
:
855 val
= wm8350_codec_read(codec
, WM8350_CLOCK_CONTROL_1
) &
856 ~WM8350_BCLK_DIV_MASK
;
857 wm8350_codec_write(codec
, WM8350_CLOCK_CONTROL_1
, val
| div
);
859 case WM8350_OPCLK_CLKDIV
:
860 val
= wm8350_codec_read(codec
, WM8350_CLOCK_CONTROL_1
) &
861 ~WM8350_OPCLK_DIV_MASK
;
862 wm8350_codec_write(codec
, WM8350_CLOCK_CONTROL_1
, val
| div
);
864 case WM8350_SYS_CLKDIV
:
865 val
= wm8350_codec_read(codec
, WM8350_CLOCK_CONTROL_1
) &
866 ~WM8350_MCLK_DIV_MASK
;
867 wm8350_codec_write(codec
, WM8350_CLOCK_CONTROL_1
, val
| div
);
869 case WM8350_DACLR_CLKDIV
:
870 val
= wm8350_codec_read(codec
, WM8350_DAC_LR_RATE
) &
871 ~WM8350_DACLRC_RATE_MASK
;
872 wm8350_codec_write(codec
, WM8350_DAC_LR_RATE
, val
| div
);
874 case WM8350_ADCLR_CLKDIV
:
875 val
= wm8350_codec_read(codec
, WM8350_ADC_LR_RATE
) &
876 ~WM8350_ADCLRC_RATE_MASK
;
877 wm8350_codec_write(codec
, WM8350_ADC_LR_RATE
, val
| div
);
886 static int wm8350_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
888 struct snd_soc_codec
*codec
= codec_dai
->codec
;
889 u16 iface
= wm8350_codec_read(codec
, WM8350_AI_FORMATING
) &
890 ~(WM8350_AIF_BCLK_INV
| WM8350_AIF_LRCLK_INV
| WM8350_AIF_FMT_MASK
);
891 u16 master
= wm8350_codec_read(codec
, WM8350_AI_DAC_CONTROL
) &
893 u16 dac_lrc
= wm8350_codec_read(codec
, WM8350_DAC_LR_RATE
) &
895 u16 adc_lrc
= wm8350_codec_read(codec
, WM8350_ADC_LR_RATE
) &
898 /* set master/slave audio interface */
899 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
900 case SND_SOC_DAIFMT_CBM_CFM
:
901 master
|= WM8350_BCLK_MSTR
;
902 dac_lrc
|= WM8350_DACLRC_ENA
;
903 adc_lrc
|= WM8350_ADCLRC_ENA
;
905 case SND_SOC_DAIFMT_CBS_CFS
:
911 /* interface format */
912 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
913 case SND_SOC_DAIFMT_I2S
:
916 case SND_SOC_DAIFMT_RIGHT_J
:
918 case SND_SOC_DAIFMT_LEFT_J
:
921 case SND_SOC_DAIFMT_DSP_A
:
924 case SND_SOC_DAIFMT_DSP_B
:
925 iface
|= 0x3 << 8 | WM8350_AIF_LRCLK_INV
;
931 /* clock inversion */
932 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
933 case SND_SOC_DAIFMT_NB_NF
:
935 case SND_SOC_DAIFMT_IB_IF
:
936 iface
|= WM8350_AIF_LRCLK_INV
| WM8350_AIF_BCLK_INV
;
938 case SND_SOC_DAIFMT_IB_NF
:
939 iface
|= WM8350_AIF_BCLK_INV
;
941 case SND_SOC_DAIFMT_NB_IF
:
942 iface
|= WM8350_AIF_LRCLK_INV
;
948 wm8350_codec_write(codec
, WM8350_AI_FORMATING
, iface
);
949 wm8350_codec_write(codec
, WM8350_AI_DAC_CONTROL
, master
);
950 wm8350_codec_write(codec
, WM8350_DAC_LR_RATE
, dac_lrc
);
951 wm8350_codec_write(codec
, WM8350_ADC_LR_RATE
, adc_lrc
);
955 static int wm8350_pcm_trigger(struct snd_pcm_substream
*substream
,
956 int cmd
, struct snd_soc_dai
*codec_dai
)
958 struct snd_soc_codec
*codec
= codec_dai
->codec
;
959 int master
= wm8350_codec_cache_read(codec
, WM8350_AI_DAC_CONTROL
) &
963 /* Check that the DACs or ADCs are enabled since they are
964 * required for LRC in master mode. The DACs or ADCs need a
965 * valid audio path i.e. pin -> ADC or DAC -> pin before
966 * the LRC will be enabled in master mode. */
967 if (!master
|| cmd
!= SNDRV_PCM_TRIGGER_START
)
970 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
971 enabled
= wm8350_codec_cache_read(codec
, WM8350_POWER_MGMT_4
) &
972 (WM8350_ADCR_ENA
| WM8350_ADCL_ENA
);
974 enabled
= wm8350_codec_cache_read(codec
, WM8350_POWER_MGMT_4
) &
975 (WM8350_DACR_ENA
| WM8350_DACL_ENA
);
980 "%s: invalid audio path - no clocks available\n",
987 static int wm8350_pcm_hw_params(struct snd_pcm_substream
*substream
,
988 struct snd_pcm_hw_params
*params
,
989 struct snd_soc_dai
*codec_dai
)
991 struct snd_soc_codec
*codec
= codec_dai
->codec
;
992 struct wm8350
*wm8350
= codec
->control_data
;
993 u16 iface
= wm8350_codec_read(codec
, WM8350_AI_FORMATING
) &
997 switch (params_format(params
)) {
998 case SNDRV_PCM_FORMAT_S16_LE
:
1000 case SNDRV_PCM_FORMAT_S20_3LE
:
1003 case SNDRV_PCM_FORMAT_S24_LE
:
1006 case SNDRV_PCM_FORMAT_S32_LE
:
1011 wm8350_codec_write(codec
, WM8350_AI_FORMATING
, iface
);
1013 /* The sloping stopband filter is recommended for use with
1014 * lower sample rates to improve performance.
1016 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1017 if (params_rate(params
) < 24000)
1018 wm8350_set_bits(wm8350
, WM8350_DAC_MUTE_VOLUME
,
1019 WM8350_DAC_SB_FILT
);
1021 wm8350_clear_bits(wm8350
, WM8350_DAC_MUTE_VOLUME
,
1022 WM8350_DAC_SB_FILT
);
1028 static int wm8350_mute(struct snd_soc_dai
*dai
, int mute
)
1030 struct snd_soc_codec
*codec
= dai
->codec
;
1031 struct wm8350
*wm8350
= codec
->control_data
;
1034 wm8350_set_bits(wm8350
, WM8350_DAC_MUTE
, WM8350_DAC_MUTE_ENA
);
1036 wm8350_clear_bits(wm8350
, WM8350_DAC_MUTE
, WM8350_DAC_MUTE_ENA
);
1042 int div
; /* FLL_OUTDIV */
1045 int ratio
; /* FLL_FRATIO */
1048 /* The size in bits of the fll divide multiplied by 10
1049 * to allow rounding later */
1050 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1052 static inline int fll_factors(struct _fll_div
*fll_div
, unsigned int input
,
1053 unsigned int output
)
1056 unsigned int t1
, t2
, K
, Nmod
;
1058 if (output
>= 2815250 && output
<= 3125000)
1060 else if (output
>= 5625000 && output
<= 6250000)
1062 else if (output
>= 11250000 && output
<= 12500000)
1064 else if (output
>= 22500000 && output
<= 25000000)
1067 printk(KERN_ERR
"wm8350: fll freq %d out of range\n", output
);
1076 t1
= output
* (1 << (fll_div
->div
+ 1));
1077 t2
= input
* fll_div
->ratio
;
1079 fll_div
->n
= t1
/ t2
;
1083 Kpart
= FIXED_FLL_SIZE
* (long long)Nmod
;
1085 K
= Kpart
& 0xFFFFFFFF;
1087 /* Check if we need to round */
1091 /* Move down to proper range now rounding is done */
1100 static int wm8350_set_fll(struct snd_soc_dai
*codec_dai
,
1101 int pll_id
, int source
, unsigned int freq_in
,
1102 unsigned int freq_out
)
1104 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1105 struct wm8350
*wm8350
= codec
->control_data
;
1106 struct wm8350_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1107 struct _fll_div fll_div
;
1111 if (freq_in
== priv
->fll_freq_in
&& freq_out
== priv
->fll_freq_out
)
1114 /* power down FLL - we need to do this for reconfiguration */
1115 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_4
,
1116 WM8350_FLL_ENA
| WM8350_FLL_OSC_ENA
);
1118 if (freq_out
== 0 || freq_in
== 0)
1121 ret
= fll_factors(&fll_div
, freq_in
, freq_out
);
1124 dev_dbg(wm8350
->dev
,
1125 "FLL in %u FLL out %u N 0x%x K 0x%x div %d ratio %d",
1126 freq_in
, freq_out
, fll_div
.n
, fll_div
.k
, fll_div
.div
,
1129 /* set up N.K & dividers */
1130 fll_1
= wm8350_codec_read(codec
, WM8350_FLL_CONTROL_1
) &
1131 ~(WM8350_FLL_OUTDIV_MASK
| WM8350_FLL_RSP_RATE_MASK
| 0xc000);
1132 wm8350_codec_write(codec
, WM8350_FLL_CONTROL_1
,
1133 fll_1
| (fll_div
.div
<< 8) | 0x50);
1134 wm8350_codec_write(codec
, WM8350_FLL_CONTROL_2
,
1135 (fll_div
.ratio
<< 11) | (fll_div
.
1136 n
& WM8350_FLL_N_MASK
));
1137 wm8350_codec_write(codec
, WM8350_FLL_CONTROL_3
, fll_div
.k
);
1138 fll_4
= wm8350_codec_read(codec
, WM8350_FLL_CONTROL_4
) &
1139 ~(WM8350_FLL_FRAC
| WM8350_FLL_SLOW_LOCK_REF
);
1140 wm8350_codec_write(codec
, WM8350_FLL_CONTROL_4
,
1141 fll_4
| (fll_div
.k
? WM8350_FLL_FRAC
: 0) |
1142 (fll_div
.ratio
== 8 ? WM8350_FLL_SLOW_LOCK_REF
: 0));
1145 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_4
, WM8350_FLL_OSC_ENA
);
1146 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_4
, WM8350_FLL_ENA
);
1148 priv
->fll_freq_out
= freq_out
;
1149 priv
->fll_freq_in
= freq_in
;
1154 static int wm8350_set_bias_level(struct snd_soc_codec
*codec
,
1155 enum snd_soc_bias_level level
)
1157 struct wm8350
*wm8350
= codec
->control_data
;
1158 struct wm8350_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1159 struct wm8350_audio_platform_data
*platform
=
1160 wm8350
->codec
.platform_data
;
1165 case SND_SOC_BIAS_ON
:
1166 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
) &
1167 ~(WM8350_VMID_MASK
| WM8350_CODEC_ISEL_MASK
);
1168 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1169 pm1
| WM8350_VMID_50K
|
1170 platform
->codec_current_on
<< 14);
1173 case SND_SOC_BIAS_PREPARE
:
1174 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
);
1175 pm1
&= ~WM8350_VMID_MASK
;
1176 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1177 pm1
| WM8350_VMID_50K
);
1180 case SND_SOC_BIAS_STANDBY
:
1181 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
1182 ret
= regulator_bulk_enable(ARRAY_SIZE(priv
->supplies
),
1187 /* Enable the system clock */
1188 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_4
,
1191 /* mute DAC & outputs */
1192 wm8350_set_bits(wm8350
, WM8350_DAC_MUTE
,
1193 WM8350_DAC_MUTE_ENA
);
1195 /* discharge cap memory */
1196 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
,
1197 platform
->dis_out1
|
1198 (platform
->dis_out2
<< 2) |
1199 (platform
->dis_out3
<< 4) |
1200 (platform
->dis_out4
<< 6));
1202 /* wait for discharge */
1203 schedule_timeout_interruptible(msecs_to_jiffies
1205 cap_discharge_msecs
));
1207 /* enable antipop */
1208 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
,
1209 (platform
->vmid_s_curve
<< 8));
1212 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1214 codec_current_charge
<< 14) |
1215 WM8350_VMID_5K
| WM8350_VMIDEN
|
1219 schedule_timeout_interruptible(msecs_to_jiffies
1221 vmid_charge_msecs
));
1223 /* turn on vmid 300k */
1224 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
) &
1225 ~(WM8350_VMID_MASK
| WM8350_CODEC_ISEL_MASK
);
1226 pm1
|= WM8350_VMID_300K
|
1227 (platform
->codec_current_standby
<< 14);
1228 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1232 /* enable analogue bias */
1233 pm1
|= WM8350_BIASEN
;
1234 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
, pm1
);
1236 /* disable antipop */
1237 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
, 0);
1240 /* turn on vmid 300k and reduce current */
1241 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
) &
1242 ~(WM8350_VMID_MASK
| WM8350_CODEC_ISEL_MASK
);
1243 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1244 pm1
| WM8350_VMID_300K
|
1246 codec_current_standby
<< 14));
1251 case SND_SOC_BIAS_OFF
:
1253 /* mute DAC & enable outputs */
1254 wm8350_set_bits(wm8350
, WM8350_DAC_MUTE
, WM8350_DAC_MUTE_ENA
);
1256 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_3
,
1257 WM8350_OUT1L_ENA
| WM8350_OUT1R_ENA
|
1258 WM8350_OUT2L_ENA
| WM8350_OUT2R_ENA
);
1260 /* enable anti pop S curve */
1261 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
,
1262 (platform
->vmid_s_curve
<< 8));
1265 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
) &
1267 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
, pm1
);
1270 schedule_timeout_interruptible(msecs_to_jiffies
1272 vmid_discharge_msecs
));
1274 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
,
1275 (platform
->vmid_s_curve
<< 8) |
1276 platform
->dis_out1
|
1277 (platform
->dis_out2
<< 2) |
1278 (platform
->dis_out3
<< 4) |
1279 (platform
->dis_out4
<< 6));
1281 /* turn off VBuf and drain */
1282 pm1
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_1
) &
1283 ~(WM8350_VBUFEN
| WM8350_VMID_MASK
);
1284 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
,
1285 pm1
| WM8350_OUTPUT_DRAIN_EN
);
1288 schedule_timeout_interruptible(msecs_to_jiffies
1289 (platform
->drain_msecs
));
1291 pm1
&= ~WM8350_BIASEN
;
1292 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_1
, pm1
);
1294 /* disable anti-pop */
1295 wm8350_reg_write(wm8350
, WM8350_ANTI_POP_CONTROL
, 0);
1297 wm8350_clear_bits(wm8350
, WM8350_LOUT1_VOLUME
,
1299 wm8350_clear_bits(wm8350
, WM8350_ROUT1_VOLUME
,
1301 wm8350_clear_bits(wm8350
, WM8350_LOUT2_VOLUME
,
1303 wm8350_clear_bits(wm8350
, WM8350_ROUT2_VOLUME
,
1306 /* disable clock gen */
1307 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_4
,
1310 regulator_bulk_disable(ARRAY_SIZE(priv
->supplies
),
1314 codec
->dapm
.bias_level
= level
;
1318 static int wm8350_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
1320 wm8350_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1324 static int wm8350_resume(struct snd_soc_codec
*codec
)
1326 wm8350_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1331 static void wm8350_hp_work(struct wm8350_data
*priv
,
1332 struct wm8350_jack_data
*jack
,
1335 struct wm8350
*wm8350
= priv
->codec
.control_data
;
1339 reg
= wm8350_reg_read(wm8350
, WM8350_JACK_PIN_STATUS
);
1341 report
= jack
->report
;
1345 snd_soc_jack_report(jack
->jack
, report
, jack
->report
);
1349 static void wm8350_hpl_work(struct work_struct
*work
)
1351 struct wm8350_data
*priv
=
1352 container_of(work
, struct wm8350_data
, hpl
.work
.work
);
1354 wm8350_hp_work(priv
, &priv
->hpl
, WM8350_JACK_L_LVL
);
1357 static void wm8350_hpr_work(struct work_struct
*work
)
1359 struct wm8350_data
*priv
=
1360 container_of(work
, struct wm8350_data
, hpr
.work
.work
);
1362 wm8350_hp_work(priv
, &priv
->hpr
, WM8350_JACK_R_LVL
);
1365 static irqreturn_t
wm8350_hp_jack_handler(int irq
, void *data
)
1367 struct wm8350_data
*priv
= data
;
1368 struct wm8350
*wm8350
= priv
->codec
.control_data
;
1369 struct wm8350_jack_data
*jack
= NULL
;
1371 switch (irq
- wm8350
->irq_base
) {
1372 case WM8350_IRQ_CODEC_JCK_DET_L
:
1373 #ifndef CONFIG_SND_SOC_WM8350_MODULE
1374 trace_snd_soc_jack_irq("WM8350 HPL");
1379 case WM8350_IRQ_CODEC_JCK_DET_R
:
1380 #ifndef CONFIG_SND_SOC_WM8350_MODULE
1381 trace_snd_soc_jack_irq("WM8350 HPR");
1390 if (device_may_wakeup(wm8350
->dev
))
1391 pm_wakeup_event(wm8350
->dev
, 250);
1393 schedule_delayed_work(&jack
->work
, 200);
1399 * wm8350_hp_jack_detect - Enable headphone jack detection.
1401 * @codec: WM8350 codec
1402 * @which: left or right jack detect signal
1403 * @jack: jack to report detection events on
1404 * @report: value to report
1406 * Enables the headphone jack detection of the WM8350. If no report
1407 * is specified then detection is disabled.
1409 int wm8350_hp_jack_detect(struct snd_soc_codec
*codec
, enum wm8350_jack which
,
1410 struct snd_soc_jack
*jack
, int report
)
1412 struct wm8350_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1413 struct wm8350
*wm8350
= codec
->control_data
;
1419 priv
->hpl
.jack
= jack
;
1420 priv
->hpl
.report
= report
;
1421 irq
= WM8350_IRQ_CODEC_JCK_DET_L
;
1422 ena
= WM8350_JDL_ENA
;
1426 priv
->hpr
.jack
= jack
;
1427 priv
->hpr
.report
= report
;
1428 irq
= WM8350_IRQ_CODEC_JCK_DET_R
;
1429 ena
= WM8350_JDR_ENA
;
1437 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_4
, WM8350_TOCLK_ENA
);
1438 wm8350_set_bits(wm8350
, WM8350_JACK_DETECT
, ena
);
1440 wm8350_clear_bits(wm8350
, WM8350_JACK_DETECT
, ena
);
1444 wm8350_hp_jack_handler(irq
+ wm8350
->irq_base
, priv
);
1448 EXPORT_SYMBOL_GPL(wm8350_hp_jack_detect
);
1450 static irqreturn_t
wm8350_mic_handler(int irq
, void *data
)
1452 struct wm8350_data
*priv
= data
;
1453 struct wm8350
*wm8350
= priv
->codec
.control_data
;
1457 #ifndef CONFIG_SND_SOC_WM8350_MODULE
1458 trace_snd_soc_jack_irq("WM8350 mic");
1461 reg
= wm8350_reg_read(wm8350
, WM8350_JACK_PIN_STATUS
);
1462 if (reg
& WM8350_JACK_MICSCD_LVL
)
1463 report
|= priv
->mic
.short_report
;
1464 if (reg
& WM8350_JACK_MICSD_LVL
)
1465 report
|= priv
->mic
.report
;
1467 snd_soc_jack_report(priv
->mic
.jack
, report
,
1468 priv
->mic
.report
| priv
->mic
.short_report
);
1474 * wm8350_mic_jack_detect - Enable microphone jack detection.
1476 * @codec: WM8350 codec
1477 * @jack: jack to report detection events on
1478 * @detect_report: value to report when presence detected
1479 * @short_report: value to report when microphone short detected
1481 * Enables the microphone jack detection of the WM8350. If both reports
1482 * are specified as zero then detection is disabled.
1484 int wm8350_mic_jack_detect(struct snd_soc_codec
*codec
,
1485 struct snd_soc_jack
*jack
,
1486 int detect_report
, int short_report
)
1488 struct wm8350_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1489 struct wm8350
*wm8350
= codec
->control_data
;
1491 priv
->mic
.jack
= jack
;
1492 priv
->mic
.report
= detect_report
;
1493 priv
->mic
.short_report
= short_report
;
1495 if (detect_report
|| short_report
) {
1496 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_4
, WM8350_TOCLK_ENA
);
1497 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_1
,
1498 WM8350_MIC_DET_ENA
);
1500 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_1
,
1501 WM8350_MIC_DET_ENA
);
1506 EXPORT_SYMBOL_GPL(wm8350_mic_jack_detect
);
1508 #define WM8350_RATES (SNDRV_PCM_RATE_8000_96000)
1510 #define WM8350_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1511 SNDRV_PCM_FMTBIT_S20_3LE |\
1512 SNDRV_PCM_FMTBIT_S24_LE)
1514 static struct snd_soc_dai_ops wm8350_dai_ops
= {
1515 .hw_params
= wm8350_pcm_hw_params
,
1516 .digital_mute
= wm8350_mute
,
1517 .trigger
= wm8350_pcm_trigger
,
1518 .set_fmt
= wm8350_set_dai_fmt
,
1519 .set_sysclk
= wm8350_set_dai_sysclk
,
1520 .set_pll
= wm8350_set_fll
,
1521 .set_clkdiv
= wm8350_set_clkdiv
,
1524 static struct snd_soc_dai_driver wm8350_dai
= {
1525 .name
= "wm8350-hifi",
1527 .stream_name
= "Playback",
1530 .rates
= WM8350_RATES
,
1531 .formats
= WM8350_FORMATS
,
1534 .stream_name
= "Capture",
1537 .rates
= WM8350_RATES
,
1538 .formats
= WM8350_FORMATS
,
1540 .ops
= &wm8350_dai_ops
,
1543 static int wm8350_codec_probe(struct snd_soc_codec
*codec
)
1545 struct wm8350
*wm8350
= dev_get_platdata(codec
->dev
);
1546 struct wm8350_data
*priv
;
1547 struct wm8350_output
*out1
;
1548 struct wm8350_output
*out2
;
1551 if (wm8350
->codec
.platform_data
== NULL
) {
1552 dev_err(codec
->dev
, "No audio platform data supplied\n");
1556 priv
= kzalloc(sizeof(struct wm8350_data
), GFP_KERNEL
);
1559 snd_soc_codec_set_drvdata(codec
, priv
);
1561 for (i
= 0; i
< ARRAY_SIZE(supply_names
); i
++)
1562 priv
->supplies
[i
].supply
= supply_names
[i
];
1564 ret
= regulator_bulk_get(wm8350
->dev
, ARRAY_SIZE(priv
->supplies
),
1569 wm8350
->codec
.codec
= codec
;
1570 codec
->control_data
= wm8350
;
1572 /* Put the codec into reset if it wasn't already */
1573 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_5
, WM8350_CODEC_ENA
);
1575 INIT_DELAYED_WORK(&codec
->dapm
.delayed_work
, wm8350_pga_work
);
1576 INIT_DELAYED_WORK(&priv
->hpl
.work
, wm8350_hpl_work
);
1577 INIT_DELAYED_WORK(&priv
->hpr
.work
, wm8350_hpr_work
);
1579 /* Enable the codec */
1580 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_5
, WM8350_CODEC_ENA
);
1582 /* Enable robust clocking mode in ADC */
1583 wm8350_codec_write(codec
, WM8350_SECURITY
, 0xa7);
1584 wm8350_codec_write(codec
, 0xde, 0x13);
1585 wm8350_codec_write(codec
, WM8350_SECURITY
, 0);
1587 /* read OUT1 & OUT2 volumes */
1590 out1
->left_vol
= (wm8350_reg_read(wm8350
, WM8350_LOUT1_VOLUME
) &
1591 WM8350_OUT1L_VOL_MASK
) >> WM8350_OUT1L_VOL_SHIFT
;
1592 out1
->right_vol
= (wm8350_reg_read(wm8350
, WM8350_ROUT1_VOLUME
) &
1593 WM8350_OUT1R_VOL_MASK
) >> WM8350_OUT1R_VOL_SHIFT
;
1594 out2
->left_vol
= (wm8350_reg_read(wm8350
, WM8350_LOUT2_VOLUME
) &
1595 WM8350_OUT2L_VOL_MASK
) >> WM8350_OUT1L_VOL_SHIFT
;
1596 out2
->right_vol
= (wm8350_reg_read(wm8350
, WM8350_ROUT2_VOLUME
) &
1597 WM8350_OUT2R_VOL_MASK
) >> WM8350_OUT1R_VOL_SHIFT
;
1598 wm8350_reg_write(wm8350
, WM8350_LOUT1_VOLUME
, 0);
1599 wm8350_reg_write(wm8350
, WM8350_ROUT1_VOLUME
, 0);
1600 wm8350_reg_write(wm8350
, WM8350_LOUT2_VOLUME
, 0);
1601 wm8350_reg_write(wm8350
, WM8350_ROUT2_VOLUME
, 0);
1603 /* Latch VU bits & mute */
1604 wm8350_set_bits(wm8350
, WM8350_LOUT1_VOLUME
,
1605 WM8350_OUT1_VU
| WM8350_OUT1L_MUTE
);
1606 wm8350_set_bits(wm8350
, WM8350_LOUT2_VOLUME
,
1607 WM8350_OUT2_VU
| WM8350_OUT2L_MUTE
);
1608 wm8350_set_bits(wm8350
, WM8350_ROUT1_VOLUME
,
1609 WM8350_OUT1_VU
| WM8350_OUT1R_MUTE
);
1610 wm8350_set_bits(wm8350
, WM8350_ROUT2_VOLUME
,
1611 WM8350_OUT2_VU
| WM8350_OUT2R_MUTE
);
1613 /* Make sure AIF tristating is disabled by default */
1614 wm8350_clear_bits(wm8350
, WM8350_AI_FORMATING
, WM8350_AIF_TRI
);
1616 /* Make sure we've got a sane companding setup too */
1617 wm8350_clear_bits(wm8350
, WM8350_ADC_DAC_COMP
,
1618 WM8350_DAC_COMP
| WM8350_LOOPBACK
);
1620 /* Make sure jack detect is disabled to start off with */
1621 wm8350_clear_bits(wm8350
, WM8350_JACK_DETECT
,
1622 WM8350_JDL_ENA
| WM8350_JDR_ENA
);
1624 wm8350_register_irq(wm8350
, WM8350_IRQ_CODEC_JCK_DET_L
,
1625 wm8350_hp_jack_handler
, 0, "Left jack detect",
1627 wm8350_register_irq(wm8350
, WM8350_IRQ_CODEC_JCK_DET_R
,
1628 wm8350_hp_jack_handler
, 0, "Right jack detect",
1630 wm8350_register_irq(wm8350
, WM8350_IRQ_CODEC_MICSCD
,
1631 wm8350_mic_handler
, 0, "Microphone short", priv
);
1632 wm8350_register_irq(wm8350
, WM8350_IRQ_CODEC_MICD
,
1633 wm8350_mic_handler
, 0, "Microphone detect", priv
);
1636 snd_soc_add_controls(codec
, wm8350_snd_controls
,
1637 ARRAY_SIZE(wm8350_snd_controls
));
1638 wm8350_add_widgets(codec
);
1640 wm8350_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1649 static int wm8350_codec_remove(struct snd_soc_codec
*codec
)
1651 struct wm8350_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1652 struct wm8350
*wm8350
= dev_get_platdata(codec
->dev
);
1654 wm8350_clear_bits(wm8350
, WM8350_JACK_DETECT
,
1655 WM8350_JDL_ENA
| WM8350_JDR_ENA
);
1656 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_4
, WM8350_TOCLK_ENA
);
1658 wm8350_free_irq(wm8350
, WM8350_IRQ_CODEC_MICD
, priv
);
1659 wm8350_free_irq(wm8350
, WM8350_IRQ_CODEC_MICSCD
, priv
);
1660 wm8350_free_irq(wm8350
, WM8350_IRQ_CODEC_JCK_DET_L
, priv
);
1661 wm8350_free_irq(wm8350
, WM8350_IRQ_CODEC_JCK_DET_R
, priv
);
1663 priv
->hpl
.jack
= NULL
;
1664 priv
->hpr
.jack
= NULL
;
1665 priv
->mic
.jack
= NULL
;
1667 cancel_delayed_work_sync(&priv
->hpl
.work
);
1668 cancel_delayed_work_sync(&priv
->hpr
.work
);
1670 /* if there was any work waiting then we run it now and
1671 * wait for its completion */
1672 flush_delayed_work_sync(&codec
->dapm
.delayed_work
);
1674 wm8350_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1676 wm8350_clear_bits(wm8350
, WM8350_POWER_MGMT_5
, WM8350_CODEC_ENA
);
1678 regulator_bulk_free(ARRAY_SIZE(priv
->supplies
), priv
->supplies
);
1683 static struct snd_soc_codec_driver soc_codec_dev_wm8350
= {
1684 .probe
= wm8350_codec_probe
,
1685 .remove
= wm8350_codec_remove
,
1686 .suspend
= wm8350_suspend
,
1687 .resume
= wm8350_resume
,
1688 .read
= wm8350_codec_read
,
1689 .write
= wm8350_codec_write
,
1690 .set_bias_level
= wm8350_set_bias_level
,
1693 static int __devinit
wm8350_probe(struct platform_device
*pdev
)
1695 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_wm8350
,
1699 static int __devexit
wm8350_remove(struct platform_device
*pdev
)
1701 snd_soc_unregister_codec(&pdev
->dev
);
1705 static struct platform_driver wm8350_codec_driver
= {
1707 .name
= "wm8350-codec",
1708 .owner
= THIS_MODULE
,
1710 .probe
= wm8350_probe
,
1711 .remove
= __devexit_p(wm8350_remove
),
1714 static __init
int wm8350_init(void)
1716 return platform_driver_register(&wm8350_codec_driver
);
1718 module_init(wm8350_init
);
1720 static __exit
void wm8350_exit(void)
1722 platform_driver_unregister(&wm8350_codec_driver
);
1724 module_exit(wm8350_exit
);
1726 MODULE_DESCRIPTION("ASoC WM8350 driver");
1727 MODULE_AUTHOR("Liam Girdwood");
1728 MODULE_LICENSE("GPL");
1729 MODULE_ALIAS("platform:wm8350-codec");