Skip to contentSkip to conversation

ASoC: mid-x86 add jack detect support

PATCH In review Harsha Priya 3 participants 4 messages Jan 19, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 4/4 ASoC: mid-x86 add jack detect support

This patch adds the jack detection in machine driver and adds the jack detect logic in sn95031 codec. This also adds code for reading the ADC to figure out the mic bias and therby report jack type detected

---
 sound/soc/codecs/sn95031.c             |   82 +++++++++++++-
 sound/soc/codecs/sn95031.h             |   20 ++++
 sound/soc/codecs/sn95031_adc_control.h |  194 ++++++++++++++++++++++++++++++++
 sound/soc/mid-x86/mfld_machine.c       |  159 +++++++++++++++++++++-----
 4 files changed, 424 insertions(+), 31 deletions(-)
 create mode 100644 sound/soc/codecs/sn95031_adc_control.h
sound/soc/codecs/sn95031.c +79 -3
index 5e8d091..c436311 100644
@@ -27,6 +27,7 @@
 
 #include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/input.h>
 #include <asm/intel_scu_ipc.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
@@ -35,13 +36,15 @@
 #include <sound/initval.h>
 #include <sound/tlv.h>
 #include "sn95031.h"
+#include "sn95031_adc_control.h"
 
 #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
 #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
 
+struct snd_soc_codec *sn95031_codec;
+
 /*
  * todo:
- * jack detection
  * PM functions
  */
 
@@ -113,6 +116,15 @@ static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
 	return 0;
 }
 
+/* enables mic bias voltage */
+void sn95031_enable_mic_bias(struct snd_soc_codec *codec)
+{
+	snd_soc_write(codec, SN95031_VAUD,
+			BIT(2)|BIT(1)|BIT(0));
+	snd_soc_update_bits(codec, SN95031_MICBIAS,
+				BIT(2), BIT(2));
+}
+
 static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
 		    struct snd_kcontrol *kcontrol, int event)
 {
@@ -310,6 +322,7 @@ static const struct snd_kcontrol_new sn95031_snd_controls[] = {
 static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
 
 	/* all end points mic, hs etc */
+	SND_SOC_DAPM_HP("HeadPhones", NULL),
 	SND_SOC_DAPM_OUTPUT("HPOUTL"),
 	SND_SOC_DAPM_OUTPUT("HPOUTR"),
 	SND_SOC_DAPM_OUTPUT("EPOUT"),
@@ -428,6 +441,8 @@ static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
 
 static const struct snd_soc_dapm_route sn95031_audio_map[] = {
 	/* headset and earpiece map */
+	{ "HeadPhones", NULL, "HPOUTR" },
+	{ "HeadPhones", NULL, "HPOUTL" },
 	{ "HPOUTL", NULL, "Headset Left Playback" },
 	{ "HPOUTR", NULL, "Headset Right Playback" },
 	{ "EPOUT", NULL, "Earpiece Playback" },
@@ -658,6 +673,66 @@ struct snd_soc_dai_driver sn95031_dais[] = {
 },
 };
 
+int sn95031_get_headset_state(void)
+{
+	unsigned int mic_bias = sn95031_get_mic_bias(sn95031_codec);
+
+	if (mic_bias >= SN_HP_START && mic_bias < SN_HP_END) {
+		pr_debug("sst: Detected Headphone!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+	} else if (mic_bias >= SN_AM_HS_START && mic_bias < SN_AM_HS_END) {
+		pr_debug("sst: Detected American headset\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(1));
+	} else if (mic_bias >= SN_HS_START && mic_bias < SN_HS_END) {
+		pr_debug("sst: Detected Headset!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+		enable_jack_btn(sn95031_codec);
+		return SND_JACK_HEADSET;
+	} else
+		pr_debug("sst: Detected Open Cable!!!\n");
+
+	return SND_JACK_HEADPHONE;
+}
+
+void sn95031_jack_detection(struct snd_soc_jack *mfld_jack,
+		struct mfld_jack_msg_wq *jack_msg)
+{
+	unsigned int status, mask;
+	static int jack_state = SND_JACK_HEADSET;
+
+	if (!sn95031_codec) {
+		pr_err("codec is null\n");
+		return;
+	}
+	pr_debug("interrupt id read in sram = 0x%x\n", jack_msg->intr_id);
+	if (jack_msg->intr_id & 0x1) {
+		pr_debug("short_push detected\n");
+		mask = status = SND_JACK_BTN_0;
+	} else if (jack_msg->intr_id & 0x2) {
+		pr_debug("long_push detected\n");
+		mask = status = SND_JACK_BTN_1;
+	} else if (jack_msg->intr_id & 0x4) {
+		mask = status = sn95031_get_headset_state();
+		jack_state = mask;
+	} else if (jack_msg->intr_id & 0x8) {
+		pr_debug("headset or headphones removed\n");
+		status = 0;
+		mask = jack_state;
+		disable_jack_btn(sn95031_codec);
+	} else {
+		pr_err("unidentified interrupt\n");
+		return;
+	}
+
+	snd_soc_jack_report(mfld_jack, status, mask);
+	/*button pressed and released */
+	if (status == SND_JACK_BTN_0 || status == SND_JACK_BTN_1)
+		snd_soc_jack_report(mfld_jack, 0, mask);
+}
+EXPORT_SYMBOL_GPL(sn95031_jack_detection);
 /* codec registration */
 static int sn95031_codec_probe(struct snd_soc_codec *codec)
 {
@@ -667,6 +742,7 @@ static int sn95031_codec_probe(struct snd_soc_codec *codec)
 
 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
 	codec->dapm.idle_bias_off = 1;
+	sn95031_codec = codec;
 
 	/* PCM interface config
 	 * This sets the pcm rx slot conguration to max 6 slots
@@ -733,7 +809,7 @@ static int sn95031_codec_remove(struct snd_soc_codec *codec)
 	return 0;
 }
 
-struct snd_soc_codec_driver sn95031_codec = {
+struct snd_soc_codec_driver sn95031_soc_codec = {
 	.probe		= sn95031_codec_probe,
 	.remove		= sn95031_codec_remove,
 	.read		= sn95031_read,
@@ -744,7 +820,7 @@ struct snd_soc_codec_driver sn95031_codec = {
 static int __devinit sn95031_device_probe(struct platform_device *pdev)
 {
 	pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
-	return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
+	return snd_soc_register_codec(&pdev->dev, &sn95031_soc_codec,
 			sn95031_dais, ARRAY_SIZE(sn95031_dais));
 }
 
sound/soc/codecs/sn95031.h +20 -0
index e2b17d9..e79176d 100644
@@ -25,6 +25,7 @@
  */
 #ifndef _SN95031_H
 #define _SN95031_H
+#include <sound/jack.h>
 
 /*register map*/
 #define SN95031_VAUD			0xDB
@@ -96,4 +97,23 @@
 #define SN95031_SSR5			0x384
 #define SN95031_SSR6			0x385
 
+struct mfld_jack_msg_wq {
+	u8 intr_id;
+	struct work_struct wq;
+};
+
+static inline void disable_jack_btn(struct snd_soc_codec *codec)
+{
+	snd_soc_write(codec, SN95031_BTNCTRL2, 0x00);
+}
+
+static inline void enable_jack_btn(struct snd_soc_codec *codec)
+{
+	snd_soc_write(codec, SN95031_BTNCTRL1, 0x77);
+	snd_soc_write(codec, SN95031_BTNCTRL2, 0x01);
+}
+
+extern void sn95031_jack_detection(struct snd_soc_jack *mfld_jack,
+		struct mfld_jack_msg_wq *jack_msg);
+
 #endif
sound/soc/codecs/sn95031_adc_control.h new file +194 -0

194 lines not on this page · raw message

sound/soc/mid-x86/mfld_machine.c +131 -28

236 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#1 lore raw
Quoted 4 lines · Harsha Priya
 sound/soc/codecs/sn95031.c             |   82 +++++++++++++-
 sound/soc/codecs/sn95031.h             |   20 ++++
 sound/soc/codecs/sn95031_adc_control.h |  194 ++++++++++++++++++++++++++++++++
 sound/soc/mid-x86/mfld_machine.c       |  159 +++++++++++++++++++++-----

This should be split into two: one patch adding the feature in the CODEC, the other adding the use of the feature in the machine driver.

sound/soc/codecs/sn95031.c line 45

Quoted 2 lines
sound/soc/codecs/sn95031.c +2 -0
+struct snd_soc_codec *sn95031_codec;
+

Why do we need a global variable?

sound/soc/codecs/sn95031.c static const struct snd_kcontrol_new sn95031_snd_controls[] = { · line 325

Quoted 2 lines
sound/soc/codecs/sn95031.c +1 -0
 	/* all end points mic, hs etc */
+	SND_SOC_DAPM_HP("HeadPhones", NULL),

Again this is a machine driver widget.

Quoted 10 lines
sound/soc/codecs/sn95031.c +10 -0
+int sn95031_get_headset_state(void)
+{
+	unsigned int mic_bias = sn95031_get_mic_bias(sn95031_codec);
+
+	if (mic_bias >= SN_HP_START && mic_bias < SN_HP_END) {
+		pr_debug("sst: Detected Headphone!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+	} else if (mic_bias >= SN_AM_HS_START && mic_bias < SN_AM_HS_END) {
+		pr_debug("sst: Detected American headset\n");

sound/soc/codecs/sn95031.c struct snd_soc_dai_driver sn95031_dais[] = { · line 695

Quoted 10 lines
sound/soc/codecs/sn95031.c +10 -0
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(1));
+	} else if (mic_bias >= SN_HS_START && mic_bias < SN_HS_END) {
+		pr_debug("sst: Detected Headset!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+		enable_jack_btn(sn95031_codec);
+		return SND_JACK_HEADSET;
+	} else
+		pr_debug("sst: Detected Open Cable!!!\n");

What exactly is this doing? It looks like it's measuring the voltage on micbias and using that to identify the accessory connected? That's normally a machine specific thing - different manufacturers have different ideas about what they want to do with this. Some will do things like have multiple buttons on the jack with different resistances connected between them and measure the voltage to determine which button has been pressed, take a look at the Nexus S kernel for one example.

Looking at all this fairly briefly it looks like all the CODEC is doing is providing an ADC and possibly some signals fed directly into the CPU. The detection then mostly belongs externally to the CODEC driver - the CPU then generates interrupts and the machine driver goes off and does some ADC readings using the facility provided by the CODEC.

Since this is a common pattern we should ideally have some utility code which can take any table of ADC values in conjunction with any ADC and use those to do detection.

sound/soc/codecs/sn95031_adc_control.h line 130

Quoted 5 lines
sound/soc/codecs/sn95031_adc_control.h new file +5 -0
+static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
+{
+	int base_addr, chnl_addr;
+	int value;
+	static int channel_index;

The functions added in this header look like they'd be better placed in the source file?

#2 lore raw
Quoted 2 lines

This should be split into two: one patch adding the feature in the CODEC, the other adding the use of the feature in the machine driver.

Agreed

Quoted 4 lines

sound/soc/codecs/sn95031.c static const struct snd_kcontrol_new sn95031_snd_controls[] = { · line 325

Quoted 2 lines
sound/soc/codecs/sn95031.c +1 -0
 	/* all end points mic, hs etc */
+	SND_SOC_DAPM_HP("HeadPhones", NULL),

Again this is a machine driver widget.

ACK

Quoted 35 lines
Quoted 10 lines
sound/soc/codecs/sn95031.c +10 -0
+int sn95031_get_headset_state(void)
+{
+	unsigned int mic_bias = sn95031_get_mic_bias(sn95031_codec);
+
+	if (mic_bias >= SN_HP_START && mic_bias < SN_HP_END) {
+		pr_debug("sst: Detected Headphone!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+	} else if (mic_bias >= SN_AM_HS_START && mic_bias < SN_AM_HS_END) {
+		pr_debug("sst: Detected American headset\n");

sound/soc/codecs/sn95031.c struct snd_soc_dai_driver sn95031_dais[] = { · line 695

Quoted 10 lines
sound/soc/codecs/sn95031.c +10 -0
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(1));
+	} else if (mic_bias >= SN_HS_START && mic_bias < SN_HS_END) {
+		pr_debug("sst: Detected Headset!!!\n");
+		snd_soc_update_bits(sn95031_codec, AUDIO_GPIO_CTRL,
+					BIT(1), BIT(0));
+		enable_jack_btn(sn95031_codec);
+		return SND_JACK_HEADSET;
+	} else
+		pr_debug("sst: Detected Open Cable!!!\n");

What exactly is this doing? It looks like it's measuring the voltage on micbias and using that to identify the accessory connected? That's normally a machine specific thing - different manufacturers have different ideas about what they want to do with this. Some will do things like have multiple buttons on the jack with different resistances connected between them and measure the voltage to determine which button has been pressed, take a look at the Nexus S kernel for one example.

Looking at all this fairly briefly it looks like all the CODEC is doing is providing an ADC and possibly some signals fed directly into the CPU. The detection then mostly belongs externally to the CODEC driver - the CPU then generates interrupts and the machine driver goes off and does some ADC readings using the facility provided by the CODEC.

Quoted 3 lines

Since this is a common pattern we should ideally have some utility code which can take any table of ADC values in conjunction with any ADC and use those to do detection.

Yes the codec reads into the ADC to measure the micbias voltage. It has map where we know based on micbais present what kind of accessory maybe connected. Since this is entirely codec domain it should be in codec. Let me know if you disagree.

Quoted 9 lines

sound/soc/codecs/sn95031_adc_control.h line 130

Quoted 6 lines
sound/soc/codecs/sn95031_adc_control.h new file +5 -0
+static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
+{
+	int base_addr, chnl_addr;
+	int value;
+	static int channel_index;

The functions added in this header look like they'd be better placed in the source file?

Typically ADC should be a separate driver but we don't have that yet so though of adding these as helpers.
When we get a proper adc driver we can replace this with code calling adc.

~Vinod

#3 lore raw
Quoted 8 lines · Vinod
Quoted 3 lines

Since this is a common pattern we should ideally have some utility code which can take any table of ADC values in conjunction with any ADC and use those to do detection.

Yes the codec reads into the ADC to measure the micbias voltage. It has map where we know based on micbais present what kind of accessory maybe connected. Since this is entirely codec domain it should be in codec. Let me know if you disagree.

Well, the ADC is clearly part of the CODEC but the logic surrounding walking through the table and coming up with jack statuses to report based off it isn't, that's a generic thing which should probably be sitting along with soc-jack.c (possibly in there, possibly as a separate file).

Quoted 9 lines
Quoted 8 lines

sound/soc/codecs/sn95031_adc_control.h line 130

Quoted 5 lines
sound/soc/codecs/sn95031_adc_control.h new file +5 -0
+static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
+{
+	int base_addr, chnl_addr;
+	int value;
+	static int channel_index;

The functions added in this header look like they'd be better placed in the source file?

Quoted 3 lines

Typically ADC should be a separate driver but we don't have that yet so though of adding these as helpers.
When we get a proper adc driver we can replace this with code calling adc.

I'd suggest putting the bodies of the function into the main CODEC driver for now.