From patchwork Mon Jun 24 17:46:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 13709932 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CBE519E81B for ; Mon, 24 Jun 2024 17:46:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719251190; cv=none; b=lukM/xq7x9gQkqEzAcH2/tGC1876J64IuxzefF/l8pnwHyhswUcE8eSEiWQrKRtftSb8KTOsDHse+iDkbxDQZYwckFfHUvlzC/yqWaHe4vh1imumBy3peP8ztYoHjYu4ScyyViKdn9DhHY23AFw3yqMAXpM0BBM0JzXBzg7H9H8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719251190; c=relaxed/simple; bh=wejDnFkIHlBIhIot9CtzBrJmkO8rWvq2lRq5kHDRBSA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gjvmxHlfkritnbKD6gAkz3+d62vtqdpJvalmii98w6RbKY0vSRKy2IcKLS/2A1Yz2U6OEPYXd+QB2W6kjX1KS//OVPUcKEmKHH28kUJIWm63dv3jvhBMVsmRbnEr7I1Pwum0yYDMItNc9nR8PVkxnBNnu6bprr8iZ9PAv2LZEh8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=u7C0L2Ol; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="u7C0L2Ol" X-Envelope-To: jic23@kernel.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1719251185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WdQIawjGALB68yh7k2xfDgOAVt1XzSJ4v8yH3dVsoZc=; b=u7C0L2OlX8TsMGuCt/RuP3dYqRT2sMnrUFkrVXbes03Lr5OBnPi+IscUYm1f1RhzBFoGSs ti3qAIpBiLO8FAxXUvv6Uk/iNHa+V0/IlOb2YT4jHj5jZvDkc8GM0xy26J203uPm+gF0n5 5VaJVF+RQrDq1e+87OFHaWlsroQEmNw= X-Envelope-To: jdelvare@suse.com X-Envelope-To: linux@roeck-us.net X-Envelope-To: linux-iio@vger.kernel.org X-Envelope-To: linux-hwmon@vger.kernel.org X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: lars@metafoo.de X-Envelope-To: sean.anderson@linux.dev X-Envelope-To: jonathan.cameron@huawei.com X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Sean Anderson To: Jonathan Cameron , Jean Delvare , Guenter Roeck , linux-iio@vger.kernel.org, linux-hwmon@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lars-Peter Clausen , Sean Anderson , Jonathan Cameron Subject: [PATCH v2 1/2] iio: Add iio_read_channel_label to inkern API Date: Mon, 24 Jun 2024 13:46:00 -0400 Message-Id: <20240624174601.1527244-2-sean.anderson@linux.dev> In-Reply-To: <20240624174601.1527244-1-sean.anderson@linux.dev> References: <20240624174601.1527244-1-sean.anderson@linux.dev> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT It can be convenient for other in-kernel drivers to reuse IIO channel labels. Export the iio_read_channel_label function to allow this. The signature is different depending on where we are calling it from, so the meat is moved to do_iio_read_channel_label. Signed-off-by: Sean Anderson Acked-by: Jonathan Cameron --- (no changes since v1) drivers/iio/iio_core.h | 4 ++++ drivers/iio/industrialio-core.c | 23 ++++++++++++++--------- drivers/iio/inkern.c | 6 ++++++ include/linux/iio/consumer.h | 10 ++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h index 1a38b1915e7a..b7d5f4f0fada 100644 --- a/drivers/iio/iio_core.h +++ b/drivers/iio/iio_core.h @@ -34,6 +34,10 @@ void iio_device_ioctl_handler_register(struct iio_dev *indio_dev, struct iio_ioctl_handler *h); void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h); +ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev, + const struct iio_chan_spec *c, + char *buf); + int __iio_add_chan_devattr(const char *postfix, struct iio_chan_spec const *chan, ssize_t (*func)(struct device *dev, diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 2f185b386949..0f6cda7ffe45 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -727,22 +727,27 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) } EXPORT_SYMBOL_GPL(iio_format_value); -static ssize_t iio_read_channel_label(struct device *dev, - struct device_attribute *attr, - char *buf) +ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev, + const struct iio_chan_spec *c, + char *buf) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - if (indio_dev->info->read_label) - return indio_dev->info->read_label(indio_dev, this_attr->c, buf); + return indio_dev->info->read_label(indio_dev, c, buf); - if (this_attr->c->extend_name) - return sysfs_emit(buf, "%s\n", this_attr->c->extend_name); + if (c->extend_name) + return sysfs_emit(buf, "%s\n", c->extend_name); return -EINVAL; } +static ssize_t iio_read_channel_label(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return do_iio_read_channel_label(dev_to_iio_dev(dev), + to_iio_dev_attr(attr)->c, buf); +} + static ssize_t iio_read_channel_info(struct device *dev, struct device_attribute *attr, char *buf) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 39cf26d69d17..9f484c94bc6e 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -1010,3 +1010,9 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr, chan->channel, buf, len); } EXPORT_SYMBOL_GPL(iio_write_channel_ext_info); + +ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf) +{ + return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf); +} +EXPORT_SYMBOL_GPL(iio_read_channel_label); diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index e9910b41d48e..333d1d8ccb37 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -441,4 +441,14 @@ ssize_t iio_read_channel_ext_info(struct iio_channel *chan, ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr, const char *buf, size_t len); +/** + * iio_read_channel_label() - read label for a given channel + * @chan: The channel being queried. + * @buf: Where to store the attribute value. Assumed to hold + * at least PAGE_SIZE bytes. + * + * Returns the number of bytes written to buf, or an error code. + */ +ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf); + #endif From patchwork Mon Jun 24 17:46:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 13709933 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20D3519F47C; Mon, 24 Jun 2024 17:46:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719251191; cv=none; b=EpPwXUQzCbROArYttXMedrEi21Td8I9d5EzBq9nCMLD4qljVm5LYYyiTd/zn8K+/JkCJMS/+YeveULrfgKP+aHGAeOdlmb+gD2bYaXDEtN8BhEZi1DaJZD6CCcvvNVXHY0HOV/RII06FubkrWB7WLyGJm2bmctEEgJmwJ+HnbJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719251191; c=relaxed/simple; bh=p9OSa2J+J61U4skMpTA3cbTTUVpHDeNkIwWDyqHX8mM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Dv76SURzThESE5dzb+zPfwj3LCALKR9KIX04SqTArSeqP3PHYt4A2NGPkdHQi1DYm60YrWbPJNDWIwMW7h8vGGdUSEmCTEaVx4F11FrMcKSUS1IeE7EQSTcABJJcyynsTvypg2ASy84fcJ98ETX7ukwOO98NXbXedJ3JfbRl0+U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=hXfKQ5gY; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="hXfKQ5gY" X-Envelope-To: jic23@kernel.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1719251187; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XqOkN6IUDIXk9Ibe53TqX76Qt4EwVJgv68G4Bznvn6Q=; b=hXfKQ5gYYKr+KlXMgt1ZUXk7HwgZDDl/ULzron+ysYdbVrF2c8epe+auqRfapYCOs1QZ0h Lq1rXbK2NQHFeRoTPed5q3zKjYulOzrl3i4ubgW/QRxkMfrcKC7jREvBW+JtRRmgn8Gr3U C04dhHl074Fy9hJKIYsoF9vRmN6pinc= X-Envelope-To: jdelvare@suse.com X-Envelope-To: linux@roeck-us.net X-Envelope-To: linux-iio@vger.kernel.org X-Envelope-To: linux-hwmon@vger.kernel.org X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: lars@metafoo.de X-Envelope-To: sean.anderson@linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Sean Anderson To: Jonathan Cameron , Jean Delvare , Guenter Roeck , linux-iio@vger.kernel.org, linux-hwmon@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lars-Peter Clausen , Sean Anderson Subject: [PATCH v2 2/2] hwmon: iio: Add labels from IIO channels Date: Mon, 24 Jun 2024 13:46:01 -0400 Message-Id: <20240624174601.1527244-3-sean.anderson@linux.dev> In-Reply-To: <20240624174601.1527244-1-sean.anderson@linux.dev> References: <20240624174601.1527244-1-sean.anderson@linux.dev> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5". Although lm-sensors gracefully handles errors when reading channel labels, the ABI says the label attribute > Should only be created if the driver has hints about what this voltage > channel is being used for, and user-space doesn't. Therefore, we test to see if the channel has a label before creating the attribute. Signed-off-by: Sean Anderson Acked-by: Guenter Roeck --- Changes in v2: - Check if the label exists before creating the attribute drivers/hwmon/iio_hwmon.c | 45 ++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c index 4c8a80847891..5722cb9d81f9 100644 --- a/drivers/hwmon/iio_hwmon.c +++ b/drivers/hwmon/iio_hwmon.c @@ -33,6 +33,17 @@ struct iio_hwmon_state { struct attribute **attrs; }; +static ssize_t iio_hwmon_read_label(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); + struct iio_hwmon_state *state = dev_get_drvdata(dev); + struct iio_channel *chan = &state->channels[sattr->index]; + + return iio_read_channel_label(chan, buf); +} + /* * Assumes that IIO and hwmon operate in the same base units. * This is supposed to be true, but needs verification for @@ -68,12 +79,13 @@ static int iio_hwmon_probe(struct platform_device *pdev) struct device *dev = &pdev->dev; struct iio_hwmon_state *st; struct sensor_device_attribute *a; - int ret, i; + int ret, i, attr = 0; int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1; enum iio_chan_type type; struct iio_channel *channels; struct device *hwmon_dev; char *sname; + void *buf; channels = devm_iio_channel_get_all(dev); if (IS_ERR(channels)) { @@ -85,17 +97,18 @@ static int iio_hwmon_probe(struct platform_device *pdev) } st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); - if (st == NULL) + buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + if (!st || !buf) return -ENOMEM; st->channels = channels; - /* count how many attributes we have */ + /* count how many channels we have */ while (st->channels[st->num_channels].indio_dev) st->num_channels++; st->attrs = devm_kcalloc(dev, - st->num_channels + 1, sizeof(*st->attrs), + 2 * st->num_channels + 1, sizeof(*st->attrs), GFP_KERNEL); if (st->attrs == NULL) return -ENOMEM; @@ -147,9 +160,31 @@ static int iio_hwmon_probe(struct platform_device *pdev) a->dev_attr.show = iio_hwmon_read_val; a->dev_attr.attr.mode = 0444; a->index = i; - st->attrs[i] = &a->dev_attr.attr; + st->attrs[attr++] = &a->dev_attr.attr; + + /* Let's see if we have a label... */ + if (iio_read_channel_label(&st->channels[i], buf) < 0) + continue; + + a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL); + if (a == NULL) + return -ENOMEM; + + sysfs_attr_init(&a->dev_attr.attr); + a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, + "%s%d_label", + prefix, n); + if (!a->dev_attr.attr.name) + return -ENOMEM; + + a->dev_attr.show = iio_hwmon_read_label; + a->dev_attr.attr.mode = 0444; + a->index = i; + st->attrs[attr++] = &a->dev_attr.attr; } + devm_free_pages(dev, (unsigned long)buf); + st->attr_group.attrs = st->attrs; st->groups[0] = &st->attr_group;