From nobody Tue Jun 30 19:07:03 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0367DC433F5 for ; Mon, 10 Jan 2022 18:23:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239098AbiAJSXL (ORCPT ); Mon, 10 Jan 2022 13:23:11 -0500 Received: from aposti.net ([89.234.176.197]:35278 "EHLO aposti.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239088AbiAJSXJ (ORCPT ); Mon, 10 Jan 2022 13:23:09 -0500 From: Paul Cercueil To: Jean Delvare , Guenter Roeck , Jonathan Corbet Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, list@opendingux.net, Paul Cercueil Subject: [PATCH v3 1/2] ABI: hwmon: Document "label" sysfs attribute Date: Mon, 10 Jan 2022 18:22:55 +0000 Message-Id: <20220110182256.30763-2-paul@crapouillou.net> In-Reply-To: <20220110182256.30763-1-paul@crapouillou.net> References: <20220110182256.30763-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the "label" sysfs attribute, which can contain a descriptive label that allows to uniquely identify a device within the system. Signed-off-by: Paul Cercueil --- Notes: v2: New patch v3: No change Documentation/ABI/testing/sysfs-class-hwmon | 8 ++++++++ Documentation/hwmon/sysfs-interface.rst | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-hwmon b/Documentation/AB= I/testing/sysfs-class-hwmon index 1f20687def44..653d4c75eddb 100644 --- a/Documentation/ABI/testing/sysfs-class-hwmon +++ b/Documentation/ABI/testing/sysfs-class-hwmon @@ -9,6 +9,14 @@ Description: =20 RO =20 +What: /sys/class/hwmon/hwmonX/label +Description: + A descriptive label that allows to uniquely identify a + device within the system. + The contents of the label are free-form. + + RO + What: /sys/class/hwmon/hwmonX/update_interval Description: The interval at which the chip will update readings. diff --git a/Documentation/hwmon/sysfs-interface.rst b/Documentation/hwmon/= sysfs-interface.rst index 85652a6aaa3e..209626fb2405 100644 --- a/Documentation/hwmon/sysfs-interface.rst +++ b/Documentation/hwmon/sysfs-interface.rst @@ -99,6 +99,10 @@ Global attributes `name` The chip name. =20 +`label` + A descriptive label that allows to uniquely identify a device + within the system. + `update_interval` The interval at which the chip will update readings. =20 --=20 2.34.1 From nobody Tue Jun 30 19:07:03 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AD9DC4332F for ; Mon, 10 Jan 2022 18:23:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239113AbiAJSXT (ORCPT ); Mon, 10 Jan 2022 13:23:19 -0500 Received: from aposti.net ([89.234.176.197]:35302 "EHLO aposti.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239088AbiAJSXQ (ORCPT ); Mon, 10 Jan 2022 13:23:16 -0500 From: Paul Cercueil To: Jean Delvare , Guenter Roeck , Jonathan Corbet Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, list@opendingux.net, Paul Cercueil , Cosmin Tanislav Subject: [PATCH v3 2/2] hwmon: Add "label" attribute Date: Mon, 10 Jan 2022 18:22:56 +0000 Message-Id: <20220110182256.30763-3-paul@crapouillou.net> In-Reply-To: <20220110182256.30763-1-paul@crapouillou.net> References: <20220110182256.30763-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If a label is defined in the device tree for this device add that to the device specific attributes. This is useful for userspace to be able to identify an individual device when multiple identical chips are present in the system. Signed-off-by: Paul Cercueil Tested-by: Cosmin Tanislav --- Notes: v2: - Cache label into hwmon_device - Rename hwmon_dev_name_is_visible() to hwmon_dev_attr_is_visible() v3: Move the code setting the label around in the probe function to fix error handling issue. drivers/hwmon/hwmon.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index 3501a3ead4ba..8d787dba3e38 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,7 @@ =20 struct hwmon_device { const char *name; + const char *label; struct device dev; const struct hwmon_chip_info *chip; struct list_head tzdata; @@ -71,17 +73,29 @@ name_show(struct device *dev, struct device_attribute *= attr, char *buf) } static DEVICE_ATTR_RO(name); =20 +static ssize_t +label_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label); +} +static DEVICE_ATTR_RO(label); + static struct attribute *hwmon_dev_attrs[] =3D { &dev_attr_name.attr, + &dev_attr_label.attr, NULL }; =20 -static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, +static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) { struct device *dev =3D kobj_to_dev(kobj); + struct hwmon_device *hdev =3D to_hwmon_device(dev); =20 - if (to_hwmon_device(dev)->name =3D=3D NULL) + if (attr =3D=3D &dev_attr_name.attr && hdev->name =3D=3D NULL) + return 0; + + if (attr =3D=3D &dev_attr_label.attr && hdev->label =3D=3D NULL) return 0; =20 return attr->mode; @@ -89,7 +103,7 @@ static umode_t hwmon_dev_name_is_visible(struct kobject = *kobj, =20 static const struct attribute_group hwmon_dev_attr_group =3D { .attrs =3D hwmon_dev_attrs, - .is_visible =3D hwmon_dev_name_is_visible, + .is_visible =3D hwmon_dev_attr_is_visible, }; =20 static const struct attribute_group *hwmon_dev_attr_groups[] =3D { @@ -117,6 +131,7 @@ static void hwmon_dev_release(struct device *dev) if (hwdev->group.attrs) hwmon_free_attrs(hwdev->group.attrs); kfree(hwdev->groups); + kfree(hwdev->label); kfree(hwdev); } =20 @@ -733,6 +748,7 @@ __hwmon_device_register(struct device *dev, const char = *name, void *drvdata, const struct attribute_group **groups) { struct hwmon_device *hwdev; + const char *label; struct device *hdev; int i, err, id; =20 @@ -788,6 +804,18 @@ __hwmon_device_register(struct device *dev, const char= *name, void *drvdata, hdev->groups =3D groups; } =20 + if (device_property_present(dev, "label")) { + err =3D device_property_read_string(dev, "label", &label); + if (err < 0) + goto free_hwmon; + + hwdev->label =3D kstrdup(label, GFP_KERNEL); + if (hwdev->label =3D=3D NULL) { + err =3D -ENOMEM; + goto free_hwmon; + } + } + hwdev->name =3D name; hdev->class =3D &hwmon_class; hdev->parent =3D dev; --=20 2.34.1