Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp540427imm; Sat, 8 Sep 2018 04:09:31 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYu+ZeTd9Np4q3E4Sea4VyeL5IWY9zRyYJ8R26VN/Jq86mFt4wFOj4f9VVhyw+SD0qqaKGI X-Received: by 2002:a17:902:7d87:: with SMTP id a7-v6mr12673712plm.103.1536404971223; Sat, 08 Sep 2018 04:09:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536404971; cv=none; d=google.com; s=arc-20160816; b=AWggv2Ln8lq59lSuItHqbb4Yx6rz9FIz2VgbVb1uSBe5d77fF8wCVXWIiQevr5b6BU TQQKriKvWnNpwtV/TkbZME/9kRKHhGuyi6GCK+Ls4t+z4WN9J1759s8aE7TFsyMnaABv HlO0zy7RoEbnJ0B6q60pf/LprfonNzvwze2+ul1w0/NYu83mLlPZlir3UMkmOJDWLNr8 EO5aMCjLNX9UWv7v4NwSRpoKOVbGcRXZ8CLvR1gkiWUsNVjxEBeDWqdCmpPHCblih8Xo 10flkmM5BZG83EenhOpYXbGWV68hPJb9glOPNBGx44x//whWPzBvoclHLh2SsA225NhX BWsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=YEXkIyOj3xnxARZndSGse+B9TOcVpQcbmMejkEFOQDA=; b=H6RKXlxk6zJ5c+FjFyQzhHPF5wTHejllyM3IIDuBoBCjODcLxyZHiFOL8Js6TRAHMO 5lwZqo33iXGDwOczCkQhuEeCUzwcpwkakpeUadFSj6EXVvYzEX8WDi/u3R+PwaXejFA3 cVpXHsb1AQ8KTsBinkLIvDMoSFGxo0lCs5KesVl7qelW6eF9ABy3gPDTZ22oDHGyHkGm ovJTjQhdi62U9UdZf5v4kCjIiD7Swn+W5jaM6/UijY5IFPVWPJvxaR63swZ10Q4V8iPr Vr/9WN1GR2DEOMn9VUf5g62ru+D23oGOh7iTVhsY0kq/Ah8gnFAtT/Biee1aMPv77+WE 76ng== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w3-v6si10796007ply.370.2018.09.08.04.09.15; Sat, 08 Sep 2018 04:09:31 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727416AbeIHPx0 (ORCPT + 99 others); Sat, 8 Sep 2018 11:53:26 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:18326 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727157AbeIHPxY (ORCPT ); Sat, 8 Sep 2018 11:53:24 -0400 X-UUID: 214bd91a96594787b7fd4e4febccc8b1-20180908 Received: from mtkcas08.mediatek.inc [(172.21.101.126)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 585127549; Sat, 08 Sep 2018 19:07:44 +0800 Received: from mtkcas09.mediatek.inc (172.21.101.178) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Sat, 8 Sep 2018 19:07:42 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas09.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Sat, 8 Sep 2018 19:07:43 +0800 From: To: , CC: , , , Sean Wang Subject: [PATCH v2 21/22] pinctrl: mediatek: extend eint build to pinctrl-mtk-common-v2.c Date: Sat, 8 Sep 2018 19:07:37 +0800 Message-ID: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Wang Almost all MediaTek SoCs apply the exact same logic to build eint, so move the common functions into pinctrl-mtk-common-v2.c to allow each new pinctrl driver to reuse them. Also, add a protection checker on hw->soc->eint_hw to avoid invalid memory access when there's certain SoC not to define its eint_hw properly in the code flow. Signed-off-by: Sean Wang --- drivers/pinctrl/mediatek/pinctrl-moore.c | 124 --------------------- drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c | 135 +++++++++++++++++++++++ drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.h | 2 + 3 files changed, 137 insertions(+), 124 deletions(-) diff --git a/drivers/pinctrl/mediatek/pinctrl-moore.c b/drivers/pinctrl/mediatek/pinctrl-moore.c index f0390b3..7cfab0c 100644 --- a/drivers/pinctrl/mediatek/pinctrl-moore.c +++ b/drivers/pinctrl/mediatek/pinctrl-moore.c @@ -584,130 +584,6 @@ static int mtk_build_functions(struct mtk_pinctrl *hw) return 0; } -static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, - unsigned long eint_n) -{ - const struct mtk_pin_desc *desc; - int i = 0; - - desc = (const struct mtk_pin_desc *)hw->soc->pins; - - while (i < hw->soc->npins) { - if (desc[i].eint.eint_n == eint_n) - return desc[i].number; - i++; - } - - return EINT_NA; -} - -static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n, - unsigned int *gpio_n, - struct gpio_chip **gpio_chip) -{ - struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; - const struct mtk_pin_desc *desc; - - desc = (const struct mtk_pin_desc *)hw->soc->pins; - *gpio_chip = &hw->chip; - - /* Be greedy to guess first gpio_n is equal to eint_n */ - if (desc[eint_n].eint.eint_n == eint_n) - *gpio_n = eint_n; - else - *gpio_n = mtk_xt_find_eint_num(hw, eint_n); - - return *gpio_n == EINT_NA ? -EINVAL : 0; -} - -static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n) -{ - struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; - struct gpio_chip *gpio_chip; - unsigned int gpio_n; - int err; - - err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip); - if (err) - return err; - - return mtk_gpio_get(gpio_chip, gpio_n); -} - -static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n) -{ - struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; - const struct mtk_pin_desc *desc; - struct gpio_chip *gpio_chip; - unsigned int gpio_n; - int err; - - err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip); - if (err) - return err; - - desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n]; - - err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, - desc->eint.eint_m); - if (err) - return err; - - err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT); - if (err) - return err; - - err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE); - if (err) - return err; - - return 0; -} - -static const struct mtk_eint_xt mtk_eint_xt = { - .get_gpio_n = mtk_xt_get_gpio_n, - .get_gpio_state = mtk_xt_get_gpio_state, - .set_gpio_as_eint = mtk_xt_set_gpio_as_eint, -}; - -static int -mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev) -{ - struct device_node *np = pdev->dev.of_node; - struct resource *res; - - if (!IS_ENABLED(CONFIG_EINT_MTK)) - return 0; - - if (!of_property_read_bool(np, "interrupt-controller")) - return -ENODEV; - - hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL); - if (!hw->eint) - return -ENOMEM; - - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eint"); - if (!res) { - dev_err(&pdev->dev, "Unable to get eint resource\n"); - return -ENODEV; - } - - hw->eint->base = devm_ioremap_resource(&pdev->dev, res); - if (IS_ERR(hw->eint->base)) - return PTR_ERR(hw->eint->base); - - hw->eint->irq = irq_of_parse_and_map(np, 0); - if (!hw->eint->irq) - return -EINVAL; - - hw->eint->dev = &pdev->dev; - hw->eint->hw = hw->soc->eint_hw; - hw->eint->pctl = hw; - hw->eint->gpio_xlate = &mtk_eint_xt; - - return mtk_eint_do_init(hw->eint); -} - int mtk_moore_pinctrl_probe(struct platform_device *pdev, const struct mtk_pin_soc *soc) { diff --git a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c index 7d5f570..e8a2066 100644 --- a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c +++ b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c @@ -9,8 +9,11 @@ #include #include #include +#include #include +#include +#include "mtk-eint.h" #include "pinctrl-mtk-common-v2.h" /** @@ -207,6 +210,138 @@ int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc, return 0; } +static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, unsigned long eint_n) +{ + const struct mtk_pin_desc *desc; + int i = 0; + + desc = (const struct mtk_pin_desc *)hw->soc->pins; + + while (i < hw->soc->npins) { + if (desc[i].eint.eint_n == eint_n) + return desc[i].number; + i++; + } + + return EINT_NA; +} + +static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n, + unsigned int *gpio_n, + struct gpio_chip **gpio_chip) +{ + struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; + const struct mtk_pin_desc *desc; + + desc = (const struct mtk_pin_desc *)hw->soc->pins; + *gpio_chip = &hw->chip; + + /* Be greedy to guess first gpio_n is equal to eint_n */ + if (desc[eint_n].eint.eint_n == eint_n) + *gpio_n = eint_n; + else + *gpio_n = mtk_xt_find_eint_num(hw, eint_n); + + return *gpio_n == EINT_NA ? -EINVAL : 0; +} + +static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n) +{ + struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; + const struct mtk_pin_desc *desc; + struct gpio_chip *gpio_chip; + unsigned int gpio_n; + int value, err; + + err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip); + if (err) + return err; + + desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n]; + + err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value); + if (err) + return err; + + return !!value; +} + +static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n) +{ + struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data; + const struct mtk_pin_desc *desc; + struct gpio_chip *gpio_chip; + unsigned int gpio_n; + int err; + + err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip); + if (err) + return err; + + desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n]; + + err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, + desc->eint.eint_m); + if (err) + return err; + + err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT); + if (err) + return err; + + err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE); + if (err) + return err; + + return 0; +} + +static const struct mtk_eint_xt mtk_eint_xt = { + .get_gpio_n = mtk_xt_get_gpio_n, + .get_gpio_state = mtk_xt_get_gpio_state, + .set_gpio_as_eint = mtk_xt_set_gpio_as_eint, +}; + +int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct resource *res; + + if (!IS_ENABLED(CONFIG_EINT_MTK)) + return 0; + + if (!of_property_read_bool(np, "interrupt-controller")) + return -ENODEV; + + hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL); + if (!hw->eint) + return -ENOMEM; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eint"); + if (!res) { + dev_err(&pdev->dev, "Unable to get eint resource\n"); + return -ENODEV; + } + + hw->eint->base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(hw->eint->base)) + return PTR_ERR(hw->eint->base); + + hw->eint->irq = irq_of_parse_and_map(np, 0); + if (!hw->eint->irq) + return -EINVAL; + + if (!hw->soc->eint_hw) + return -ENODEV; + + hw->eint->dev = &pdev->dev; + hw->eint->hw = hw->soc->eint_hw; + hw->eint->pctl = hw; + hw->eint->gpio_xlate = &mtk_eint_xt; + + return mtk_eint_do_init(hw->eint); +} + /* Revision 0 */ int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc) diff --git a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.h b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.h index 05b9b39..e0d4e68 100644 --- a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.h +++ b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.h @@ -245,6 +245,8 @@ int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc, int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc, int field, int *value); +int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev); + int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc); int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw, -- 2.7.4