From patchwork Fri Jul 15 09:59:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Schulz X-Patchwork-Id: 9231483 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4101860865 for ; Fri, 15 Jul 2016 10:01:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3205828319 for ; Fri, 15 Jul 2016 10:01:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2480928324; Fri, 15 Jul 2016 10:01:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BBC092819A for ; Fri, 15 Jul 2016 10:01:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932447AbcGOKBg (ORCPT ); Fri, 15 Jul 2016 06:01:36 -0400 Received: from down.free-electrons.com ([37.187.137.238]:43845 "EHLO mail.free-electrons.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932435AbcGOKB3 (ORCPT ); Fri, 15 Jul 2016 06:01:29 -0400 Received: by mail.free-electrons.com (Postfix, from userid 110) id 5793A3E8; Fri, 15 Jul 2016 12:01:23 +0200 (CEST) Received: from localhost.localdomain (lan31-h06-87-91-62-133.dsl.sta.abo.bbox.fr [87.91.62.133]) by mail.free-electrons.com (Postfix) with ESMTPSA id D8F4D3F2; Fri, 15 Jul 2016 12:01:09 +0200 (CEST) From: Quentin Schulz To: jdelvare@suse.com, linux@roeck-us.net, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, maxime.ripard@free-electrons.com, wens@csie.org, lee.jones@linaro.org Cc: Quentin Schulz , linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, thomas.petazzoni@free-electrons.com, antoine.tenart@free-electrons.com Subject: [PATCH v2 4/4] hwmon: iio: add label for channels read by iio_hwmon Date: Fri, 15 Jul 2016 11:59:14 +0200 Message-Id: <1468576754-3273-5-git-send-email-quentin.schulz@free-electrons.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1468576754-3273-1-git-send-email-quentin.schulz@free-electrons.com> References: <1468576754-3273-1-git-send-email-quentin.schulz@free-electrons.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, iio_hwmon only exposes values of the IIO channels it can read but no label by channel is exposed. This adds exposition of sysfs files containing label for IIO channels it can read based on extended_name field of the iio_chan_spec of the channel. If the extended_name field is empty, the sysfs file is not created by iio_hwmon. Signed-off-by: Quentin Schulz --- patch added in v2 drivers/hwmon/iio_hwmon.c | 77 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c index c0da4d9..28d15b2 100644 --- a/drivers/hwmon/iio_hwmon.c +++ b/drivers/hwmon/iio_hwmon.c @@ -16,6 +16,7 @@ #include #include #include +#include #include /** @@ -57,12 +58,26 @@ static ssize_t iio_hwmon_read_val(struct device *dev, return sprintf(buf, "%d\n", result); } +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); + const char *label = state->channels[sattr->index].channel->extend_name; + + if (label) + return sprintf(buf, "%s\n", label); + + return 0; +} + 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; + struct sensor_device_attribute *a, *b; + int ret, i, j = 0; int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1; enum iio_chan_type type; struct iio_channel *channels; @@ -92,7 +107,8 @@ static int iio_hwmon_probe(struct platform_device *pdev) st->num_channels++; st->attrs = devm_kzalloc(dev, - sizeof(*st->attrs) * (st->num_channels + 1), + sizeof(*st->attrs) * + (2 * st->num_channels + 1), GFP_KERNEL); if (st->attrs == NULL) { ret = -ENOMEM; @@ -107,6 +123,18 @@ static int iio_hwmon_probe(struct platform_device *pdev) } sysfs_attr_init(&a->dev_attr.attr); + + b = NULL; + if (st->channels[i].channel->extend_name) { + b = devm_kzalloc(dev, sizeof(*b), GFP_KERNEL); + if (b == NULL) { + ret = -ENOMEM; + goto error_release_channels; + } + + sysfs_attr_init(&b->dev_attr.attr); + } + ret = iio_get_channel_type(&st->channels[i], &type); if (ret < 0) goto error_release_channels; @@ -115,35 +143,66 @@ static int iio_hwmon_probe(struct platform_device *pdev) case IIO_VOLTAGE: a->dev_attr.attr.name = kasprintf(GFP_KERNEL, "in%d_input", - in_i++); + in_i); + if (b) + b->dev_attr.attr.name = kasprintf(GFP_KERNEL, + "in%d_label", + in_i); + in_i++; break; case IIO_TEMP: a->dev_attr.attr.name = kasprintf(GFP_KERNEL, "temp%d_input", - temp_i++); + temp_i); + + if (b) + b->dev_attr.attr.name = kasprintf(GFP_KERNEL, + "temp%d_label", + temp_i); + temp_i++; break; case IIO_CURRENT: a->dev_attr.attr.name = kasprintf(GFP_KERNEL, "curr%d_input", - curr_i++); + curr_i); + + if (b) + b->dev_attr.attr.name = kasprintf(GFP_KERNEL, + "curr%d_label", + curr_i); + curr_i++; break; case IIO_HUMIDITYRELATIVE: a->dev_attr.attr.name = kasprintf(GFP_KERNEL, "humidity%d_input", - humidity_i++); + humidity_i); + + if (b) + b->dev_attr.attr.name = kasprintf(GFP_KERNEL, + "humidity%d_label", + humidity_i); + humidity_i++; break; default: ret = -EINVAL; goto error_release_channels; } - if (a->dev_attr.attr.name == NULL) { + if (a->dev_attr.attr.name == NULL || + (b && b->dev_attr.attr.name == NULL)) { ret = -ENOMEM; goto error_release_channels; } a->dev_attr.show = iio_hwmon_read_val; a->dev_attr.attr.mode = S_IRUGO; a->index = i; - st->attrs[i] = &a->dev_attr.attr; + st->attrs[j++] = &a->dev_attr.attr; + + if (b) { + b->dev_attr.show = iio_hwmon_read_label; + b->dev_attr.attr.mode = S_IRUGO; + b->index = i; + st->attrs[j++] = &b->dev_attr.attr; + } } st->attr_group.attrs = st->attrs;