From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 666BE44C70 for ; Wed, 27 Dec 2023 12:35:12 +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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="NYee9cWR" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3368d1c7b23so4323714f8f.0 for ; Wed, 27 Dec 2023 04:35:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1703680511; x=1704285311; 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=epGZ+q6UP32u7muTF0woV3dsSCz73pHRzHknNCbCjuk=; b=NYee9cWRFPvsEpx0dF5LnheMrt8B/W4GJ4PMZuBgyx1tvM6mkncvmzEfVc/6Af3lsy HU24YMFOOWLczA068ikGJ2ov7XYhxLMq+ZrV3qFtmgWG53NSEnYCOLTjMDf3EOw967EB 9luacZZIPIUMTezcKQdbNv3XYw8kH2Rt0b5HzLkrCQ7818+eUgTEK72pJeckSL58j2t+ T5uxsMncSBfIOAWDig3h9MLQ/DiWAzGzXE7zyPFvtUGnn/I2dz2sJOIDKo3rlTTbdPz1 ThAOmW65oVuwT1vPd2pqnew2nA//zOi+4Veuj/tEqf7FSNHQwroLeDneoCv4djPJ9Sko Go+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703680511; x=1704285311; 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=epGZ+q6UP32u7muTF0woV3dsSCz73pHRzHknNCbCjuk=; b=ZVaUvyNbk4E0aV9J/S5U5dBuXcfcpQRqFJ2A/wTihgxufSNOHdR/B5bnE3QyhcVFRX EbCzSkBPZ5/Y9GGa0RSYKTQ0DLnekdkkByFQf4OLlvpuApqkXvJfJGLiBlRgZ319OlD/ N4PPEXg06ne4rHLLOt4ORavkQ7Yw63LFiadDI9geFPdy9pAChPVrYwy5m+C6LBPqBM+7 wi6cc3fyrywgt7SRythYbLiF3qZI8Y7EFC5jhLnIft+sxsfefFAT483pQXtr7Hb5UMh7 m7H2kSW9X3wZ6lCE0cW99pmyBXvNzMyQZDdIeVsWMRYKfR6wJqDlc0ZZDv52nnJ6Ys+C NJog== X-Gm-Message-State: AOJu0YwCAyiaUx/bjwE4uV9XdBmNU1ki8PeQr/PsBv0qTEZ/2JZhp75g xnfwo0fqVqxvX6SM8bZddIC51PrPIEbLJQ== X-Google-Smtp-Source: AGHT+IH2LTK8BtQpyT3te/10HSe9eiLkrR1TeIv9Jc4wR6leMJ4eWm8bv8fbJ+b0b7TPx4oZzw5Xgg== X-Received: by 2002:a5d:5404:0:b0:336:1aef:7352 with SMTP id g4-20020a5d5404000000b003361aef7352mr5001123wrv.109.1703680510694; Wed, 27 Dec 2023 04:35:10 -0800 (PST) Received: from [192.168.1.20] ([178.197.218.27]) by smtp.gmail.com with ESMTPSA id t18-20020a5d49d2000000b00336751cd4ebsm14772953wrs.72.2023.12.27.04.35.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 Dec 2023 04:35:09 -0800 (PST) Message-ID: Date: Wed, 27 Dec 2023 13:35:07 +0100 Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/4] reset: instantiate reset GPIO controller for shared reset-gpios Content-Language: en-US To: Bartosz Golaszewski Cc: Bjorn Andersson , Konrad Dybcio , Srinivas Kandagatla , Banajit Goswami , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Philipp Zabel , Jaroslav Kysela , Takashi Iwai , linux-arm-msm@vger.kernel.org, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Anderson References: <20231222150133.732662-1-krzysztof.kozlowski@linaro.org> <20231222150133.732662-2-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/BIfXXfC68nMp9BZoy3So4ZsbOlBmtAPvMYX6U8VwD TNeBxJu5Ex0Izf1NV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BQt2CopXtHtYfPAO5nAkIvKSNp3jmGxZw4aTc5xoAZBLo0OV+Ezo71pg3AYvq0a3/oGRG KQ06ztUMRrj8eVtpImjsWCd0bDWRaaR4vqhCHvAG9iWXZu4qh3ipie2Y0oSJygcZT7H3UZxq fyYKiqEmRuqsvv6dcbblD8ZLkz1EVZL6djImH5zc5x8qpVxlA0A0i23v5QvN00m6G9NFF0Le D2GYIS41Kv4Isx2dEFh+/Q== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 22/12/2023 18:31, Bartosz Golaszewski wrote: >> >> +static int __reset_add_reset_gpio_device(struct device_node *node, >> + const struct gpio_desc **out) >> +{ >> + struct platform_device *pdev; >> + int gpio; >> + >> + /* Don't care about deprecated '-gpio' suffix. */ >> + gpio = of_get_named_gpio(node, "reset-gpios", 0); > > Sorry but NAK. This is a legacy API, we've managed to remove all of_ No worries, I expected problems with this patchset. I should have title it RFC. > GPIO interfaces but this one. Please don't use it. Any function that > deals with the global GPIO numberspace is deprecated. See below how I > would approach it. > > Side note: do we have any formal way of deprecating interfaces in the kernel? "DEPRECATED" message in kerneldoc would be enough for me. This could be later used by tooling like checkpatch/sparse/toolchains. > >> + if (!gpio_is_valid(gpio)) >> + return gpio; >> + >> + pdev = platform_device_register_data(NULL, "reset-gpio", >> + PLATFORM_DEVID_AUTO, &node, >> + sizeof(node)); > > You seem to make an assumption (in __reset_find_rcdev()) that this > device will get bound to its driver before this function returns. > There is no such guarantee. There are many situations where this may > not happen immediately (think loading the reset-gpio driver as a > module from the filesystem). You should set up a notifier callback on Yeah, this code is poor. In the case you mentioned, requesting a reset line would spawn multiple platform devices of which none would bind. > the platform bus that will get invoked on bus events and wait for this > device's BUS_NOTIFY_BOUND_DRIVER event. Hm, good point. Thanks. > >> + if (!IS_ERR(pdev)) >> + *out = gpio_to_desc(gpio); >> + >> + return PTR_ERR_OR_ZERO(pdev); >> +} >> + >> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, >> + const void *cookie) >> +{ >> + 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 && args->np) { >> + if (args->np == r->of_node) { >> + rcdev = r; >> + break; >> + } >> + } else if (cookie) { >> + if (cookie == r->cookie) { > > As I said in private: there's no guarantee that the same GPIO > descriptor will always reference the same GPIO line. It happens to be > the case currently but it's an implementation detail. I guess you commented on this further, so I will reply there. > >> + 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) >> { >> + const struct gpio_desc *gpio = NULL; >> + struct of_phandle_args args = {0}; >> 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,17 +888,16 @@ __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) { >> + ret = __reset_add_reset_gpio_device(node, &gpio); >> + if (ret) >> + return optional ? NULL : ERR_PTR(ret); >> + >> + args.args_count = 1; /* reset-gpio has only one reset line */ >> + } >> >> 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; >> - } >> - } >> + rcdev = __reset_find_rcdev(&args, gpio); >> >> if (!rcdev) { >> rstc = ERR_PTR(-EPROBE_DEFER); >> diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h >> index 0fa4f60e1186..c0a99a8ea29e 100644 >> --- a/include/linux/reset-controller.h >> +++ b/include/linux/reset-controller.h >> @@ -61,6 +61,7 @@ struct reset_control_lookup { >> * @dev: corresponding driver model device struct >> * @of_node: corresponding device tree node as phandle target >> * @of_reset_n_cells: number of cells in reset line specifiers >> + * @cookie: for reset-gpios controllers: corresponding GPIO instead of of_node >> * @of_xlate: translation function to translate from specifier as found in the >> * device tree to id as given to the reset control ops, defaults >> * to :c:func:`of_reset_simple_xlate`. >> @@ -74,6 +75,7 @@ struct reset_controller_dev { >> struct device *dev; >> struct device_node *of_node; >> int of_reset_n_cells; >> + const void *cookie; >> int (*of_xlate)(struct reset_controller_dev *rcdev, >> const struct of_phandle_args *reset_spec); >> unsigned int nr_resets; >> -- >> 2.34.1 >> > > I dislike this approach entirely. Here's what I would do: > > In the reset core: parse the phandle of the reset-gpios property. Use > the resulting node with gpio_device_find_by_fwnode(). If the device is > not up yet, defer probe. You'll now have the GPIO device object. > Retrieve its label using gpio_device_get_label(). Now you should have > everything you need (the offset and flags of the GPIO you'll get from > __of_parse_phandle_with_args()) to set up the lookup tables. See I don't get exactly what do you want to use as the cookie for lookup reset controller? 1. GPIO controller node? No way, not enough 2. GPIO label? 3. Or some combination of above? This would work, I guess, a bit more complicated cookie than just one pointer/unsigned long. > include/linux/gpio/machine.h. An example of that would be in: > bcm2835_spi_setup() in drivers/spi/spi-bcm2835.c. > > Then with a lookup table in place, you instantiate your device and > it'll get its GPIO as it should. This is not ideal but much better > than the above. Best regards, Krzysztof