Skip to contentSkip to conversation

Add support for AF8133J magnetometer

PATCH In review Ondřej Jirman 3 participants 9 messages Feb 22, 2024 – Feb 25, 2024

Subsystems (3)
lore
Share & tools
lore

Ondřej Jirman

author maintainer
#0 lore raw

v4 cover Add support for AF8133J magnetometer

From: Ondrej Jirman <[email protected]>

This series adds support for AF8133J magnetometer sensor. It's a simple 3-axis sensor with two sensitivity options and not much else to it.

This sensor is used on both Pinephone and Pinephone Pro. DT patches adding it will come later, once this driver is merged.

Please take a look. :)

Thank you very much,
Ondřej Jirman

v4:
- move RPM enable in probe function before iio device registration

v3:
- collect more tags
- if (ret < 0) -> (ret) where appropriate
- scoped guard move to af8133j_set_scale()
- remove pm_runtime_disable/enable guard from af8133j_power_down_action()
- pretty much just this:
https://megous.com/dl/tmp/0001-if-ret-0-ret-where-appropriate.patch
https://megous.com/dl/tmp/0002-scoped-guard-move-to-af8133j_set_scale.patch
https://megous.com/dl/tmp/0003-remove-pm_runtime_disable-enable-guard-from-af8133j_.patch

v2:
- move maintainers patch to the end of series
- bindings:
- fix compatible definition in bindings file
- require power supplies
- fix descriptions
- driver:
- sort includes
- rework RPM, the driver should now work with RPM disabled among other improvements
- I've tested RPM left and right doing device bind/unbind under various conditions, system suspend under various conditions, etc.
- use scoped_guard for mutexes
- use devm for power down and handle power down correctly with both

    RPM enabled/disabled without tracking power state in data->powered

- fix issue with changing scale while RPM suspended
- various code formatting issues resolved
- as for sign-offs, I've added co-developed-by for people I know for sure worked on the driver, and left other tags as they were when I picked up the patch 2 years ago to my Linux branch

Icenowy Zheng (3):
dt-bindings: vendor-prefix: Add prefix for Voltafield
dt-bindings: iio: magnetometer: Add Voltafield AF8133J
iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

Ondrej Jirman (1):
MAINTAINERS: Add an entry for AF8133J driver

 .../iio/magnetometer/voltafield,af8133j.yaml  |  60 ++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 MAINTAINERS                                   |   6 +
 drivers/iio/magnetometer/Kconfig              |  12 +
 drivers/iio/magnetometer/Makefile             |   1 +
 drivers/iio/magnetometer/af8133j.c            | 524 ++++++++++++++++++
 6 files changed, 605 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/voltafield,af8133j.yaml
 create mode 100644 drivers/iio/magnetometer/af8133j.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v4 1/4 dt-bindings: vendor-prefix: Add prefix for Voltafield

From: Icenowy Zheng <[email protected]>

Voltafile Technology Corp. is a company that produces MEMS sensors.

Add a DT vendor prefix for it.

---
 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 1a0dc04f1db4..82e9f64c90ff 100644
@@ -1534,6 +1534,8 @@ patternProperties:
     description: VoCore Studio
   "^voipac,.*":
     description: Voipac Technologies s.r.o.
+  "^voltafield,.*":
+    description: Voltafield Technology Corp.
   "^vot,.*":
     description: Vision Optical Technology Co., Ltd.
   "^vxt,.*":
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v4 2/4 dt-bindings: iio: magnetometer: Add Voltafield AF8133J

In reply to Ondřej Jirman

From: Icenowy Zheng <[email protected]>

Voltafield AF8133J is a simple magnetometer sensor produced by Voltafield Technology Corp, with dual power supplies (one for core and one for I/O) and active-low reset pin.

The sensor has configurable range 1.2 - 2.2 mT and a software controlled standby mode.

Add a device tree binding for it.

---
 .../iio/magnetometer/voltafield,af8133j.yaml  | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/voltafield,af8133j.yaml
Documentation/devicetree/bindings/iio/magnetometer/voltafield,af8133j.yaml new file +60 -0
new file mode 100644
index 000000000000..b6ab01a6914a
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/magnetometer/voltafield,af8133j.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Voltafield AF8133J magnetometer sensor
+
+maintainers:
+  - Ondřej Jirman <[email protected]>
+
+properties:
+  compatible:
+    const: voltafield,af8133j
+
+  reg:
+    maxItems: 1
+
+  reset-gpios:
+    description:
+      A signal for active low reset input of the sensor. (optional; if not
+      used, software reset over I2C will be used instead)
+
+  avdd-supply:
+    description:
+      A regulator that provides AVDD power (Working power, usually 3.3V) to
+      the sensor.
+
+  dvdd-supply:
+    description:
+      A regulator that provides DVDD power (Digital IO power, 1.8V - AVDD)
+      to the sensor.
+
+  mount-matrix:
+    description: An optional 3x3 mounting rotation matrix.
+
+required:
+  - compatible
+  - reg
+  - avdd-supply
+  - dvdd-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        magnetometer@1c {
+            compatible = "voltafield,af8133j";
+            reg = <0x1c>;
+            avdd-supply = <&reg_dldo1>;
+            dvdd-supply = <&reg_dldo1>;
+            reset-gpios = <&pio 1 1 GPIO_ACTIVE_LOW>;
+        };
+    };
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v4 3/4 iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

In reply to Ondřej Jirman

From: Icenowy Zheng <[email protected]>

AF8133J is a simple I2C-connected magnetometer, without interrupts.

Add a simple IIO driver for it.

---
 drivers/iio/magnetometer/Kconfig   |  12 +
 drivers/iio/magnetometer/Makefile  |   1 +
 drivers/iio/magnetometer/af8133j.c | 524 +++++++++++++++++++++++++++++
 3 files changed, 537 insertions(+)
 create mode 100644 drivers/iio/magnetometer/af8133j.c
drivers/iio/magnetometer/Kconfig +12 -0
index 38532d840f2a..cd2917d71904 100644
@@ -6,6 +6,18 @@
 
 menu "Magnetometer sensors"
 
+config AF8133J
+	tristate "Voltafield AF8133J 3-Axis Magnetometer"
+	depends on I2C
+	depends on OF
+	select REGMAP_I2C
+	help
+	  Say yes here to build support for Voltafield AF8133J I2C-based
+	  3-axis magnetometer chip.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called af8133j.
+
 config AK8974
 	tristate "Asahi Kasei AK8974 3-Axis Magnetometer"
 	depends on I2C
drivers/iio/magnetometer/Makefile +1 -0
index b1c784ea71c8..ec5c46fbf999 100644
@@ -4,6 +4,7 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_AF8133J)	+= af8133j.o
 obj-$(CONFIG_AK8974)	+= ak8974.o
 obj-$(CONFIG_AK8975)	+= ak8975.o
 obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o
drivers/iio/magnetometer/af8133j.c new file +524 -0

524 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v4 3/4] iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

Hi,

drivers/iio/magnetometer/Kconfig

Quoted 26 lines · Ondřej Jirman

From: Icenowy Zheng <[email protected]>

AF8133J is a simple I2C-connected magnetometer, without interrupts.

Add a simple IIO driver for it.

---
 drivers/iio/magnetometer/Kconfig   |  12 +
 drivers/iio/magnetometer/Makefile  |   1 +
 drivers/iio/magnetometer/af8133j.c | 524 +++++++++++++++++++++++++++++
 3 files changed, 537 insertions(+)
 create mode 100644 drivers/iio/magnetometer/af8133j.c
drivers/iio/magnetometer/Kconfig +0 -0
index 38532d840f2a..cd2917d71904 100644
@@ -6,6 +6,18 @@

I've tested v4 driver again on 6.8-rc5. Works like before.

Signature · 2 lines
Best regards,
Andrey Skvortsov
maintainer
#5 lore raw

Re: [PATCH v4 3/4] iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

In reply to Ondřej Jirman
Quoted 8 lines · On Thu, 22 Feb 2024 02:13:37 +0100 Ondřej Jirman <[email protected]> wrote:

From: Icenowy Zheng <[email protected]>

AF8133J is a simple I2C-connected magnetometer, without interrupts.

Add a simple IIO driver for it.

Check patch correct moaned that Icenowy is the author (from:) so doesn't need a co-developed.

Quoted 6 lines

Hi.

A few really minor things noticed during a final review. I'll tweak them whilst applying. Diff is

drivers/iio/magnetometer/af8133j.c +8 -4
index c75d545e152b..40657a08ce37 100644
@@ -40,6 +40,10 @@ static const char * const af8133j_supply_names[] = {
 struct af8133j_data {
        struct i2c_client *client;
        struct regmap *regmap;
+       /*
+        * Protect device internal state between starting a measurement
+        * and reading the result.
+        */
        struct mutex mutex;
        struct iio_mount_matrix orientation;
 
@@ -107,8 +111,8 @@ static int af8133j_product_check(struct af8133j_data *data)
        }
 
        if (val != AF8133J_REG_PCODE_VAL) {
-               dev_err(dev, "Invalid product code (0x%02x)\n", val);
-               return -EINVAL;
+               dev_warn(dev, "Invalid product code (0x%02x)\n", val);
+               return 0; /* Allow unknown ID so fallback compatibles work */
        }
 
        return 0;
@@ -237,8 +241,8 @@ static int af8133j_read_measurement(struct af8133j_data *data, __le16 buf[3])
 }
 
 static const int af8133j_scales[][2] = {
-       [0] = { 0, 366210 }, // 12 gauss
-       [1] = { 0, 671386 }, // 22 gauss
+       [0] = { 0, 366210 }, /* 12 gauss */
+       [1] = { 0, 671386 }, /* 22 gauss */
 };
 
 

drivers/iio/magnetometer/af8133j.c

Quoted 11 lines
drivers/iio/magnetometer/af8133j.c new file +5 -0
new file mode 100644
index 000000000000..c75d545e152b
 
+
+struct af8133j_data {
+	struct i2c_client *client;
+	struct regmap *regmap;
+	struct mutex mutex;

I thought checkpatch still moaned that every lock should have documentation. I guess not. However it's still a nice to have.

Here it seems this is about protecting device state between triggering a measurement and getting the reading.

Quoted 14 lines
drivers/iio/magnetometer/af8133j.c new file +13 -0
+	struct iio_mount_matrix orientation;
+
+	struct gpio_desc *reset_gpiod;
+	struct regulator_bulk_data supplies[ARRAY_SIZE(af8133j_supply_names)];
+
+	u8 range;
+};
 
+
+static int af8133j_product_check(struct af8133j_data *data)
+{
+	struct device *dev = &data->client->dev;
+	unsigned int val;
+	int ret;

drivers/iio/magnetometer/af8133j.c

Quoted 10 lines
drivers/iio/magnetometer/af8133j.c new file +10 -0
+
+	ret = regmap_read(data->regmap, AF8133J_REG_PCODE, &val);
+	if (ret) {
+		dev_err(dev, "Error reading product code (%d)\n", ret);
+		return ret;
+	}
+
+	if (val != AF8133J_REG_PCODE_VAL) {
+		dev_err(dev, "Invalid product code (0x%02x)\n", val);
+		return -EINVAL;

This should be warn only and we should carry on regardless. The reason behind this is to support fallback compatible values in DT to potentially enable a newer device to be supported on an older kernel.

Many IIO drivers do this wrong as my understanding on what counted on 'compatible' used to be different. Long discussions on this with the DT maintainers led me to accept that letting ID checks fail was fine, but
that a message was appropriate. Often a fail here actually means no device.
We have some exceptions to this rule for devices where we know the same FW ids are in use in the wild for devices supported by different Linux drivers - but those are thankfully rare!

drivers/iio/magnetometer/af8133j.c

Quoted 5 lines
drivers/iio/magnetometer/af8133j.c new file +5 -0
+	}
+
+	return 0;
+}
+

}

drivers/iio/magnetometer/af8133j.c

Quoted 3 lines
drivers/iio/magnetometer/af8133j.c new file +3 -0
+static const int af8133j_scales[][2] = {
+	[0] = { 0, 366210 }, // 12 gauss
+	[1] = { 0, 671386 }, // 22 gauss

Trivial so I'll fix it up: IIO comments are /* */
not C++ style (with exception of the SPDX stuff that needs to be).

Quoted 1 line

+};

author maintainer
#6 lore raw

Re: [PATCH v4 3/4] iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

Hello Jonathan,

Quoted 21 lines · Jonathan Cameron
Quoted 8 lines · On Thu, 22 Feb 2024 02:13:37 +0100 Ondřej Jirman <[email protected]> wrote:

From: Icenowy Zheng <[email protected]>

AF8133J is a simple I2C-connected magnetometer, without interrupts.

Add a simple IIO driver for it.

Check patch correct moaned that Icenowy is the author (from:) so doesn't need a co-developed.

Quoted 6 lines
Quoted 4 lines

Hi.

A few really minor things noticed during a final review. I'll tweak them whilst applying. Diff is

Thank you very much for finishing touches.

Quoted 20 lines
Quoted 5 lines
(quoted diff) +5 -0
+static int af8133j_product_check(struct af8133j_data *data)
+{
+	struct device *dev = &data->client->dev;
+	unsigned int val;
+	int ret;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	ret = regmap_read(data->regmap, AF8133J_REG_PCODE, &val);
+	if (ret) {
+		dev_err(dev, "Error reading product code (%d)\n", ret);
+		return ret;
+	}
+
+	if (val != AF8133J_REG_PCODE_VAL) {
+		dev_err(dev, "Invalid product code (0x%02x)\n", val);
+		return -EINVAL;

This should be warn only and we should carry on regardless. The reason behind this is to support fallback compatible values in DT to potentially enable a newer device to be supported on an older kernel.

Quoted 7 lines

Many IIO drivers do this wrong as my understanding on what counted on 'compatible' used to be different. Long discussions on this with the DT maintainers led me to accept that letting ID checks fail was fine, but
that a message was appropriate. Often a fail here actually means no device.
We have some exceptions to this rule for devices where we know the same FW ids are in use in the wild for devices supported by different Linux drivers - but those are thankfully rare!

Makes sense. If newer device variant has the same register meanings, but just a different ID register, this way it can be supported without driver modifications. I'll keep it in mind when doing ID checks in other drivers.

Thanks for all your detailed notes during the review. I learned a few new subtleties.

Kind regards,

	o.
Quoted 16 lines

Review

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

}

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const int af8133j_scales[][2] = {
+	[0] = { 0, 366210 }, // 12 gauss
+	[1] = { 0, 671386 }, // 22 gauss

Trivial so I'll fix it up: IIO comments are /* */
not C++ style (with exception of the SPDX stuff that needs to be).

Quoted 1 line

+};

maintainer
#7 lore raw

Re: [PATCH v4 3/4] iio: magnetometer: add a driver for Voltafield AF8133J magnetometer

In reply to Ondřej Jirman
Quoted 6 lines · On Thu, 22 Feb 2024 02:13:37 +0100 Ondřej Jirman <[email protected]> wrote:

From: Icenowy Zheng <[email protected]>

AF8133J is a simple I2C-connected magnetometer, without interrupts.

Add a simple IIO driver for it.

Quoted 8 lines

One additional tweak from my local build tests.

static

Quoted 4 lines
drivers/iio/magnetometer/af8133j.c new file +4 -0
+const struct dev_pm_ops af8133j_pm_ops = {
+	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
+	RUNTIME_PM_OPS(af8133j_runtime_suspend, af8133j_runtime_resume, NULL)
+};
author maintainer
#8 lore raw

PATCH v4 4/4 MAINTAINERS: Add an entry for AF8133J driver

In reply to Ondřej Jirman

From: Ondrej Jirman <[email protected]>

As I am submitting the driver and have the device to test. I'll maintain the driver.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 9ed4d3868539..e027d3ec3f8c 100644
@@ -579,6 +579,12 @@ F:	drivers/iio/accel/adxl372.c
 F:	drivers/iio/accel/adxl372_i2c.c
 F:	drivers/iio/accel/adxl372_spi.c
 
+AF8133J THREE-AXIS MAGNETOMETER DRIVER
+M:	Ondřej Jirman <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/magnetometer/voltafield,af8133j.yaml
+F:	drivers/iio/magnetometer/af8133j.c
+
 AF9013 MEDIA DRIVER
 L:	[email protected]
 S:	Orphan
Signature · 1 line
2.43.0