Skip to content

Commit 821edd2

Browse files
committed
ASoC: Add WM8958 microphone detection support
The WM8958 contains an advanced accessory detection feature which allows detection of up to seven different impedence levels on the microphone bias output, including detection of video outputs. Since some of the more involved accessory interfaces may involve noticable interactions with external components a simple detection scheme is provided by default with the option to provide custom handling of accessory detect. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Liam Girdwood <lrg@slimlogic.co.uk>
1 parent d6addcc commit 821edd2

3 files changed

Lines changed: 197 additions & 0 deletions

File tree

‎include/linux/mfd/wm8994/registers.h‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
#define WM8994_DC_SERVO_4 0x57
7171
#define WM8994_DC_SERVO_READBACK 0x58
7272
#define WM8994_ANALOGUE_HP_1 0x60
73+
#define WM8958_MIC_DETECT_1 0xD0
74+
#define WM8958_MIC_DETECT_2 0xD1
75+
#define WM8958_MIC_DETECT_3 0xD2
7376
#define WM8994_CHIP_REVISION 0x100
7477
#define WM8994_CONTROL_INTERFACE 0x101
7578
#define WM8994_WRITE_SEQUENCER_CTRL_1 0x110
@@ -1970,6 +1973,46 @@
19701973
#define WM8994_HPOUT1R_DLY_SHIFT 1 /* HPOUT1R_DLY */
19711974
#define WM8994_HPOUT1R_DLY_WIDTH 1 /* HPOUT1R_DLY */
19721975

1976+
/*
1977+
* R208 (0xD0) - Mic Detect 1
1978+
*/
1979+
#define WM8958_MICD_BIAS_STARTTIME_MASK 0xF000 /* MICD_BIAS_STARTTIME - [15:12] */
1980+
#define WM8958_MICD_BIAS_STARTTIME_SHIFT 12 /* MICD_BIAS_STARTTIME - [15:12] */
1981+
#define WM8958_MICD_BIAS_STARTTIME_WIDTH 4 /* MICD_BIAS_STARTTIME - [15:12] */
1982+
#define WM8958_MICD_RATE_MASK 0x0F00 /* MICD_RATE - [11:8] */
1983+
#define WM8958_MICD_RATE_SHIFT 8 /* MICD_RATE - [11:8] */
1984+
#define WM8958_MICD_RATE_WIDTH 4 /* MICD_RATE - [11:8] */
1985+
#define WM8958_MICD_DBTIME 0x0002 /* MICD_DBTIME */
1986+
#define WM8958_MICD_DBTIME_MASK 0x0002 /* MICD_DBTIME */
1987+
#define WM8958_MICD_DBTIME_SHIFT 1 /* MICD_DBTIME */
1988+
#define WM8958_MICD_DBTIME_WIDTH 1 /* MICD_DBTIME */
1989+
#define WM8958_MICD_ENA 0x0001 /* MICD_ENA */
1990+
#define WM8958_MICD_ENA_MASK 0x0001 /* MICD_ENA */
1991+
#define WM8958_MICD_ENA_SHIFT 0 /* MICD_ENA */
1992+
#define WM8958_MICD_ENA_WIDTH 1 /* MICD_ENA */
1993+
1994+
/*
1995+
* R209 (0xD1) - Mic Detect 2
1996+
*/
1997+
#define WM8958_MICD_LVL_SEL_MASK 0x00FF /* MICD_LVL_SEL - [7:0] */
1998+
#define WM8958_MICD_LVL_SEL_SHIFT 0 /* MICD_LVL_SEL - [7:0] */
1999+
#define WM8958_MICD_LVL_SEL_WIDTH 8 /* MICD_LVL_SEL - [7:0] */
2000+
2001+
/*
2002+
* R210 (0xD2) - Mic Detect 3
2003+
*/
2004+
#define WM8958_MICD_LVL_MASK 0x07FC /* MICD_LVL - [10:2] */
2005+
#define WM8958_MICD_LVL_SHIFT 2 /* MICD_LVL - [10:2] */
2006+
#define WM8958_MICD_LVL_WIDTH 9 /* MICD_LVL - [10:2] */
2007+
#define WM8958_MICD_VALID 0x0002 /* MICD_VALID */
2008+
#define WM8958_MICD_VALID_MASK 0x0002 /* MICD_VALID */
2009+
#define WM8958_MICD_VALID_SHIFT 1 /* MICD_VALID */
2010+
#define WM8958_MICD_VALID_WIDTH 1 /* MICD_VALID */
2011+
#define WM8958_MICD_STS 0x0001 /* MICD_STS */
2012+
#define WM8958_MICD_STS_MASK 0x0001 /* MICD_STS */
2013+
#define WM8958_MICD_STS_SHIFT 0 /* MICD_STS */
2014+
#define WM8958_MICD_STS_WIDTH 1 /* MICD_STS */
2015+
19732016
/*
19742017
* R256 (0x100) - Chip Revision
19752018
*/

‎sound/soc/codecs/wm8994.c‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/regulator/consumer.h>
2222
#include <linux/slab.h>
2323
#include <sound/core.h>
24+
#include <sound/jack.h>
2425
#include <sound/pcm.h>
2526
#include <sound/pcm_params.h>
2627
#include <sound/soc.h>
@@ -95,6 +96,11 @@ struct wm8994_priv {
9596

9697
struct wm8994_micdet micdet[2];
9798

99+
wm8958_micdet_cb jack_cb;
100+
void *jack_cb_data;
101+
bool jack_is_mic;
102+
bool jack_is_video;
103+
98104
int revision;
99105
struct wm8994_pdata *pdata;
100106
};
@@ -140,6 +146,7 @@ static int wm8994_volatile(unsigned int reg)
140146
case WM8994_LDO_1:
141147
case WM8994_LDO_2:
142148
case WM8958_DSP2_EXECCONTROL:
149+
case WM8958_MIC_DETECT_3:
143150
return 1;
144151
default:
145152
return 0;
@@ -2633,6 +2640,133 @@ static irqreturn_t wm8994_mic_irq(int irq, void *data)
26332640
return IRQ_HANDLED;
26342641
}
26352642

2643+
/* Default microphone detection handler for WM8958 - the user can
2644+
* override this if they wish.
2645+
*/
2646+
static void wm8958_default_micdet(u16 status, void *data)
2647+
{
2648+
struct snd_soc_codec *codec = data;
2649+
struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2650+
int report = 0;
2651+
2652+
/* If nothing present then clear our statuses */
2653+
if (!(status & WM8958_MICD_STS)) {
2654+
wm8994->jack_is_video = false;
2655+
wm8994->jack_is_mic = false;
2656+
goto done;
2657+
}
2658+
2659+
/* Assume anything over 475 ohms is a microphone and remember
2660+
* that we've seen one (since buttons override it) */
2661+
if (status & 0x600)
2662+
wm8994->jack_is_mic = true;
2663+
if (wm8994->jack_is_mic)
2664+
report |= SND_JACK_MICROPHONE;
2665+
2666+
/* Video has an impedence of approximately 75 ohms; assume
2667+
* this isn't used as a button and remember it since buttons
2668+
* override it. */
2669+
if (status & 0x40)
2670+
wm8994->jack_is_video = true;
2671+
if (wm8994->jack_is_video)
2672+
report |= SND_JACK_VIDEOOUT;
2673+
2674+
/* Everything else is buttons; just assign slots */
2675+
if (status & 0x4)
2676+
report |= SND_JACK_BTN_0;
2677+
if (status & 0x8)
2678+
report |= SND_JACK_BTN_1;
2679+
if (status & 0x10)
2680+
report |= SND_JACK_BTN_2;
2681+
if (status & 0x20)
2682+
report |= SND_JACK_BTN_3;
2683+
if (status & 0x80)
2684+
report |= SND_JACK_BTN_4;
2685+
if (status & 0x100)
2686+
report |= SND_JACK_BTN_5;
2687+
2688+
done:
2689+
snd_soc_jack_report(wm8994->micdet[0].jack,
2690+
SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2 |
2691+
SND_JACK_BTN_3 | SND_JACK_BTN_4 | SND_JACK_BTN_5 |
2692+
SND_JACK_MICROPHONE | SND_JACK_VIDEOOUT,
2693+
report);
2694+
}
2695+
2696+
/**
2697+
* wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
2698+
*
2699+
* @codec: WM8958 codec
2700+
* @jack: jack to report detection events on
2701+
*
2702+
* Enable microphone detection functionality for the WM8958. By
2703+
* default simple detection which supports the detection of up to 6
2704+
* buttons plus video and microphone functionality is supported.
2705+
*
2706+
* The WM8958 has an advanced jack detection facility which is able to
2707+
* support complex accessory detection, especially when used in
2708+
* conjunction with external circuitry. In order to provide maximum
2709+
* flexiblity a callback is provided which allows a completely custom
2710+
* detection algorithm.
2711+
*/
2712+
int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2713+
wm8958_micdet_cb cb, void *cb_data)
2714+
{
2715+
struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2716+
struct wm8994 *control = codec->control_data;
2717+
2718+
if (control->type != WM8958)
2719+
return -EINVAL;
2720+
2721+
if (jack) {
2722+
if (!cb) {
2723+
dev_dbg(codec->dev, "Using default micdet callback\n");
2724+
cb = wm8958_default_micdet;
2725+
cb_data = codec;
2726+
}
2727+
2728+
wm8994->micdet[0].jack = jack;
2729+
wm8994->jack_cb = cb;
2730+
wm8994->jack_cb_data = cb_data;
2731+
2732+
snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2733+
WM8958_MICD_ENA, WM8958_MICD_ENA);
2734+
} else {
2735+
snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2736+
WM8958_MICD_ENA, 0);
2737+
}
2738+
2739+
return 0;
2740+
}
2741+
EXPORT_SYMBOL_GPL(wm8958_mic_detect);
2742+
2743+
static irqreturn_t wm8958_mic_irq(int irq, void *data)
2744+
{
2745+
struct wm8994_priv *wm8994 = data;
2746+
struct snd_soc_codec *codec = wm8994->codec;
2747+
int reg;
2748+
2749+
reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
2750+
if (reg < 0) {
2751+
dev_err(codec->dev, "Failed to read mic detect status: %d\n",
2752+
reg);
2753+
return IRQ_NONE;
2754+
}
2755+
2756+
if (!(reg & WM8958_MICD_VALID)) {
2757+
dev_dbg(codec->dev, "Mic detect data not valid\n");
2758+
goto out;
2759+
}
2760+
2761+
if (wm8994->jack_cb)
2762+
wm8994->jack_cb(reg, wm8994->jack_cb_data);
2763+
else
2764+
dev_warn(codec->dev, "Accessory detection with no callback\n");
2765+
2766+
out:
2767+
return IRQ_HANDLED;
2768+
}
2769+
26362770
static int wm8994_codec_probe(struct snd_soc_codec *codec)
26372771
{
26382772
struct wm8994 *control;
@@ -2732,6 +2866,17 @@ static int wm8994_codec_probe(struct snd_soc_codec *codec)
27322866
"Failed to request Mic2 short IRQ: %d\n",
27332867
ret);
27342868
break;
2869+
2870+
case WM8958:
2871+
ret = wm8994_request_irq(codec->control_data,
2872+
WM8994_IRQ_MIC1_DET,
2873+
wm8958_mic_irq, "Mic detect",
2874+
wm8994);
2875+
if (ret != 0)
2876+
dev_warn(codec->dev,
2877+
"Failed to request Mic detect IRQ: %d\n",
2878+
ret);
2879+
break;
27352880
}
27362881

27372882
/* Remember if AIFnLRCLK is configured as a GPIO. This should be
@@ -2866,6 +3011,11 @@ static int wm8994_codec_remove(struct snd_soc_codec *codec)
28663011
wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
28673012
wm8994);
28683013
break;
3014+
3015+
case WM8958:
3016+
wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3017+
wm8994);
3018+
break;
28693019
}
28703020
kfree(wm8994->retune_mobile_texts);
28713021
kfree(wm8994->drc_texts);

‎sound/soc/codecs/wm8994.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
#define WM8994_FLL_SRC_LRCLK 3
2929
#define WM8994_FLL_SRC_BCLK 4
3030

31+
typedef void (*wm8958_micdet_cb)(u16 status, void *data);
32+
3133
int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3234
int micbias, int det, int shrt);
35+
int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
36+
wm8958_micdet_cb cb, void *cb_data);
3337

3438
#define WM8994_CACHE_SIZE 1570
3539

0 commit comments

Comments
 (0)