Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1336665ybi; Fri, 14 Jun 2019 12:51:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqwV7lc1Tm+17useAtlSxIDiFzraU2Lda6m7pZKO4dDfKrwOAe8Calxf4CYKVAsqL8KGm+ZB X-Received: by 2002:a17:90a:9f8b:: with SMTP id o11mr12932301pjp.102.1560541904590; Fri, 14 Jun 2019 12:51:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560541904; cv=none; d=google.com; s=arc-20160816; b=WI+LwjLkiA16YIZXRLqjK19QPE7hb4j8m4QJgNLNDAfa1nWQ+3nug7pDEdScW1UHif hiJpQiOVSwY/IG2fRJfw70JI/23y/YktDaGbHz3N0ReA9urKGfxwWTBb9oCQiM/MU2b6 W5rv3qLBzG5SUImjASV7Ai4rnTh6MRj5OZVTL43F6O5GKS84SEzYfSllxzUn6hqVAmTR rg17j1fQedDEp8r0crC7l67kHUCl7plahRgZRNXaAZ9P4tebCKScMyCNBp99mEdaJQup xYEbjomYJ7nFUGHetW8Nirjjv+Dxab+ssd40S4cvLUK7fqu8DVYqnEt89gSQu6X5zsSt XrwQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IRv7HK9MDuCS79nTbOugG3YBL8/8pkDfcXyiktbGzRo=; b=QKatyF1b7TgBogVrqyRA2UFoJM3ts7MVUlrWuyhLOEx0NuiBJlcm+/+YJbhcEmczN0 aWu6r7LNKKpEpzEL4Vd9Q6jlk57ioxjKT/J2UkHEXfkIsLpmv4L0wMpMwJon3vMj7ow6 8S7ysVsVpeA91NVWKGb89b17+IxaRdv9Zye3RhY0E34yokGmm1ODkOJTxCPZjetQHwrY tJD8CbSpSMVDup6QuSXxupjDZ1xyoB0DMgwmdLTWarxPTV/doMjJail1kvKQadxzjtC8 wwr2Pf4RqkUUolZV5gxJB9TQGSPrJHIgMvtXoWqOcnM3L6e7ALryhoDiHwkoarQaIne0 X6Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Tc9kZZPg; 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 d19si2866885pls.221.2019.06.14.12.51.29; Fri, 14 Jun 2019 12:51:44 -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=Tc9kZZPg; 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 S1726264AbfFNTtJ (ORCPT + 99 others); Fri, 14 Jun 2019 15:49:09 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:39099 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726213AbfFNTtH (ORCPT ); Fri, 14 Jun 2019 15:49:07 -0400 Received: by mail-io1-f68.google.com with SMTP id r185so2385737iod.6 for ; Fri, 14 Jun 2019 12:49:06 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=IRv7HK9MDuCS79nTbOugG3YBL8/8pkDfcXyiktbGzRo=; b=Tc9kZZPgMmbGTf6tfttpLMHqjLzoTX/pB5C4rpfkXNBCK4+s9oTaC7g7pg0e4KMiSF s/9rZZfW0ttLS7PrSR5omnHmijxqphkK2ZUv5+tQ4m+yrCiVNM/gCgSBkz09rOrlZqOI rUG8yt8rqaMI1M9esezWE7sv8ME0ZOwrws28Y= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=IRv7HK9MDuCS79nTbOugG3YBL8/8pkDfcXyiktbGzRo=; b=uKtUDGrSskjThi6JxaZaQGL0PzF2aDkDR9z/OhhPEFZYUdS7aFyPt04AdltA/Gooa7 Og/6t6lN0fEl7QOSRPvj+drW463m3eWijFMqI2RrZA27Qcd5zg7EG5WDFKBNKRxWe1Pm YIbhQEVW+BrxRLL025yrivvvaNiuao7WlrBt+TpJFAmXDrjTNZtkOCvUF/VcFHA0Xoju vg5N9TP3uQbPE/DMwf2uUn1QXla9Mkm/aiDwIk7ZSbBgz5C3lzm284CNa4tAcnJYYfHD t/HOnlmMHNh7yZXMPyvsJG/or5hWGdaGh4JWXJjFKu7CPNGs2OlVywpGraw0B7ePJ2I1 qUsw== X-Gm-Message-State: APjAAAV6cW7IgbiQ869296/0V/DJDgg0UGZxLuFxjhikuSU1ZKBtEO3y QIw5W5VmNZFDidVi3FkPvW9eAoKW1OtAqA== X-Received: by 2002:a02:69d7:: with SMTP id e206mr70772097jac.21.1560541745833; Fri, 14 Jun 2019 12:49:05 -0700 (PDT) Received: from localhost ([2620:15c:183:200:33ce:f5cf:f863:d3a6]) by smtp.gmail.com with ESMTPSA id p10sm2762876iob.54.2019.06.14.12.49.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jun 2019 12:49:05 -0700 (PDT) From: Fletcher Woodruff To: linux-kernel@vger.kernel.org Cc: Ben Zhang , Jaroslav Kysela , Liam Girdwood , Mark Brown , Oder Chiou , Takashi Iwai , Curtis Malainey , Ross Zwisler , alsa-devel@alsa-project.org, Fletcher Woodruff Subject: [PATCH v7 3/4] ASoC: rt5677: clear interrupts by polarity flip Date: Fri, 14 Jun 2019 13:48:53 -0600 Message-Id: <20190614194854.208436-4-fletcherw@chromium.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190614194854.208436-1-fletcherw@chromium.org> References: <20190614194854.208436-1-fletcherw@chromium.org> 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 The rt5677 jack detection function has a requirement that the polarity of an interrupt be flipped after it fires in order to clear the interrupt. This patch implements an irq_chip with irq_domain directly instead of using regmap-irq, so that interrupt source line polarities can be flipped in the irq handler. The reason that this patch does not add this feature within regmap-irq is that future patches will add hotword detection support to this irq handler. Those patches will require adding additional logic that would not make sense to have in regmap-irq. Signed-off-by: Ben Zhang Signed-off-by: Fletcher Woodruff --- sound/soc/codecs/rt5677.c | 170 ++++++++++++++++++++++++++++++-------- sound/soc/codecs/rt5677.h | 7 +- 2 files changed, 143 insertions(+), 34 deletions(-) diff --git a/sound/soc/codecs/rt5677.c b/sound/soc/codecs/rt5677.c index 87a92ba0d040b7..87466ee222ee59 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 = { @@ -5042,30 +5045,130 @@ static void rt5677_read_device_properties(struct rt5677_priv *rt5677, rt5677->pdata.jd3_gpio = val; } -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 struct regmap_irq_chip rt5677_irq_chip = { - .name = RT5677_DRV_NAME, - .irqs = rt5677_irqs, - .num_irqs = ARRAY_SIZE(rt5677_irqs), +static irqreturn_t rt5677_irq(int unused, void *data) +{ + struct rt5677_priv *rt5677 = data; + int ret = 0, i, reg_irq, virq; + bool irq_fired = false; + + mutex_lock(&rt5677->irq_lock); + /* Read interrupt status */ + ret = regmap_read(rt5677->regmap, RT5677_IRQ_CTRL1, ®_irq); + if (ret) { + pr_err("rt5677: failed reading IRQ status: %d\n", ret); + goto exit; + } + + for (i = 0; i < RT5677_IRQ_NUM; i++) { + if (reg_irq & rt5677_irq_descs[i].status_mask) { + irq_fired = true; + virq = irq_find_mapping(rt5677->domain, i); + if (virq) + handle_nested_irq(virq); + + /* Clear the interrupt by flipping the polarity of the + * interrupt source line that fired + */ + reg_irq ^= rt5677_irq_descs[i].polarity_mask; + } + } + + if (!irq_fired) + goto exit; + + ret = regmap_write(rt5677->regmap, RT5677_IRQ_CTRL1, reg_irq); + if (ret) { + pr_err("rt5677: failed updating IRQ status: %d\n", ret); + goto exit; + } +exit: + mutex_unlock(&rt5677->irq_lock); + if (irq_fired) + return IRQ_HANDLED; + else + return IRQ_NONE; +} - .num_regs = 1, - .status_base = RT5677_IRQ_CTRL1, - .mask_base = RT5677_IRQ_CTRL1, - .mask_invert = 1, +static void rt5677_irq_bus_lock(struct irq_data *data) +{ + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); + + mutex_lock(&rt5677->irq_lock); +} + +static void rt5677_irq_bus_sync_unlock(struct irq_data *data) +{ + struct rt5677_priv *rt5677 = irq_data_get_irq_chip_data(data); + + // Set the enable/disable bits for the jack detect IRQs. + 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); +} + +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 const struct irq_domain_ops rt5677_domain_ops = { + .map = rt5677_irq_map, + .xlate = irq_domain_xlate_twocell, }; static int rt5677_init_irq(struct i2c_client *i2c) @@ -5084,6 +5187,8 @@ static int rt5677_init_irq(struct i2c_client *i2c) return -EINVAL; } + mutex_init(&rt5677->irq_lock); + /* * Select RC as the debounce clock so that GPIO works even when * MCLK is gated which happens when there is no audio stream @@ -5092,7 +5197,6 @@ static int rt5677_init_irq(struct i2c_client *i2c) 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); @@ -5115,26 +5219,25 @@ static int rt5677_init_irq(struct i2c_client *i2c) regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL1, RT5677_GPIO1_PIN_MASK, RT5677_GPIO1_PIN_IRQ); - ret = regmap_add_irq_chip(rt5677->regmap, i2c->irq, - IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0, - &rt5677_irq_chip, &rt5677->irq_data); + /* 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 -ENOMEM; + } - if (ret != 0) { - dev_err(&i2c->dev, "Failed to register IRQ chip: %d\n", ret); + 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 ret; } return 0; } -static void rt5677_free_irq(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); -} - static int rt5677_i2c_probe(struct i2c_client *i2c) { struct rt5677_priv *rt5677; @@ -5259,7 +5362,9 @@ static int rt5677_i2c_probe(struct i2c_client *i2c) RT5677_MICBIAS1_CTRL_VDD_3_3V); rt5677_init_gpio(i2c); - rt5677_init_irq(i2c); + ret = rt5677_init_irq(i2c); + if (ret) + dev_err(&i2c->dev, "Failed to initialize irq: %d\n", ret); return devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_rt5677, @@ -5268,7 +5373,6 @@ static int rt5677_i2c_probe(struct i2c_client *i2c) static int rt5677_i2c_remove(struct i2c_client *i2c) { - rt5677_free_irq(i2c); rt5677_free_gpio(i2c); return 0; diff --git a/sound/soc/codecs/rt5677.h b/sound/soc/codecs/rt5677.h index c26edd387e340b..d0ac26e562eb5f 100644 --- a/sound/soc/codecs/rt5677.h +++ b/sound/soc/codecs/rt5677.h @@ -1749,6 +1749,7 @@ enum { RT5677_IRQ_JD1, RT5677_IRQ_JD2, RT5677_IRQ_JD3, + RT5677_IRQ_NUM, }; enum rt5677_type { @@ -1847,9 +1848,13 @@ 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; }; int rt5677_sel_asrc_clk_src(struct snd_soc_component *component, -- 2.22.0.410.gd8fdbe21b5-goog