Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp247265rdd; Tue, 9 Jan 2024 02:59:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6UoteFTzINOAN2dQCRcJwMrkp17e9ImPXtKrHYwgZmmCc/YEg1yVrM3nRyGqaI5oAf1Th X-Received: by 2002:a17:906:fd3:b0:a23:321e:48e9 with SMTP id c19-20020a1709060fd300b00a23321e48e9mr200158ejk.67.1704797973164; Tue, 09 Jan 2024 02:59:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704797973; cv=none; d=google.com; s=arc-20160816; b=rKL2i9dBxr9cr+odTcVysydBXHZlTaxXxDuweGFR5JVixzetwdfmKSBc7Xh2amAip+ 1Q5e0klqH7u0WaZxPNSkx98pev5AdrkbTCEm16ErJlvX+eoDCC+B6c6nAOq2/7DPc4p5 94kjfXY36tb1A/0oiMlPCzLEshCscgo75Ih5XOMcuIS37lrTyoK2CAdmfu169mKqYfJg S8Scn1thSXcTo203jJCTkFhrCFg7koxfUB494IKs/NjWAPCQvFaDXMaTK/BPXk8+DYeu CJCCFsPTIbVjiKrjA0Y+jiMR1qRWZfRAfJLzM0s6B8YJbO+Qu1iEvahikPy1lGldhuf9 cGeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=RZD8BJ+52q0rFPbMqYUgabHlbkDv0ZmsXi/Y7tv+U8k=; fh=Kj8VHwtO20zPm6tbqcfhRUeHvXD9k8SgMrXQf/TnRAY=; b=AoneKudSphdrktKgYU4aOOFoP4lLqbm8JKBqHlUBRs1XmZVDWc/avpzuHUwMSL5oof g1/A/U9EQaSC40vNKfN+GLCEPrZuXXZC3YmFCbDid8l42Xdkx8UiUgVmahz+9ud8o633 RT2ooJ43JJxVyl8ri48CTDWCd3JMsNFF9CDbJmzNIamyzzZ/khZV4+RfI2FJPmJlVY6Q QolH79FRZw9pCGzaHJsKhxddPquxyWjs4pesc/16wFH8YQvvS8UNOlogSaMgT4nBFxyJ ggN4mrwuUfEYvYEsegXNfg34L5y9XxqUWU/KzRSCnoyKTu3nvXjDk09SFmtL62wIJYKJ YqsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="NMBlj/sf"; spf=pass (google.com: domain of linux-kernel+bounces-20737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20737-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id v4-20020a1709062f0400b00a2b2ceba0cdsi653073eji.51.2024.01.09.02.59.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 02:59:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="NMBlj/sf"; spf=pass (google.com: domain of linux-kernel+bounces-20737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20737-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BA4121F2522A for ; Tue, 9 Jan 2024 10:59:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 401D436AFC; Tue, 9 Jan 2024 10:59:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="NMBlj/sf" Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6FB836AE3 for ; Tue, 9 Jan 2024 10:59:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a28b1095064so301842466b.2 for ; Tue, 09 Jan 2024 02:59:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704797957; x=1705402757; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=RZD8BJ+52q0rFPbMqYUgabHlbkDv0ZmsXi/Y7tv+U8k=; b=NMBlj/sfrkEXZMaOm/VfkJiKfPJu6Uu8WP4T/RYaIocMkmEsP3DosbUGGegkbwut03 xVFViWb7/4LjNWggQefL7jjeOCjC6OI482gKq8j4VsmZ/qYAlYPoUkTZLik4K4bvzypP ROfMdwX6XkFmhQdRuChG8rjcAxI4lwW60Zd9cuZCrzipV69/Qug3Q09E+nvh0VYz38k0 X8vYLD+TmrcuxlJ48BvHMkJi2CxFowv6OrxIwrWiJZg/RDU6jCR6OnCL/QiMJ0Eu9X1V oqDxhL8KNHSEEGvlfZDUZyYS3slIXGt28F0das6dNRWhyQ0eMlp8SWraTMq8ojAkkMiy pS6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704797957; x=1705402757; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RZD8BJ+52q0rFPbMqYUgabHlbkDv0ZmsXi/Y7tv+U8k=; b=Gc/7KPqiZ1bYZRIujLfP4mJ1vbctGyM0smFucJXD5paVRAs7j0qeaBVbh2YlKX4tqU 1Zgyc2hTJi9HugifOD65Y6KZd+Au4sDvURw73aifB0plHa52LMfhiju/Td+dnX+9I91/ 1P6QOgsnjmNmdG6/wU0q5donVdTJU+jQX0RVG4nTZ9cTjc4R0kBr/9C7MbVTJlxCBgMr x3q4fXb4j8RiqDP5gzZZr2UdV8/87qb9EoWyt8qK5IS/ukaNVjEc2c4gLbdy+eCcMPr7 fuV7PIbW9bxrxtjpLHVUOasKTK5Ah3tF2D1oYB/h2skch561kWB+fEn6zT06SzPL2ghf VAwA== X-Gm-Message-State: AOJu0YxH0F8okGh0QMC1ITBHtWpMjkGD5K4Ox67IsHVAlfelME6FdNi8 0ZK4quWtm9ieyTFelu+eCMsVXL3PYwNQaA== X-Received: by 2002:a17:907:1b16:b0:a23:3b67:a14d with SMTP id mp22-20020a1709071b1600b00a233b67a14dmr267694ejc.189.1704797956975; Tue, 09 Jan 2024 02:59:16 -0800 (PST) Received: from [192.168.1.20] ([178.197.223.112]) by smtp.gmail.com with ESMTPSA id d11-20020a170906344b00b00a26a061ae1esm908636ejb.97.2024.01.09.02.59.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Jan 2024 02:59:16 -0800 (PST) Message-ID: <767645a7-835d-4ac8-ac70-a701cea6df30@linaro.org> Date: Tue, 9 Jan 2024 11:59:13 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/4] reset: Instantiate reset GPIO controller for shared reset-gpios Content-Language: en-US To: Philipp Zabel , Srinivas Kandagatla , Banajit Goswami , Bjorn Andersson , Konrad Dybcio , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jaroslav Kysela , Takashi Iwai , alsa-devel@alsa-project.org, linux-arm-msm@vger.kernel.org, linux-sound@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Bartosz Golaszewski , Sean Anderson References: <20240105155918.279657-1-krzysztof.kozlowski@linaro.org> <20240105155918.279657-3-krzysztof.kozlowski@linaro.org> From: Krzysztof Kozlowski Autocrypt: addr=krzysztof.kozlowski@linaro.org; keydata= xsFNBFVDQq4BEAC6KeLOfFsAvFMBsrCrJ2bCalhPv5+KQF2PS2+iwZI8BpRZoV+Bd5kWvN79 cFgcqTTuNHjAvxtUG8pQgGTHAObYs6xeYJtjUH0ZX6ndJ33FJYf5V3yXqqjcZ30FgHzJCFUu JMp7PSyMPzpUXfU12yfcRYVEMQrmplNZssmYhiTeVicuOOypWugZKVLGNm0IweVCaZ/DJDIH gNbpvVwjcKYrx85m9cBVEBUGaQP6AT7qlVCkrf50v8bofSIyVa2xmubbAwwFA1oxoOusjPIE J3iadrwpFvsZjF5uHAKS+7wHLoW9hVzOnLbX6ajk5Hf8Pb1m+VH/E8bPBNNYKkfTtypTDUCj NYcd27tjnXfG+SDs/EXNUAIRefCyvaRG7oRYF3Ec+2RgQDRnmmjCjoQNbFrJvJkFHlPeHaeS BosGY+XWKydnmsfY7SSnjAzLUGAFhLd/XDVpb1Een2XucPpKvt9ORF+48gy12FA5GduRLhQU vK4tU7ojoem/G23PcowM1CwPurC8sAVsQb9KmwTGh7rVz3ks3w/zfGBy3+WmLg++C2Wct6nM Pd8/6CBVjEWqD06/RjI2AnjIq5fSEH/BIfXXfC68nMp9BZoy3So4ZsbOlBmtA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5V9v45lUwmdnMrSzZVgC4jRGXzsU EViBQt2CopXtHtYfPAO5nAkIvKSNp3jmGxZw4aTc5xoAZBLo0OV+Ezo71pg3AYvq0a3/oGRG KQ06ztUMRrj8eVtpImjsWCd0bDWRaaR4vqhCHvAG9iWXZu4qh3ipie2Y0oSJygcZT7H3UZxq fyYKiqEmRuqsvv6dcbblD8ZLkz1EVZL6djImH5zc5x8qpVxlA0A0i23v5QvN00m6G9NFF0Le D2GYIS41Kv4Isx2dEFh+/Q== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 08/01/2024 13:17, Philipp Zabel wrote: > On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski wrote: >> Devices sharing a reset GPIO could use the reset framework for >> coordinated handling of that shared GPIO line. We have several cases of >> such needs, at least for Devicetree-based platforms. >> >> If Devicetree-based device requests a reset line, which is missing but > ^^^^^^^^^^^^^^^^ > Nitpick: the "resets" property is missing, not the reset line. > > "If Devicetree-based device requests a reset line, but there only is a > reset-gpios property instead of a "resets" property, ..." maybe? Ack > >> there is a reset-gpios property, instantiate a new "reset-gpio" platform >> device which will handle such reset line. This allows seamless handling >> of such shared reset-gpios without need of changing Devicetree binding [1]. >> >> All newly registered "reset-gpio" platform devices will be stored on >> their own list to avoid any duplicated devices. > > That's not strictly true. The reset_gpio_device_list only contains the > of_phandle_args for lookup. Ack, I will re-phrase it. > >> The key to find each of >> such platform device is the entire Devicetree GPIO specifier: phandle to >> GPIO controller, GPIO number and GPIO flags. If two devices have >> conflicting "reset-gpios" property, e.g. with different ACTIVE_xxx >> flags, this would spawn two separate "reset-gpio" devices, where the >> second would fail probing on busy GPIO reques > > request. Ack > > Is that true? It should be true and my tests confirmed it. The code below looks like overwrites of_phandle_args so > that only one reset-gpio device is spawned for each gpio node. The code will iterate over list of of_node and of_phandle_args and compare them with __reset_gpios_args_match(). If all match: do not create new platform device. If they do not match, e.g. ACTIVE_LOW -> ACTIVE_HIGH, create new platform device. This will be the second device for the same GPIO. Probing of that device in reset-gpio driver will fail: [ 19.198775] reset-gpio reset-gpio.2.auto: error -EBUSY: Could not get reset gpios because GPIO is used by reset-gpio.1.auto already. > >> Link: https://lore.kernel.org/all/YXi5CUCEi7YmNxXM@robh.at.kernel.org/ [1] >> Cc: Bartosz Golaszewski >> Cc: Sean Anderson >> Signed-off-by: Krzysztof Kozlowski >> --- >> drivers/reset/core.c | 176 ++++++++++++++++++++++++++++--- >> include/linux/reset-controller.h | 4 + >> 2 files changed, 167 insertions(+), 13 deletions(-) >> >> diff --git a/drivers/reset/core.c b/drivers/reset/core.c >> index 4d5a78d3c085..ec9b3ff419cf 100644 >> --- a/drivers/reset/core.c >> +++ b/drivers/reset/core.c >> @@ -13,6 +13,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >> #include >> @@ -23,6 +24,10 @@ static LIST_HEAD(reset_controller_list); >> static DEFINE_MUTEX(reset_lookup_mutex); >> static LIST_HEAD(reset_lookup_list); >> >> +/* Protects reset_gpio_device_list */ >> +static DEFINE_MUTEX(reset_gpio_device_mutex); >> +static LIST_HEAD(reset_gpio_device_list); > > I would call this reset_gpio_lookup_list or > reset_gpio_phandle_args_list. Ack > >> + >> /** >> * struct reset_control - a reset control >> * @rcdev: a pointer to the reset controller device >> @@ -63,6 +68,16 @@ struct reset_control_array { >> struct reset_control *rstc[] __counted_by(num_rstcs); >> }; >> >> +/** >> + * struct reset_gpio_device - ad-hoc created reset-gpio device >> + * @of_args: phandle to the reset controller with all the args like GPIO number >> + * @list: list entry for the reset_lookup_list >> + */ >> +struct reset_gpio_device { > > Similarly, I would call this reset_gpio_lookup or > reset_gpio_phandle_args. Ack > >> + struct of_phandle_args of_args; >> + struct list_head list; >> +}; >> + >> static const char *rcdev_name(struct reset_controller_dev *rcdev) >> { >> if (rcdev->dev) >> @@ -813,13 +828,119 @@ static void __reset_control_put_internal(struct reset_control *rstc) >> kref_put(&rstc->refcnt, __reset_control_release); >> } >> >> +static bool __reset_gpios_args_match(const struct of_phandle_args *a1, >> + const struct of_phandle_args *a2) >> +{ >> + unsigned int i; >> + >> + if (!a2) >> + return false; >> + >> + if (a1->args_count != a2->args_count) >> + return false; >> + >> + for (i = 0; i < a1->args_count; i++) >> + if (a1->args[i] != a2->args[i]) >> + break; > > Just return false in the loop and simplify the following to return > true. Ack > >> + >> + /* All args matched? */ >> + if (i == a1->args_count) >> + return true; >> + >> + return false; >> +} >> + >> +/* >> + * @node: node of the device requesting reset >> + * @reset_args: phandle to the reset controller with all the args like GPIO number >> + */ >> +static int __reset_add_reset_gpio_device(struct device_node *node, >> + struct of_phandle_args *args) >> +{ >> + struct reset_gpio_device *rgpio_dev; >> + struct platform_device *pdev; >> + int ret; >> + >> + lockdep_assert_not_held(&reset_list_mutex); >> + >> + mutex_lock(&reset_gpio_device_mutex); >> + >> + list_for_each_entry(rgpio_dev, &reset_gpio_device_list, list) { >> + if (args->np == rgpio_dev->of_args.np) { >> + if (__reset_gpios_args_match(args, >> + &rgpio_dev->of_args)) { >> + ret = 0; >> + goto out_unlock; >> + } >> + } >> + } >> + >> + /* Not freed in normal path, persisent subsyst data */ >> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); > > Since this is persistent, instead of letting the reset-gpio driver call > of_parse_phandle_with_args() again, this could be passed in via > platform data. Is there a reason not to do that instead? We can pass it as read only platform data, but we cannot pass the ownership. This is associated with registered platform device, not with bound one device->driver. Imagine case: 1. modprobe reset-gpio, 2. Driver is bound to the device, 3. unbind (echo > unbind) 4. rmmod 5. goto 1 > >> + if (!rgpio_dev) { >> + ret = -ENOMEM; >> + goto out_unlock; >> + } >> + >> + rgpio_dev->of_args = *args; >> + pdev = platform_device_register_data(NULL, "reset-gpio", >> + PLATFORM_DEVID_AUTO, &node, >> + sizeof(node)); >> + ret = PTR_ERR_OR_ZERO(pdev); >> + if (!ret) >> + list_add(&rgpio_dev->list, &reset_gpio_device_list); >> + else >> + kfree(rgpio_dev); >> + >> +out_unlock: >> + mutex_unlock(&reset_gpio_device_mutex); >> + >> + return ret; >> +} >> + >> +static struct reset_controller_dev *__reset_find_rcdev(struct of_phandle_args *args, >> + bool gpio_fallback, >> + const void *cookie) > > Unused cookie. Ack > >> +{ >> + struct reset_controller_dev *r, *rcdev; >> + >> + lockdep_assert_held(&reset_list_mutex); >> + >> + rcdev = NULL; >> + list_for_each_entry(r, &reset_controller_list, list) { >> + if (args->np == r->of_node) { >> + if (gpio_fallback) { >> + if (__reset_gpios_args_match(args, r->of_args)) { >> + /* >> + * Fake args (take first reset) and >> + * args_count (to matcg reset-gpio > > match Ack > >> + * of_reset_n_cells) because reset-gpio >> + * has only one reset and does not care >> + * about reset of GPIO specifier. >> + */ >> + args->args[0] = 0; >> + args->args_count = 1; > > I'd expect args to be an input-only argument, but here its contents are > overwritten after a match. Why? > > This has an effect in __of_reset_control_get(), that I find hard to > follow. See below. > >> + rcdev = r; >> + break; >> + } >> + } else { >> + rcdev = r; >> + break; >> + } >> + } >> + } >> + >> + return rcdev; >> +} >> + >> struct reset_control * >> __of_reset_control_get(struct device_node *node, const char *id, int index, >> bool shared, bool optional, bool acquired) >> { >> + struct of_phandle_args args = {0}; >> + bool gpio_fallback = false; >> struct reset_control *rstc; >> - struct reset_controller_dev *r, *rcdev; >> - struct of_phandle_args args; >> + struct reset_controller_dev *rcdev; >> int rstc_id; >> int ret; >> >> @@ -839,21 +960,50 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, >> index, &args); >> if (ret == -EINVAL) >> return ERR_PTR(ret); >> - if (ret) >> - return optional ? NULL : ERR_PTR(ret); >> + if (ret) { >> + /* >> + * There can be only one reset-gpio for regular devices, so >> + * don't bother with GPIO index. >> + */ > > I don't understand this comment. The GPIO index should be checked as > part of __reset_gpios_args_match(), or should it not? This and earlier comment are result of a bit hacky approach to the problem: how to find reset controllers for that GPIO? The point is that our reset gpio controller has only 1 reset, thus of_reset_n_cells=1. However args_count from of_parse_handle is >0, which later is compared in reset core: https://elixir.bootlin.com/linux/latest/source/drivers/reset/core.c#L859 That part we need to match. I could make the reset-gpio driver to have of_reset_n_cells=2, but what would be the point? The rest of the cells are not really relevant, because you cannot refer to this reset gpio controller with any other arguments. To remind: my solution spawns one reset-gpio controller for one GPIO. > >> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", >> + 0, &args); >> + if (ret) >> + return optional ? NULL : ERR_PTR(ret); >> >> - mutex_lock(&reset_list_mutex); >> - rcdev = NULL; >> - list_for_each_entry(r, &reset_controller_list, list) { >> - if (args.np == r->of_node) { >> - rcdev = r; >> - break; >> - } >> + gpio_fallback = true; > > Is there a reason not just call __reset_add_reset_gpio_device() here? > With that, there should be no need to call __reset_find_rcdev() twice. Hm, could be, although not sure if code would be simpler. This entire function handles two cases: 1. Get normal reset controller ("resets" OF property), 2. If above fails, get reset-gpio controller ("reset-gpios" OF property) Therefore the entire solution is following approach: 1. of_parse_phandle(resets) 1b. error? Then of_parse_phandle(reset-gpios) 2. Find reset-controller based on any of above phandles. 3. error? Check if we created reset-gpios platform device. If not: create new reset-gpios platform device/ 3b. Platform device could probe, so lookup again for reset controller or defer probe. What type of flow do you propose? > >> } >> >> + mutex_lock(&reset_list_mutex); >> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL); > > This gets called with args as parsed. If there is a match, this will > overwrite args (in the gpio_fallback case) and return NULL. Overwrite not complete. It will only overwrite args_count and return a valid rcdev. I do not see overwriting in case of returning NULL. > >> + >> if (!rcdev) { >> - rstc = ERR_PTR(-EPROBE_DEFER); >> - goto out; >> + if (gpio_fallback) { >> + /* >> + * Registering reset-gpio device might cause immediate >> + * bind, thus taking reset_list_mutex lock via >> + * reset_controller_register(). >> + */ >> + mutex_unlock(&reset_list_mutex); >> + ret = __reset_add_reset_gpio_device(node, &args); > > So this will also be called with args as parsed. > >> + mutex_lock(&reset_list_mutex); >> + if (ret) { >> + rstc = ERR_PTR(ret); >> + goto out; >> + } >> + /* >> + * Success: reset-gpio could probe immediately, so >> + * re-check the lookup. >> + */ >> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL); > > And this will again be called with args as parsed and overwrite args > again.> >> + if (!rcdev) { >> + rstc = ERR_PTR(-EPROBE_DEFER); >> + goto out; >> + } >> + /* Success, rcdev is valid thus do not bail out */ >> + } else { >> + rstc = ERR_PTR(-EPROBE_DEFER); >> + goto out; >> + } >> } > > So at this point args is overwritten in the gpio_fallback case. I would > find it much clearer to just overwrite args here and make the first > parameter to __reset_find_rcdev() const. I think I get your point. Overwriting happens after we store the original of_args, but the code is indeed not intuitive. I think I can move it further, as you suggested. Best regards, Krzysztof