Skip to contentSkip to conversation

rtc: isl12022: cleanups and hwmon support

PATCH Superseded Rasmus Villemoes 3 participants 35 messages Aug 30, 2022 – Nov 14, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rtc: isl12022: cleanups and hwmon support

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

and finally hooks up the temperatur sensor to hwmon.

Rasmus Villemoes (6):
rtc: isl12022: stop using deprecated devm_rtc_device_register()
rtc: isl12022: simplify some expressions
rtc: isl12022: use dev_set_drvdata() instead of i2c_set_clientdata()
rtc: isl12022: drop redundant write to HR register
rtc: isl12022: switch to using regmap API
rtc: isl12022: add support for temperature sensor

 drivers/rtc/Kconfig        |   1 +
 drivers/rtc/rtc-isl12022.c | 195 ++++++++++++++++++++-----------------
 2 files changed, 107 insertions(+), 89 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/6 rtc: isl12022: stop using deprecated devm_rtc_device_register()

The comments say that devm_rtc_device_register() is deprecated and that one should instead use devm_rtc_allocate_device() and [devm_]rtc_register_device. So do that.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/rtc/rtc-isl12022.c +7 -4
index 79461ded1a48..2dc19061cf5f 100644
@@ -246,10 +246,13 @@ static int isl12022_probe(struct i2c_client *client)
 
 	i2c_set_clientdata(client, isl12022);
 
-	isl12022->rtc = devm_rtc_device_register(&client->dev,
-					isl12022_driver.driver.name,
-					&isl12022_rtc_ops, THIS_MODULE);
-	return PTR_ERR_OR_ZERO(isl12022->rtc);
+	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
+	if (IS_ERR(isl12022->rtc))
+		return PTR_ERR(isl12022->rtc);
+
+	isl12022->rtc->ops = &isl12022_rtc_ops;
+
+	return devm_rtc_register_device(isl12022->rtc);
 }
 
 #ifdef CONFIG_OF
Signature · 1 line
2.37.2
#2 lore raw

PATCH 2/6 rtc: isl12022: simplify some expressions

In reply to Rasmus Villemoes

These instances of '&client->dev' might as well be spelled 'dev', since 'client' has been computed from 'dev' via 'client = to_i2c_client(dev)'.

Later patches will get rid of that local variable 'client', so remove these unnecessary references so those later patches become easier to read.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/rtc/rtc-isl12022.c +5 -6
index 2dc19061cf5f..5e6bb9153c89 100644
@@ -112,13 +112,13 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 		return ret;
 
 	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(&client->dev,
+		dev_warn(dev,
 			 "voltage dropped below %u%%, "
 			 "date and time is not reliable.\n",
 			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
 	}
 
-	dev_dbg(&client->dev,
+	dev_dbg(dev,
 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
 		"mday=%02x, mon=%02x, year=%02x, wday=%02x, "
 		"sr=%02x, int=%02x",
@@ -141,7 +141,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
 	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
@@ -158,7 +158,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
@@ -173,8 +173,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 		/* Check if WRTC (write rtc enable) is set factory default is
 		 * 0 (not set) */
 		if (!(buf[0] & ISL12022_INT_WRTC)) {
-			dev_info(&client->dev,
-				 "init write enable and 24 hour format\n");
+			dev_info(dev, "init write enable and 24 hour format\n");
 
 			/* Set the write enable bit. */
 			ret = isl12022_write_reg(client,
Signature · 1 line
2.37.2
#3 lore raw

Re: [PATCH 2/6] rtc: isl12022: simplify some expressions

Hi,

Quoted 18 lines · Rasmus Villemoes

These instances of '&client->dev' might as well be spelled 'dev', since 'client' has been computed from 'dev' via 'client = to_i2c_client(dev)'.

Later patches will get rid of that local variable 'client', so remove these unnecessary references so those later patches become easier to read.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 2dc19061cf5f..5e6bb9153c89 100644

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm) · line 115

Quoted 5 lines
drivers/rtc/rtc-isl12022.c +1 -1
@@ -112,13 +112,13 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 		return ret;
 
 	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(&client->dev,
+		dev_warn(dev,

While at it, I would prefer that one to also become a dev_dbg

Quoted 11 lines
drivers/rtc/rtc-isl12022.c +1 -1
 			 "voltage dropped below %u%%, "
 			 "date and time is not reliable.\n",
 			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
 	}
 
-	dev_dbg(&client->dev,
+	dev_dbg(dev,
 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
 		"mday=%02x, mon=%02x, year=%02x, wday=%02x, "
 		"sr=%02x, int=%02x",

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm) · line 144

Quoted 5 lines
drivers/rtc/rtc-isl12022.c +1 -1
@@ -141,7 +141,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
 	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "

You probably want to convert that to %ptR

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 161

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +1 -1
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
@@ -158,7 +158,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "

ditto

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 176

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +1 -2
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
@@ -173,8 +173,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 		/* Check if WRTC (write rtc enable) is set factory default is
 		 * 0 (not set) */
 		if (!(buf[0] & ISL12022_INT_WRTC)) {
-			dev_info(&client->dev,
-				 "init write enable and 24 hour format\n");
+			dev_info(dev, "init write enable and 24 hour format\n");

I guess that one should simply be removed

Quoted 6 lines
drivers/rtc/rtc-isl12022.c +0 -0
 			/* Set the write enable bit. */
 			ret = isl12022_write_reg(client,
Signature · 1 line
2.37.2
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [PATCH 2/6] rtc: isl12022: simplify some expressions

Quoted 23 lines · Alexandre Belloni

Hi,

Quoted 20 lines · Rasmus Villemoes

These instances of '&client->dev' might as well be spelled 'dev', since 'client' has been computed from 'dev' via 'client = to_i2c_client(dev)'.

Later patches will get rid of that local variable 'client', so remove these unnecessary references so those later patches become easier to read.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 2dc19061cf5f..5e6bb9153c89 100644
@@ -112,13 +112,13 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 		return ret;
 
Quoted 5 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm) · line 115

Quoted 3 lines
drivers/rtc/rtc-isl12022.c +1 -1
 	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(&client->dev,
+		dev_warn(dev,

While at it, I would prefer that one to also become a dev_dbg

Well, I prefer to keep it, because my customer actually wants to use this information. Grepping it out from dmesg is of course not the best interface, but if it gets demoted to a dev_dbg() it doesn't even get there.

I'll address your other comments and respin. Thanks.

Rasmus

#5 lore raw

Re: [PATCH 2/6] rtc: isl12022: simplify some expressions

Quoted 30 lines · Rasmus Villemoes
Quoted 23 lines · Alexandre Belloni

Hi,

Quoted 20 lines · Rasmus Villemoes

These instances of '&client->dev' might as well be spelled 'dev', since 'client' has been computed from 'dev' via 'client = to_i2c_client(dev)'.

Later patches will get rid of that local variable 'client', so remove these unnecessary references so those later patches become easier to read.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 2dc19061cf5f..5e6bb9153c89 100644
@@ -112,13 +112,13 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 		return ret;
 
Quoted 5 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm) · line 115

Quoted 3 lines
drivers/rtc/rtc-isl12022.c +1 -1
 	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(&client->dev,
+		dev_warn(dev,

While at it, I would prefer that one to also become a dev_dbg

Quoted 4 lines

Well, I prefer to keep it, because my customer actually wants to use this information. Grepping it out from dmesg is of course not the best interface, but if it gets demoted to a dev_dbg() it doesn't even get there.

There is a proper userspace interface, look for RTC_VL_READ.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

PATCH 3/6 rtc: isl12022: use dev_set_drvdata() instead of i2c_set_clientdata()

In reply to Rasmus Villemoes

As another preparation for removing direct references to the i2c_client in the helper functions, stash a pointer to the private data via dev_set_drvdata() instead of i2c_set_clientdata().

---
 drivers/rtc/rtc-isl12022.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/rtc/rtc-isl12022.c +2 -3
index 5e6bb9153c89..234201ea2f7d 100644
@@ -153,7 +153,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
 	struct i2c_client *client = to_i2c_client(dev);
-	struct isl12022 *isl12022 = i2c_get_clientdata(client);
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
 	size_t i;
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
@@ -242,8 +242,7 @@ static int isl12022_probe(struct i2c_client *client)
 				GFP_KERNEL);
 	if (!isl12022)
 		return -ENOMEM;
-
-	i2c_set_clientdata(client, isl12022);
+	dev_set_drvdata(&client->dev, isl12022);
 
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
Signature · 1 line
2.37.2
#7 lore raw

PATCH 4/6 rtc: isl12022: drop redundant write to HR register

In reply to Rasmus Villemoes

There's nothing in the data sheet that says writing to one of the time keeping registers is necessary to start the RTC. It does so at the stop condition of the i2c transfer setting the WRTC bit:

Upon initialization or power-up, the WRTC must be set to "1" to enable the RTC. Upon the completion of a valid write (STOP), the RTC starts counting.

Moreover, even if such a write to one of the timekeeping registers was necessary, that's exactly what we do anyway just below when we actually write the given struct rtc_time to the device.

---
 drivers/rtc/rtc-isl12022.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)
drivers/rtc/rtc-isl12022.c +1 -15
index 234201ea2f7d..659f8e7f33f0 100644
@@ -173,7 +173,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 		/* Check if WRTC (write rtc enable) is set factory default is
 		 * 0 (not set) */
 		if (!(buf[0] & ISL12022_INT_WRTC)) {
-			dev_info(dev, "init write enable and 24 hour format\n");
+			dev_info(dev, "init write enable\n");
 
 			/* Set the write enable bit. */
 			ret = isl12022_write_reg(client,
@@ -181,20 +181,6 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 						 buf[0] | ISL12022_INT_WRTC);
 			if (ret)
 				return ret;
-
-			/* Write to any RTC register to start RTC, we use the
-			 * HR register, setting the MIL bit to use the 24 hour
-			 * format. */
-			ret = isl12022_read_regs(client, ISL12022_REG_HR,
-						 buf, 1);
-			if (ret)
-				return ret;
-
-			ret = isl12022_write_reg(client,
-						 ISL12022_REG_HR,
-						 buf[0] | ISL12022_HR_MIL);
-			if (ret)
-				return ret;
 		}
 
 		isl12022->write_enabled = true;
Signature · 1 line
2.37.2
#8 lore raw

PATCH 5/6 rtc: isl12022: switch to using regmap API

In reply to Rasmus Villemoes

The regmap abstraction allows us to avoid the private i2c transfer helpers, and also offers some nice utility functions such as the regmap_update_bits family.

---
 drivers/rtc/Kconfig        |   1 +
 drivers/rtc/rtc-isl12022.c | 101 +++++++++----------------------------
 2 files changed, 26 insertions(+), 76 deletions(-)
drivers/rtc/Kconfig +1 -0
index b8de25118ad0..bb63edb507da 100644
@@ -423,6 +423,7 @@ config RTC_DRV_ISL1208
 
 config RTC_DRV_ISL12022
 	tristate "Intersil ISL12022"
+	select REGMAP_I2C
 	help
 	  If you say yes here you get support for the
 	  Intersil ISL12022 RTC chip.
drivers/rtc/rtc-isl12022.c +25 -76

163 lines not on this page · raw message

Signature · 1 line
2.37.2
#9 lore raw

PATCH 6/6 rtc: isl12022: add support for temperature sensor

In reply to Rasmus Villemoes
---
 drivers/rtc/rtc-isl12022.c | 81 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
drivers/rtc/rtc-isl12022.c +81 -0
index b295ec92ee17..1bd72f436318 100644
@@ -17,6 +17,8 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 
 /* ISL register offsets */
 #define ISL12022_REG_SC		0x00
@@ -30,6 +32,9 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
 /* ISL register bits */
 #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +43,7 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
 static struct i2c_driver isl12022_driver;
 
@@ -48,6 +54,79 @@ struct isl12022 {
 	bool write_enabled;	/* true if write enable is set */
 };
 
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, s32 *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	s32 temp;
+	int ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static ssize_t
+isl12022_hwmon_show_temp(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	int ret;
+	s32 temp;
+
+	ret = isl12022_hwmon_read_temp(dev, &temp);
+	if (ret)
+		return ret;
+
+	return sprintf(buf, "%d\n", temp);
+}
+static SENSOR_DEVICE_ATTR(temp1_input, 0444, isl12022_hwmon_show_temp,
+			  NULL, 0);
+
+static struct attribute *isl12022_hwmon_attrs[] = {
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(isl12022_hwmon);
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_groups(dev, "isl12022", isl12022,
+						       isl12022_hwmon_groups);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
 /*
  * In the routines that deal directly with the isl12022 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -179,6 +258,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->regmap);
 	}
 
+	isl12022_hwmon_register(&client->dev);
+
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
 		return PTR_ERR(isl12022->rtc);
Signature · 1 line
2.37.2
maintainer
#10 lore raw

Re: [PATCH 6/6] rtc: isl12022: add support for temperature sensor

Quoted 99 lines · Rasmus Villemoes
---
  drivers/rtc/rtc-isl12022.c | 81 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 81 insertions(+)
drivers/rtc/rtc-isl12022.c +65 -0
index b295ec92ee17..1bd72f436318 100644
@@ -17,6 +17,8 @@
  #include <linux/of.h>
  #include <linux/of_device.h>
  #include <linux/regmap.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 
  /* ISL register offsets */
  #define ISL12022_REG_SC		0x00
@@ -30,6 +32,9 @@
  #define ISL12022_REG_SR		0x07
  #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
  /* ISL register bits */
  #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +43,7 @@
 
  #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
  static struct i2c_driver isl12022_driver;
 
@@ -48,6 +54,79 @@ struct isl12022 {
  	bool write_enabled;	/* true if write enable is set */
  };
 
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, s32 *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	s32 temp;
+	int ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static ssize_t
+isl12022_hwmon_show_temp(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	int ret;
+	s32 temp;
+
+	ret = isl12022_hwmon_read_temp(dev, &temp);
+	if (ret)
+		return ret;
+
+	return sprintf(buf, "%d\n", temp);
+}
+static SENSOR_DEVICE_ATTR(temp1_input, 0444, isl12022_hwmon_show_temp,
+			  NULL, 0);
+
+static struct attribute *isl12022_hwmon_attrs[] = {
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(isl12022_hwmon);
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);

drivers/rtc/rtc-isl12022.c struct isl12022 { · line 125

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_groups(dev, "isl12022", isl12022,
+						       isl12022_hwmon_groups);

Please use devm_hwmon_device_register_with_info().

Thanks,
Guenter

Quoted 16 lines
drivers/rtc/rtc-isl12022.c +6 -0
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
  /*
   * In the routines that deal directly with the isl12022 hardware, we use
   * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -179,6 +258,8 @@ static int isl12022_probe(struct i2c_client *client)
  		return PTR_ERR(isl12022->regmap);
  	}
 
+	isl12022_hwmon_register(&client->dev);
+
  	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
  	if (IS_ERR(isl12022->rtc))
  		return PTR_ERR(isl12022->rtc);
#11 lore raw

Re: [PATCH 6/6] rtc: isl12022: add support for temperature sensor

In reply to Rasmus Villemoes
Quoted 10 lines · Rasmus Villemoes
---
 drivers/rtc/rtc-isl12022.c | 81 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
drivers/rtc/rtc-isl12022.c +0 -0
index b295ec92ee17..1bd72f436318 100644

drivers/rtc/rtc-isl12022.c line 21

Quoted 5 lines
drivers/rtc/rtc-isl12022.c +2 -0
@@ -17,6 +17,8 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>

You should keep that list ordered

Quoted 113 lines
drivers/rtc/rtc-isl12022.c +79 -0
 /* ISL register offsets */
 #define ISL12022_REG_SC		0x00
@@ -30,6 +32,9 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
 /* ISL register bits */
 #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +43,7 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
 static struct i2c_driver isl12022_driver;
 
@@ -48,6 +54,79 @@ struct isl12022 {
 	bool write_enabled;	/* true if write enable is set */
 };
 
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, s32 *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	s32 temp;
+	int ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static ssize_t
+isl12022_hwmon_show_temp(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	int ret;
+	s32 temp;
+
+	ret = isl12022_hwmon_read_temp(dev, &temp);
+	if (ret)
+		return ret;
+
+	return sprintf(buf, "%d\n", temp);
+}
+static SENSOR_DEVICE_ATTR(temp1_input, 0444, isl12022_hwmon_show_temp,
+			  NULL, 0);
+
+static struct attribute *isl12022_hwmon_attrs[] = {
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(isl12022_hwmon);
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_groups(dev, "isl12022", isl12022,
+						       isl12022_hwmon_groups);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
 /*
  * In the routines that deal directly with the isl12022 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -179,6 +258,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->regmap);
 	}
 
+	isl12022_hwmon_register(&client->dev);
+
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
 		return PTR_ERR(isl12022->rtc);
Signature · 1 line
2.37.2
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#12 lore raw

Re: [PATCH 6/6] rtc: isl12022: add support for temperature sensor

Quoted 6 lines · Alexandre Belloni
Quoted 5 lines · Rasmus Villemoes
---
 drivers/rtc/rtc-isl12022.c | 81 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
Quoted 12 lines

drivers/rtc/rtc-isl12022.c line 21

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +2 -0
index b295ec92ee17..1bd72f436318 100644
@@ -17,6 +17,8 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>

You should keep that list ordered

While the three lines of context happen to be sorted, the list as a whole is not. I can of course include a patch sorting it.

Rasmus

#13 lore raw

Re: [PATCH 6/6] rtc: isl12022: add support for temperature sensor

Quoted 20 lines · Rasmus Villemoes
Quoted 6 lines · Alexandre Belloni
Quoted 5 lines · Rasmus Villemoes
---
 drivers/rtc/rtc-isl12022.c | 81 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
Quoted 12 lines

drivers/rtc/rtc-isl12022.c line 21

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +2 -0
index b295ec92ee17..1bd72f436318 100644
@@ -17,6 +17,8 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>

You should keep that list ordered

Quoted 3 lines

While the three lines of context happen to be sorted, the list as a whole is not. I can of course include a patch sorting it.

Nevermind then

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#14 lore raw
In reply to Rasmus Villemoes
Quoted 7 lines · Rasmus Villemoes

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

and finally hooks up the temperatur sensor to hwmon.

ping. Any comments before I respin 6/6 to address Guenter's request?

Rasmus

#15 lore raw
Quoted 11 lines · Rasmus Villemoes
Quoted 7 lines · Rasmus Villemoes

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

and finally hooks up the temperatur sensor to hwmon.

ping. Any comments before I respin 6/6 to address Guenter's request?

This series is missing one patch setting the datetime range supported by the RTC which I guess is 2000 to 2099.

Quoted 1 line

Rasmus

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#16 lore raw

v2 cover rtc: isl12022: cleanups and hwmon support

In reply to Rasmus Villemoes

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

It also provides range_min, range_max values and finally hooks up the temperatur sensor to hwmon.

Changes in v2:

- Use devm_hwmon_device_register_with_info() (Guenter), and use the opportunity to also expose an update_interval with the constant value 60000.

- Convert some dev_dbgs to use %ptR, remove a dev_info() (Alexandre)

- Add patch setting range_min, range_max (Alexandre)

Not changed in v2: For now, I've kept the low-battery dev_warn(). Implementing support for the ioctls RTC_VL_READ/RTC_VL_CLR is on my radar, but I will need some more time for reading up on and deciding on the details of what the chip provides and how to map that information to the various defined RTC_VL_* bits. I also need to figure out a good way to reliably trigger a low-battery condition so I can test the ioctl support. So I hope these patches can go in for now and not be blocked on the series expanding even further.

Rasmus Villemoes (9):
rtc: isl12022: stop using deprecated devm_rtc_device_register()
rtc: isl12022: specify range_min and range_max
rtc: isl12022: drop a dev_info()
rtc: isl12022: simplify some expressions
rtc: isl12022: use %ptR
rtc: isl12022: use dev_set_drvdata() instead of i2c_set_clientdata()
rtc: isl12022: drop redundant write to HR register
rtc: isl12022: switch to using regmap API
rtc: isl12022: add support for temperature sensor

 drivers/rtc/Kconfig        |   1 +
 drivers/rtc/rtc-isl12022.c | 237 ++++++++++++++++++++-----------------
 2 files changed, 131 insertions(+), 107 deletions(-)
Signature · 1 line
2.37.2
#17 lore raw

PATCH v2 1/9 rtc: isl12022: stop using deprecated devm_rtc_device_register()

The comments say that devm_rtc_device_register() is deprecated and that one should instead use devm_rtc_allocate_device() and [devm_]rtc_register_device. So do that.

---
 drivers/rtc/rtc-isl12022.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/rtc/rtc-isl12022.c +7 -4
index 79461ded1a48..2dc19061cf5f 100644
@@ -246,10 +246,13 @@ static int isl12022_probe(struct i2c_client *client)
 
 	i2c_set_clientdata(client, isl12022);
 
-	isl12022->rtc = devm_rtc_device_register(&client->dev,
-					isl12022_driver.driver.name,
-					&isl12022_rtc_ops, THIS_MODULE);
-	return PTR_ERR_OR_ZERO(isl12022->rtc);
+	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
+	if (IS_ERR(isl12022->rtc))
+		return PTR_ERR(isl12022->rtc);
+
+	isl12022->rtc->ops = &isl12022_rtc_ops;
+
+	return devm_rtc_register_device(isl12022->rtc);
 }
 
 #ifdef CONFIG_OF
Signature · 1 line
2.37.2
#18 lore raw

PATCH v2 2/9 rtc: isl12022: specify range_min and range_max

In reply to Rasmus Villemoes

The isl12022 can (only) keep track of times in the range 2000-2099. The data sheet says

The calendar registers track date, month, year, and day of the week and are accurate through 2099, with automatic leap year correction.

The lower bound of 2000 is obtained by simply observing that its YR register only counts from 00 through 99.

---
 drivers/rtc/rtc-isl12022.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/rtc/rtc-isl12022.c +2 -0
index 2dc19061cf5f..3bc197f5548f 100644
@@ -251,6 +251,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->rtc);
 
 	isl12022->rtc->ops = &isl12022_rtc_ops;
+	isl12022->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
+	isl12022->rtc->range_max = RTC_TIMESTAMP_END_2099;
 
 	return devm_rtc_register_device(isl12022->rtc);
 }
Signature · 1 line
2.37.2
#19 lore raw

PATCH v2 3/9 rtc: isl12022: drop a dev_info()

In reply to Rasmus Villemoes

This dev_info() seems to be a debug leftover, and it would only get printed once (or, once per battery change).

---
 drivers/rtc/rtc-isl12022.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -3
index 3bc197f5548f..2fc9fbefc6fc 100644
@@ -173,9 +173,6 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 		/* Check if WRTC (write rtc enable) is set factory default is
 		 * 0 (not set) */
 		if (!(buf[0] & ISL12022_INT_WRTC)) {
-			dev_info(&client->dev,
-				 "init write enable and 24 hour format\n");
-
 			/* Set the write enable bit. */
 			ret = isl12022_write_reg(client,
 						 ISL12022_REG_INT,
Signature · 1 line
2.37.2
#20 lore raw

PATCH v2 4/9 rtc: isl12022: simplify some expressions

In reply to Rasmus Villemoes

These instances of '&client->dev' might as well be spelled 'dev', since 'client' has been computed from 'dev' via 'client = to_i2c_client(dev)'.

Later patches will get rid of that local variable 'client', so remove these unnecessary references so those later patches become easier to read.

---
 drivers/rtc/rtc-isl12022.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/rtc/rtc-isl12022.c +4 -4
index 2fc9fbefc6fc..7efe23fa74df 100644
@@ -112,13 +112,13 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 		return ret;
 
 	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(&client->dev,
+		dev_warn(dev,
 			 "voltage dropped below %u%%, "
 			 "date and time is not reliable.\n",
 			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
 	}
 
-	dev_dbg(&client->dev,
+	dev_dbg(dev,
 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
 		"mday=%02x, mon=%02x, year=%02x, wday=%02x, "
 		"sr=%02x, int=%02x",
@@ -141,7 +141,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
 	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
@@ -158,7 +158,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
 
-	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
+	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
 		__func__,
 		tm->tm_sec, tm->tm_min, tm->tm_hour,
Signature · 1 line
2.37.2
#21 lore raw

PATCH v2 5/9 rtc: isl12022: use %ptR

In reply to Rasmus Villemoes

Simplify the code and make the output format consistent with other RTC drivers by standardizing on using the %ptR printf extension.

---
 drivers/rtc/rtc-isl12022.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/rtc/rtc-isl12022.c +2 -10
index 7efe23fa74df..d396d6076db5 100644
@@ -141,11 +141,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
 	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
 
-	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
-		"mday=%d, mon=%d, year=%d, wday=%d\n",
-		__func__,
-		tm->tm_sec, tm->tm_min, tm->tm_hour,
-		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
+	dev_dbg(dev, "%s: %ptR\n", __func__, tm);
 
 	return 0;
 }
@@ -158,11 +154,7 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
 
-	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
-		"mday=%d, mon=%d, year=%d, wday=%d\n",
-		__func__,
-		tm->tm_sec, tm->tm_min, tm->tm_hour,
-		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
+	dev_dbg(dev, "%s: %ptR\n", __func__, tm);
 
 	if (!isl12022->write_enabled) {
 
Signature · 1 line
2.37.2
#22 lore raw

PATCH v2 6/9 rtc: isl12022: use dev_set_drvdata() instead of i2c_set_clientdata()

In reply to Rasmus Villemoes

As another preparation for removing direct references to the i2c_client in the helper functions, stash a pointer to the private data via dev_set_drvdata() instead of i2c_set_clientdata().

---
 drivers/rtc/rtc-isl12022.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/rtc/rtc-isl12022.c +2 -3
index d396d6076db5..df6d91f4e8f3 100644
@@ -149,7 +149,7 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
 	struct i2c_client *client = to_i2c_client(dev);
-	struct isl12022 *isl12022 = i2c_get_clientdata(client);
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
 	size_t i;
 	int ret;
 	uint8_t buf[ISL12022_REG_DW + 1];
@@ -232,8 +232,7 @@ static int isl12022_probe(struct i2c_client *client)
 				GFP_KERNEL);
 	if (!isl12022)
 		return -ENOMEM;
-
-	i2c_set_clientdata(client, isl12022);
+	dev_set_drvdata(&client->dev, isl12022);
 
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
Signature · 1 line
2.37.2
#23 lore raw

PATCH v2 7/9 rtc: isl12022: drop redundant write to HR register

In reply to Rasmus Villemoes

There's nothing in the data sheet that says writing to one of the time keeping registers is necessary to start the RTC. It does so at the stop condition of the i2c transfer setting the WRTC bit:

Upon initialization or power-up, the WRTC must be set to "1" to enable the RTC. Upon the completion of a valid write (STOP), the RTC starts counting.

Moreover, even if such a write to one of the timekeeping registers was necessary, that's exactly what we do anyway just below when we actually write the given struct rtc_time to the device.

---
 drivers/rtc/rtc-isl12022.c | 14 --------------
 1 file changed, 14 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -14
index df6d91f4e8f3..6fb13a5d17f1 100644
@@ -171,20 +171,6 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 						 buf[0] | ISL12022_INT_WRTC);
 			if (ret)
 				return ret;
-
-			/* Write to any RTC register to start RTC, we use the
-			 * HR register, setting the MIL bit to use the 24 hour
-			 * format. */
-			ret = isl12022_read_regs(client, ISL12022_REG_HR,
-						 buf, 1);
-			if (ret)
-				return ret;
-
-			ret = isl12022_write_reg(client,
-						 ISL12022_REG_HR,
-						 buf[0] | ISL12022_HR_MIL);
-			if (ret)
-				return ret;
 		}
 
 		isl12022->write_enabled = true;
Signature · 1 line
2.37.2
#24 lore raw

PATCH v2 8/9 rtc: isl12022: switch to using regmap API

In reply to Rasmus Villemoes

The regmap abstraction allows us to avoid the private i2c transfer helpers, and also offers some nice utility functions such as the regmap_update_bits family.

While at it, simplify the code even more by not keeping track of ->write_enabled: rtc_set_time is not a hot path, so one extra i2c read doesn't hurt (regmap_update_bits elides the write when the bits are already as desired).

---
 drivers/rtc/Kconfig        |   1 +
 drivers/rtc/rtc-isl12022.c | 110 +++++++++----------------------------
 2 files changed, 26 insertions(+), 85 deletions(-)
drivers/rtc/Kconfig +1 -0
index b8de25118ad0..bb63edb507da 100644
@@ -423,6 +423,7 @@ config RTC_DRV_ISL1208
 
 config RTC_DRV_ISL12022
 	tristate "Intersil ISL12022"
+	select REGMAP_I2C
 	help
 	  If you say yes here you get support for the
 	  Intersil ISL12022 RTC chip.
drivers/rtc/rtc-isl12022.c +25 -85

163 lines not on this page · raw message

Signature · 1 line
2.37.2
#25 lore raw

PATCH v2 9/9 rtc: isl12022: add support for temperature sensor

In reply to Rasmus Villemoes
---
 drivers/rtc/rtc-isl12022.c | 104 +++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
drivers/rtc/rtc-isl12022.c +104 -0
index ca677c4265e6..f3efe61c81e5 100644
@@ -17,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
 
 /* ISL register offsets */
 #define ISL12022_REG_SC		0x00
@@ -30,6 +31,9 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
 /* ISL register bits */
 #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +42,7 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
 static struct i2c_driver isl12022_driver;
 
@@ -46,6 +51,103 @@ struct isl12022 {
 	struct regmap *regmap;
 };
 
+static umode_t isl12022_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
+		return 0444;
+
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return 0444;
+
+	return 0;
+}
+
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, long *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	int temp, ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static int isl12022_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type,
+			       u32 attr, int channel, long *val)
+{
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+		*val = 60000;
+		return 0;
+	}
+
+	if (type == hwmon_temp && attr == hwmon_temp_input) {
+		return isl12022_hwmon_read_temp(dev, val);
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info *isl12022_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops isl12022_hwmon_ops = {
+	.is_visible = isl12022_hwmon_is_visible,
+	.read = isl12022_hwmon_read,
+};
+
+static const struct hwmon_chip_info isl12022_hwmon_chip_info = {
+	.ops = &isl12022_hwmon_ops,
+	.info = isl12022_hwmon_info,
+};
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "isl12022", isl12022,
+						     &isl12022_hwmon_chip_info,
+						     NULL);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
 /*
  * In the routines that deal directly with the isl12022 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -160,6 +262,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->regmap);
 	}
 
+	isl12022_hwmon_register(&client->dev);
+
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
 		return PTR_ERR(isl12022->rtc);
Signature · 1 line
2.37.2
maintainer
#26 lore raw

Re: [PATCH v2 9/9] rtc: isl12022: add support for temperature sensor

Quoted 77 lines · Rasmus Villemoes
---
  drivers/rtc/rtc-isl12022.c | 104 +++++++++++++++++++++++++++++++++++++
  1 file changed, 104 insertions(+)
drivers/rtc/rtc-isl12022.c +43 -0
index ca677c4265e6..f3efe61c81e5 100644
@@ -17,6 +17,7 @@
  #include <linux/of.h>
  #include <linux/of_device.h>
  #include <linux/regmap.h>
+#include <linux/hwmon.h>
 
  /* ISL register offsets */
  #define ISL12022_REG_SC		0x00
@@ -30,6 +31,9 @@
  #define ISL12022_REG_SR		0x07
  #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
  /* ISL register bits */
  #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +42,7 @@
 
  #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
  static struct i2c_driver isl12022_driver;
 
@@ -46,6 +51,103 @@ struct isl12022 {
  	struct regmap *regmap;
  };
 
+static umode_t isl12022_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
+		return 0444;
+
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return 0444;
+
+	return 0;
+}
+
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, long *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	int temp, ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;

drivers/rtc/rtc-isl12022.c struct isl12022 { · line 101

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+}
+
+static int isl12022_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type,
+			       u32 attr, int channel, long *val)
+{
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+		*val = 60000;
+		return 0;
+	}

It is not the purpose of the update_interval attribute to inform the user what the update interval of this chip happens to be. The purpose of the attribute is to inform the chip what update interval it should use.

From the documentation:
Some devices have a variable update rate or interval. This attribute can be used to change it to the desired value.

The patch is not in the hwmon subsystem, to it is up to the maintainers to decide if they want to accept the patch anyway. Please don't expect a reviewed tag, though.

drivers/rtc/rtc-isl12022.c struct isl12022 { · line 105

Quoted 4 lines
drivers/rtc/rtc-isl12022.c +4 -0
+
+	if (type == hwmon_temp && attr == hwmon_temp_input) {
+		return isl12022_hwmon_read_temp(dev, val);
+	}

Nit: { } is unnecessary.

Guenter

Quoted 57 lines
drivers/rtc/rtc-isl12022.c +47 -0
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info *isl12022_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops isl12022_hwmon_ops = {
+	.is_visible = isl12022_hwmon_is_visible,
+	.read = isl12022_hwmon_read,
+};
+
+static const struct hwmon_chip_info isl12022_hwmon_chip_info = {
+	.ops = &isl12022_hwmon_ops,
+	.info = isl12022_hwmon_info,
+};
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "isl12022", isl12022,
+						     &isl12022_hwmon_chip_info,
+						     NULL);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
  /*
   * In the routines that deal directly with the isl12022 hardware, we use
   * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -160,6 +262,8 @@ static int isl12022_probe(struct i2c_client *client)
  		return PTR_ERR(isl12022->regmap);
  	}
 
+	isl12022_hwmon_register(&client->dev);
+
  	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
  	if (IS_ERR(isl12022->rtc))
  		return PTR_ERR(isl12022->rtc);
#27 lore raw

Re: [PATCH v2 9/9] rtc: isl12022: add support for temperature sensor

Quoted 14 lines · Guenter Roeck

drivers/rtc/rtc-isl12022.c struct isl12022 { · line 101

Quoted 8 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +8 -0
+static int isl12022_hwmon_read(struct device *dev,
+                   enum hwmon_sensor_types type,
+                   u32 attr, int channel, long *val)
+{
+    if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+        *val = 60000;
+        return 0;
+    }

It is not the purpose of the update_interval attribute to inform the user what the update interval of this chip happens to be. The purpose of the attribute is to inform the chip what update interval it should use.

Well, I think it's a completely natural thing to expose a fixed and known update_interval as a 0444 property, and it might even be useful to userspace to know that there's no point reading the sensor any more often than that. And I didn't come up with this by myself, there's already at least a couple of instances of a 0444 update_interval.

I'll leave it to the RTC maintainers to decide, it's easy enough to remove.

Rasmus

maintainer
#28 lore raw

Re: [PATCH v2 9/9] rtc: isl12022: add support for temperature sensor

Quoted 16 lines · Rasmus Villemoes
Quoted 14 lines · Guenter Roeck

drivers/rtc/rtc-isl12022.c struct isl12022 { · line 101

Quoted 8 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +8 -0
+static int isl12022_hwmon_read(struct device *dev,
+                   enum hwmon_sensor_types type,
+                   u32 attr, int channel, long *val)
+{
+    if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+        *val = 60000;
+        return 0;
+    }

It is not the purpose of the update_interval attribute to inform the user what the update interval of this chip happens to be. The purpose of the attribute is to inform the chip what update interval it should use.

Quoted 6 lines

Well, I think it's a completely natural thing to expose a fixed and known update_interval as a 0444 property, and it might even be useful to userspace to know that there's no point reading the sensor any more often than that. And I didn't come up with this by myself, there's already at least a couple of instances of a 0444 update_interval.

That doesn't make it better. It is still an abuse of the ABI.

Guenter

#29 lore raw

PATCH v3 9/9 rtc: isl12022: add support for temperature sensor

In reply to Rasmus Villemoes

---
v3: drop 0444 update_interval property.

v2 of patches 1-8 are already upstream (b1a1baa657c7 and parents).

 drivers/rtc/rtc-isl12022.c | 94 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
drivers/rtc/rtc-isl12022.c +94 -0
index ca677c4265e6..a3b0de3393f5 100644
@@ -17,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
 
 /* ISL register offsets */
 #define ISL12022_REG_SC		0x00
@@ -30,6 +31,9 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
 /* ISL register bits */
 #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +42,7 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
 static struct i2c_driver isl12022_driver;
 
@@ -46,6 +51,93 @@ struct isl12022 {
 	struct regmap *regmap;
 };
 
+static umode_t isl12022_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return 0444;
+
+	return 0;
+}
+
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, long *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	int temp, ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static int isl12022_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type,
+			       u32 attr, int channel, long *val)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return isl12022_hwmon_read_temp(dev, val);
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info *isl12022_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops isl12022_hwmon_ops = {
+	.is_visible = isl12022_hwmon_is_visible,
+	.read = isl12022_hwmon_read,
+};
+
+static const struct hwmon_chip_info isl12022_hwmon_chip_info = {
+	.ops = &isl12022_hwmon_ops,
+	.info = isl12022_hwmon_info,
+};
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "isl12022", isl12022,
+						     &isl12022_hwmon_chip_info,
+						     NULL);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
 /*
  * In the routines that deal directly with the isl12022 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -160,6 +252,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->regmap);
 	}
 
+	isl12022_hwmon_register(&client->dev);
+
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
 		return PTR_ERR(isl12022->rtc);
Signature · 1 line
2.37.2
maintainer Reviewed-by on v3 9/9
#30 lore raw

Re: [PATCH v3 9/9] rtc: isl12022: add support for temperature sensor

Quoted 1 line · Rasmus Villemoes

There should be some description above. Other than that,

Quoted 141 lines

---
v3: drop 0444 update_interval property.

v2 of patches 1-8 are already upstream (b1a1baa657c7 and parents).

  drivers/rtc/rtc-isl12022.c | 94 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 94 insertions(+)
drivers/rtc/rtc-isl12022.c +94 -0
index ca677c4265e6..a3b0de3393f5 100644
@@ -17,6 +17,7 @@
  #include <linux/of.h>
  #include <linux/of_device.h>
  #include <linux/regmap.h>
+#include <linux/hwmon.h>
 
  /* ISL register offsets */
  #define ISL12022_REG_SC		0x00
@@ -30,6 +31,9 @@
  #define ISL12022_REG_SR		0x07
  #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
  /* ISL register bits */
  #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +42,7 @@
 
  #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
  static struct i2c_driver isl12022_driver;
 
@@ -46,6 +51,93 @@ struct isl12022 {
  	struct regmap *regmap;
  };
 
+static umode_t isl12022_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return 0444;
+
+	return 0;
+}
+
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, long *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	int temp, ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static int isl12022_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type,
+			       u32 attr, int channel, long *val)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return isl12022_hwmon_read_temp(dev, val);
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info *isl12022_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops isl12022_hwmon_ops = {
+	.is_visible = isl12022_hwmon_is_visible,
+	.read = isl12022_hwmon_read,
+};
+
+static const struct hwmon_chip_info isl12022_hwmon_chip_info = {
+	.ops = &isl12022_hwmon_ops,
+	.info = isl12022_hwmon_info,
+};
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "isl12022", isl12022,
+						     &isl12022_hwmon_chip_info,
+						     NULL);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
  /*
   * In the routines that deal directly with the isl12022 hardware, we use
   * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -160,6 +252,8 @@ static int isl12022_probe(struct i2c_client *client)
  		return PTR_ERR(isl12022->regmap);
  	}
 
+	isl12022_hwmon_register(&client->dev);
+
  	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
  	if (IS_ERR(isl12022->rtc))
  		return PTR_ERR(isl12022->rtc);
#31 lore raw

PATCH v4 1/1 rtc: isl12022: add support for temperature sensor

In reply to Rasmus Villemoes

The isl12022 has built-in temperature compensation effective over the range -40C to +85C. It exposes the average of the last two temperature measurements as a 10-bit value in half-Kelvins. Make this available via the hwmon framework.

---
v4: add a real commit log, include Guenter's R-b.

v3 at https://lore.kernel.org/lkml/[email protected]/

 drivers/rtc/rtc-isl12022.c | 94 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
drivers/rtc/rtc-isl12022.c +94 -0
index ca677c4265e6..a3b0de3393f5 100644
@@ -17,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/hwmon.h>
 
 /* ISL register offsets */
 #define ISL12022_REG_SC		0x00
@@ -30,6 +31,9 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_BETA	0x0d
+#define ISL12022_REG_TEMP_L	0x28
+
 /* ISL register bits */
 #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
 
@@ -38,6 +42,7 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_BETA_TSE	(1 << 7)
 
 static struct i2c_driver isl12022_driver;
 
@@ -46,6 +51,93 @@ struct isl12022 {
 	struct regmap *regmap;
 };
 
+static umode_t isl12022_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return 0444;
+
+	return 0;
+}
+
+/*
+ * A user-initiated temperature conversion is not started by this function,
+ * so the temperature is updated once every ~60 seconds.
+ */
+static int isl12022_hwmon_read_temp(struct device *dev, long *mC)
+{
+	struct isl12022 *isl12022 = dev_get_drvdata(dev);
+	struct regmap *regmap = isl12022->regmap;
+	u8 temp_buf[2];
+	int temp, ret;
+
+	ret = regmap_bulk_read(regmap, ISL12022_REG_TEMP_L,
+			       temp_buf, sizeof(temp_buf));
+	if (ret)
+		return ret;
+	/*
+	 * Temperature is represented as a 10-bit number, unit half-Kelvins.
+	 */
+	temp = (temp_buf[1] << 8) | temp_buf[0];
+	temp *= 500;
+	temp -= 273000;
+
+	*mC = temp;
+
+	return 0;
+}
+
+static int isl12022_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type,
+			       u32 attr, int channel, long *val)
+{
+	if (type == hwmon_temp && attr == hwmon_temp_input)
+		return isl12022_hwmon_read_temp(dev, val);
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info *isl12022_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops isl12022_hwmon_ops = {
+	.is_visible = isl12022_hwmon_is_visible,
+	.read = isl12022_hwmon_read,
+};
+
+static const struct hwmon_chip_info isl12022_hwmon_chip_info = {
+	.ops = &isl12022_hwmon_ops,
+	.info = isl12022_hwmon_info,
+};
+
+static void isl12022_hwmon_register(struct device *dev)
+{
+	struct isl12022 *isl12022;
+	struct device *hwmon;
+	int ret;
+
+	if (!IS_REACHABLE(CONFIG_HWMON))
+		return;
+
+	isl12022 = dev_get_drvdata(dev);
+
+	ret = regmap_update_bits(isl12022->regmap, ISL12022_REG_BETA,
+				 ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to enable temperature sensor\n");
+		return;
+	}
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "isl12022", isl12022,
+						     &isl12022_hwmon_chip_info,
+						     NULL);
+	if (IS_ERR(hwmon))
+		dev_warn(dev, "unable to register hwmon device: %pe\n", hwmon);
+}
+
 /*
  * In the routines that deal directly with the isl12022 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -160,6 +252,8 @@ static int isl12022_probe(struct i2c_client *client)
 		return PTR_ERR(isl12022->regmap);
 	}
 
+	isl12022_hwmon_register(&client->dev);
+
 	isl12022->rtc = devm_rtc_allocate_device(&client->dev);
 	if (IS_ERR(isl12022->rtc))
 		return PTR_ERR(isl12022->rtc);
Signature · 1 line
2.37.2
maintainer Applied
#32 lore raw

Re: [PATCH v4] rtc: isl12022: add support for temperature sensor

Quoted 6 lines · Rasmus Villemoes

The isl12022 has built-in temperature compensation effective over the range -40C to +85C. It exposes the average of the last two temperature measurements as a 10-bit value in half-Kelvins. Make this available via the hwmon framework.

Applied, thanks!

[1/1] rtc: isl12022: add support for temperature sensor

      commit: 1087656c079ddd3bb240a3c1f108c37b9b8f1457

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#33 lore raw
In reply to Rasmus Villemoes
Quoted 9 lines · Rasmus Villemoes

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

It also provides range_min, range_max values and finally hooks up the temperatur sensor to hwmon.

Ping. Alexandre, anything I need to do to move this forward?

Thanks,
Rasmus

maintainer Applied
#34 lore raw
In reply to Rasmus Villemoes
Quoted 10 lines · Rasmus Villemoes

This series does a few cleanups of the isl12022 driver,

- removes use of deprecated function
- removes some redundant code
- switches to regmap API instead of private helpers

It also provides range_min, range_max values and finally hooks up the temperatur sensor to hwmon.

[...]

Applied, thanks!

[1/9] rtc: isl12022: stop using deprecated devm_rtc_device_register()

      commit: a35a2ad2b88a66732ac442ad5f86dc49af51673f
[2/9] rtc: isl12022: specify range_min and range_max
      commit: ca03b7a2c0b098321365f69538823d1bcc860552
[3/9] rtc: isl12022: drop a dev_info()
      commit: 43a96b9cf67770d4bb46267e1554d3d8b4cf78ac
[4/9] rtc: isl12022: simplify some expressions
      commit: ca35887186b7c53f26c42aee1285ba213adb4365
[5/9] rtc: isl12022: use %ptR
      commit: 7093b8a471f48d49891da2108f44fd64742408cb
[6/9] rtc: isl12022: use dev_set_drvdata() instead of i2c_set_clientdata()
      commit: 31b108acc50cddf3d16472ead45c4cd0d1337289
[7/9] rtc: isl12022: drop redundant write to HR register
      commit: 0a2abbfd8586d396a8581ebf9b96fd5746f08b14
[8/9] rtc: isl12022: switch to using regmap API
      commit: b1a1baa657c738e8bb0107ce304f5e78b9847f37

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com