From patchwork Thu Nov 18 13:21:50 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: 12693043 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4694DC433F5 for ; Thu, 18 Nov 2021 13:24:50 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 108326113D for ; Thu, 18 Nov 2021 13:24:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 108326113D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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=7TBpChtKMfDxxR/iiw9hXgpJdEB9QShEF0Af3lKP5Wk=; b=bbttzhQrzSapVE f3sJl0VYU873O4BiHtgwpuiINzf9Q5VA4jhnMNe9/yAZtw3xor+QWdYxCMk5/FNEihnz5g9a3mUMd 9cVY/jrDnGycLk/8MbFr5mawEFR7Uq/vuZKchW9XMIiv+6PqyL8ZDeKGuxP1UUD+MbEx0qgoF+CkM +SkPdY/Bl5GlxBP/m/Pqheh4ksfJsRT8y5n1yhzEN1f9V/FKnePM/0lJY9Wt6U/yu+spUKHA2kL8w eJhwxRrF0H2UrCjyOxDWD+Lo+FfFYFOvEwLMFF3fA9BJ3lzpMxUGE9laskt8Hf4pL1Gb6P8DWflRJ T8gJn1McBK7930uI7F2w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnhNN-007sYQ-BP; Thu, 18 Nov 2021 13:22:53 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnhMm-007sO5-0E for linux-arm-kernel@lists.infradead.org; Thu, 18 Nov 2021 13:22:17 +0000 Received: by mail-lf1-x129.google.com with SMTP id b1so25696895lfs.13 for ; Thu, 18 Nov 2021 05:22:15 -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=W1ozSFTzW6iBlqeGsm7xnlG661/HZOcRaphE0Mn8yuQ=; b=WSn1/wog/OwyjfdshRgjE58acBoLdQrXIxGyAsMo4nxV8MNHwFCqkpgLgdnR76rwqa Nn7dsAacuOuRP8yfs1zcIgm8XtNpzSnl02LbuHNGktmHxFodApoY++CuqY5zDtMKZ21v F0KrZn6XFxC+Zt1IfaJbUZcxI+/oMFhCr6wKGxV/FbY25UWySE4YoS1wCwWCiLgyye7d TESFHk3zwvpCdTQ/T44OQNvzp0gGaKB800oIWbWCVbS6yQL9woZKMvd5vBdtfSWnO7Z7 YxYdPf85AnHhHv4AnZHU4ODsw60AA/GCXw/n6euTP833uLcEXd3GFwv71MHcotFiFzUA mc5Q== 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=W1ozSFTzW6iBlqeGsm7xnlG661/HZOcRaphE0Mn8yuQ=; b=V2FGUvHdoBbGuxxc1nAg9dGUWOXM5e7Tuh1SNyWAMaf5OSzsp1FOMk+7Dx3uZZqWm9 aYqBevjJbn2WMAuyETF/iWu8AW9VgWcXfb/0FuZ1hHCNXghu8YZjzM5wtrLuiBNkTfKl ZoVwxTUykMRB4QPBgmQyser3f9nZuBEfzEG4VRIFj1SiI/BEpqksPeWA6yYEjopC0/fr 8dgZiMhTe82AkD7I7qt2/RXsae5Af57/c7Fw347E5BkrlVD7ExJrTupO8wRJxhtZ3883 aoF3H8p9fpCX0IQPLxMCHAkCVUr5vVmsNTnYxESkJPqgK0ow+eKGmbp9G9zAb6TWtM21 L1aA== X-Gm-Message-State: AOAM531kwYvM+O5glz9ThdVh9yDfSqOsJV6pFnH44M8PuatQNE81XBKi o3uGUkZHhfCe3Vj9bgvdSYU= X-Google-Smtp-Source: ABdhPJzhPd3NwZM5kPJNaUrl8UlVEtayxqeTmnQVBr71WveoL1dTgaUVbSqEMK4nQaAyG4q2PA8DgA== X-Received: by 2002:a2e:9e94:: with SMTP id f20mr8987115ljk.401.1637241733983; Thu, 18 Nov 2021 05:22:13 -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 bp36sm356550lfb.0.2021.11.18.05.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Nov 2021 05:22:13 -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 3/5] pinctrl: add helpers reading pins, groups & functions from DT Date: Thu, 18 Nov 2021 14:21:50 +0100 Message-Id: <20211118132152.15722-4-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211118132152.15722-1-zajec5@gmail.com> References: <20211118132152.15722-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211118_052216_082057_69F47DFE X-CRM114-Status: GOOD ( 17.19 ) 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. Introduce helpers based on CONFIG_GENERIC_PINCONF, CONFIG_GENERIC_PINCTRL_GROUPS and CONFIG_GENERIC_PINMUX_FUNCTIONS for parsing that info into pinctrl generic structures. Signed-off-by: Rafał Miłecki --- drivers/pinctrl/core.c | 89 ++++++++++++++++++++++++++++++++++++++++ drivers/pinctrl/core.h | 5 +++ drivers/pinctrl/pinmux.c | 43 +++++++++++++++++++ drivers/pinctrl/pinmux.h | 2 + 4 files changed, 139 insertions(+) diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c index ffe39336fcac..8f6ed8488313 100644 --- a/drivers/pinctrl/core.c +++ b/drivers/pinctrl/core.c @@ -515,8 +515,97 @@ void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, } EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); +int pinctrl_generic_get_dt_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) { + if (of_property_read_u32(np, "reg", &descs[i].number)) { + dev_err(dev, "missing \"reg\" property in %pOF\n", np); + err = -ENOENT; + goto err_put_node; + } + + if (of_property_read_string(np, "label", &descs[i].name)) { + dev_err(dev, "missing \"label\" 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; +} +EXPORT_SYMBOL_GPL(pinctrl_generic_get_dt_pins); + #ifdef CONFIG_GENERIC_PINCTRL_GROUPS +int pinctrl_generic_get_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; +} +EXPORT_SYMBOL_GPL(pinctrl_generic_get_dt_groups); + /** * pinctrl_generic_get_group_count() - returns the number of pin groups * @pctldev: pin controller device diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h index 840103c40c14..59661d4d4cc7 100644 --- a/drivers/pinctrl/core.h +++ b/drivers/pinctrl/core.h @@ -182,6 +182,9 @@ struct pinctrl_maps { unsigned num_maps; }; +int pinctrl_generic_get_dt_pins(struct pinctrl_desc *pctldesc, + struct device *dev); + #ifdef CONFIG_GENERIC_PINCTRL_GROUPS /** @@ -198,6 +201,8 @@ struct group_desc { void *data; }; +int pinctrl_generic_get_dt_groups(struct pinctrl_dev *pctldev); + int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev); const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev, diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c index 6cdbd9ccf2f0..5e34bd3135f5 100644 --- a/drivers/pinctrl/pinmux.c +++ b/drivers/pinctrl/pinmux.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "core.h" @@ -788,6 +789,48 @@ void pinmux_init_device_debugfs(struct dentry *devroot, #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS +int pinmux_generic_get_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; +} +EXPORT_SYMBOL_GPL(pinmux_generic_get_dt_functions); + /** * pinmux_generic_get_function_count() - returns number of functions * @pctldev: pin controller device diff --git a/drivers/pinctrl/pinmux.h b/drivers/pinctrl/pinmux.h index 78c3a31be882..ca69025fce46 100644 --- a/drivers/pinctrl/pinmux.h +++ b/drivers/pinctrl/pinmux.h @@ -134,6 +134,8 @@ struct function_desc { void *data; }; +int pinmux_generic_get_dt_functions(struct pinctrl_dev *pctldev); + int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev); const char *