Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1255344ybl; Mon, 2 Dec 2019 00:11:04 -0800 (PST) X-Google-Smtp-Source: APXvYqx9ZWuZG+7B9AgiblPYcYVbl+538xb4iCDciXOcoZZkAfEp5df2tB0k5b6iegiEdUv9HXg+ X-Received: by 2002:a05:6402:b42:: with SMTP id bx2mr38556256edb.99.1575274264308; Mon, 02 Dec 2019 00:11:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575274264; cv=none; d=google.com; s=arc-20160816; b=Ww8upnViNK4pMajoqEWOQxfLwiK8xuSiMrF0FrTWMbZ9YYpNYT3+WEyjv99jb+/ueQ +liwyCgRgynXBth5JmxxNt/pZyL0H9P+7mblvUTjXbxAFdqCPLRIJZA3AipclAnfDmjd l7j05eNylEIJntzsf7kahEXX47ki+CBS2EdghSAwLw78lOsM8SzVwYDItJp0AXqHbWH8 1FJg6lC9QhoZtueOosGe8KRMIPXwHg1LZ4lsDQvtjBCBIDpZlokBLXzokccCu162jB9C g14oNMtowIcWORhvHUH4YNySF16mzOyckAtF37YoNIhghBZZ526Lo6FMg7nVpuP9VCST WoYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hB2nJ8CzTKjXCm6SOFfHQcbAJFeLp6+ruAS0J+kmEz4=; b=WAPDbnXiqRXLWd/SGh/c6a099lbXA+3ZR4tNTTQYQOfV+AH9nLFy6Oui1kiLd9uPib RiyS5yUMtxXhtLxQU0G7UI5UdJJeH/A96IcN87xh4l/wGWYQ+MLDBhtgwrn0kl4s2Xab Q43Kel+Kz0XZBfTidsz+n8ghoUMODhLk2779AqqBWL8xW9Bd0osrAmWPwuLi6eSAZbhv uD2+wOjsqpfAwnGOosTj2DiqUZhyRjXUj8uQix33Uv3oEZet1TzRnvrQGwnrCfgzLM6H KWOAs4aC4OyNenDMbM3GihuS9Jrw8c8Ff95ePSWqesg775XDVrBaEV0WLSvoG7fMzf7r ggRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XRIhVxFW; 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 o14si25147998edi.116.2019.12.02.00.10.40; Mon, 02 Dec 2019 00:11:04 -0800 (PST) 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=XRIhVxFW; 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 S1726486AbfLBIHh (ORCPT + 99 others); Mon, 2 Dec 2019 03:07:37 -0500 Received: from mail-ed1-f67.google.com ([209.85.208.67]:35336 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725977AbfLBIHh (ORCPT ); Mon, 2 Dec 2019 03:07:37 -0500 Received: by mail-ed1-f67.google.com with SMTP id f8so16103432edv.2 for ; Mon, 02 Dec 2019 00:07:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hB2nJ8CzTKjXCm6SOFfHQcbAJFeLp6+ruAS0J+kmEz4=; b=XRIhVxFW2iDfWHxbVlSiSV4PpvApyRBnSh0jlF7EDk/saneQv8Jfys+8UXjIN+iVT4 UgPX8PYXfFfL+H21cq04VIyNM1HXjeB3XCMex9nIMrzxb/+DIUKLqHyR++nk5PkAgbmP Z2E1/rUB/83oyoTGcmJ+ZjW0Yk279VnZIhn3Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hB2nJ8CzTKjXCm6SOFfHQcbAJFeLp6+ruAS0J+kmEz4=; b=iBSFuWnBjq3FiC/MRyakn9kQaV8+UkH7vhJOSzfBifjuoIMpIFF0+wW8jMxzXDWPag MYEfc2ul/RDK91niH2x7YqfRoX0Rch5sM78KPWwhpgOBM+8YuAw1boeg+fuMwwKZvqx0 K9r+VtU2g2HOwybJVqr2gtWG26pdES9xA4XqfXrWSuAGqLsaigDHa3xjWlj5a2ZLNVdQ p3YYQzACqjJfJtyx25eEZjpWzHeUu/IG9nzdOb403p1S5PlVxFWYHXu9fZolqcHsgM7M AXpMfIwX4L0NkpVzTuTlZI/77ZUsXENqpuGWy59rRg5TqaYLT26BM/siSwW3QH4AVEyi zY9A== X-Gm-Message-State: APjAAAVV5JR+lUjaz0kO6OVLL7S2o/8/n+SULBT+uc19/l2SymSTjaFb 4yNr/6oKo475lFPgH2GizY5IbA6PLfBuGDSETLhUNw== X-Received: by 2002:a50:e00a:: with SMTP id e10mr6951207edl.119.1575274055153; Mon, 02 Dec 2019 00:07:35 -0800 (PST) MIME-Version: 1.0 References: <1566531931-9772-1-git-send-email-hsin-hsiung.wang@mediatek.com> <1566531931-9772-7-git-send-email-hsin-hsiung.wang@mediatek.com> In-Reply-To: <1566531931-9772-7-git-send-email-hsin-hsiung.wang@mediatek.com> From: Pi-Hsun Shih Date: Mon, 2 Dec 2019 16:06:59 +0800 Message-ID: Subject: Re: [PATCH v5 06/10] mfd: Add support for the MediaTek MT6358 PMIC To: Hsin-Hsiung Wang Cc: Lee Jones , Rob Herring , Mark Brown , Matthias Brugger , Alexandre Belloni , Mark Rutland , Liam Girdwood , Eddie Huang , Sean Wang , Alessandro Zummo , Kate Stewart , Greg Kroah-Hartman , Thomas Gleixner , Richard Fontana , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" , open list , "open list:REAL TIME CLOCK (RTC) SUBSYSTEM" , srv_heupstream@mediatek.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Fri, Aug 23, 2019 at 11:46 AM Hsin-Hsiung Wang wrote: > > This adds support for the MediaTek MT6358 PMIC. This is a > multifunction device with the following sub modules: > > - Regulator > - RTC > - Codec > - Interrupt > > It is interfaced to the host controller using SPI interface > by a proprietary hardware called PMIC wrapper or pwrap. > MT6358 MFD is a child device of the pwrap. > > Signed-off-by: Hsin-Hsiung Wang > --- > drivers/mfd/Makefile | 3 +- > drivers/mfd/mt6358-irq.c | 231 ++++++++++++++++++++++++++++ > drivers/mfd/mt6397-core.c | 52 ++++++- > include/linux/mfd/mt6358/core.h | 158 ++++++++++++++++++++ > include/linux/mfd/mt6358/registers.h | 282 +++++++++++++++++++++++++++++++++++ > include/linux/mfd/mt6397/core.h | 3 + > 6 files changed, 727 insertions(+), 2 deletions(-) > create mode 100644 drivers/mfd/mt6358-irq.c > create mode 100644 include/linux/mfd/mt6358/core.h > create mode 100644 include/linux/mfd/mt6358/registers.h > (...) > diff --git a/drivers/mfd/mt6358-irq.c b/drivers/mfd/mt6358-irq.c > new file mode 100644 > index 0000000..760b72f > --- /dev/null > +++ b/drivers/mfd/mt6358-irq.c > @@ -0,0 +1,231 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// > +// Copyright (c) 2019 MediaTek Inc. > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static struct irq_top_t mt6358_ints[] = { > + MT6358_TOP_GEN(BUCK), > + MT6358_TOP_GEN(LDO), > + MT6358_TOP_GEN(PSC), > + MT6358_TOP_GEN(SCK), > + MT6358_TOP_GEN(BM), > + MT6358_TOP_GEN(HK), > + MT6358_TOP_GEN(AUD), > + MT6358_TOP_GEN(MISC), > +}; > + > +static void pmic_irq_enable(struct irq_data *data) > +{ > + unsigned int hwirq = irqd_to_hwirq(data); > + struct mt6397_chip *chip = irq_data_get_irq_chip_data(data); > + struct pmic_irq_data *irqd = chip->irq_data; > + > + irqd->enable_hwirq[hwirq] = true; > +} > + > +static void pmic_irq_disable(struct irq_data *data) > +{ > + unsigned int hwirq = irqd_to_hwirq(data); > + struct mt6397_chip *chip = irq_data_get_irq_chip_data(data); > + struct pmic_irq_data *irqd = chip->irq_data; > + > + irqd->enable_hwirq[hwirq] = false; > +} > + > +static void pmic_irq_lock(struct irq_data *data) > +{ > + struct mt6397_chip *chip = irq_data_get_irq_chip_data(data); > + > + mutex_lock(&chip->irqlock); > +} > + > +static void pmic_irq_sync_unlock(struct irq_data *data) > +{ > + unsigned int i, top_gp, en_reg, int_regs, shift; > + struct mt6397_chip *chip = irq_data_get_irq_chip_data(data); > + struct pmic_irq_data *irqd = chip->irq_data; > + > + for (i = 0; i < irqd->num_pmic_irqs; i++) { > + if (irqd->enable_hwirq[i] == irqd->cache_hwirq[i]) > + continue; > + > + /* Find out the irq group */ > + top_gp = 0; > + while ((top_gp + 1) < ARRAY_SIZE(mt6358_ints) && > + i >= mt6358_ints[top_gp + 1].hwirq_base) > + top_gp++; > + > + if (top_gp >= ARRAY_SIZE(mt6358_ints)) { Would this condition ever be true? The while loop before this always break when top_gp == ARRAY_SIZE(mt6358_ints) - 1. > + mutex_unlock(&chip->irqlock); > + dev_err(chip->dev, > + "Failed to get top_group: %d\n", top_gp); > + return; > + } > + > + /* Find the irq registers */ > + int_regs = (i - mt6358_ints[top_gp].hwirq_base) / > + MT6358_REG_WIDTH; > + en_reg = mt6358_ints[top_gp].en_reg + > + mt6358_ints[top_gp].en_reg_shift * int_regs; > + shift = (i - mt6358_ints[top_gp].hwirq_base) % MT6358_REG_WIDTH; > + regmap_update_bits(chip->regmap, en_reg, BIT(shift), > + irqd->enable_hwirq[i] << shift); > + irqd->cache_hwirq[i] = irqd->enable_hwirq[i]; > + } > + mutex_unlock(&chip->irqlock); > +} > + > +static struct irq_chip mt6358_irq_chip = { > + .name = "mt6358-irq", > + .flags = IRQCHIP_SKIP_SET_WAKE, > + .irq_enable = pmic_irq_enable, > + .irq_disable = pmic_irq_disable, > + .irq_bus_lock = pmic_irq_lock, > + .irq_bus_sync_unlock = pmic_irq_sync_unlock, > +}; > + > +static void mt6358_irq_sp_handler(struct mt6397_chip *chip, > + unsigned int top_gp) > +{ > + unsigned int sta_reg, irq_status; > + unsigned int hwirq, virq; > + int ret, i, j; > + > + for (i = 0; i < mt6358_ints[top_gp].num_int_regs; i++) { > + sta_reg = mt6358_ints[top_gp].sta_reg + > + mt6358_ints[top_gp].sta_reg_shift * i; > + ret = regmap_read(chip->regmap, sta_reg, &irq_status); > + if (ret) { > + dev_err(chip->dev, > + "Failed to read irq status: %d\n", ret); > + return; > + } > + > + if (!irq_status) > + continue; > + > + for (j = 0; j < MT6358_REG_WIDTH ; j++) { > + if ((irq_status & BIT(j)) == 0) > + continue; > + hwirq = mt6358_ints[top_gp].hwirq_base + > + MT6358_REG_WIDTH * i + j; > + virq = irq_find_mapping(chip->irq_domain, hwirq); > + if (virq) > + handle_nested_irq(virq); > + } > + > + regmap_write(chip->regmap, sta_reg, irq_status); > + } > +} > + > +static irqreturn_t mt6358_irq_handler(int irq, void *data) > +{ > + struct mt6397_chip *chip = data; > + struct pmic_irq_data *mt6358_irq_data = chip->irq_data; > + unsigned int top_irq_status; > + unsigned int i; > + int ret; > + > + ret = regmap_read(chip->regmap, > + mt6358_irq_data->top_int_status_reg, > + &top_irq_status); > + if (ret) { > + dev_err(chip->dev, "Can't read TOP_INT_STATUS ret=%d\n", ret); > + return IRQ_NONE; > + } > + > + for (i = 0; i < mt6358_irq_data->num_top; i++) { > + if (top_irq_status & BIT(mt6358_ints[i].top_offset)) > + mt6358_irq_sp_handler(chip, i); > + } > + > + return IRQ_HANDLED; > +} > + > +static int pmic_irq_domain_map(struct irq_domain *d, unsigned int irq, > + irq_hw_number_t hw) > +{ > + struct mt6397_chip *mt6397 = d->host_data; > + > + irq_set_chip_data(irq, mt6397); > + irq_set_chip_and_handler(irq, &mt6358_irq_chip, handle_level_irq); > + irq_set_nested_thread(irq, 1); > + irq_set_noprobe(irq); > + > + return 0; > +} > + > +static const struct irq_domain_ops mt6358_irq_domain_ops = { > + .map = pmic_irq_domain_map, > + .xlate = irq_domain_xlate_twocell, > +}; > + > +int mt6358_irq_init(struct mt6397_chip *chip) > +{ > + int i, j, ret; > + struct pmic_irq_data *irqd; > + > + irqd = devm_kzalloc(chip->dev, sizeof(struct pmic_irq_data *), > + GFP_KERNEL); > + if (!irqd) > + return -ENOMEM; > + > + chip->irq_data = irqd; > + > + mutex_init(&chip->irqlock); > + irqd->top_int_status_reg = MT6358_TOP_INT_STATUS0; > + irqd->num_pmic_irqs = MT6358_IRQ_NR; > + irqd->num_top = ARRAY_SIZE(mt6358_ints); ARRAY_SIZE(mt6358_ints) is still used in pmic_irq_sync_unlock. Is this variable needed, or should the ARRAY_SIZE(mt6358_ints) in pmic_irq_sync_unlock be changed to irqd->num_top too? > + > + irqd->enable_hwirq = devm_kcalloc(chip->dev, > + irqd->num_pmic_irqs, > + sizeof(bool), > + GFP_KERNEL); > + if (!irqd->enable_hwirq) > + return -ENOMEM; > + > + irqd->cache_hwirq = devm_kcalloc(chip->dev, > + irqd->num_pmic_irqs, > + sizeof(bool), > + GFP_KERNEL); > + if (!irqd->cache_hwirq) > + return -ENOMEM; > + > + /* Disable all interrupts for initializing */ > + for (i = 0; i < irqd->num_top; i++) { > + for (j = 0; j < mt6358_ints[i].num_int_regs; j++) > + regmap_write(chip->regmap, > + mt6358_ints[i].en_reg + > + mt6358_ints[i].en_reg_shift * j, 0); > + } > + > + chip->irq_domain = irq_domain_add_linear(chip->dev->of_node, > + irqd->num_pmic_irqs, > + &mt6358_irq_domain_ops, chip); > + if (!chip->irq_domain) { > + dev_err(chip->dev, "could not create IRQ domain\n"); > + return -ENODEV; > + } > + > + ret = devm_request_threaded_irq(chip->dev, chip->irq, NULL, > + mt6358_irq_handler, IRQF_ONESHOT, > + mt6358_irq_chip.name, chip); > + if (ret) { > + dev_err(chip->dev, "failed to register irq=%d; err: %d\n", > + chip->irq, ret); > + return ret; > + } > + > + enable_irq_wake(chip->irq); > + return ret; > +} > (...)