From patchwork Wed Nov 24 23:04:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 12693595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7B9DC433EF for ; Wed, 24 Nov 2021 23:07:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=2PqR7aBZ7tnByFYDboVt4178Oeui9kME9b1uhAV5ERQ=; b=qxaZsL/EpFS2VM rlCBku6A1XUSV8SxaovIeVE2hqyZ9bqMuiG4XhIGRsxfGuQy/GTDWazOiVBxoq+OMiD14Q+XAHUto 7BR5j7xSQ//GBVOYi9t6KkK/qA3Z+pDkiInVbVrAqA6UaokmDZtjzaipdRmFbTlbrT3M7FNJj7DUN /sPcAtGtDiVg11JBkH26CCtVAXISHxUnXDLA7SnRO8H8HclRt2f8M+Yn0wB3r6AysbGiXywezbFtJ ZvLDgU1Y7/Hc/0FtiLy9TeeuVaWWiT6a9F/xLOiYjhLpgJ1wwVKOE7nh2w2ANeTU3Tu7zK2oQ0rft r47GgG2W8m/pjjOpeU7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mq1Kl-00614h-IB; Wed, 24 Nov 2021 23:05:47 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mq1K0-0060u6-26 for linux-arm-kernel@lists.infradead.org; Wed, 24 Nov 2021 23:05:01 +0000 Received: by mail-lf1-x135.google.com with SMTP id l22so11258687lfg.7 for ; Wed, 24 Nov 2021 15:04:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jf6byrH4ANB6TExq8PGAH/b0DeVk7tVroeKAe91ZQMA=; b=YuXEfoqeeoz4pH50VVL6U1HkVptULVaVyPgPEEbDEf8T2A/euQ3lqUFLD4i0mqTlwq sxOLf4/HAHQk3xAjsdKdV4AUHXg611sp2XJu+9S/bPtWwcw2Z7SHf5UljbtHIBThyoCc xRB8wL0euX+PlgZJFw7HRcPnNuieA2M6B62lqitOlhyL8FkEKulIcx53XIF3vJXsYwX8 PbR7+4F+RocD917qxATdpLW0FWbgQRn0moCiArJWe3zIdcikvjQR8MR8580ES1pV4r8D wg+O1cwR7YK/wNhnkoCz4Qs9mKFObc561aPpzZ1r7Vqoopeha0wbh0LoDTX6t4xwAbxG F1nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jf6byrH4ANB6TExq8PGAH/b0DeVk7tVroeKAe91ZQMA=; b=8CtrQiS3t0YGb8KvF148Sv8TD/TX9JJV6V4Vy/LGNrGJv1GHoGytpwGBesJ2ES1eLz MKsRY6ERR3t8RICweChJwO7V0MbYGrKQhZpKqueEvfai6to29OW05M56wHya9UofTKhh YM6Yp55Xiei5l88AigqpqREVIOgi58Uy4AawlN/eySOqmxSKK4oVUtb5860arFcvotH5 W8legNuKDPNwBAp9wMqDvytKn8q41wkFlbjgcV85nscC48u4z2bDL8zgMxStZXAOoEwZ 0o+fGVeKUA2O6uDbdgPiBGHahYa5Rck4GLAABHsMaEKE+coIAm+MB/5tbbfBHJ0jZxEB 6bQg== X-Gm-Message-State: AOAM533+VmmhGIlg+F1+4wTnv/yKxdp2kc1ro6TpMRxPprfdt82FP5R/ B+WC1Jmc6479VW27afsUhO0= X-Google-Smtp-Source: ABdhPJyJn22Wtpwzu5pAhlIuYMkgGMQTdAOfiqhTeUMu7YgI7YJ022htjj1hLABdJ7l4WIVT61FF6Q== X-Received: by 2002:a05:6512:39d6:: with SMTP id k22mr19465971lfu.559.1637795098680; Wed, 24 Nov 2021 15:04:58 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id t15sm106243lfp.181.2021.11.24.15.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 15:04:58 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Linus Walleij , Rob Herring Cc: Tony Lindgren , Andy Shevchenko , linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Florian Fainelli , bcm-kernel-feedback-list@broadcom.com, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 4/6] pinctrl: support reading pins, groups & functions from DT Date: Thu, 25 Nov 2021 00:04:37 +0100 Message-Id: <20211124230439.17531-5-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211124230439.17531-1-zajec5@gmail.com> References: <20211124230439.17531-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211124_150500_139224_7E716948 X-CRM114-Status: GOOD ( 18.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Rafał Miłecki DT binding allows specifying pins, groups & functions now. That allows storing them in DT instead of hardcoding in drivers. This adds support for DT as data source to recently introduced API. Signed-off-by: Rafał Miłecki --- V2: Update pinctrl_generic_dt_load_pins() to support new binding --- drivers/pinctrl/core.c | 6 ++ drivers/pinctrl/devicetree.c | 131 +++++++++++++++++++++++++++++++++++ drivers/pinctrl/devicetree.h | 29 ++++++++ drivers/pinctrl/pinmux.c | 4 ++ 4 files changed, 170 insertions(+) diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c index 53b3e8b54a9b..4c39ca338896 100644 --- a/drivers/pinctrl/core.c +++ b/drivers/pinctrl/core.c @@ -517,6 +517,9 @@ EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); int pinctrl_generic_load_pins(struct pinctrl_desc *pctldesc, struct device *dev) { + if (dev->of_node) + return pinctrl_generic_dt_load_pins(pctldesc, dev); + return -ENOENT; } EXPORT_SYMBOL_GPL(pinctrl_generic_load_pins); @@ -525,6 +528,9 @@ EXPORT_SYMBOL_GPL(pinctrl_generic_load_pins); int pinctrl_generic_load_groups(struct pinctrl_dev *pctldev) { + if (pctldev->dev->of_node) + return pinctrl_generic_load_dt_groups(pctldev); + return -ENOENT; } EXPORT_SYMBOL_GPL(pinctrl_generic_load_groups); diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c index 3fb238714718..5e511e61449a 100644 --- a/drivers/pinctrl/devicetree.c +++ b/drivers/pinctrl/devicetree.c @@ -12,6 +12,7 @@ #include "core.h" #include "devicetree.h" +#include "pinmux.h" /** * struct pinctrl_dt_map - mapping table chunk parsed from device tree @@ -27,6 +28,136 @@ struct pinctrl_dt_map { unsigned num_maps; }; +int pinctrl_generic_dt_load_pins(struct pinctrl_desc *pctldesc, + struct device *dev) +{ + struct pinctrl_pin_desc *descs; + struct device_node *pins; + struct device_node *np; + int err = 0; + int i = 0; + + pins = of_get_child_by_name(dev->of_node, "pins"); + if (!pins) { + dev_err(dev, "failed to find \"pins\" DT node\n"); + err = -ENOENT; + goto err_out; + } + + pctldesc->npins = of_get_available_child_count(pins); + + descs = devm_kcalloc(dev, pctldesc->npins, sizeof(*descs), GFP_KERNEL); + if (!descs) { + err = -ENOMEM; + goto err_put_node; + } + + for_each_available_child_of_node(pins, np) { + descs[i].name = np->name; + + if (of_property_read_u32(np, "number", &descs[i].number)) { + dev_err(dev, "missing \"number\" property in %pOF\n", np); + err = -ENOENT; + goto err_put_node; + } + + i++; + } + + pctldesc->pins = descs; + +err_put_node: + of_node_put(pins); +err_out: + return err; +} + +#ifdef CONFIG_GENERIC_PINCTRL_GROUPS + +int pinctrl_generic_load_dt_groups(struct pinctrl_dev *pctldev) +{ + struct device *dev = pctldev->dev; + struct device_node *groups; + struct device_node *np; + int err = 0; + + groups = of_get_child_by_name(dev->of_node, "groups"); + if (!groups) { + dev_err(dev, "failed to find \"groups\" DT node\n"); + err = -ENOENT; + goto err_out; + } + + for_each_available_child_of_node(groups, np) { + int num_pins; + u32 *pins; + + num_pins = of_property_count_u32_elems(np, "pins"); + pins = devm_kmalloc_array(dev, num_pins, sizeof(*pins), GFP_KERNEL); + if (!pins) { + err = -ENOMEM; + goto err_put_node; + } + + if (of_property_read_u32_array(np, "pins", pins, num_pins)) { + err = -EIO; + goto err_put_node; + } + + pinctrl_generic_add_group(pctldev, np->name, pins, num_pins, np); + } + +err_put_node: + of_node_put(groups); +err_out: + return err; +} + +#endif + +#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS +int pinmux_generic_load_dt_functions(struct pinctrl_dev *pctldev) +{ + struct device *dev = pctldev->dev; + struct device_node *functions; + struct device_node *np; + int err = 0; + + functions = of_get_child_by_name(dev->of_node, "functions"); + if (!functions) { + dev_err(dev, "failed to find \"functions\" DT node\n"); + err = -ENOENT; + goto err_out; + } + + for_each_available_child_of_node(functions, np) { + int num_groups = of_count_phandle_with_args(np, "groups", NULL); + struct of_phandle_iterator it; + const char **groups; + int ret; + int i; + + groups = devm_kmalloc_array(dev, num_groups, sizeof(*groups), GFP_KERNEL); + if (!groups) { + err = -ENOMEM; + goto err_put_node; + } + + i = 0; + of_for_each_phandle(&it, ret, np, "groups", NULL, 0) { + groups[i++] = it.node->name; + } + + pinmux_generic_add_function(pctldev, np->name, groups, num_groups, np); + } + +err_put_node: + of_node_put(functions); +err_out: + return err; +} +#endif + static void dt_free_map(struct pinctrl_dev *pctldev, struct pinctrl_map *map, unsigned num_maps) { diff --git a/drivers/pinctrl/devicetree.h b/drivers/pinctrl/devicetree.h index efa80779de4f..156f13896c39 100644 --- a/drivers/pinctrl/devicetree.h +++ b/drivers/pinctrl/devicetree.h @@ -9,6 +9,15 @@ struct of_phandle_args; #ifdef CONFIG_OF +int pinctrl_generic_dt_load_pins(struct pinctrl_desc *pctldesc, + struct device *dev); +#ifdef CONFIG_GENERIC_PINCTRL_GROUPS +int pinctrl_generic_load_dt_groups(struct pinctrl_dev *pctldev); +#endif +#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS +int pinmux_generic_load_dt_functions(struct pinctrl_dev *pctldev); +#endif + void pinctrl_dt_free_maps(struct pinctrl *p); int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev); @@ -21,6 +30,26 @@ int pinctrl_parse_index_with_args(const struct device_node *np, #else +static inline int pinctrl_generic_dt_load_pins(struct pinctrl_desc *pctldesc, + struct device *dev) +{ + return -EOPNOTSUPP; +} + +#ifdef CONFIG_GENERIC_PINCTRL_GROUPS +static inline int pinctrl_generic_load_dt_groups(struct pinctrl_dev *pctldev) +{ + return -EOPNOTSUPP; +} +#endif + +#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS +static inline int pinmux_generic_load_dt_functions(struct pinctrl_dev *pctldev) +{ + return -EOPNOTSUPP; +} +#endif + static inline int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev) { diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c index ef7d2cbf0946..36a1d1af4a20 100644 --- a/drivers/pinctrl/pinmux.c +++ b/drivers/pinctrl/pinmux.c @@ -27,6 +27,7 @@ #include #include #include "core.h" +#include "devicetree.h" #include "pinmux.h" int pinmux_check_ops(struct pinctrl_dev *pctldev) @@ -790,6 +791,9 @@ void pinmux_init_device_debugfs(struct dentry *devroot, int pinmux_generic_load_functions(struct pinctrl_dev *pctldev) { + if (pctldev->dev->of_node) + return pinmux_generic_load_dt_functions(pctldev); + return -ENOENT; } EXPORT_SYMBOL_GPL(pinmux_generic_load_functions);