Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp651376yba; Mon, 1 Apr 2019 13:56:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZQxmTgozuSARKKBmcPMCZE+6l3JwF0nQXkhZyS+qCON4M4aTa1Mslt4ZV1A3eKyW94eAB X-Received: by 2002:a63:be02:: with SMTP id l2mr40886340pgf.48.1554152182576; Mon, 01 Apr 2019 13:56:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554152182; cv=none; d=google.com; s=arc-20160816; b=eo/5DSwSo0tZ4gHz9ZApmrJLi1X+f66ZvDUcuuE3q3lhW5zUXm9W+QNdicWMgP9XGx 3JDDcN29hlRbqc4UrG0mKE3rOwi9UgX7Vzm20UXnjrNwzSk2bTHJv8YVH4yj2hOu1kij H5j/GwDY2vZHHm3TkJhCBBKNzKKe6t1270Ga1xy3XRmt9ToohXmirMNm51b3xGpjT9ys dpjdR4ODxGzdE3w2IYTpsZFYgm7Llzz7Z7W3g0uQ3LNwiIHsumsG8R17QhfmSgglz4IG tp4WcXarAK8LOXyvZC12cWuIGV2ifzJlN2JOybfkHFAsoyBjstgTbU6Y5iAbE+7Oh5JI juFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=JK47rU0DWd1+5spxdfOX495JieVPe3JN24lbHksimy8=; b=tP3jNGTwqj/v+e8BbjCAy7GYV5Bi0PjZ7sCMlDP3Qcp/n7OG8Olrqf1Bzg9sBeIXLa RV2H5aQ+uSCX4upEr7v2nVVn5j34SSXT3tG+6NAAM9Nz8E6VP0//UvntdAk1iurrAVUB rKcx21dPc/2BYUueQs4FTMkYOx5sy7v/2gFnR3sDrxst/on4G4bgnRDUlCHEhrNWS0Q8 pOBo64vVWSauQWt6aV79uvP3rAolb0EFh2PKqb/0G00igr6iUdYMo+U+MGdRO7CpBRB+ E5OCaVrBBVlBJbP6nm1KXlv8OgT0IeUsNFBjQPVbnTqIbRKJq8cZ7EkjHOmbY52jvM9Q LYOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bifpHM2U; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y184si9506884pgd.423.2019.04.01.13.56.06; Mon, 01 Apr 2019 13:56:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bifpHM2U; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727011AbfDAUzb (ORCPT + 99 others); Mon, 1 Apr 2019 16:55:31 -0400 Received: from mail-io1-f66.google.com ([209.85.166.66]:45329 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726725AbfDAUzb (ORCPT ); Mon, 1 Apr 2019 16:55:31 -0400 Received: by mail-io1-f66.google.com with SMTP id s7so8996505iom.12 for ; Mon, 01 Apr 2019 13:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JK47rU0DWd1+5spxdfOX495JieVPe3JN24lbHksimy8=; b=bifpHM2UsAdCBa5yaU/Iq2YV9npSkvduLCUGWl3PSd5krgjdWmBP1A2WEinmzsw3JM mL6hXJ/s93KYhfohVaXc1Ih6lxI1oYHzec5tfCkgSS3yUJfXl+Ipx3HPm5Y1OSnuyKEr ed4WxPMQbDxXUA3obofYLSWFRy9Upbl2ON7Z4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JK47rU0DWd1+5spxdfOX495JieVPe3JN24lbHksimy8=; b=q3iRx48mIfs3ZqGqv8ZFBb7cBIr8jen6WDZc7vB0E1wUG+lpVKyL5lXEeKtk+rUD2d UoBvTb4q1oMNIlN+9pjwBdQQiHM/5VZDdinwjigzJ9ZDacHvxCLBe9egaZvlgOYPH+mz BWIqtGMOM6vD7BoWdYJTmWzCJHknsqx32HwlqxXga2S5atkvhIkXcDCieicfS2sdGWb5 lFpvAdDx5PSY1I1Tglu2wx0R2HZvErnLpds5c60PXN2pHy474k1e+CTa7gHB0I4wmHTm OdQfpgrGEfguWGVTOtS7zA9qWg0z/Ivf6OSTuSqCg2xuT7uA9NP8P+DOdkJeZCJfS3qZ Fp1w== X-Gm-Message-State: APjAAAUXJptmqyPCIUkwRQIffXxD13ejF8IKp48H8QfbPtwSlkABt2B7 hk/PfTtALSuyuW9vURN7PQeWOCNJfHjAuA== X-Received: by 2002:a6b:7401:: with SMTP id s1mr41326821iog.55.1554152130141; Mon, 01 Apr 2019 13:55:30 -0700 (PDT) Received: from fletcherw.bld.corp.google.com ([2620:15c:183:200:70a8:812a:cdeb:6e0]) by smtp.gmail.com with ESMTPSA id v133sm6688570ita.19.2019.04.01.13.55.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Apr 2019 13:55:29 -0700 (PDT) From: Fletcher Woodruff To: linux-kernel@vger.kernel.org Cc: Ben Zhang , Bard Liao , Jaroslav Kysela , Liam Girdwood , Mark Brown , Oder Chiou , Takashi Iwai , Curtis Malainey , Ross Zwisler , alsa-devel@alsa-project.org, Fletcher Woodruff Subject: [PATCH 1/2] ASoC: rt5677: allow multiple interrupt sources Date: Mon, 1 Apr 2019 14:55:18 -0600 Message-Id: <20190401205519.34023-1-fletcherw@chromium.org> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ben Zhang This patch allows headphone plug detect and mic present detect to be enabled at the same time. This patch implements an irq_chip with irq_domain directly instead of using regmap_irq, so that interrupt source line polarities can be flipped to support irq sharing. Signed-off-by: Ben Zhang Signed-off-by: Fletcher Woodruff --- sound/soc/codecs/rt5677.c | 261 ++++++++++++++++++++++++++++---------- sound/soc/codecs/rt5677.h | 14 +- 2 files changed, 208 insertions(+), 67 deletions(-) diff --git a/sound/soc/codecs/rt5677.c b/sound/soc/codecs/rt5677.c index 9b7a1833d3316c..33380f82b7008f 100644 --- a/sound/soc/codecs/rt5677.c +++ b/sound/soc/codecs/rt5677.c @@ -23,6 +23,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -4620,7 +4624,6 @@ static void rt5677_gpio_config(struct rt5677_priv *rt5677, unsigned offset, static int rt5677_to_irq(struct gpio_chip *chip, unsigned offset) { struct rt5677_priv *rt5677 = gpiochip_get_data(chip); - struct regmap_irq_chip_data *data = rt5677->irq_data; int irq; if ((rt5677->pdata.jd1_gpio == 1 && offset == RT5677_GPIO1) || @@ -4646,7 +4649,7 @@ static int rt5677_to_irq(struct gpio_chip *chip, unsigned offset) return -ENXIO; } - return regmap_irq_get_virq(data, irq); + return irq_create_mapping(rt5677->domain, irq); } static const struct gpio_chip rt5677_template_chip = { @@ -4716,37 +4719,13 @@ static int rt5677_probe(struct snd_soc_component *component) snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF); - regmap_write(rt5677->regmap, RT5677_DIG_MISC, 0x0020); + regmap_update_bits(rt5677->regmap, RT5677_DIG_MISC, + ~RT5677_IRQ_DEBOUNCE_SEL_MASK, 0x0020); regmap_write(rt5677->regmap, RT5677_PWR_DSP2, 0x0c00); for (i = 0; i < RT5677_GPIO_NUM; i++) rt5677_gpio_config(rt5677, i, rt5677->pdata.gpio_config[i]); - if (rt5677->irq_data) { - regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL1, 0x8000, - 0x8000); - regmap_update_bits(rt5677->regmap, RT5677_DIG_MISC, 0x0018, - 0x0008); - - if (rt5677->pdata.jd1_gpio) - regmap_update_bits(rt5677->regmap, RT5677_JD_CTRL1, - RT5677_SEL_GPIO_JD1_MASK, - rt5677->pdata.jd1_gpio << - RT5677_SEL_GPIO_JD1_SFT); - - if (rt5677->pdata.jd2_gpio) - regmap_update_bits(rt5677->regmap, RT5677_JD_CTRL1, - RT5677_SEL_GPIO_JD2_MASK, - rt5677->pdata.jd2_gpio << - RT5677_SEL_GPIO_JD2_SFT); - - if (rt5677->pdata.jd3_gpio) - regmap_update_bits(rt5677->regmap, RT5677_JD_CTRL1, - RT5677_SEL_GPIO_JD3_MASK, - rt5677->pdata.jd3_gpio << - RT5677_SEL_GPIO_JD3_SFT); - } - mutex_init(&rt5677->dsp_cmd_lock); mutex_init(&rt5677->dsp_pri_lock); @@ -5063,65 +5042,215 @@ static void rt5677_read_device_properties(struct rt5677_priv *rt5677, &rt5677->pdata.jd3_gpio); } -static struct regmap_irq rt5677_irqs[] = { +struct rt5677_irq_desc { + unsigned int enable_mask; + unsigned int status_mask; + unsigned int polarity_mask; +}; + +static const struct rt5677_irq_desc rt5677_irq_descs[] = { [RT5677_IRQ_JD1] = { - .reg_offset = 0, - .mask = RT5677_EN_IRQ_GPIO_JD1, + .enable_mask = RT5677_EN_IRQ_GPIO_JD1, + .status_mask = RT5677_STA_GPIO_JD1, + .polarity_mask = RT5677_INV_GPIO_JD1, }, [RT5677_IRQ_JD2] = { - .reg_offset = 0, - .mask = RT5677_EN_IRQ_GPIO_JD2, + .enable_mask = RT5677_EN_IRQ_GPIO_JD2, + .status_mask = RT5677_STA_GPIO_JD2, + .polarity_mask = RT5677_INV_GPIO_JD2, }, [RT5677_IRQ_JD3] = { - .reg_offset = 0, - .mask = RT5677_EN_IRQ_GPIO_JD3, + .enable_mask = RT5677_EN_IRQ_GPIO_JD3, + .status_mask = RT5677_STA_GPIO_JD3, + .polarity_mask = RT5677_INV_GPIO_JD3, }, }; +static irqreturn_t rt5677_irq(int unused, void *data) +{ + struct rt5677_priv *rt5677 = data; + int ret = 0, i, loop, reg_irq, virq; + bool irq_fired; + + mutex_lock(&rt5677->irq_lock); + /* + * Loop to handle interrupts until the last i2c read shows no pending + * irqs. The interrupt line is shared by multiple interrupt sources. + * After the regmap_read() below, a new interrupt source line may + * become high before the regmap_write() finishes, so there isn't a + * rising edge on the shared interrupt line for the new interrupt. Thus, + * the loop is needed to avoid missing irqs. + * + * A safeguard of 20 loops is used to avoid hanging in the irq handler + * if there is something wrong with the interrupt status update. The + * interrupt sources here are audio jack plug/unplug events which + * shouldn't happen at a high frequency for a long period of time. + * Empirically, more than 3 loops have never been seen. + */ + for (loop = 0; loop < 20; loop++) { + /* Read interrupt status */ + ret = regmap_read(rt5677->regmap, RT5677_IRQ_CTRL1, ®_irq); + if (ret) + break; + /* + * Clear the interrupt by flipping the polarity of the + * interrupt source lines that just fired + */ + irq_fired = false; + for (i = 0; i < RT5677_IRQ_NUM; i++) { + if (reg_irq & rt5677_irq_descs[i].status_mask) { + reg_irq ^= rt5677_irq_descs[i].polarity_mask; + irq_fired = true; + } + } + if (!irq_fired) + break; -static struct regmap_irq_chip rt5677_irq_chip = { - .name = "rt5677", - .irqs = rt5677_irqs, - .num_irqs = ARRAY_SIZE(rt5677_irqs), + ret = regmap_write(rt5677->regmap, RT5677_IRQ_CTRL1, reg_irq); + if (ret) + break; - .num_regs = 1, - .status_base = RT5677_IRQ_CTRL1, - .mask_base = RT5677_IRQ_CTRL1, - .mask_invert = 1, -}; + /* Process interrupts */ + for (i = 0; i < RT5677_IRQ_NUM; i++) { + if ((reg_irq & rt5677_irq_descs[i].enable_mask) && + (reg_irq & rt5677_irq_descs[i].status_mask)) { + virq = irq_find_mapping(rt5677->domain, i); + if (virq) + handle_nested_irq(virq); + } + } + } + mutex_unlock(&rt5677->irq_lock); + return IRQ_HANDLED; +} +static void rt5677_irq_work(struct work_struct *work) +{ + struct rt5677_priv *rt5677 = + container_of(work, struct rt5677_priv, irq_work.work); + + rt5677_irq(0, rt5677); +} -static int rt5677_init_irq(struct i2c_client *i2c) +static void rt5677_irq_bus_lock(struct irq_data *data) { - int ret; - struct rt5677_priv *rt5677 = i2c_get_clientdata(i2c); + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); - if (!rt5677->pdata.jd1_gpio && - !rt5677->pdata.jd2_gpio && - !rt5677->pdata.jd3_gpio) - return 0; + mutex_lock(&rt5677->irq_lock); +} - if (!i2c->irq) { - dev_err(&i2c->dev, "No interrupt specified\n"); - return -EINVAL; - } +static void rt5677_irq_bus_sync_unlock(struct irq_data *data) +{ + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); - ret = regmap_add_irq_chip(rt5677->regmap, i2c->irq, - IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0, - &rt5677_irq_chip, &rt5677->irq_data); + regmap_update_bits(rt5677->regmap, RT5677_IRQ_CTRL1, + RT5677_EN_IRQ_GPIO_JD1 | RT5677_EN_IRQ_GPIO_JD2 | + RT5677_EN_IRQ_GPIO_JD3, rt5677->irq_en); + mutex_unlock(&rt5677->irq_lock); +} - if (ret != 0) { - dev_err(&i2c->dev, "Failed to register IRQ chip: %d\n", ret); - return ret; - } +static void rt5677_irq_enable(struct irq_data *data) +{ + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); + + rt5677->irq_en |= rt5677_irq_descs[data->hwirq].enable_mask; +} + +static void rt5677_irq_disable(struct irq_data *data) +{ + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); + + rt5677->irq_en &= ~rt5677_irq_descs[data->hwirq].enable_mask; +} +static struct irq_chip rt5677_irq_chip = { + .name = "rt5677_irq_chip", + .irq_bus_lock = rt5677_irq_bus_lock, + .irq_bus_sync_unlock = rt5677_irq_bus_sync_unlock, + .irq_disable = rt5677_irq_disable, + .irq_enable = rt5677_irq_enable, +}; + +static int rt5677_irq_map(struct irq_domain *h, unsigned int virq, + irq_hw_number_t hw) +{ + struct rt5677_priv *rt5677 = h->host_data; + + irq_set_chip_data(virq, rt5677); + irq_set_chip(virq, &rt5677_irq_chip); + irq_set_nested_thread(virq, 1); + irq_set_noprobe(virq); return 0; } -static void rt5677_free_irq(struct i2c_client *i2c) + +static const struct irq_domain_ops rt5677_domain_ops = { + .map = rt5677_irq_map, + .xlate = irq_domain_xlate_twocell, +}; + +static void rt5677_init_irq(struct i2c_client *i2c) +{ + struct rt5677_priv *rt5677 = i2c_get_clientdata(i2c); + int ret; + unsigned int jd_mask = 0, jd_val = 0; + + /* No irq has been assigned to the codec */ + if (!i2c->irq) + return; + + mutex_init(&rt5677->irq_lock); + INIT_DELAYED_WORK(&rt5677->irq_work, rt5677_irq_work); + + /* + * 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 */ + regmap_update_bits(rt5677->regmap, RT5677_GEN_CTRL1, 0xff, 0xff); + + /* Select and enable jack detection sources per platform data */ + if (rt5677->pdata.jd1_gpio) { + jd_mask |= RT5677_SEL_GPIO_JD1_MASK; + jd_val |= rt5677->pdata.jd1_gpio << RT5677_SEL_GPIO_JD1_SFT; + } + if (rt5677->pdata.jd2_gpio) { + jd_mask |= RT5677_SEL_GPIO_JD2_MASK; + jd_val |= rt5677->pdata.jd2_gpio << RT5677_SEL_GPIO_JD2_SFT; + } + if (rt5677->pdata.jd3_gpio) { + jd_mask |= RT5677_SEL_GPIO_JD3_MASK; + jd_val |= rt5677->pdata.jd3_gpio << RT5677_SEL_GPIO_JD3_SFT; + } + regmap_update_bits(rt5677->regmap, RT5677_JD_CTRL1, jd_mask, jd_val); + + /* Set GPIO1 pin to be IRQ output */ + regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL1, + RT5677_GPIO1_PIN_MASK, RT5677_GPIO1_PIN_IRQ); + + /* Ready to listen for interrupts */ + rt5677->domain = irq_domain_add_linear(i2c->dev.of_node, + RT5677_IRQ_NUM, &rt5677_domain_ops, rt5677); + if (!rt5677->domain) { + dev_err(&i2c->dev, "Failed to create IRQ domain\n"); + return; + } + ret = devm_request_threaded_irq(&i2c->dev, 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; + } +} + +static void rt5677_irq_exit(struct i2c_client *i2c) { struct rt5677_priv *rt5677 = i2c_get_clientdata(i2c); - if (rt5677->irq_data) - regmap_del_irq_chip(i2c->irq, rt5677->irq_data); + cancel_delayed_work_sync(&rt5677->irq_work); } static int rt5677_i2c_probe(struct i2c_client *i2c) @@ -5259,7 +5388,7 @@ static int rt5677_i2c_probe(struct i2c_client *i2c) static int rt5677_i2c_remove(struct i2c_client *i2c) { - rt5677_free_irq(i2c); + rt5677_irq_exit(i2c); rt5677_free_gpio(i2c); return 0; diff --git a/sound/soc/codecs/rt5677.h b/sound/soc/codecs/rt5677.h index 183d92b030459f..9c73bea8635508 100644 --- a/sound/soc/codecs/rt5677.h +++ b/sound/soc/codecs/rt5677.h @@ -1636,6 +1636,12 @@ #define RT5677_GPIO6_P_NOR (0x0 << 0) #define RT5677_GPIO6_P_INV (0x1 << 0) +/* General Control (0xfa) */ +#define RT5677_IRQ_DEBOUNCE_SEL_MASK (0x3 << 3) +#define RT5677_IRQ_DEBOUNCE_SEL_MCLK (0x0 << 3) +#define RT5677_IRQ_DEBOUNCE_SEL_RC (0x1 << 3) +#define RT5677_IRQ_DEBOUNCE_SEL_SLIM (0x2 << 3) + /* Virtual DSP Mixer Control (0xf7 0xf8 0xf9) */ #define RT5677_DSP_IB_01_H (0x1 << 15) #define RT5677_DSP_IB_01_H_SFT 15 @@ -1713,6 +1719,7 @@ enum { RT5677_IRQ_JD1, RT5677_IRQ_JD2, RT5677_IRQ_JD3, + RT5677_IRQ_NUM, }; enum rt5677_type { @@ -1811,9 +1818,14 @@ struct rt5677_priv { struct gpio_chip gpio_chip; #endif bool dsp_vad_en; - struct regmap_irq_chip_data *irq_data; bool is_dsp_mode; bool is_vref_slow; + + /* Interrupt handling */ + struct irq_domain *domain; + struct mutex irq_lock; + unsigned int irq_en; + struct delayed_work irq_work; }; int rt5677_sel_asrc_clk_src(struct snd_soc_component *component, -- 2.21.0.392.gf8f6787159e-goog