linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device
@ 2017-07-12 21:41 Jaghathiswari Rankappagounder Natarajan
  2017-07-12 21:41 ` [PATCH linux v2 1/3] drivers: w1: add hwmon support structures Jaghathiswari Rankappagounder Natarajan
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Jaghathiswari Rankappagounder Natarajan @ 2017-07-12 21:41 UTC (permalink / raw)
  To: zbr, linux-kernel, linux, jdelvare, linux-hwmon
  Cc: Jaghathiswari Rankappagounder Natarajan

Our board has 4 DS18B20 1-wire temperature sensors. Each 1-wire bus and the
sensor under it is already configured against the Linux 1-wire driver
(called w1). They have a sysfs file(e.g.
/sys/bus/w1/devices/w1_bus_master1/28-000007704f4c/w1_slave) which, when read,
runs code to read the temperature. We'd like the temperatures to show up in
hwmon, so that the BMC IPMI sensor plumbing can forward those to host.

The changes in version 2 are mentioned in the individual patches.

This patchset is based on linux mainline version v4.10.

Tested:
Yes; On a board with 4 DS18B20 1-wire temperature sensors:
root@zaius:/sys/class/hwmon# ls
hwmon0  hwmon1  hwmon2  hwmon3  hwmon4  hwmon5
root@zaius:/sys/class/hwmon# cd hwmon0
root@zaius:/sys/class/hwmon/hwmon0# ls
device       name         subsystem    temp1_input  uevent
root@zaius:/sys/class/hwmon/hwmon0# cat temp1_input
24500
root@zaius:/sys/class/hwmon/hwmon0# cd ..
root@zaius:/sys/class/hwmon# cd hwmon1
root@zaius:/sys/class/hwmon/hwmon1# cat temp1_input
26562
root@zaius:/sys/class/hwmon/hwmon1# cd ..
root@zaius:/sys/class/hwmon# cd hwmon2
root@zaius:/sys/class/hwmon/hwmon2# cat temp1_input
27250
root@zaius:/sys/class/hwmon/hwmon2# cd ..
root@zaius:/sys/class/hwmon# cd hwmon3
root@zaius:/sys/class/hwmon/hwmon3# cat temp1_input
22250
root@zaius:/sys/class/hwmon/hwmon3#

Jaghathiswari Rankappagounder Natarajan (3):
  drivers: w1: add hwmon support structures
  drivers: w1: refactor w1_slave_show to make the temp reading
    functionality separate
  drivers: w1: add hwmon temp support for w1_therm

 drivers/w1/slaves/w1_therm.c | 197 ++++++++++++++++++++++++++++++++++++-------
 drivers/w1/w1.c              |  21 ++++-
 drivers/w1/w1.h              |   2 +
 drivers/w1/w1_family.h       |   2 +
 4 files changed, 191 insertions(+), 31 deletions(-)

--
2.13.2.932.g7449e964c-goog

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH linux v2 1/3] drivers: w1: add hwmon support structures
  2017-07-12 21:41 [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Jaghathiswari Rankappagounder Natarajan
@ 2017-07-12 21:41 ` Jaghathiswari Rankappagounder Natarajan
  2017-07-13 13:48   ` Guenter Roeck
  2017-07-12 21:41 ` [PATCH linux v2 2/3] drivers: w1: refactor w1_slave_show to make the temp reading functionality separate Jaghathiswari Rankappagounder Natarajan
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Jaghathiswari Rankappagounder Natarajan @ 2017-07-12 21:41 UTC (permalink / raw)
  To: zbr, linux-kernel, linux, jdelvare, linux-hwmon
  Cc: Jaghathiswari Rankappagounder Natarajan

This patch has changes to w1.h/w1.c/w1_family.h generic files to
add (optional) hwmon support structures.

Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
---
v2
- made changes to support hwmon_device_register_with_info mentioned by Guenter.

 drivers/w1/w1.c        | 21 ++++++++++++++++++++-
 drivers/w1/w1.h        |  2 ++
 drivers/w1/w1_family.h |  2 ++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index e213c678bbfe..c37e21597c5c 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -32,6 +32,7 @@
 #include <linux/sched.h>
 #include <linux/kthread.h>
 #include <linux/freezer.h>
+#include <linux/hwmon.h>

 #include <linux/atomic.h>

@@ -659,13 +660,31 @@ static int w1_family_notify(unsigned long action, struct w1_slave *sl)
 				return err;
 			}
 		}
-
+#ifdef CONFIG_HWMON
+		if (fops->chip_info) {
+			struct device *hwmon
+				= hwmon_device_register_with_info(&sl->dev,
+						"w1_slave_temp", sl,
+						fops->chip_info,
+						NULL);
+			if (IS_ERR(hwmon)) {
+				dev_warn(&sl->dev,
+					 "could not create hwmon device\n");
+			} else {
+				sl->hwmon = hwmon;
+			}
+		}
+#endif
 		break;
 	case BUS_NOTIFY_DEL_DEVICE:
 		if (fops->remove_slave)
 			sl->family->fops->remove_slave(sl);
 		if (fops->groups)
 			sysfs_remove_groups(&sl->dev.kobj, fops->groups);
+#ifdef CONFIG_HWMON
+		if (fops->chip_info && sl->hwmon)
+			hwmon_device_unregister(sl->hwmon);
+#endif
 		break;
 	}
 	return 0;
diff --git a/drivers/w1/w1.h b/drivers/w1/w1.h
index 129895f562b0..74ef1a543856 100644
--- a/drivers/w1/w1.h
+++ b/drivers/w1/w1.h
@@ -83,6 +83,7 @@ struct w1_reg_num
  * @family: module for device family type
  * @family_data: pointer for use by the family module
  * @dev: kernel device identifier
+ * @hwmon: pointer to hwmon device
  *
  */
 struct w1_slave
@@ -99,6 +100,7 @@ struct w1_slave
 	struct w1_family	*family;
 	void			*family_data;
 	struct device		dev;
+	struct device		*hwmon;
 };

 typedef void (*w1_slave_found_callback)(struct w1_master *, u64);
diff --git a/drivers/w1/w1_family.h b/drivers/w1/w1_family.h
index 10a7a0767187..9001851e1316 100644
--- a/drivers/w1/w1_family.h
+++ b/drivers/w1/w1_family.h
@@ -55,12 +55,14 @@ struct w1_slave;
  * @add_slave: add_slave
  * @remove_slave: remove_slave
  * @groups: sysfs group
+ * @chip_info: pointer to struct hwmon_chip_info
  */
 struct w1_family_ops
 {
 	int  (* add_slave)(struct w1_slave *);
 	void (* remove_slave)(struct w1_slave *);
 	const struct attribute_group **groups;
+	const struct hwmon_chip_info *chip_info;
 };

 /**
--
2.13.2.932.g7449e964c-goog

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH linux v2 2/3] drivers: w1: refactor w1_slave_show to make the temp reading functionality separate
  2017-07-12 21:41 [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Jaghathiswari Rankappagounder Natarajan
  2017-07-12 21:41 ` [PATCH linux v2 1/3] drivers: w1: add hwmon support structures Jaghathiswari Rankappagounder Natarajan
@ 2017-07-12 21:41 ` Jaghathiswari Rankappagounder Natarajan
  2017-07-12 21:41 ` [PATCH linux v2 3/3] drivers: w1: add hwmon temp support for w1_therm Jaghathiswari Rankappagounder Natarajan
  2017-07-13 15:36 ` [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Evgeniy Polyakov
  3 siblings, 0 replies; 10+ messages in thread
From: Jaghathiswari Rankappagounder Natarajan @ 2017-07-12 21:41 UTC (permalink / raw)
  To: zbr, linux-kernel, linux, jdelvare, linux-hwmon
  Cc: Jaghathiswari Rankappagounder Natarajan

Inside the w1_slave_show function refactor the code to read the temp
into a separate function.

Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
---
v2
- no changes

 drivers/w1/slaves/w1_therm.c | 82 +++++++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 31 deletions(-)

diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c
index 82611f197b0a..88f4cc8c3908 100644
--- a/drivers/w1/slaves/w1_therm.c
+++ b/drivers/w1/slaves/w1_therm.c
@@ -64,6 +64,12 @@ struct w1_therm_family_data {
 	atomic_t refcnt;
 };

+struct therm_info {
+	u8 rom[9];
+	u8 crc;
+	u8 verdict;
+};
+
 /* return the address of the refcnt in the family data */
 #define THERM_REFCNT(family_data) \
 	(&((struct w1_therm_family_data *)family_data)->refcnt)
@@ -427,33 +433,23 @@ static ssize_t w1_slave_store(struct device *device,
 	return ret ? : size;
 }

-static ssize_t w1_slave_show(struct device *device,
-	struct device_attribute *attr, char *buf)
+static ssize_t read_therm(struct device *device,
+			  struct w1_slave *sl, struct therm_info *info)
 {
-	struct w1_slave *sl = dev_to_w1_slave(device);
 	struct w1_master *dev = sl->master;
-	u8 rom[9], crc, verdict, external_power;
-	int i, ret, max_trying = 10;
-	ssize_t c = PAGE_SIZE;
-	u8 *family_data = sl->family_data;
+	u8 external_power;
+	int ret, max_trying = 10;

 	ret = mutex_lock_interruptible(&dev->bus_mutex);
 	if (ret != 0)
 		goto post_unlock;

-	if (!sl->family_data) {
-		ret = -ENODEV;
-		goto pre_unlock;
-	}
-
-	/* prevent the slave from going away in sleep */
-	atomic_inc(THERM_REFCNT(family_data));
-	memset(rom, 0, sizeof(rom));
+	memset(info->rom, 0, sizeof(info->rom));

 	while (max_trying--) {

-		verdict = 0;
-		crc = 0;
+		info->verdict = 0;
+		info->crc = 0;

 		if (!w1_reset_select_slave(sl)) {
 			int count = 0;
@@ -496,30 +492,57 @@ static ssize_t w1_slave_show(struct device *device,
 			if (!w1_reset_select_slave(sl)) {

 				w1_write_8(dev, W1_READ_SCRATCHPAD);
-				count = w1_read_block(dev, rom, 9);
+				count = w1_read_block(dev, info->rom, 9);
 				if (count != 9) {
 					dev_warn(device, "w1_read_block() "
 						"returned %u instead of 9.\n",
 						count);
 				}

-				crc = w1_calc_crc8(rom, 8);
+				info->crc = w1_calc_crc8(info->rom, 8);

-				if (rom[8] == crc)
-					verdict = 1;
+				if (info->rom[8] == info->crc)
+					info->verdict = 1;
 			}
 		}

-		if (verdict)
+		if (info->verdict)
 			break;
 	}

+pre_unlock:
+	mutex_unlock(&dev->bus_mutex);
+
+post_unlock:
+	return ret;
+}
+
+static ssize_t w1_slave_show(struct device *device,
+			     struct device_attribute *attr, char *buf)
+{
+	struct w1_slave *sl = dev_to_w1_slave(device);
+	u8 *family_data = sl->family_data;
+	struct therm_info info;
+	int ret;
+	ssize_t c = PAGE_SIZE;
+	int i;
+
+	if (!sl->family_data)
+		return -ENODEV;
+
+	/* prevent the slave from going away in sleep */
+	atomic_inc(THERM_REFCNT(family_data));
+
+	ret = read_therm(device, sl, &info);
+	if (ret)
+		goto dec_refcnt;
+
 	for (i = 0; i < 9; ++i)
-		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
+		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
 	c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
-		      crc, (verdict) ? "YES" : "NO");
-	if (verdict)
-		memcpy(family_data, rom, sizeof(rom));
+		      info.crc, (info.verdict) ? "YES" : "NO");
+	if (info.verdict)
+		memcpy(family_data, info.rom, sizeof(info.rom));
 	else
 		dev_warn(device, "Read failed CRC check\n");

@@ -528,13 +551,10 @@ static ssize_t w1_slave_show(struct device *device,
 			      ((u8 *)family_data)[i]);

 	c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
-		w1_convert_temp(rom, sl->family->fid));
+			w1_convert_temp(info.rom, sl->family->fid));
 	ret = PAGE_SIZE - c;

-pre_unlock:
-	mutex_unlock(&dev->bus_mutex);
-
-post_unlock:
+dec_refcnt:
 	atomic_dec(THERM_REFCNT(family_data));
 	return ret;
 }
--
2.13.2.932.g7449e964c-goog

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH linux v2 3/3] drivers: w1: add hwmon temp support for w1_therm
  2017-07-12 21:41 [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Jaghathiswari Rankappagounder Natarajan
  2017-07-12 21:41 ` [PATCH linux v2 1/3] drivers: w1: add hwmon support structures Jaghathiswari Rankappagounder Natarajan
  2017-07-12 21:41 ` [PATCH linux v2 2/3] drivers: w1: refactor w1_slave_show to make the temp reading functionality separate Jaghathiswari Rankappagounder Natarajan
@ 2017-07-12 21:41 ` Jaghathiswari Rankappagounder Natarajan
  2017-07-13 15:10   ` [linux,v2,3/3] " Guenter Roeck
  2017-07-13 15:36 ` [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Evgeniy Polyakov
  3 siblings, 1 reply; 10+ messages in thread
From: Jaghathiswari Rankappagounder Natarajan @ 2017-07-12 21:41 UTC (permalink / raw)
  To: zbr, linux-kernel, linux, jdelvare, linux-hwmon
  Cc: Jaghathiswari Rankappagounder Natarajan

This change adds hwmon temp support for w1_therm.

Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
---
v2
- make changes to support hwmon_device_register_with_info mentioned by Guenter.

 drivers/w1/slaves/w1_therm.c | 117 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)

diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c
index 88f4cc8c3908..147f9dfa13de 100644
--- a/drivers/w1/slaves/w1_therm.c
+++ b/drivers/w1/slaves/w1_therm.c
@@ -29,6 +29,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <linux/delay.h>
+#include <linux/hwmon.h>

 #include "../w1.h"
 #include "../w1_int.h"
@@ -118,19 +119,61 @@ static struct attribute *w1_ds28ea00_attrs[] = {
 	&dev_attr_w1_seq.attr,
 	NULL,
 };
+
 ATTRIBUTE_GROUPS(w1_therm);
 ATTRIBUTE_GROUPS(w1_ds28ea00);

+static umode_t w1_is_visible(const void *_data,
+			     enum hwmon_sensor_types type,
+			     u32 attr, int channel);
+
+static int w1_read_temp(struct device *dev, u32 attr, int channel,
+			long *val);
+
+static int w1_read(struct device *dev, enum hwmon_sensor_types type,
+		   u32 attr, int channel, long *val);
+
+static int w1_write(struct device *dev, enum hwmon_sensor_types type,
+		    u32 attr, int channel, long val);
+
+static const u32 w1_temp_config[] = {
+	HWMON_T_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info w1_temp = {
+	.type = hwmon_temp,
+	.config = w1_temp_config,
+};
+
+static const struct hwmon_channel_info *w1_info[] = {
+	&w1_temp,
+	NULL
+};
+
+static const struct hwmon_ops w1_hwmon_ops = {
+	.is_visible = w1_is_visible,
+	.read = w1_read,
+	.write = w1_write,
+};
+
+static const struct hwmon_chip_info w1_chip_info = {
+	.ops = &w1_hwmon_ops,
+	.info = w1_info,
+};
+
 static struct w1_family_ops w1_therm_fops = {
 	.add_slave	= w1_therm_add_slave,
 	.remove_slave	= w1_therm_remove_slave,
 	.groups		= w1_therm_groups,
+	.chip_info	= &w1_chip_info,
 };

 static struct w1_family_ops w1_ds28ea00_fops = {
 	.add_slave	= w1_therm_add_slave,
 	.remove_slave	= w1_therm_remove_slave,
 	.groups		= w1_ds28ea00_groups,
+	.chip_info	= &w1_chip_info,
 };

 static struct w1_family w1_therm_family_DS18S20 = {
@@ -559,6 +602,80 @@ static ssize_t w1_slave_show(struct device *device,
 	return ret;
 }

+static int w1_read_temp(struct device *device, u32 attr, int channel,
+			long *val)
+{
+	struct w1_slave *sl = dev_get_drvdata(device);
+	u8 *family_data = sl->family_data;
+	struct therm_info info;
+	int ret;
+
+	if (!sl->family_data)
+		return -ENODEV;
+
+	/* prevent the slave from going away in sleep */
+	atomic_inc(THERM_REFCNT(family_data));
+
+	switch (attr) {
+	case hwmon_temp_input:
+	ret = read_therm(device, sl, &info);
+	if (ret)
+		goto dec_refcnt;
+
+	if (!info.verdict) {
+		dev_warn(device, "Read failed CRC check\n");
+		ret = -EIO;
+		goto dec_refcnt;
+	}
+
+	*val = w1_convert_temp(info.rom, sl->family->fid);
+	ret = 0;
+	break;
+	default:
+		ret = -EOPNOTSUPP;
+		break;
+	}
+
+dec_refcnt:
+	atomic_dec(THERM_REFCNT(family_data));
+	return ret;
+}
+
+static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
+			     u32 attr, int channel)
+{
+	umode_t mode = 0444;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		break;
+	default:
+		mode = 0;
+		break;
+	}
+	return mode;
+}
+
+static int w1_read(struct device *dev, enum hwmon_sensor_types type,
+		   u32 attr, int channel, long *val)
+{
+	switch (type) {
+	case hwmon_temp:
+		return w1_read_temp(dev, attr, channel, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int w1_write(struct device *dev, enum hwmon_sensor_types type,
+		    u32 attr, int channel, long val)
+{
+	switch (type) {
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 #define W1_42_CHAIN	0x99
 #define W1_42_CHAIN_OFF	0x3C
 #define W1_42_CHAIN_OFF_INV	0xC3
--
2.13.2.932.g7449e964c-goog

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH linux v2 1/3] drivers: w1: add hwmon support structures
  2017-07-12 21:41 ` [PATCH linux v2 1/3] drivers: w1: add hwmon support structures Jaghathiswari Rankappagounder Natarajan
@ 2017-07-13 13:48   ` Guenter Roeck
  0 siblings, 0 replies; 10+ messages in thread
From: Guenter Roeck @ 2017-07-13 13:48 UTC (permalink / raw)
  To: Jaghathiswari Rankappagounder Natarajan, zbr, linux-kernel,
	jdelvare, linux-hwmon

On 07/12/2017 02:41 PM, Jaghathiswari Rankappagounder Natarajan wrote:
> This patch has changes to w1.h/w1.c/w1_family.h generic files to
> add (optional) hwmon support structures.
> 
> Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
> Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
> ---
> v2
> - made changes to support hwmon_device_register_with_info mentioned by Guenter.
> 
>   drivers/w1/w1.c        | 21 ++++++++++++++++++++-
>   drivers/w1/w1.h        |  2 ++
>   drivers/w1/w1_family.h |  2 ++
>   3 files changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
> index e213c678bbfe..c37e21597c5c 100644
> --- a/drivers/w1/w1.c
> +++ b/drivers/w1/w1.c
> @@ -32,6 +32,7 @@
>   #include <linux/sched.h>
>   #include <linux/kthread.h>
>   #include <linux/freezer.h>
> +#include <linux/hwmon.h>
> 
>   #include <linux/atomic.h>
> 
> @@ -659,13 +660,31 @@ static int w1_family_notify(unsigned long action, struct w1_slave *sl)
>   				return err;
>   			}
>   		}
> -
> +#ifdef CONFIG_HWMON

HWMON can be built as module. If so, this won't be enabled, even if W1 is also built as module.
Better would be

#if IS_REACHABLE(CONFIG_HWMON)

I personally would probably use

	if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {

to avoid the ifdef and to ensure that the code always compiles. But that is a personal preference.

> +		if (fops->chip_info) {
> +			struct device *hwmon
> +				= hwmon_device_register_with_info(&sl->dev,
> +						"w1_slave_temp", sl,
> +						fops->chip_info,
> +						NULL);
> +			if (IS_ERR(hwmon)) {
> +				dev_warn(&sl->dev,
> +					 "could not create hwmon device\n");
> +			} else {
> +				sl->hwmon = hwmon;
> +			}
> +		}
> +#endif
>   		break;
>   	case BUS_NOTIFY_DEL_DEVICE:
>   		if (fops->remove_slave)
>   			sl->family->fops->remove_slave(sl);
>   		if (fops->groups)
>   			sysfs_remove_groups(&sl->dev.kobj, fops->groups);
> +#ifdef CONFIG_HWMON
> +		if (fops->chip_info && sl->hwmon)
> +			hwmon_device_unregister(sl->hwmon);
> +#endif

Not completely understanding the code, I may be wrong, but wouldn't it be
better to remove the sysfs group and the hwmon device before calling
remove_slave() ?

>   		break;
>   	}
>   	return 0;
> diff --git a/drivers/w1/w1.h b/drivers/w1/w1.h
> index 129895f562b0..74ef1a543856 100644
> --- a/drivers/w1/w1.h
> +++ b/drivers/w1/w1.h
> @@ -83,6 +83,7 @@ struct w1_reg_num
>    * @family: module for device family type
>    * @family_data: pointer for use by the family module
>    * @dev: kernel device identifier
> + * @hwmon: pointer to hwmon device
>    *
>    */
>   struct w1_slave
> @@ -99,6 +100,7 @@ struct w1_slave
>   	struct w1_family	*family;
>   	void			*family_data;
>   	struct device		dev;
> +	struct device		*hwmon;
>   };
> 
>   typedef void (*w1_slave_found_callback)(struct w1_master *, u64);
> diff --git a/drivers/w1/w1_family.h b/drivers/w1/w1_family.h
> index 10a7a0767187..9001851e1316 100644
> --- a/drivers/w1/w1_family.h
> +++ b/drivers/w1/w1_family.h
> @@ -55,12 +55,14 @@ struct w1_slave;
>    * @add_slave: add_slave
>    * @remove_slave: remove_slave
>    * @groups: sysfs group
> + * @chip_info: pointer to struct hwmon_chip_info
>    */
>   struct w1_family_ops
>   {
>   	int  (* add_slave)(struct w1_slave *);
>   	void (* remove_slave)(struct w1_slave *);
>   	const struct attribute_group **groups;
> +	const struct hwmon_chip_info *chip_info;
>   };
> 
>   /**
> --
> 2.13.2.932.g7449e964c-goog
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [linux,v2,3/3] drivers: w1: add hwmon temp support for w1_therm
  2017-07-12 21:41 ` [PATCH linux v2 3/3] drivers: w1: add hwmon temp support for w1_therm Jaghathiswari Rankappagounder Natarajan
@ 2017-07-13 15:10   ` Guenter Roeck
  0 siblings, 0 replies; 10+ messages in thread
From: Guenter Roeck @ 2017-07-13 15:10 UTC (permalink / raw)
  To: Jaghathiswari Rankappagounder Natarajan
  Cc: zbr, linux-kernel, jdelvare, linux-hwmon

On Wed, Jul 12, 2017 at 02:41:18PM -0700, Jaghathiswari Rankappagounder Natarajan wrote:
> This change adds hwmon temp support for w1_therm.
> 
> Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
> Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
> ---
> v2
> - make changes to support hwmon_device_register_with_info mentioned by Guenter.
> 
>  drivers/w1/slaves/w1_therm.c | 117 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 117 insertions(+)
> 
> --
> 2.13.2.932.g7449e964c-goog
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c
> index 88f4cc8c3908..147f9dfa13de 100644
> --- a/drivers/w1/slaves/w1_therm.c
> +++ b/drivers/w1/slaves/w1_therm.c
> @@ -29,6 +29,7 @@
>  #include <linux/types.h>
>  #include <linux/slab.h>
>  #include <linux/delay.h>
> +#include <linux/hwmon.h>
> 
>  #include "../w1.h"
>  #include "../w1_int.h"
> @@ -118,19 +119,61 @@ static struct attribute *w1_ds28ea00_attrs[] = {
>  	&dev_attr_w1_seq.attr,
>  	NULL,
>  };
> +
>  ATTRIBUTE_GROUPS(w1_therm);
>  ATTRIBUTE_GROUPS(w1_ds28ea00);
> 

Do you want the following code all the time, or enclose it in
IS_REACHABLE() ?

> +static umode_t w1_is_visible(const void *_data,
> +			     enum hwmon_sensor_types type,
> +			     u32 attr, int channel);
> +
> +static int w1_read_temp(struct device *dev, u32 attr, int channel,
> +			long *val);
> +
> +static int w1_read(struct device *dev, enum hwmon_sensor_types type,
> +		   u32 attr, int channel, long *val);
> +
> +static int w1_write(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long val);
> +

Any chance you can rearrange the code to not require forward declarations ?

> +static const u32 w1_temp_config[] = {
> +	HWMON_T_INPUT,
> +	0
> +};
> +
> +static const struct hwmon_channel_info w1_temp = {
> +	.type = hwmon_temp,
> +	.config = w1_temp_config,
> +};
> +
> +static const struct hwmon_channel_info *w1_info[] = {
> +	&w1_temp,
> +	NULL
> +};
> +
> +static const struct hwmon_ops w1_hwmon_ops = {
> +	.is_visible = w1_is_visible,
> +	.read = w1_read,
> +	.write = w1_write,
> +};
> +
> +static const struct hwmon_chip_info w1_chip_info = {
> +	.ops = &w1_hwmon_ops,
> +	.info = w1_info,
> +};
> +
>  static struct w1_family_ops w1_therm_fops = {
>  	.add_slave	= w1_therm_add_slave,
>  	.remove_slave	= w1_therm_remove_slave,
>  	.groups		= w1_therm_groups,
> +	.chip_info	= &w1_chip_info,
>  };
> 
>  static struct w1_family_ops w1_ds28ea00_fops = {
>  	.add_slave	= w1_therm_add_slave,
>  	.remove_slave	= w1_therm_remove_slave,
>  	.groups		= w1_ds28ea00_groups,
> +	.chip_info	= &w1_chip_info,
>  };
> 
>  static struct w1_family w1_therm_family_DS18S20 = {
> @@ -559,6 +602,80 @@ static ssize_t w1_slave_show(struct device *device,
>  	return ret;
>  }
> 
> +static int w1_read_temp(struct device *device, u32 attr, int channel,
> +			long *val)
> +{
> +	struct w1_slave *sl = dev_get_drvdata(device);
> +	u8 *family_data = sl->family_data;
> +	struct therm_info info;
> +	int ret;
> +
> +	if (!sl->family_data)

Why not use family_data here ?

> +		return -ENODEV;
> +
> +	/* prevent the slave from going away in sleep */
> +	atomic_inc(THERM_REFCNT(family_data));

The original code increments the reference count with the bus mutex locked.
Similar, the original code only checks if family_data is NULL after locking
the bus mutex.

Are you sure this change in sequence doesn't cause race conditions ?
If this function can be called with family_data == NULL, what guarantees
that it isn't set to NULL after being checked but before the reference
count is increased (or, in other words, that the atomic_inc() doesn't
increment a reference counter in freed memory) ?

> +
> +	switch (attr) {
> +	case hwmon_temp_input:

indentation below is off.

> +	ret = read_therm(device, sl, &info);
> +	if (ret)
> +		goto dec_refcnt;
> +
> +	if (!info.verdict) {
> +		dev_warn(device, "Read failed CRC check\n");

I personally dislike such noise. Personal preference, of course.

> +		ret = -EIO;
> +		goto dec_refcnt;
> +	}
> +
> +	*val = w1_convert_temp(info.rom, sl->family->fid);
> +	ret = 0;
> +	break;
> +	default:
> +		ret = -EOPNOTSUPP;
> +		break;
> +	}
> +
> +dec_refcnt:
> +	atomic_dec(THERM_REFCNT(family_data));
> +	return ret;
> +}
> +
> +static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
> +			     u32 attr, int channel)
> +{
> +	umode_t mode = 0444;
> +
> +	switch (attr) {
> +	case hwmon_temp_input:
> +		break;
> +	default:
> +		mode = 0;
> +		break;
> +	}
> +	return mode;

	return attr == hwmon_temp_input ? 0444 : 0;

would be a bit simpler here.

> +}
> +
> +static int w1_read(struct device *dev, enum hwmon_sensor_types type,
> +		   u32 attr, int channel, long *val)
> +{
> +	switch (type) {
> +	case hwmon_temp:
> +		return w1_read_temp(dev, attr, channel, val);
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int w1_write(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long val)

You don't need a write function, since attributes are never writeable.

> +{
> +	switch (type) {
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
>  #define W1_42_CHAIN	0x99
>  #define W1_42_CHAIN_OFF	0x3C
>  #define W1_42_CHAIN_OFF_INV	0xC3

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device
  2017-07-12 21:41 [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Jaghathiswari Rankappagounder Natarajan
                   ` (2 preceding siblings ...)
  2017-07-12 21:41 ` [PATCH linux v2 3/3] drivers: w1: add hwmon temp support for w1_therm Jaghathiswari Rankappagounder Natarajan
@ 2017-07-13 15:36 ` Evgeniy Polyakov
  2017-07-13 15:39   ` Evgeniy Polyakov
  3 siblings, 1 reply; 10+ messages in thread
From: Evgeniy Polyakov @ 2017-07-13 15:36 UTC (permalink / raw)
  To: Jaghathiswari Rankappagounder Natarajan, linux-kernel, linux,
	jdelvare, linux-hwmon
  Cc: Greg Kroah-Hartman

Hi

13.07.2017, 00:41, "Jaghathiswari Rankappagounder Natarajan" <jaghu@google.com>:
> Our board has 4 DS18B20 1-wire temperature sensors. Each 1-wire bus and the
> sensor under it is already configured against the Linux 1-wire driver
> (called w1). They have a sysfs file(e.g.
> /sys/bus/w1/devices/w1_bus_master1/28-000007704f4c/w1_slave) which, when read,
> runs code to read the temperature. We'd like the temperatures to show up in
> hwmon, so that the BMC IPMI sensor plumbing can forward those to host.
>
> The changes in version 2 are mentioned in the individual patches.
>
> This patchset is based on linux mainline version v4.10.

I believe this is a resend of your previous patchet, isn't it?
Greg, if you hadn't yet, please pull it into the tree.

Thank you.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device
  2017-07-13 15:36 ` [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Evgeniy Polyakov
@ 2017-07-13 15:39   ` Evgeniy Polyakov
  2017-07-17 16:06     ` Jaghathiswari Rankappagounder Natarajan
  0 siblings, 1 reply; 10+ messages in thread
From: Evgeniy Polyakov @ 2017-07-13 15:39 UTC (permalink / raw)
  To: Jaghathiswari Rankappagounder Natarajan, linux-kernel, linux,
	jdelvare, linux-hwmon
  Cc: Greg Kroah-Hartman

Hi

13.07.2017, 18:36, "Evgeniy Polyakov" <zbr@ioremap.net>:
> I believe this is a resend of your previous patchet, isn't it?
> Greg, if you hadn't yet, please pull it into the tree.

Aaand it looks like we have a discussion now, so please postpone it for a while :)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device
  2017-07-13 15:39   ` Evgeniy Polyakov
@ 2017-07-17 16:06     ` Jaghathiswari Rankappagounder Natarajan
  2017-07-17 16:22       ` Greg Kroah-Hartman
  0 siblings, 1 reply; 10+ messages in thread
From: Jaghathiswari Rankappagounder Natarajan @ 2017-07-17 16:06 UTC (permalink / raw)
  To: Evgeniy Polyakov
  Cc: linux-kernel, linux, jdelvare, linux-hwmon, Greg Kroah-Hartman

Hi Evgeniy/Greg,

Please pull in version 4 patchset into the tree.

On Thu, Jul 13, 2017 at 8:39 AM, Evgeniy Polyakov <zbr@ioremap.net> wrote:
> Hi
>
> 13.07.2017, 18:36, "Evgeniy Polyakov" <zbr@ioremap.net>:
>> I believe this is a resend of your previous patchet, isn't it?
>> Greg, if you hadn't yet, please pull it into the tree.
>
> Aaand it looks like we have a discussion now, so please postpone it for a while :)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device
  2017-07-17 16:06     ` Jaghathiswari Rankappagounder Natarajan
@ 2017-07-17 16:22       ` Greg Kroah-Hartman
  0 siblings, 0 replies; 10+ messages in thread
From: Greg Kroah-Hartman @ 2017-07-17 16:22 UTC (permalink / raw)
  To: Jaghathiswari Rankappagounder Natarajan
  Cc: Evgeniy Polyakov, linux-kernel, linux, jdelvare, linux-hwmon

On Mon, Jul 17, 2017 at 09:06:00AM -0700, Jaghathiswari Rankappagounder Natarajan wrote:
> Hi Evgeniy/Greg,
> 
> Please pull in version 4 patchset into the tree.

I have no idea what that means :(

Please resend the correct ones so I know what to do here.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-07-17 16:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-12 21:41 [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Jaghathiswari Rankappagounder Natarajan
2017-07-12 21:41 ` [PATCH linux v2 1/3] drivers: w1: add hwmon support structures Jaghathiswari Rankappagounder Natarajan
2017-07-13 13:48   ` Guenter Roeck
2017-07-12 21:41 ` [PATCH linux v2 2/3] drivers: w1: refactor w1_slave_show to make the temp reading functionality separate Jaghathiswari Rankappagounder Natarajan
2017-07-12 21:41 ` [PATCH linux v2 3/3] drivers: w1: add hwmon temp support for w1_therm Jaghathiswari Rankappagounder Natarajan
2017-07-13 15:10   ` [linux,v2,3/3] " Guenter Roeck
2017-07-13 15:36 ` [PATCH linux v2 0/3] Export 1-wire thermal sensors as hwmon device Evgeniy Polyakov
2017-07-13 15:39   ` Evgeniy Polyakov
2017-07-17 16:06     ` Jaghathiswari Rankappagounder Natarajan
2017-07-17 16:22       ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).