Skip to contentSkip to conversation

iio: magnetometer: add support for Si7210

PATCH Superseded Antoni Pokusinski 3 participants 10 messages Jan 12, 2025 – Jan 15, 2025

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 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 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             | 428 ++++++++++++++++++
 4 files changed, 489 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 v3 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
maintainer
#2 lore raw

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

Quoted 5 lines · On Sun, 12 Jan 2025 11:44:52 +0100 Antoni Pokusinski <[email protected]>…

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

Trivial comment inline. No need to change anything.

Quoted 28 lines
---
 .../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 +17 -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:

Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml line 27

Quoted 10 lines
Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml new file +10 -0
+    const: silabs,si7210
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor

Not important, but this one is obvious enough I'd not have bothered with the description text.
vdd-suppy: true
would have been fine.

Jonathan

Quoted 21 lines
Documentation/devicetree/bindings/iio/magnetometer/silabs,si7210.yaml new file +21 -0
+
+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>;
+        };
+    };
#3 lore raw

PATCH v3 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 | 428 ++++++++++++++++++++++++++++++
 3 files changed, 441 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 +428 -0

428 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

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

Quoted 6 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)

...

Many header inclusions are being missed.

+ array_size.h

Quoted 1 line

+#include <linux/bitfield.h>

+ bits.h (it's even mentioned in the top comment of bitfield.h)

+ cleanup.h
+ err.h

drivers/iio/magnetometer/si7210.c line 17

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

+ types.h

+ asm/byteorder.h (or is it already available as linux/byteorder.h?), but it seems that what you actually wanted is linux/unaligned.h (see below why).

...

drivers/iio/magnetometer/si7210.c line 68

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+static const unsigned int a20_otp_regs[A_REGS_COUNT] = {
+       SI7210_OTPREG_A0_20, SI7210_OTPREG_A1_20, SI7210_OTPREG_A2_20,
+       SI7210_OTPREG_A3_20, SI7210_OTPREG_A4_20, SI7210_OTPREG_A5_20

Please, leave trailing comma(s) when it's clearly not a terminator entry.

drivers/iio/magnetometer/si7210.c line 73

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+};
+
+static const unsigned int a200_otp_regs[A_REGS_COUNT] = {
+       SI7210_OTPREG_A0_200, SI7210_OTPREG_A1_200, SI7210_OTPREG_A2_200,
+       SI7210_OTPREG_A3_200, SI7210_OTPREG_A4_200, SI7210_OTPREG_A5_200

Ditto.

Quoted 1 line

+};

...

drivers/iio/magnetometer/si7210.c line 121

Quoted 2 lines
drivers/iio/magnetometer/si7210.c new file +2 -0
+struct si7210_data {
+       struct i2c_client *client;

Do we really need a room for that? Isn't it derivable from the below regmap? Also note the frequency of use of client vs. regmap. The result in the object file can be much better if regmap becomes the first member here. Check it (with bloat-o-meter, for example).

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       struct regmap *regmap;
+       struct regulator *vdd;
+       struct mutex fetch_lock; /* lock for a single measurement fetch */
+       s8 temp_offset;
+       s8 temp_gain;
+       s8 scale_20_a[A_REGS_COUNT];
+       s8 scale_200_a[A_REGS_COUNT];
+       u8 curr_scale;
+};
+

drivers/iio/magnetometer/si7210.c line 141

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+static const struct iio_chan_spec si7210_channels[] = {
+       {
+               .type = IIO_MAGN,
+               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+                       BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
+       },
+       {
+               .type = IIO_TEMP,
+               .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+       }

Leave trailing comma.

drivers/iio/magnetometer/si7210.c line 149

Quoted 8 lines
drivers/iio/magnetometer/si7210.c new file +8 -0
+};
+
+static int si7210_fetch_measurement(struct si7210_data *data,
+                                   struct iio_chan_spec const *chan,
+                                   __be16 *buf)
+{
+       u8 dspsigsel = chan->type == IIO_MAGN ? 0 : 1;
+       int ret, result;

Why is the result signed? I believe even regmap APIs have it unsigned in the prototypes.
Ah, it's even worse... See below.

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+       guard(mutex)(&data->fetch_lock);
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_DSPSIGSEL,
+                                SI7210_MASK_DSPSIGSEL, dspsigsel);
+       if (ret < 0)

drivers/iio/magnetometer/si7210.c line 165

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+               return ret;
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_POWER_CTRL,
+                                SI7210_MASK_ONEBURST | SI7210_MASK_STOP,
+                                SI7210_MASK_ONEBURST & ~SI7210_MASK_STOP);
+       if (ret < 0)
+               return ret;
+
+       /* Read the contents of the registers containing the result: DSPSIGM, DSPSIGL */
+       ret = regmap_bulk_read(data->regmap, SI7210_REG_DSPSIGM, &result, 2);

I stumbled over this...

drivers/iio/magnetometer/si7210.c line 169

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+       if (ret < 0)
+               return ret;
+
+       *buf = cpu_to_be16(result);

...and this piece and I think you got it wrong. What you should do is just supply a buf with sizeof one element.

ret = ..., buf, sizeof(buf[0]));

Otherwise this needs a very good comment explaining what the heck is done here.

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+       return 0;
+}
+
+static int si7210_read_raw(struct iio_dev *indio_dev,
+                          struct iio_chan_spec const *chan,

drivers/iio/magnetometer/si7210.c line 185

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+                          int *val, int *val2, long mask)
+{
+       struct si7210_data *data = iio_priv(indio_dev);
+       long long temp;
+       __be16 dspsig;
+       int ret;
+
+       switch (mask) {
+       case IIO_CHAN_INFO_RAW:
+               ret = si7210_fetch_measurement(data, chan, &dspsig);

Oh, but why...

Quoted 1 line

+ if (ret < 0)

...then the ' < 0' part? What is the positive ret meaning?

Quoted 11 lines
drivers/iio/magnetometer/si7210.c new file +11 -0
+                       return ret;
+
+               *val = dspsig & GENMASK(14, 0);
+               return IIO_VAL_INT;
+       case IIO_CHAN_INFO_SCALE:
+               *val = 0;
+               if (data->curr_scale == 20)
+                       *val2 = 1250;
+               else /* data->curr_scale == 200 */
+                       *val2 = 12500;
+               return IIO_VAL_INT_PLUS_MICRO;

drivers/iio/magnetometer/si7210.c line 207

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       case IIO_CHAN_INFO_OFFSET:
+               *val = -16384;
+               return IIO_VAL_INT;
+       case IIO_CHAN_INFO_PROCESSED:
+               ret = si7210_fetch_measurement(data, chan, &dspsig);
+               if (ret < 0)
+                       return ret;
+
+               temp = FIELD_GET(GENMASK(14, 3), dspsig);
+               temp = div_s64(-383 * temp * temp, 100) + 160940 * temp - 279800000;

HECTO/CENTI, but I think in this case it's not needed as it is most likely in alignment with the datasheet.

Quoted 1 line

+ temp = (1 + (data->temp_gain / 2048)) * temp + (1000000 / 16) * data->temp_offset;

But here MICRO? MEGA? would make sense to show the scale.

drivers/iio/magnetometer/si7210.c line 214

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+               ret = regulator_get_voltage(data->vdd);
+               if (ret < 0)
+                       return ret;
 
+               temp -= 222 * div_s64(ret, 1000);

This is conversion from uV to mV IIUC, so replacing it with MILLI would make it harder to understand I suppose.

Quoted 1 line

+ *val = div_s64(temp, 1000);

MILLI?

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+               return IIO_VAL_INT;
+       default:
+               return -EINVAL;
+       }
+}

...

Quoted 6 lines
drivers/iio/magnetometer/si7210.c new file +6 -0
+static int si7210_read_otpreg_val(struct si7210_data *data, unsigned int otpreg, u8 *val)
+{
+       int ret;
+       unsigned int otpdata;
+
+       ret = regmap_write(data->regmap, SI7210_REG_OTP_ADDR, otpreg);

drivers/iio/magnetometer/si7210.c line 290

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+       if (ret < 0)
+               return ret;
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_OTP_CTRL,
+                                SI7210_MASK_OTP_READ_EN, SI7210_MASK_OTP_READ_EN);
+       if (ret < 0)
+               return ret;
 
+       ret = regmap_read(data->regmap, SI7210_REG_OTP_DATA, &otpdata);
+       if (ret < 0)

What are those ' < 0' parts for in many cases? Does it mean we ignore positive output? Why is it so?

drivers/iio/magnetometer/si7210.c line 293

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+               return ret;
+
+       *val = (u8)otpdata;

Why casting?

Quoted 2 lines
(quoted diff) +2 -0
+       return 0;
+}

...

drivers/iio/magnetometer/si7210.c line 303

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+       /*
+        * According to the datasheet, the primary method to wake up a
+        * device is to send an empty write. However this is not feasible
+        * using current API so we use the other method i.e. read a single

the current

drivers/iio/magnetometer/si7210.c line 306

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+        * byte. The device should respond with 0xFF.
+        */
 
+

Unneeded blank line, and TBH, the comment sounds like it should be rather for the entire function.

drivers/iio/magnetometer/si7210.c line 316

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       int ret;
+
+       ret = i2c_smbus_read_byte(data->client);
+       if (ret < 0)
+               return ret;
+
+       if (ret != 0xFF)
+               return -EIO;
+
+       return 0;

Btw, is this the only reason for having the client member in the private structure? If so, you can derive it from regmap.

...

drivers/iio/magnetometer/si7210.c line 372

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +12 -0
+static int si7210_probe(struct i2c_client *client)
+{
+       struct si7210_data *data;
+       struct iio_dev *indio_dev;
+       int ret;
+
+       indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+       if (!indio_dev)
+               return -ENOMEM;
+
+       data = iio_priv(indio_dev);
 
+       data->client = client;

(Almost) useless?

Quoted 1 line

+ mutex_init(&data->fetch_lock);

Why not devm_mutex_init()?

Quoted 27 lines
drivers/iio/magnetometer/si7210.c new file +27 -0
+       data->regmap = devm_regmap_init_i2c(client, &si7210_regmap_conf);
+       if (IS_ERR(data->regmap))
+               return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
+                                    "failed to register regmap\n");
+
+       data->vdd = devm_regulator_get(&client->dev, "vdd");
+       if (IS_ERR(data->vdd))
+               return dev_err_probe(&client->dev, PTR_ERR(data->vdd),
+                                    "failed to get VDD regulator\n");
+
+       ret = regulator_enable(data->vdd);
+       if (ret)
+               return ret;
+
+       indio_dev->name = dev_name(&client->dev);
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->info = &si7210_info;
+       indio_dev->channels = si7210_channels;
+       indio_dev->num_channels = ARRAY_SIZE(si7210_channels);
+
+       ret = si7210_device_init(data);
+       if (ret)
+               return dev_err_probe(&client->dev, ret,
+                                    "device initialization failed\n");
+
+       return devm_iio_device_register(&client->dev, indio_dev);
+}

...

drivers/iio/magnetometer/si7210.c line 424

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+static struct i2c_driver si7210_driver = {
+       .driver = {
+               .name = "si7210",
+               .of_match_table = si7210_dt_ids,
+       },
+       .probe = si7210_probe,
+       .id_table = si7210_id,
+};
 
+

Wrong place of this blank line...

Quoted 1 line

+module_i2c_driver(si7210_driver);

...should be here.

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+MODULE_AUTHOR("Antoni Pokusinski <[email protected]>");
+MODULE_DESCRIPTION("Silicon Labs Si7210 Hall Effect sensor I2C driver");
+MODULE_LICENSE("GPL");
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

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

Hi Andy,
Thanks for the review. I'm currently implementing some changes in the driver according to the review, however I have some doubts regarding removal of the i2c_client from si7210_data.

Kind regards,
Antoni

Quoted 56 lines · Andy Shevchenko
Quoted 6 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)

...

Many header inclusions are being missed.

+ array_size.h

Quoted 1 line

+#include <linux/bitfield.h>

+ bits.h (it's even mentioned in the top comment of bitfield.h)

+ cleanup.h
+ err.h

drivers/iio/magnetometer/si7210.c line 17

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

+ types.h

+ asm/byteorder.h (or is it already available as linux/byteorder.h?), but it seems that what you actually wanted is linux/unaligned.h (see below why).

...

drivers/iio/magnetometer/si7210.c line 68

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+static const unsigned int a20_otp_regs[A_REGS_COUNT] = {
+       SI7210_OTPREG_A0_20, SI7210_OTPREG_A1_20, SI7210_OTPREG_A2_20,
+       SI7210_OTPREG_A3_20, SI7210_OTPREG_A4_20, SI7210_OTPREG_A5_20

Please, leave trailing comma(s) when it's clearly not a terminator entry.

drivers/iio/magnetometer/si7210.c line 73

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+};
+
+static const unsigned int a200_otp_regs[A_REGS_COUNT] = {
+       SI7210_OTPREG_A0_200, SI7210_OTPREG_A1_200, SI7210_OTPREG_A2_200,
+       SI7210_OTPREG_A3_200, SI7210_OTPREG_A4_200, SI7210_OTPREG_A5_200

Ditto.

Quoted 1 line

+};

...

Quoted 8 lines

drivers/iio/magnetometer/si7210.c line 121

Quoted 2 lines
drivers/iio/magnetometer/si7210.c new file +2 -0
+struct si7210_data {
+       struct i2c_client *client;

Do we really need a room for that? Isn't it derivable from the below regmap? Also note the frequency of use of client vs. regmap. The result in the object file can be much better if regmap becomes the first member here. Check it (with bloat-o-meter, for example).

I used arm-linux-nm and the bloat-o-meter to compare the sizes and it turned out that the version which contains the i2c_client has slightly smaller size actually. Here are the results:

$ ./scripts/bloat-o-meter -p arm-linux-  ./old_si7210.ko  ./new_si7210.ko
add/remove: 0/0 grow/shrink: 1/0 up/down: 4/0 (4)
Function                                     old     new   delta
si7210_probe                                 556     560      +4
Total: Before=4021, After=4025, chg +0.10%

Here is the diff (shortened for better readability) between the old_si7210.ko (uses si7210_data->i2c_client) and new_si7210.ko (does not use si7210_data->i2c_client):

struct si7210_data {

-       struct i2c_client *client;
        struct regmap *regmap;
...
 static int si7210_device_wake(struct si7210_data *data)
 {
+       struct device *dev = regmap_get_device(data->regmap);
        int ret;

- ret = i2c_smbus_read_byte(data->client);
+ ret = i2c_smbus_read_byte(to_i2c_client(dev));
...
static int si7210_probe(struct i2c_client *client)

        data = iio_priv(indio_dev);

- data->client = client;

Hence, I guess that it's actually better to leave the i2c_client as it is.

Quoted 152 lines
Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       struct regmap *regmap;
+       struct regulator *vdd;
+       struct mutex fetch_lock; /* lock for a single measurement fetch */
+       s8 temp_offset;
+       s8 temp_gain;
+       s8 scale_20_a[A_REGS_COUNT];
+       s8 scale_200_a[A_REGS_COUNT];
+       u8 curr_scale;
+};
+

drivers/iio/magnetometer/si7210.c line 141

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+static const struct iio_chan_spec si7210_channels[] = {
+       {
+               .type = IIO_MAGN,
+               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+                       BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
+       },
+       {
+               .type = IIO_TEMP,
+               .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+       }

Leave trailing comma.

drivers/iio/magnetometer/si7210.c line 149

Quoted 8 lines
drivers/iio/magnetometer/si7210.c new file +8 -0
+};
+
+static int si7210_fetch_measurement(struct si7210_data *data,
+                                   struct iio_chan_spec const *chan,
+                                   __be16 *buf)
+{
+       u8 dspsigsel = chan->type == IIO_MAGN ? 0 : 1;
+       int ret, result;

Why is the result signed? I believe even regmap APIs have it unsigned in the prototypes.
Ah, it's even worse... See below.

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+       guard(mutex)(&data->fetch_lock);
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_DSPSIGSEL,
+                                SI7210_MASK_DSPSIGSEL, dspsigsel);
+       if (ret < 0)

drivers/iio/magnetometer/si7210.c line 165

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+               return ret;
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_POWER_CTRL,
+                                SI7210_MASK_ONEBURST | SI7210_MASK_STOP,
+                                SI7210_MASK_ONEBURST & ~SI7210_MASK_STOP);
+       if (ret < 0)
+               return ret;
+
+       /* Read the contents of the registers containing the result: DSPSIGM, DSPSIGL */
+       ret = regmap_bulk_read(data->regmap, SI7210_REG_DSPSIGM, &result, 2);

I stumbled over this...

drivers/iio/magnetometer/si7210.c line 169

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+       if (ret < 0)
+               return ret;
+
+       *buf = cpu_to_be16(result);

...and this piece and I think you got it wrong. What you should do is just supply a buf with sizeof one element.

ret = ..., buf, sizeof(buf[0]));

Otherwise this needs a very good comment explaining what the heck is done here.

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+       return 0;
+}
+
+static int si7210_read_raw(struct iio_dev *indio_dev,
+                          struct iio_chan_spec const *chan,

drivers/iio/magnetometer/si7210.c line 185

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+                          int *val, int *val2, long mask)
+{
+       struct si7210_data *data = iio_priv(indio_dev);
+       long long temp;
+       __be16 dspsig;
+       int ret;
+
+       switch (mask) {
+       case IIO_CHAN_INFO_RAW:
+               ret = si7210_fetch_measurement(data, chan, &dspsig);

Oh, but why...

Quoted 1 line

+ if (ret < 0)

...then the ' < 0' part? What is the positive ret meaning?

Quoted 11 lines
drivers/iio/magnetometer/si7210.c new file +11 -0
+                       return ret;
+
+               *val = dspsig & GENMASK(14, 0);
+               return IIO_VAL_INT;
+       case IIO_CHAN_INFO_SCALE:
+               *val = 0;
+               if (data->curr_scale == 20)
+                       *val2 = 1250;
+               else /* data->curr_scale == 200 */
+                       *val2 = 12500;
+               return IIO_VAL_INT_PLUS_MICRO;

drivers/iio/magnetometer/si7210.c line 207

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       case IIO_CHAN_INFO_OFFSET:
+               *val = -16384;
+               return IIO_VAL_INT;
+       case IIO_CHAN_INFO_PROCESSED:
+               ret = si7210_fetch_measurement(data, chan, &dspsig);
+               if (ret < 0)
+                       return ret;
+
+               temp = FIELD_GET(GENMASK(14, 3), dspsig);
+               temp = div_s64(-383 * temp * temp, 100) + 160940 * temp - 279800000;

HECTO/CENTI, but I think in this case it's not needed as it is most likely in alignment with the datasheet.

Quoted 1 line

+ temp = (1 + (data->temp_gain / 2048)) * temp + (1000000 / 16) * data->temp_offset;

But here MICRO? MEGA? would make sense to show the scale.

drivers/iio/magnetometer/si7210.c line 214

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+               ret = regulator_get_voltage(data->vdd);
+               if (ret < 0)
+                       return ret;
 
+               temp -= 222 * div_s64(ret, 1000);

This is conversion from uV to mV IIUC, so replacing it with MILLI would make it harder to understand I suppose.

Quoted 1 line

+ *val = div_s64(temp, 1000);

MILLI?

Quoted 5 lines
drivers/iio/magnetometer/si7210.c new file +5 -0
+               return IIO_VAL_INT;
+       default:
+               return -EINVAL;
+       }
+}

...

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +13 -0
+static int si7210_read_otpreg_val(struct si7210_data *data, unsigned int otpreg, u8 *val)
+{
+       int ret;
+       unsigned int otpdata;
+
+       ret = regmap_write(data->regmap, SI7210_REG_OTP_ADDR, otpreg);
+       if (ret < 0)
+               return ret;
+
+       ret = regmap_update_bits(data->regmap, SI7210_REG_OTP_CTRL,
+                                SI7210_MASK_OTP_READ_EN, SI7210_MASK_OTP_READ_EN);
+       if (ret < 0)
+               return ret;
Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       ret = regmap_read(data->regmap, SI7210_REG_OTP_DATA, &otpdata);
+       if (ret < 0)

What are those ' < 0' parts for in many cases? Does it mean we ignore positive output? Why is it so?

The regmap functions return value <=0 so I decided to handle errors in the 'if (ret < 0)' way.
But in the next version I'll change that to a simpler 'if (ret)' wherever possible

Quoted 116 lines

drivers/iio/magnetometer/si7210.c line 293

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+               return ret;
+
+       *val = (u8)otpdata;

Why casting?

Quoted 2 lines
(quoted diff) +2 -0
+       return 0;
+}

...

drivers/iio/magnetometer/si7210.c line 303

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+       /*
+        * According to the datasheet, the primary method to wake up a
+        * device is to send an empty write. However this is not feasible
+        * using current API so we use the other method i.e. read a single

the current

drivers/iio/magnetometer/si7210.c line 306

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+        * byte. The device should respond with 0xFF.
+        */
 
+

Unneeded blank line, and TBH, the comment sounds like it should be rather for the entire function.

drivers/iio/magnetometer/si7210.c line 316

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+       int ret;
+
+       ret = i2c_smbus_read_byte(data->client);
+       if (ret < 0)
+               return ret;
+
+       if (ret != 0xFF)
+               return -EIO;
+
+       return 0;

Btw, is this the only reason for having the client member in the private structure? If so, you can derive it from regmap.

...

drivers/iio/magnetometer/si7210.c line 372

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +12 -0
+static int si7210_probe(struct i2c_client *client)
+{
+       struct si7210_data *data;
+       struct iio_dev *indio_dev;
+       int ret;
+
+       indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+       if (!indio_dev)
+               return -ENOMEM;
+
+       data = iio_priv(indio_dev);
 
+       data->client = client;

(Almost) useless?

Quoted 1 line

+ mutex_init(&data->fetch_lock);

Why not devm_mutex_init()?

Quoted 27 lines
drivers/iio/magnetometer/si7210.c new file +27 -0
+       data->regmap = devm_regmap_init_i2c(client, &si7210_regmap_conf);
+       if (IS_ERR(data->regmap))
+               return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
+                                    "failed to register regmap\n");
+
+       data->vdd = devm_regulator_get(&client->dev, "vdd");
+       if (IS_ERR(data->vdd))
+               return dev_err_probe(&client->dev, PTR_ERR(data->vdd),
+                                    "failed to get VDD regulator\n");
+
+       ret = regulator_enable(data->vdd);
+       if (ret)
+               return ret;
+
+       indio_dev->name = dev_name(&client->dev);
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->info = &si7210_info;
+       indio_dev->channels = si7210_channels;
+       indio_dev->num_channels = ARRAY_SIZE(si7210_channels);
+
+       ret = si7210_device_init(data);
+       if (ret)
+               return dev_err_probe(&client->dev, ret,
+                                    "device initialization failed\n");
+
+       return devm_iio_device_register(&client->dev, indio_dev);
+}

...

drivers/iio/magnetometer/si7210.c line 424

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+static struct i2c_driver si7210_driver = {
+       .driver = {
+               .name = "si7210",
+               .of_match_table = si7210_dt_ids,
+       },
+       .probe = si7210_probe,
+       .id_table = si7210_id,
+};
 
+

Wrong place of this blank line...

Quoted 1 line

+module_i2c_driver(si7210_driver);

...should be here.

Quoted 3 lines
drivers/iio/magnetometer/si7210.c new file +3 -0
+MODULE_AUTHOR("Antoni Pokusinski <[email protected]>");
+MODULE_DESCRIPTION("Silicon Labs Si7210 Hall Effect sensor I2C driver");
+MODULE_LICENSE("GPL");
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

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

Quoted 3 lines · Antoni Pokusinski

Thanks for the review. I'm currently implementing some changes in the driver according to the review, however I have some doubts regarding removal of the i2c_client from si7210_data.

...

Quoted 36 lines
Quoted 8 lines

drivers/iio/magnetometer/si7210.c line 121

Quoted 2 lines
drivers/iio/magnetometer/si7210.c new file +2 -0
+struct si7210_data {
+       struct i2c_client *client;

Do we really need a room for that? Isn't it derivable from the below regmap? Also note the frequency of use of client vs. regmap. The result in the object file can be much better if regmap becomes the first member here. Check it (with bloat-o-meter, for example).

I used arm-linux-nm and the bloat-o-meter to compare the sizes and it turned out that the version which contains the i2c_client has slightly smaller size actually. Here are the results:

$ ./scripts/bloat-o-meter -p arm-linux-  ./old_si7210.ko  ./new_si7210.ko
add/remove: 0/0 grow/shrink: 1/0 up/down: 4/0 (4)
Function                                     old     new   delta
si7210_probe                                 556     560      +4
Total: Before=4021, After=4025, chg +0.10%

Here is the diff (shortened for better readability) between the old_si7210.ko (uses si7210_data->i2c_client) and new_si7210.ko (does not use si7210_data->i2c_client):

struct si7210_data {

(quoted diff) +2 -2
-       struct i2c_client *client;
        struct regmap *regmap;
...
 static int si7210_device_wake(struct si7210_data *data)
 {
+       struct device *dev = regmap_get_device(data->regmap);
        int ret;
 
-       ret = i2c_smbus_read_byte(data->client);
+       ret = i2c_smbus_read_byte(to_i2c_client(dev));
...

static int si7210_probe(struct i2c_client *client)

Quoted 4 lines
        data = iio_priv(indio_dev);

- data->client = client;

Hence, I guess that it's actually better to leave the i2c_client as it is.

I don't think you have tested all that I was talking about, i.e. have you tried to swap the positions of client and regmap? What I expect is that when you swap them you will see a good size reduction due to pointer arithmetics becoming no-op for the regmap pointer. And then the dropping of the client might waste all that beneficial size.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+       struct regmap *regmap;
+       struct regulator *vdd;
+       struct mutex fetch_lock; /* lock for a single measurement fetch */
+       s8 temp_offset;
+       s8 temp_gain;
+       s8 scale_20_a[A_REGS_COUNT];
+       s8 scale_200_a[A_REGS_COUNT];
+       u8 curr_scale;
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

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

Quoted 48 lines · Andy Shevchenko
Quoted 3 lines · Antoni Pokusinski

Thanks for the review. I'm currently implementing some changes in the driver according to the review, however I have some doubts regarding removal of the i2c_client from si7210_data.

...

Quoted 39 lines
Quoted 8 lines

drivers/iio/magnetometer/si7210.c line 121

Quoted 2 lines
drivers/iio/magnetometer/si7210.c new file +2 -0
+struct si7210_data {
+       struct i2c_client *client;

Do we really need a room for that? Isn't it derivable from the below regmap? Also note the frequency of use of client vs. regmap. The result in the object file can be much better if regmap becomes the first member here. Check it (with bloat-o-meter, for example).

I used arm-linux-nm and the bloat-o-meter to compare the sizes and it turned out that the version which contains the i2c_client has slightly smaller size actually. Here are the results:

$ ./scripts/bloat-o-meter -p arm-linux-  ./old_si7210.ko  ./new_si7210.ko
add/remove: 0/0 grow/shrink: 1/0 up/down: 4/0 (4)
Function                                     old     new   delta
si7210_probe                                 556     560      +4
Total: Before=4021, After=4025, chg +0.10%

Here is the diff (shortened for better readability) between the old_si7210.ko (uses si7210_data->i2c_client) and new_si7210.ko (does not use si7210_data->i2c_client):

struct si7210_data {

(quoted diff) +2 -2
-       struct i2c_client *client;
        struct regmap *regmap;
...
 static int si7210_device_wake(struct si7210_data *data)
 {
+       struct device *dev = regmap_get_device(data->regmap);
        int ret;
 
-       ret = i2c_smbus_read_byte(data->client);
+       ret = i2c_smbus_read_byte(to_i2c_client(dev));
...

static int si7210_probe(struct i2c_client *client)

        data = iio_priv(indio_dev);

- data->client = client;

Quoted 8 lines
Quoted 1 line

Hence, I guess that it's actually better to leave the i2c_client as it is.

I don't think you have tested all that I was talking about, i.e. have you tried to swap the positions of client and regmap? What I expect is that when you swap them you will see a good size reduction due to pointer arithmetics becoming no-op for the regmap pointer. And then the dropping of the client might waste all that beneficial size.

Ok, so I've tried to swap the i2c_client and regmap pointers and... there was no change shown by the bloat-o-meter. The only improvement was that the new object file (that is after moving the regmap to the beginning of the struct) was 8 bytes smaller in file size.

Out of curiosity I've also tried moving
the regmap further away in the structure (e.g. I placed it after the regulator and mutex) but there was still no change. I am a bit confused, since this behavior is different from what you described that it should be.

Quoted 13 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+       struct regmap *regmap;
+       struct regulator *vdd;
+       struct mutex fetch_lock; /* lock for a single measurement fetch */
+       s8 temp_offset;
+       s8 temp_gain;
+       s8 scale_20_a[A_REGS_COUNT];
+       s8 scale_200_a[A_REGS_COUNT];
+       u8 curr_scale;
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko

Kinds regards,
Antoni

#8 lore raw

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

Quoted 3 lines · Antoni Pokusinski
Quoted 2 lines · Andy Shevchenko

On Tue, Jan 14, 2025 at 12:19 AM Antoni Pokusinski <[email protected]> wrote:

...

Quoted 52 lines
Quoted 38 lines
Quoted 38 lines
Quoted 7 lines

drivers/iio/magnetometer/si7210.c line 121

Quoted 2 lines
drivers/iio/magnetometer/si7210.c new file +2 -0
+struct si7210_data {
+       struct i2c_client *client;

Do we really need a room for that? Isn't it derivable from the below regmap? Also note the frequency of use of client vs. regmap. The result in the object file can be much better if regmap becomes the first member here. Check it (with bloat-o-meter, for example).

I used arm-linux-nm and the bloat-o-meter to compare the sizes and it turned out that the version which contains the i2c_client has slightly smaller size actually. Here are the results:

$ ./scripts/bloat-o-meter -p arm-linux-  ./old_si7210.ko  ./new_si7210.ko
add/remove: 0/0 grow/shrink: 1/0 up/down: 4/0 (4)
Function                                     old     new   delta
si7210_probe                                 556     560      +4
Total: Before=4021, After=4025, chg +0.10%

Here is the diff (shortened for better readability) between the old_si7210.ko (uses si7210_data->i2c_client) and new_si7210.ko (does not use si7210_data->i2c_client):

struct si7210_data {

(quoted diff) +2 -2
-       struct i2c_client *client;
        struct regmap *regmap;
...
 static int si7210_device_wake(struct si7210_data *data)
 {
+       struct device *dev = regmap_get_device(data->regmap);
        int ret;
 
-       ret = i2c_smbus_read_byte(data->client);
+       ret = i2c_smbus_read_byte(to_i2c_client(dev));
...

static int si7210_probe(struct i2c_client *client)

        data = iio_priv(indio_dev);

- data->client = client;

Quoted 8 lines
Quoted 1 line

Hence, I guess that it's actually better to leave the i2c_client as it is.

I don't think you have tested all that I was talking about, i.e. have you tried to swap the positions of client and regmap? What I expect is that when you swap them you will see a good size reduction due to pointer arithmetics becoming no-op for the regmap pointer. And then the dropping of the client might waste all that beneficial size.

Ok, so I've tried to swap the i2c_client and regmap pointers and... there was no change shown by the bloat-o-meter. The only improvement was that the new object file (that is after moving the regmap to the beginning of the struct) was 8 bytes smaller in file size.

Quoted 5 lines

Out of curiosity I've also tried moving
the regmap further away in the structure (e.g. I placed it after the regulator and mutex) but there was still no change. I am a bit confused, since this behavior is different from what you described that it should be.

I haven't told "should" I have told "expected". So this means that for the compiler in use and platform (architecture) there are no differences. Good to have a confirmation for that. Nevertheless, I would keep them swapped, because it's more natural to see the most used member first at the structure.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/magnetometer/si7210.c new file +9 -0
+       struct regmap *regmap;
+       struct regulator *vdd;
+       struct mutex fetch_lock; /* lock for a single measurement fetch */
+       s8 temp_offset;
+       s8 temp_gain;
+       s8 scale_20_a[A_REGS_COUNT];
+       s8 scale_200_a[A_REGS_COUNT];
+       u8 curr_scale;
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#9 lore raw

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

In reply to Antoni Pokusinski
Quoted 7 lines · On Sun, 12 Jan 2025 11:44:53 +0100 Antoni Pokusinski <[email protected]>…

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)

Hi Antoni,

Some issues in the endian handling as your fix for the build bot warnings is backwards I think.
A few other comments inline.

Jonathan

Quoted 13 lines
drivers/iio/magnetometer/si7210.c new file +7 -0
new file mode 100644
index 000000000000..107312d127e6
@@ -0,0 +1,428 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Silicon Labs Si7210 Hall Effect sensor driver
+ *
+ * Copyright (c) 2024 Antoni Pokusinski <[email protected]>
+ *
+ * Datasheet:

drivers/iio/magnetometer/si7210.c line 17

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+ *  https://www.silabs.com/documents/public/data-sheets/si7210-datasheet.pdf
+ */
+
+#include <linux/bitfield.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/math64.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>

At lease linux/mod_devicetable.h is missing here.
we more or less go by include what you use in kernel drivers so you should make minimal assumptions about what includes what. There are a few headers that are subsections of a wider interface where that isn't necessary but for most things should be here.

drivers/iio/magnetometer/si7210.c line 138

Quoted 7 lines
drivers/iio/magnetometer/si7210.c new file +7 -0
+static const struct iio_chan_spec si7210_channels[] = {
+	{
+		.type = IIO_MAGN,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
+	},
+	{

Slight preference for more compact

	}, {
Quoted 17 lines
drivers/iio/magnetometer/si7210.c new file +17 -0
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+	}
+};
+
+static int si7210_fetch_measurement(struct si7210_data *data,
+				    struct iio_chan_spec const *chan,
+				    __be16 *buf)
+{
+	u8 dspsigsel = chan->type == IIO_MAGN ? 0 : 1;
+	int ret, result;
+
+	guard(mutex)(&data->fetch_lock);
+
+	ret = regmap_update_bits(data->regmap, SI7210_REG_DSPSIGSEL,
+				 SI7210_MASK_DSPSIGSEL, dspsigsel);
+	if (ret < 0)

drivers/iio/magnetometer/si7210.c line 165

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+		return ret;
+
+	ret = regmap_update_bits(data->regmap, SI7210_REG_POWER_CTRL,
+				 SI7210_MASK_ONEBURST | SI7210_MASK_STOP,
+				 SI7210_MASK_ONEBURST & ~SI7210_MASK_STOP);
+	if (ret < 0)
+		return ret;
+
+	/* Read the contents of the registers containing the result: DSPSIGM, DSPSIGL */
+	ret = regmap_bulk_read(data->regmap, SI7210_REG_DSPSIGM, &result, 2);

use sizeof to replace that 2.

drivers/iio/magnetometer/si7210.c line 169

Quoted 4 lines
drivers/iio/magnetometer/si7210.c new file +4 -0
+	if (ret < 0)
+		return ret;
+
+	*buf = cpu_to_be16(result);

You've lost me. The regmap bulk will load in 'an order'. Not sure whether thant is big endian or little endian here, but it's definitely not CPU endian. I 'think' you can read directly into buf. Must be a wrong endian conversion somewhere though as on typical le system you are currently reversing the bytes here and at the callsite.

However, I think what you actually want is
static int si7210_fetch_measurement(struct si7210_data *data,

				    struct iio_chan_spec const *chan,
				    u16 *buf)
	__be16 result;

...

	ret = regmap_bulk_read(..., &result, sizeof(result));
...
	*buf = be16_to_cpu(result);
Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+
+	return 0;
+}
+
+static int si7210_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
+{
+	struct si7210_data *data = iio_priv(indio_dev);
+	long long temp;

drivers/iio/magnetometer/si7210.c line 189

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+	__be16 dspsig;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = si7210_fetch_measurement(data, chan, &dspsig);
+		if (ret < 0)
+			return ret;
+
+		*val = dspsig & GENMASK(14, 0);

It is big endian. You can't do this and expect to hit the right bits. More to the point you can't set val to the big endian anyway so markings are wrong somewhere.

Quoted 6 lines
drivers/iio/magnetometer/si7210.c new file +6 -0
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		if (data->curr_scale == 20)
+			*val2 = 1250;
+		else /* data->curr_scale == 200 */

drivers/iio/magnetometer/si7210.c line 205

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+			*val2 = 12500;
+		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_OFFSET:
+		*val = -16384;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_PROCESSED:
+		ret = si7210_fetch_measurement(data, chan, &dspsig);
+		if (ret < 0)
+			return ret;
+

Big endian, You can't do any of this safely. Convert it to cpu endian (e.g. u16) first.

Quoted 17 lines
drivers/iio/magnetometer/si7210.c new file +17 -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 + (1000000 / 16) * data->temp_offset;
+
+		ret = regulator_get_voltage(data->vdd);
+		if (ret < 0)
+			return ret;
+
+		temp -= 222 * div_s64(ret, 1000);
+
+		*val = div_s64(temp, 1000);
+
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}

...

Quoted 25 lines
drivers/iio/magnetometer/si7210.c new file +25 -0
+static int si7210_device_wake(struct si7210_data *data)
+{
+	/*
+	 * According to the datasheet, the primary method to wake up a
+	 * device is to send an empty write. However this is not feasible
+	 * using current API so we use the other method i.e. read a single
+	 * byte. The device should respond with 0xFF.
+	 */
+
+	int ret;
+
+	ret = i2c_smbus_read_byte(data->client);
+	if (ret < 0)
+		return ret;
+
+	if (ret != 0xFF)
+		return -EIO;
+
+	return 0;
+}
+
+static int si7210_device_init(struct si7210_data *data)
+{
+	int ret;
+	unsigned int i;

drivers/iio/magnetometer/si7210.c line 332

Quoted 10 lines
drivers/iio/magnetometer/si7210.c new file +10 -0
+
+	ret = si7210_device_wake(data);
+	if (ret < 0)
+		return ret;
+
+	fsleep(1000);
+
+	ret = si7210_read_otpreg_val(data, SI7210_OTPREG_TMP_GAIN, &data->temp_gain);
+	if (ret < 0)
+		return ret;

Blank line here and similar places where you have a call / check result pair before doing something more or less unrelated.

Quoted 23 lines
drivers/iio/magnetometer/si7210.c new file +22 -0
+	ret = si7210_read_otpreg_val(data, SI7210_OTPREG_TMP_OFF, &data->temp_offset);
+	if (ret < 0)
+		return ret;
+
+	for (i = 0; i < A_REGS_COUNT; i++) {
+		ret = si7210_read_otpreg_val(data, a20_otp_regs[i], &data->scale_20_a[i]);
+		if (ret < 0)
+			return ret;
+	}
+	for (i = 0; i < A_REGS_COUNT; i++) {
+		ret = si7210_read_otpreg_val(data, a200_otp_regs[i], &data->scale_200_a[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	ret = regmap_update_bits(data->regmap, SI7210_REG_ARAUTOINC,
+				 SI7210_MASK_ARAUTOINC, SI7210_MASK_ARAUTOINC);
+	if (ret < 0)
+		return ret;
+
+	return si7210_set_scale(data, 20);
+}