Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp5194950rwb; Wed, 17 Aug 2022 12:52:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR6ytWhPQ9wolPpetzgX7tjG7+fb19KR8XMFrVrdkWQ2gRog+O5IleBdkDYa6JlrCbX7dCa6 X-Received: by 2002:a17:902:9882:b0:172:793b:ccc3 with SMTP id s2-20020a170902988200b00172793bccc3mr10220559plp.27.1660765940236; Wed, 17 Aug 2022 12:52:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660765940; cv=none; d=google.com; s=arc-20160816; b=B99vfl7sv9uTTiKbYEQtRKPG56pY8dl8vb8JpI6Bfe9RYcikCZ6fYjg6nraTrJEFZd wLZNk3M2LB646aUXoSwarZiOIM6LHFENrLiqA+0RQecm8g8nXYsWDJDE1NArsxHin82L eBytUgFY5WaSjBKWSNu4YFhuKL1mnGWJ7rFZ41R+bovbqZNupGd6AoytyQbW8J5chUL1 8/tPCD+aXetkLK6mBCbzIr2kyIVNH48W5NjT8Ysx/F83z1JbkwZ2kGb5br/pdAkT0BzH K3ji8NhY5uZdSFd0ZtTFw8iH838ayPw5nm2qdYC0kovhhV5MK9roE2sHqX/Axczzadpk Oc4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1A26NHEXJGQ5ktOhy8XakgJ8zbqvAVv7YAEc8olZVGQ=; b=H/l7UWfKNPP9jJaQC/RJ5wycRgF+yQ30U0skieqMJggzipuoP0a7Xm87AlpvWILpFq 7KX1kRQqXQDo8jziCY8GAixPQrqQll/Bdv6GfbsSN7ToTkfd1nZDomGuRkQvfADNAsdj q2BukVXLmsCfcBut49XxHGs+jdJ8Mh3Ih1g79y+Xe8sKV4/rTChTibXRku+AK0AIY9L9 eEMVyMzlu17S2SRNRs29acm3nsutcZwtL6i61pwds+S5ApgpT9VQkIIO29W73YlVt5Oh s/3wNM8ZaylZ7WRo8CFnJogFoSKb7Wyb0yoOs/Zf/stfExKuHofl94/qC26tN8V3WRGj xs1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=prJLdY+J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p5-20020a63c145000000b004121ba59eb5si16113595pgi.200.2022.08.17.12.52.08; Wed, 17 Aug 2022 12:52:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=prJLdY+J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241489AbiHQTdE (ORCPT + 99 others); Wed, 17 Aug 2022 15:33:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241545AbiHQTcf (ORCPT ); Wed, 17 Aug 2022 15:32:35 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA9DBA5709 for ; Wed, 17 Aug 2022 12:31:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0C8EDB81F4B for ; Wed, 17 Aug 2022 19:31:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F712C433D6; Wed, 17 Aug 2022 19:31:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660764690; bh=h9jJLisusQCTKq1uyl3xtKgwVeHl5sOQ3o5Kt7G3JHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=prJLdY+J42uJNxFqSFVxhzLN8b2mQBv+PDtw2hJIQ2jxq67UW/dEu7wgUkL2dOsMo sMSHoCd5e1ge/LpjF+55NnoNqT+CANvLc42aZ3QrMSE8vJUWUq/Zu6Y/xyoTJ/cXAI ympXraJSWCzcnRU/qtWns0le0MbcaHNi4+4gezZoXs1gO3RQWtY/o9pKJhJJoGRFcN EG4+oTTQEDnLGQLRJhVvNtW+LjvKkOXMdOd8qMRpCMIqjcipdRy5zciYYPdMEaJxqy 4OXbXnvQsTYQI/ke4pstH+C7s417rl8pYiJUD5kcSZG0JoEstgskKVWxYNe8Z1RK69 Xb+ANStIxFaTw== From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: Vinod Koul , Kishon Vijay Abraham I , Linux Phy , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus Cc: Gregory Clement , Kees Cook , linux-kernel@vger.kernel.org, pali@kernel.org, josef.schlehofer@nic.cz, =?UTF-8?q?Marek=20Beh=C3=BAn?= Subject: [PATCH linux-phy 2/4] device property: Add {fwnode/device}_get_tx_p2p_amplitude() Date: Wed, 17 Aug 2022 21:31:17 +0200 Message-Id: <20220817193119.4463-3-kabel@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220817193119.4463-1-kabel@kernel.org> References: <20220817193119.4463-1-kabel@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add functions fwnode_get_tx_p2p_amplitude() and device_get_tx_p2p_amplitude() that parse the 'tx-p2p-microvolt' and 'tx-p2p-microvolt-names' properties and return peak to peak transmit amplitude in microvolts for given PHY mode. The functions search for mode name in 'tx-p2p-microvolt-names' property, and return amplitude at the corresponding index in the 'tx-p2p-microvolt' property. If given mode is not matched in 'tx-p2p-microvolt-names' array, the mode name is generalized (for example "pcie3" -> "pcie" -> "default", or "usb-ss" -> "usb" -> "default"). If the 'tx-p2p-microvolt-names' is not present, the 'tx-p2p-microvolt' property is expected to contain only one value, which is considered default, and will be returned for any mode. Signed-off-by: Marek BehĂșn --- Andy et al. can I get Ack for this if this is okay? --- drivers/base/property.c | 130 +++++++++++++++++++++++++++++++++++++++ include/linux/property.h | 5 ++ 2 files changed, 135 insertions(+) diff --git a/drivers/base/property.c b/drivers/base/property.c index ed6f449f8e5c..34b763436c30 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -921,6 +921,136 @@ int device_get_phy_mode(struct device *dev) } EXPORT_SYMBOL_GPL(device_get_phy_mode); +/** + * fwnode_get_tx_p2p_amplitude - Get peak to peak transmit amplitude for given + * PHY mode + * @fwnode: Pointer to the given node + * @mode: Name of the PHY mode, or "default" / NULL + * @amplitude: Pointer where to store the amplitude + * + * Gets the peak to peak transmit amplitude in microvolts for a given PHY mode + * by parsing the 'tx-p2p-microvolt' and 'tx-p2p-microvolt-names' properties. + * If amplitude is not specified for @mode exactly, tries a more generic mode, + * and if that isn't specified, tries "default". + * + * For example if @mode is "pcie3", we first try searching for value + * corresponding to "pcie3", then to "pcie", and finally to "default". + * + * Return: %0 if the amplitude was read (success), + * %-EINVAL if given arguments are not valid, + * %-ENODATA if the required properties do not have a value, + * %-EPROTO if the property is not an array of strings, + * %-ENXIO if no suitable firmware interface is present, + * %-ENOMEM if out of memory. + */ +int fwnode_get_tx_p2p_amplitude(struct fwnode_handle *fwnode, const char *mode, + u32 *amplitude) +{ + static const char *names_prop = "tx-p2p-microvolt-names", + *vals_prop = "tx-p2p-microvolt"; + const char **names; + int cnt, idx, ret; + u32 *vals; + + cnt = fwnode_property_string_array_count(fwnode, names_prop); + if (!cnt || cnt == -EINVAL) + /* + * If the names property does not exist or is empty, we expect + * the values property to contain only one, default value. + */ + return fwnode_property_read_u32(fwnode, vals_prop, amplitude); + else if (cnt < 0) + return cnt; + + names = kcalloc(cnt, sizeof(*names), GFP_KERNEL); + if (!names) + return -ENOMEM; + + ret = fwnode_property_read_string_array(fwnode, names_prop, names, cnt); + if (ret < 0) { + kfree(names); + return ret; + } + + if (!mode) + mode = "default"; + + do { + static const char *gen_table[] = { + "pcie", "usb", "ufs-hs", "dp", "mipi-dphy", + }; + size_t i; + + idx = match_string(names, cnt, mode); + if (idx >= 0) + break; + + /* If mode was not matched, try more generic mode */ + for (i = 0; i < ARRAY_SIZE(gen_table); ++i) { + if (str_has_proper_prefix(mode, gen_table[i])) { + mode = gen_table[i]; + break; + } + } + + /* Or "default" */ + if (i == ARRAY_SIZE(gen_table)) { + if (strcmp(mode, "default")) + mode = "default"; + else + mode = NULL; + } + } while (mode); + + kfree(names); + + if (idx < 0) + return -ENODATA; + + vals = kcalloc(cnt, sizeof(*vals), GFP_KERNEL); + if (!vals) + return -ENOMEM; + + ret = fwnode_property_read_u32_array(fwnode, vals_prop, vals, cnt); + if (ret) + goto out; + + *amplitude = vals[idx]; +out: + kfree(vals); + return ret; +} +EXPORT_SYMBOL_GPL(fwnode_get_tx_p2p_amplitude); + +/** + * device_get_tx_p2p_amplitude - Get peak to peak transmit amplitude for given + * PHY mode + * @dev: Pointer to the given device + * @mode: Name of the PHY mode, or "default" / NULL + * @amplitude: Pointer where to store the amplitude + * + * Gets the peak to peak transmit amplitude in microvolts for a given PHY mode + * by parsing the 'tx-p2p-microvolt' and 'tx-p2p-microvolt-names' properties. + * If amplitude is not specified for @mode exactly, tries a more generic mode, + * and if that isn't specified, tries "default". + * + * For example if @mode is "pcie3", we first try searching for value + * corresponding to "pcie3", then to "pcie", and finally to "default". + * + * Return: %0 if the amplitude was read (success), + * %-EINVAL if given arguments are not valid, + * %-ENODATA if the required properties do not have a value, + * %-EPROTO if the property is not an array of strings, + * %-ENXIO if no suitable firmware interface is present, + * %-ENOMEM if out of memory. + */ +int device_get_tx_p2p_amplitude(struct device *dev, const char *mode, + u32 *amplitude) +{ + return fwnode_get_tx_p2p_amplitude(dev_fwnode(dev), mode, amplitude); +} +EXPORT_SYMBOL_GPL(device_get_tx_p2p_amplitude); + /** * fwnode_iomap - Maps the memory mapped IO for a given fwnode * @fwnode: Pointer to the firmware node diff --git a/include/linux/property.h b/include/linux/property.h index a5b429d623f6..91b12a79e245 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -392,6 +392,11 @@ const void *device_get_match_data(struct device *dev); int device_get_phy_mode(struct device *dev); int fwnode_get_phy_mode(struct fwnode_handle *fwnode); +int fwnode_get_tx_p2p_amplitude(struct fwnode_handle *fwnode, const char *mode, + u32 *amplitude); +int device_get_tx_p2p_amplitude(struct device *dev, const char *mode, + u32 *amplitude); + void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index); struct fwnode_handle *fwnode_graph_get_next_endpoint( -- 2.35.1