ASoC: rt5677: Include gpio driver header
Subsystems (3)
PATCH 1/3 ASoC: rt5677: Include gpio driver header
The header file is needed because struct gpio_chip is placed in struct rt5677_priv.
--- sound/soc/codecs/rt5677.h | 1 + 1 file changed, 1 insertion(+)
sound/soc/codecs/rt5677.h +1 -0
@@ -13,6 +13,7 @@#define __RT5677_H__#include <sound/rt5677.h> +#include <linux/gpio/driver.h> /* Info */ #define RT5677_RESET 0x00
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH 2/3 ASoC: rt5677: Add ACPI device probing
The rt5677 codec driver looks for ACPI device ID "RT5677CE", which is specified in coreboot.
Reviewed-on: https://chromium-review.googlesource.com/207622
--- sound/soc/codecs/rt5677.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
sound/soc/codecs/rt5677.c +12 -0
@@ -20,6 +20,7 @@#include <linux/i2c.h> #include <linux/platform_device.h> #include <linux/spi/spi.h> +#include <linux/acpi.h> #include <linux/gpio.h> #include <sound/core.h> #include <sound/pcm.h>
@@ -3680,10 +3681,21 @@ static int rt5677_i2c_remove(struct i2c_client *i2c)return 0 ; } +#ifdef CONFIG_ACPI +static const struct acpi_device_id rt5677_acpi_id[] = { + {"RT5677CE" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (acpi, rt5677_acpi_id); +#endif +static struct i2c_driver rt5677_i2c_driver = { .driver = { .name ="rt5677" , .owner =THIS_MODULE , +#ifdef CONFIG_ACPI + .acpi_match_table =ACPI_PTR (rt5677_acpi_id), +#endif }, .probe = rt5677_i2c_probe, .remove = rt5677_i2c_remove,
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Mark Brown
maintainerRe: [PATCH 2/3] ASoC: rt5677: Add ACPI device probing
sound/soc/codecs/rt5677.c static int rt5677_i2c_remove(struct i2c_client *i2c) · line 3698
Quoted 3 lines · Ben Zhang
sound/soc/codecs/rt5677.c +3
-0+#ifdef CONFIG_ACPI + .acpi_match_table = ACPI_PTR(rt5677_acpi_id), +#endif
Why do you need this ifdef - surely the whole point of ACPI_PTR() is to avoid needing it?
PATCH 3/3 ASoC: rt5677: Add jack detection support
In reply to Ben ZhangThis patch adds support for jack detection using GPIOs on the codec. Plug detect signal and mic present signal can be routed from the physical audio jack to GPIOs on the codec. The codec is configured so that upon signal change, an IRQ(GPIO1) is fired. The codec interrupt handler reads related status registers, figures out what has been plugged/unplugged from the audio jack, and report jack states via snd_soc_jack_report().
ASoC machine driver should register audio jacks for detection with the codec driver using rt5677_register_jack_detect().
Board setup code should assign GPIOs receiving plug detect/mic present signal to the codec device itself, via Device Tree, ACPI or platform data. The corresponding GPIO indexes are:RT5677_GPIO_PLUG_DET - 0RT5677_GPIO_MIC_PRESENT_L - 1
--- sound/soc/codecs/rt5677.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++ sound/soc/codecs/rt5677.h | 92 ++++++++++++++++++++++++ 2 files changed, 271 insertions(+)
sound/soc/codecs/rt5677.c +179 -0
@@ -29,6 +29,7 @@#include <sound/soc-dapm.h> #include <sound/initval.h> #include <sound/tlv.h> +#include <sound/jack.h> #include "rl6231.h" #include "rt5677.h"
@@ -3341,6 +3342,25 @@ static void rt5677_free_gpio(struct i2c_client *i2c)gpiochip_remove (&rt5677->gpio_chip); } + +static void rt5677_request_codec_gpios (struct rt5677_priv *rt5677, +struct i2c_client *i2c) +{ +struct gpio_desc *desc; + + desc =devm_gpiod_get_index (&i2c->dev,"RT5677_GPIO" , +RT5677_GPIO_PLUG_DET ); +if (!IS_ERR (desc) && !gpiod_direction_input (desc)) + rt5677->gpio_plug_det =desc_to_gpio (desc) + - rt5677->gpio_chip.base +1 ; + + desc =devm_gpiod_get_index (&i2c->dev,"RT5677_GPIO" , +RT5677_GPIO_MIC_PRESENT_L ); +if (!IS_ERR (desc) && !gpiod_direction_input (desc)) + rt5677->gpio_mic_present_l =desc_to_gpio (desc) + - rt5677->gpio_chip.base +1 ; +} +#else static void rt5677_init_gpio (struct i2c_client *i2c) {
@@ -3349,8 +3369,155 @@ static void rt5677_init_gpio(struct i2c_client *i2c)static void rt5677_free_gpio (struct i2c_client *i2c) { } +static void rt5677_request_codec_gpios (struct rt5677_priv *rt5677, +struct i2c_client *i2c) +{ +}#endif +static void rt5677_report_jack_status (struct rt5677_priv *rt5677,int reg_irq) +{ +/* reg_irq: IRQ Control register MX-BDh */ +int polarity; +if (reg_irq &RT5677_JD2_STATUS_MASK ) { + polarity = reg_irq &RT5677_JD2_POLARITY_MASK ; +if (rt5677->headphone_jack) { +snd_soc_jack_report (rt5677->headphone_jack, + polarity ?SND_JACK_HEADPHONE :0 , +SND_JACK_HEADPHONE ); + } + } + +if (reg_irq &RT5677_JD3_STATUS_MASK ) { + polarity = reg_irq &RT5677_JD3_POLARITY_MASK ; +if (rt5677->mic_jack) { +snd_soc_jack_report (rt5677->mic_jack, + polarity ?0 :SND_JACK_MICROPHONE , +SND_JACK_MICROPHONE ); + } + } +} + +static irqreturn_t rt5677_irq (int unused,void *data) +{ +struct rt5677_priv *rt5677 = data; +int ret =0 , i; +bool irq_fired; +int reg_irq; + +/* +* Loop to handle interrupts until the last i2c read shows no pending +* irqs with a safeguard of 20 loops +*/ +for (i =0 ; i <20 ; i++) { +/* Read interrupt status */ + ret =regmap_read (rt5677->regmap,RT5677_IRQ_CTRL1 , ®_irq); +if (ret) +break ; + +/* Flip polarity for interrupts that just fired */ + irq_fired = false; +if (reg_irq &RT5677_JD2_STATUS_MASK ) { + reg_irq ^=RT5677_JD2_POLARITY_MASK ; + irq_fired = true; + } + +if (reg_irq &RT5677_JD3_STATUS_MASK ) { + reg_irq ^=RT5677_JD3_POLARITY_MASK ; + irq_fired = true; + } + +if (!irq_fired) +break ; + +/* Clear interrupts */ + ret =regmap_write (rt5677->regmap,RT5677_IRQ_CTRL1 , reg_irq); +if (ret) +break ; + +/* Process interrupts */ +rt5677_report_jack_status (rt5677, reg_irq); + } +return IRQ_HANDLED ; +} + +int rt5677_register_jack_detect (struct snd_soc_codec *codec, +struct snd_soc_jack *headphone_jack,struct snd_soc_jack *mic_jack) +{ +struct rt5677_priv *rt5677 =snd_soc_codec_get_drvdata (codec); + + rt5677->headphone_jack = headphone_jack; + rt5677->mic_jack = mic_jack; + +/* Report initial jack status */ +rt5677_irq (0 , rt5677); +return 0 ; +} +EXPORT_SYMBOL_GPL (rt5677_register_jack_detect); + +static void rt5677_setup_jack_detect (struct snd_soc_codec *codec, +struct rt5677_priv *rt5677) +{ +unsigned int val; + +/* Skip jack detect setup if there are no assigned GPIOs */ +if (!rt5677->gpio_plug_det && !rt5677->gpio_mic_present_l) +return ; +/* GPIO range check */ +if (rt5677->gpio_plug_det <4 || rt5677->gpio_plug_det >6 ) { +dev_err (codec->dev,"PLUG_DET can only use GPIO 4~6, given %d\n" , + rt5677->gpio_plug_det); + } +if (rt5677->gpio_mic_present_l <4 || rt5677->gpio_mic_present_l >6 ) { +dev_err (codec->dev,"MIC_PRESENT_L can only use GPIO 4~6, given %d\n" , + rt5677->gpio_mic_present_l); + } + +/* +* Select RC as the debounce clock so that GPIO works even when +* MCLK is gated which happens when there is no audio stream +* (SND_SOC_BIAS_OFF). +*/ +regmap_update_bits (rt5677->regmap,RT5677_DIG_MISC , +RT5677_IRQ_DEBOUNCE_SEL_MASK , +RT5677_IRQ_DEBOUNCE_SEL_RC ); +/* Enable auto power on RC when GPIO states are changed */ + val =RT5677_AUTO_RC_ON_GPIO_CHANGE_MASK ; +if (rt5677->gpio_plug_det) + val |=1 << (rt5677->gpio_plug_det -1 ); +if (rt5677->gpio_mic_present_l) + val |=1 << (rt5677->gpio_mic_present_l -1 ); +regmap_update_bits (rt5677->regmap,RT5677_GEN_CTRL1 , val, val); + +/* +* Select jack detection source +* JD2: PLUG_DET +* JD3: !MIC_PRESENT_L +*/ + val =0 ; +if (rt5677->gpio_plug_det) + val |= (rt5677->gpio_plug_det -3 ) + <<RT5677_JD2_SRC_SEL_SFT ; +if (rt5677->gpio_mic_present_l) + val |= (rt5677->gpio_mic_present_l -3 ) + <<RT5677_JD3_SRC_SEL_SFT ; +regmap_update_bits (rt5677->regmap,RT5677_JD_CTRL1 , +RT5677_JD2_SRC_SEL_MASK |RT5677_JD3_SRC_SEL_MASK , + val); + +/* Enable JD2 and/or JD3 as IRQ source */ + val =0 ; +if (rt5677->gpio_plug_det) + val |=RT5677_JD2_IRQ_EN_MASK ; +if (rt5677->gpio_mic_present_l) + val |=RT5677_JD3_IRQ_EN_MASK |RT5677_JD3_POLARITY_INV ; +regmap_update_bits (rt5677->regmap,RT5677_IRQ_CTRL1 , val, val); + +/* Set GPIO1 to be IRQ */ +regmap_update_bits (rt5677->regmap,RT5677_GPIO_CTRL1 , +RT5677_GPIO1_PIN_MASK ,RT5677_GPIO1_PIN_IRQ ); +} +static int rt5677_probe (struct snd_soc_codec *codec) {struct rt5677_priv *rt5677 =snd_soc_codec_get_drvdata (codec);
@@ -3372,6 +3539,7 @@ static int rt5677_probe(struct snd_soc_codec *codec)regmap_write (rt5677->regmap,RT5677_DIG_MISC ,0x0020 );regmap_write (rt5677->regmap,RT5677_PWR_DSP2 ,0x0c00 ); +rt5677_setup_jack_detect (codec, rt5677);return 0 ; }
@@ -3668,6 +3836,17 @@ static int rt5677_i2c_probe(struct i2c_client *i2c, }rt5677_init_gpio (i2c); +rt5677_request_codec_gpios (rt5677, i2c); + +if (i2c->irq) { + ret =request_threaded_irq (i2c->irq,NULL , rt5677_irq, +IRQF_TRIGGER_RISING |IRQF_ONESHOT , +"rt5677" , rt5677); +if (ret) { +dev_err (&i2c->dev,"Failed to request IRQ: %d\n" , ret); +return ret; + } + }return snd_soc_register_codec (&i2c->dev, &soc_codec_dev_rt5677, rt5677_dai,ARRAY_SIZE (rt5677_dai));
sound/soc/codecs/rt5677.h +92 -0
120 lines not on this page · raw message
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Mark Brown
maintainerRe: [PATCH 3/3] ASoC: rt5677: Add jack detection support
Quoted 4 lines · Ben Zhang
This patch adds support for jack detection using GPIOs on the codec. Plug detect signal and mic present signal can be routed from the physical audio jack to GPIOs on the codec. The codec is configured so that upon signal change, an IRQ(GPIO1) is fired. The codec interrupt handler reads
This then assumes that GPIO1 was configured as the IRQ - what happens if it was connected with some other function on the board and some other GPIO was assigned as IRQ?
sound/soc/codecs/rt5677.c static void rt5677_free_gpio(struct i2c_client *i2c) · line 3355
Quoted 5 lines
sound/soc/codecs/rt5677.c +5
-0+ desc =devm_gpiod_get_index (&i2c->dev,"RT5677_GPIO" , +RT5677_GPIO_PLUG_DET ); +if (!IS_ERR (desc) && !gpiod_direction_input (desc)) + rt5677->gpio_plug_det =desc_to_gpio (desc) + - rt5677->gpio_chip.base +1 ;
I'm having trouble reading this, I think other reasonable users would too. It's not entirely clear to me what it's supposed to be doing or why we are mixing the descriptor and number based GPIO APIs.
sound/soc/codecs/rt5677.c static void rt5677_init_gpio(struct i2c_client *i2c) · line 3412
Quoted 5 lines
sound/soc/codecs/rt5677.c +5
-0+/* +* Loop to handle interrupts until the last i2c read shows no pending +* irqs with a safeguard of 20 loops +*/ +for (i =0 ; i <20 ; i++) {
Eh? Why are we doing this and why are we so sure that 20 is a good value?
Quoted 5 lines
static int
rt5677_probe(struct snd_soc_codec *codec){+ rt5677_setup_jack_detect(codec, rt5677); return 0;
This sets up GPIO1 as an interrupt without verifying that we have an interrupt assigned for it successfully and only in the ASoC level probe, not in the chip level probe. This means we might not have an interrupt at all if the board configuration was buggy and means that we'll start paying attention to the interrupt (which is requested in the i2c level probe as it should be) without configuring the chip to be in the appropriate state which means that it might start falsely flagging interrupts. I'd expect the configuration to be closer together, and to have the CODEC set up ready to generate interrupts prior to starting to listen to them.
Quoted 2 lines · Ben Zhang
The header file is needed because struct
gpio_chipis placed in structrt5677_priv.
Applied, thanks.