From patchwork Wed Aug 17 20:03:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 12946388 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 DBEABC32773 for ; Wed, 17 Aug 2022 20:04:00 +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=FGqRP5PyGi/ZNbo8r5SkCO/nw9HXGJK5mlKmo1AjXWw=; b=wLnMcY0P0mZVkA a4Vspo1nz997dhCIcbZN2M5MI4P8obC06u7z1H2M0T88L/FX49KTnrs921x88XENy+SdOBZQcjZsF bDmW0ngr7SCHd1MUO8qjXjgu/Gj2QdS/dwm+qcoExMFtQYr57OtXlP3apkkFk2uKCeIL+eC1It3SI QM8dB4jt8wb0UmMTOSdGPQfyG78aFwjvHj9NPvhHiY2vQ+0a9YZzCKrCVtfzI4489z4Xh8RLLXYMG QlnhtvbgDVT5AhiSaQgA3gISPTHoiYGBbHZLrroFz0aCDnHX3iOCsGXj7ILxNv/qRJkj7+2KQcszk gNlBk3f1RKQ3WbS05QPQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOPGi-0065Ru-7O; Wed, 17 Aug 2022 20:04:00 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOPGe-0065MV-Ig for linux-phy@lists.infradead.org; Wed, 17 Aug 2022 20:03:59 +0000 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 59434B81F5D; Wed, 17 Aug 2022 20:03:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8820C43470; Wed, 17 Aug 2022 20:03:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660766626; bh=xoSj1JpELAuTG/WCX3GOaVx+5vM2XwQeuT06KHfLqk8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y2XewTojGKRqC7pumHvuCgCJUMC1tOuAq2KQOSSO4uALMisMqEEZhGqGfA0rKk2wc OZyjqQOXqLFPcYUaXUXnE0pOm6ifAz+HWXj8qKgqapeM83eOL+lr3C4YGeHdo2Xuc3 aGwjf7IHbKtxIGbwwzRbVWgQiG8BR2kuCgT862+B56pkYVakQk4r8ismVoqWmte7YG kVcWOeV678nbduRAkHOZYvJDKmxcM5obtfNXk5nrynAv5sUi4W/FaQQMGJo9BXdbEl qGAfsnO+ccXb9eFu+9XTYOVl3knpWy3rTYrZ4fMqzNgaoKBUfF+SoTERUZYj/arV3U NUKjjzcY4KYiQ== From: =?utf-8?q?Marek_Beh=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_Beh=C3=BAn?= Subject: [PATCH linux-phy v2 2/4] device property: Add {fwnode/device}_get_tx_p2p_amplitude() Date: Wed, 17 Aug 2022 22:03:33 +0200 Message-Id: <20220817200335.911-3-kabel@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220817200335.911-1-kabel@kernel.org> References: <20220817200335.911-1-kabel@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220817_130356_982869_D3B1B57B X-CRM114-Status: GOOD ( 21.48 ) X-BeenThere: linux-phy@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux Phy Mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-phy" Errors-To: linux-phy-bounces+linux-phy=archiver.kernel.org@lists.infradead.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 * const 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(