Skip to contentSkip to conversation

iio: magnetometer: add support for Si7210

PATCH Superseded Antoni Pokusinski 5 participants 9 messages Jan 15, 2025 – Jan 18, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover iio: magnetometer: add support for Si7210

This patch series adds support for the Si7210 Hall effect I2C sensor. The driver currently supports the basic functionalities (i.e. making temperature and magnetic field measurements and changing the measurements scale) but I plan to add support for some other features in the future as well (e.g. the digital output interrupt).

---
Changes since v3:
* si7210: fetch_measurement/read_raw: fix issue with endianess
* si7210: replace if (ret < 0) with if (ret) wherever possible
* si7210: read_raw: use SI metric prefixes (MICRO, MILLI)
* si7210: si7210_data: swap i2c_client with regmap
* si7210: read_otpreg_val: remove unnecessary cast
* si7210: minor formatting updates
(add missing trailing commas, spaces etc)
* si7210: probe: use devm_mutex_init
* si7210: add missing includes

Changes since v2:
* Makefile: fix alignment
* si7210: fetch_measurement: use temporary variable to read the measurement instead of reading directly to buf
* si7210: read_raw: change tmp to temp
* si7210: read_raw: adjust temperature computation to match the spec better
* si7210: device_wake: do not init ret to 0
* si7210: MODULE_LICENSE: change license type to GPL
* si7210: minor improvements in comments

Changes since v1:
* dt-binding: add vdd-supply
* si7210: reorder includes in alphabetic order
* si7210: add comment for fetch_lock
* si7210: remove otp_lock
* si7210: fetch_measurement: change result type to __be16
* si7210: use guard(mutex) instead of scoped_guard
* si7210: read_raw: use FIELD_GET to get raw temperature value
* si7210: read_raw: return temperature in milli-celsius
* si7210: use regulator for getting the voltage
* si7210: si7210_device_wake: remove unnecessary cast
* si7210: si7210_device_init: use fsleep instead of usleep_range
* si7210: si7210_probe: remove i2c_set_clientdata()
* si7210: minor alignment and formatting fixes

Antoni Pokusinski (2):
dt-bindings: iio: magnetometer: add binding for Si7210
iio: magnetometer: si7210: add driver for Si7210

 .../iio/magnetometer/silabs,si7210.yaml       |  48 ++
 drivers/iio/magnetometer/Kconfig              |  11 +
 drivers/iio/magnetometer/Makefile             |   2 +
 drivers/iio/magnetometer/si7210.c             | 444 ++++++++++++++++++
 4 files changed, 505 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml
 create mode 100644 drivers/iio/magnetometer/si7210.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/2 dt-bindings: iio: magnetometer: add binding for Si7210

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor.

---
 .../iio/magnetometer/silabs,si7210.yaml       | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml
Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml new file +48 -0
new file mode 100644
index 000000000000..d4a3f7981c36
@@ -0,0 +1,48 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/magnetometer/silabs,si7210.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Si7210 magnetic position and temperature sensor
+
+maintainers:
+  - Antoni Pokusinski <[email protected]>
+
+description: |
+  Silabs Si7210 I2C Hall effect magnetic position and temperature sensor.
+  https://www.silabs.com/documents/public/data-sheets/si7210-datasheet.pdf
+
+properties:
+  compatible:
+    const: silabs,si7210
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+        magnetometer@30 {
+            compatible = "silabs,si7210";
+            reg = <0x30>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <4 IRQ_TYPE_EDGE_FALLING>;
+            vdd-supply = <&vdd_3v3_reg>;
+        };
+    };
Signature · 1 line
2.25.1
author Reviewed-by Krzysztof Kozlowski on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: iio: magnetometer: add binding for Si7210

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor.

---
In the first email I forgot about the Reviewed-by tag. Added it in here. Sorry for the confusion.

Kind regards,
Antoni

---
 .../iio/magnetometer/silabs,si7210.yaml       | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml
Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml new file +48 -0
new file mode 100644
index 000000000000..d4a3f7981c36
@@ -0,0 +1,48 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/magnetometer/silabs,si7210.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Si7210 magnetic position and temperature sensor
+
+maintainers:
+  - Antoni Pokusinski <[email protected]>
+
+description: |
+  Silabs Si7210 I2C Hall effect magnetic position and temperature sensor.
+  https://www.silabs.com/documents/public/data-sheets/si7210-datasheet.pdf
+
+properties:
+  compatible:
+    const: silabs,si7210
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+        magnetometer@30 {
+            compatible = "silabs,si7210";
+            reg = <0x30>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <4 IRQ_TYPE_EDGE_FALLING>;
+            vdd-supply = <&vdd_3v3_reg>;
+        };
+    };

--
2.25.1

Reviewed-by on v4 1/2
#3 lore raw

Re: [PATCH v4 1/2] dt-bindings: iio: magnetometer: add binding for Si7210

In reply to Antoni Pokusinski
Quoted 8 lines · Antoni Pokusinski

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor.

---
 .../iio/magnetometer/silabs,si7210.yaml       | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml

Best regards,
Krzysztof

#4 lore raw

PATCH v4 2/2 iio: magnetometer: si7210: add driver for Si7210

In reply to Antoni Pokusinski

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor. The driver supports the following functionalities:
* reading the temperature measurements
* reading the magnetic field measurements in a single-shot mode
* choosing the magnetic field measurement scale (20 or 200 mT)

---
 drivers/iio/magnetometer/Kconfig  |  11 +
 drivers/iio/magnetometer/Makefile |   2 +
 drivers/iio/magnetometer/si7210.c | 444 ++++++++++++++++++++++++++++++
 3 files changed, 457 insertions(+)
 create mode 100644 drivers/iio/magnetometer/si7210.c
drivers/iio/magnetometer/Kconfig +11 -0
index 7177cd1d67cb..3debf1320ad1 100644
@@ -235,6 +235,17 @@ config SENSORS_RM3100_SPI
 	  To compile this driver as a module, choose M here: the module
 	  will be called rm3100-spi.
 
+config SI7210
+	tristate "SI7210 Hall effect sensor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  Say Y here to add support for the SI7210 Hall effect sensor.
+
+	  This driver can also be compiled as a module.
+	  To compile this driver as a module, choose M here: the module
+	  will be called si7210.
+
 config TI_TMAG5273
 	tristate "TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor"
 	depends on I2C
drivers/iio/magnetometer/Makefile +2 -0
index 3e4c2ecd9adf..7455115a052f 100644
@@ -31,6 +31,8 @@ obj-$(CONFIG_SENSORS_RM3100)		+= rm3100-core.o
 obj-$(CONFIG_SENSORS_RM3100_I2C)	+= rm3100-i2c.o
 obj-$(CONFIG_SENSORS_RM3100_SPI)	+= rm3100-spi.o
 
+obj-$(CONFIG_SI7210) 			+= si7210.o
+
 obj-$(CONFIG_TI_TMAG5273)		+= tmag5273.o
 
 obj-$(CONFIG_YAMAHA_YAS530)		+= yamaha-yas530.o
drivers/iio/magnetometer/si7210.c new file +444 -0

444 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 2/2] iio: magnetometer: si7210: add driver for Si7210

Quoted 5 lines · Antoni Pokusinski

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor. The driver supports the following functionalities:
* reading the temperature measurements
* reading the magnetic field measurements in a single-shot mode
* choosing the magnetic field measurement scale (20 or 200 mT)

...

Quoted 1 line

+obj-$(CONFIG_SI7210) += si7210.o

Looks like TAB/space mixture in the middle.

...

Quoted 1 line

+#include <asm/byteorder.h>

asm/* usually goes after linux/*

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>

drivers/iio/magnetometer/si7210.c line 25

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/math64.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+#include <linux/units.h>

...

Despite a good formatting I would still add a comment with a formula in math-human-readable form.

drivers/iio/magnetometer/si7210.c line 227

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+		temp = FIELD_GET(GENMASK(14, 3), dspsig);
+		temp = div_s64(-383 * temp * temp, 100) + 160940 * temp - 279800000;
+		temp *= (1 + (data->temp_gain / 2048));
+		temp += (int)(MICRO / 16) * data->temp_offset;
 
+		ret = regulator_get_voltage(data->vdd);
+		if (ret < 0)
+			return ret;
+
+		temp -= 222 * div_s64(ret, MILLI);

Including this piece.

Quoted 1 line
drivers/iio/magnetometer/si7210.c new file +1 -0
+		*val = div_s64(temp, MILLI);

...

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +13 -0
+static int si7210_set_scale(struct si7210_data *data, unsigned int scale)
+{
+	s8 *a_otp_values;
+	int ret;
+
+	if (scale == 20)
+		a_otp_values = data->scale_20_a;
+	else if (scale == 200)
+		a_otp_values = data->scale_200_a;
+	else
+		return -EINVAL;
+
+	guard(mutex)(&data->fetch_lock);

drivers/iio/magnetometer/si7210.c line 259

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+
+	/* Write the registers 0xCA - 0xCC */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A0, a_otp_values, 3);
+	if (ret)
+		return ret;
 
+	/* Write the registers 0xCE - 0xD0 */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A3, &a_otp_values[3], 3);
+	if (ret)
+		return ret;

Just to be sure I understand the above. There are two of 24-bit values or there are two sets of 3 byte arrays? How does datasheet refers to them? What does common sense tell us here?

drivers/iio/magnetometer/si7210.c line 264

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	data->curr_scale = scale;
+
+	return 0;
+}

...

Overall LGTM, there is no need for resend as I believe the three things above may be tweaked by Jonathan. The last one can go even if there are 2 24-bit values, but ideally in that case we should use those as a such and apply put_unaligned_be24/le24() whichever suits better.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v4 2/2] iio: magnetometer: si7210: add driver for Si7210

Quoted 75 lines · Andy Shevchenko
Quoted 5 lines · Antoni Pokusinski

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor. The driver supports the following functionalities:
* reading the temperature measurements
* reading the magnetic field measurements in a single-shot mode
* choosing the magnetic field measurement scale (20 or 200 mT)

...

Quoted 1 line

+obj-$(CONFIG_SI7210) += si7210.o

Looks like TAB/space mixture in the middle.

...

Quoted 1 line

+#include <asm/byteorder.h>

asm/* usually goes after linux/*

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>

drivers/iio/magnetometer/si7210.c line 25

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/math64.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+#include <linux/units.h>

...

Despite a good formatting I would still add a comment with a formula in math-human-readable form.

drivers/iio/magnetometer/si7210.c line 227

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+		temp = FIELD_GET(GENMASK(14, 3), dspsig);
+		temp = div_s64(-383 * temp * temp, 100) + 160940 * temp - 279800000;
+		temp *= (1 + (data->temp_gain / 2048));
+		temp += (int)(MICRO / 16) * data->temp_offset;
 
+		ret = regulator_get_voltage(data->vdd);
+		if (ret < 0)
+			return ret;
+
+		temp -= 222 * div_s64(ret, MILLI);

Including this piece.

Quoted 1 line
drivers/iio/magnetometer/si7210.c new file +1 -0
+		*val = div_s64(temp, MILLI);

...

Quoted 18 lines
drivers/iio/magnetometer/si7210.c new file +18 -0
+static int si7210_set_scale(struct si7210_data *data, unsigned int scale)
+{
+	s8 *a_otp_values;
+	int ret;
+
+	if (scale == 20)
+		a_otp_values = data->scale_20_a;
+	else if (scale == 200)
+		a_otp_values = data->scale_200_a;
+	else
+		return -EINVAL;
+
+	guard(mutex)(&data->fetch_lock);
+
+	/* Write the registers 0xCA - 0xCC */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A0, a_otp_values, 3);
+	if (ret)
+		return ret;
Quoted 9 lines

drivers/iio/magnetometer/si7210.c line 259

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	/* Write the registers 0xCE - 0xD0 */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A3, &a_otp_values[3], 3);
+	if (ret)
+		return ret;

Just to be sure I understand the above. There are two of 24-bit values or there are two sets of 3 byte arrays? How does datasheet refers to them? What does common sense tell us here?

It's the second option: we have 2 arrays of 3 elements each (a0, a1, a2 and a3, a4, a5). In the datasheet the names of the values correspond to the names I used in the driver, that is there are 6 values a0, ..., a5.

The point is that the their registers are separated by the 0xCD register. Therefore I had to call regmap_bulk_write() twice in order to write values a0 - a2 to the registers 0xCA - 0xCC and similarly the values a3 - a5 to the regs 0xCE - 0xD0.

Quoted 7 lines
Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	data->curr_scale = scale;
+
+	return 0;
+}

...

Quoted 11 lines

Overall LGTM, there is no need for resend as I believe the three things above may be tweaked by Jonathan. The last one can go even if there are 2 24-bit values, but ideally in that case we should use those as a such and apply put_unaligned_be24/le24() whichever suits better.

Signature · 2 lines
With Best Regards,
Andy Shevchenko

Kind regards,
Antoni

Reviewed-by
#7 lore raw

Re: [PATCH v4 2/2] iio: magnetometer: si7210: add driver for Si7210

Quoted 2 lines · Antoni Pokusinski
Quoted 1 line · Andy Shevchenko

On Wed, Jan 15, 2025 at 09:16:22PM +0100, Antoni Pokusinski wrote:

...

Quoted 9 lines
Quoted 8 lines

drivers/iio/magnetometer/si7210.c line 259

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	/* Write the registers 0xCE - 0xD0 */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A3, &a_otp_values[3], 3);
+	if (ret)
+		return ret;

Just to be sure I understand the above. There are two of 24-bit values or there are two sets of 3 byte arrays? How does datasheet refers to them? What does common sense tell us here?

Quoted 8 lines

It's the second option: we have 2 arrays of 3 elements each (a0, a1, a2 and a3, a4, a5). In the datasheet the names of the values correspond to the names I used in the driver, that is there are 6 values a0, ..., a5.

The point is that the their registers are separated by the 0xCD register. Therefore I had to call regmap_bulk_write() twice in order to write values a0 - a2 to the registers 0xCA - 0xCC and similarly the values a3 - a5 to the regs 0xCE - 0xD0.

Thanks for the explanation! Looks reasonable to me.

...

Quoted 4 lines
Quoted 4 lines

Overall LGTM, there is no need for resend as I believe the three things above may be tweaked by Jonathan. The last one can go even if there are 2 24-bit values, but ideally in that case we should use those as a such and apply put_unaligned_be24/le24() whichever suits better.

FWIW,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw

Re: [PATCH v4 2/2] iio: magnetometer: si7210: add driver for Si7210

In reply to Andy Shevchenko
Quoted 36 lines · On Thu, 16 Jan 2025 10:26:55 +0200 Andy Shevchenko <[email protected]> wrote:
Quoted 5 lines · Antoni Pokusinski

Silicon Labs Si7210 is an I2C Hall effect magnetic position and temperature sensor. The driver supports the following functionalities:
* reading the temperature measurements
* reading the magnetic field measurements in a single-shot mode
* choosing the magnetic field measurement scale (20 or 200 mT)

...

Quoted 1 line

+obj-$(CONFIG_SI7210) += si7210.o

Looks like TAB/space mixture in the middle.

...

Quoted 1 line

+#include <asm/byteorder.h>

asm/* usually goes after linux/*

Quoted 14 lines
drivers/iio/magnetometer/si7210.c new file +14 -0
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/math64.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+#include <linux/units.h>  

...

Quoted 2 lines

Despite a good formatting I would still add a comment with a formula in math-human-readable form.

The rest of the comments are the sort of thing I might tweak. This one not so much as I'll probably get the maths wrong.

Given where we are with the cycle (too late 6.14, too early for 6.15 beyond me queuing it up in testing for autobuilders to get an early lok) we have plenty of time so I'd prefer to pick up a v5 with this comment added and the other minor stuff tidied up.

FWIW I took a look at the overall driver and have nothing to add in the way of review comments!

Jonathan

Quoted 58 lines

drivers/iio/magnetometer/si7210.c line 227

Quoted 11 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+		temp = FIELD_GET(GENMASK(14, 3), dspsig);
+		temp = div_s64(-383 * temp * temp, 100) + 160940 * temp - 279800000;
+		temp *= (1 + (data->temp_gain / 2048));
+		temp += (int)(MICRO / 16) * data->temp_offset;  
 
+		ret = regulator_get_voltage(data->vdd);
+		if (ret < 0)
+			return ret;
+
+		temp -= 222 * div_s64(ret, MILLI);  

Including this piece.

Quoted 1 line
drivers/iio/magnetometer/si7210.c new file +1 -0
+		*val = div_s64(temp, MILLI);  

...

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +13 -0
+static int si7210_set_scale(struct si7210_data *data, unsigned int scale)
+{
+	s8 *a_otp_values;
+	int ret;
+
+	if (scale == 20)
+		a_otp_values = data->scale_20_a;
+	else if (scale == 200)
+		a_otp_values = data->scale_200_a;
+	else
+		return -EINVAL;
+
+	guard(mutex)(&data->fetch_lock);

drivers/iio/magnetometer/si7210.c line 259

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+
+	/* Write the registers 0xCA - 0xCC */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A0, a_otp_values, 3);
+	if (ret)
+		return ret;  
 
+	/* Write the registers 0xCE - 0xD0 */
+	ret = regmap_bulk_write(data->regmap, SI7210_REG_A3, &a_otp_values[3], 3);
+	if (ret)
+		return ret;  

Just to be sure I understand the above. There are two of 24-bit values or there are two sets of 3 byte arrays? How does datasheet refers to them? What does common sense tell us here?

drivers/iio/magnetometer/si7210.c line 264

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	data->curr_scale = scale;
+
+	return 0;
+}  

...

Overall LGTM, there is no need for resend as I believe the three things above may be tweaked by Jonathan. The last one can go even if there are 2 24-bit values, but ideally in that case we should use those as a such and apply put_unaligned_be24/le24() whichever suits better.