Skip to contentSkip to conversation

add ti tmag5273 driver

PATCH Superseded Gerald Loacker 2 participants 9 messages Nov 29, 2022 – Nov 30, 2022

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Gerald Loacker

author maintainer
#0 lore raw

v4 cover add ti tmag5273 driver

Hi all,

This patch set adds support for the TI TMAG5273 Low-Power Linear 3D Hall- Effect Sensor. Additionally to temperature and magnetic X, Y and Z-axes the angle and magnitude are reported. The sensor is operating in continuous measurement mode and changes to sleep mode if not used for 5 seconds.

Tests were done on a ROCK3 Model A board using the TMAG5273 evaluation module.

Changes in v4:
- Add only struct for IIO_VAL_INT_PLUS_MICRO iio type to iio.h.
- Simplified tmag5273_write_scale()
- Removed unnecessary '!= 0'
- Fixed regmap max_register
- Simplified tmag5273_read_device_property()
- Fixed some line breaks.

Changes in v3:
- Added structs for iio types to iio.h. Using these structs for iio type arrays such as IIO_AVAIL_LIST makes the code more readable than just using (int *). It was suggested by Andy Shevchenko to move these structs to the iio headers to avoid different approaches.
- dt-bindings: dropped quotes from strings
- Added include <linux/bitfield.h>
| Reported-by: kernel test robot <[email protected]>
- Added include <linux/bits.h>
- Removed <asm/unaligned.h>
- Added missing "static const" for tmag5273_avg_table
- Documented Device ID
- Fixed index of tmag5273_scale definition
- Clarify TMAG5273_MAG_CH_EN_X_Y_Z as an index
- Removed unnecessary print
- Introduced tmag5273_write_scale() and tmag5273_write_osr() helper functions
- Use of match_string()
- Format

Changes in v2:
Thanks to Krzysztof, Andy and Jonathan for your detailed review and explanations on the first version. This patch includes all your suggestions and some additional cleanup in the probe function.

Gerald Loacker (3):
iio: add struct declaration for iio types
dt-bindings: iio: magnetometer: add ti tmag5273 documentation file
iio: magnetometer: add ti tmag5273 driver

 .../iio/magnetometer/ti,tmag5273.yaml         |  75 ++
 MAINTAINERS                                   |   7 +
 drivers/iio/magnetometer/Kconfig              |  12 +
 drivers/iio/magnetometer/Makefile             |   2 +
 drivers/iio/magnetometer/tmag5273.c           | 731 ++++++++++++++++++
 include/linux/iio/iio.h                       |   5 +
 6 files changed, 832 insertions(+)
 create mode 100644 .../bindings/iio/magnetometer/ti,tmag5273.yaml
 create mode 100644 drivers/iio/magnetometer/tmag5273.c
Signature · 1 line
2.37.2
author maintainer
#1 lore raw

PATCH v4 1/3 iio: add struct declaration for iio types

Add struct for iio type arrays such as IIO_AVAIL_LIST which can be used instead of int arrays.

---
Changes in v4:
- Add only struct for IIO_VAL_INT_PLUS_MICRO iio type.

 include/linux/iio/iio.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/iio/iio.h +5 -0
index f0ec8a5e5a7a..e5101bef50c5 100644
@@ -381,6 +381,11 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev);
 
 #define INDIO_MAX_RAW_ELEMENTS		4
 
+struct iio_val_int_plus_micro {
+	int integer;
+	int micro;
+};
+
 struct iio_trigger; /* forward declaration */
 
 /**
Signature · 1 line
2.37.2
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] iio: add struct declaration for iio types

Quoted 2 lines · Gerald Loacker

Add struct for iio type arrays such as IIO_AVAIL_LIST which can be used instead of int arrays.

Quoted 27 lines

---
Changes in v4:
- Add only struct for IIO_VAL_INT_PLUS_MICRO iio type.

 include/linux/iio/iio.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/iio/iio.h +5 -0
index f0ec8a5e5a7a..e5101bef50c5 100644
@@ -381,6 +381,11 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev);
 
 #define INDIO_MAX_RAW_ELEMENTS		4
 
+struct iio_val_int_plus_micro {
+	int integer;
+	int micro;
+};
+
 struct iio_trigger; /* forward declaration */
 
 /**
Signature · 1 line
2.37.2
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#3 lore raw

PATCH v4 2/3 dt-bindings: iio: magnetometer: add ti tmag5273 documentation file

In reply to Gerald Loacker

Add bindings for TI TMAG5273.

---
Changes in v4:
- no changes

Changes in v3:
- dropped quotes from strings

Changes in v2:
- Removed nodename
- Changed angle-enable to angle-measurement and used strings
- Added interrupts
- Removed vcc-supply from required properties
- Changed i2c-0 to i2c in examples

 .../iio/magnetometer/ti,tmag5273.yaml         | 75 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/ti,tmag5273.yaml
Documentation/devicetree/bindings/iio/magnetometer/ti,tmag5273.yaml new file +75 -0
new file mode 100644
index 000000000000..121d540b7b6e
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/magnetometer/ti,tmag5273.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor
+
+maintainers:
+  - Gerald Loacker <[email protected]>
+
+description:
+  The TI TMAG5273 is a low-power linear 3D Hall-effect sensor. This device
+  integrates three independent Hall-effect sensors in the X, Y, and Z axes.
+  The device has an integrated temperature sensor available. The TMAG5273
+  can be configured through the I2C interface to enable any combination of
+  magnetic axes and temperature measurements. An integrated angle calculation
+  engine (CORDIC) provides full 360° angular position information for both
+  on-axis and off-axis angle measurement topologies. The angle calculation is
+  performed using two user-selected magnetic axes.
+
+properties:
+  compatible:
+    const: ti,tmag5273
+
+  reg:
+    maxItems: 1
+
+  "#io-channel-cells":
+    const: 1
+
+  ti,angle-measurement:
+    $ref: /schemas/types.yaml#/definitions/string
+    description:
+      Enables angle measurement in the selected plane.
+      If not specified, "x-y" will be anables as default.
+    enum:
+      - off
+      - x-y
+      - y-z
+      - x-z
+
+  vcc-supply:
+    description:
+      A regulator providing 1.7 V to 3.6 V supply voltage on the VCC pin,
+      typically 3.3 V.
+
+  interrupts:
+    description:
+      The low active interrupt can be configured to be fixed width or latched.
+      Interrupt events can be configured to be generated from magnetic
+      thresholds or when a conversion is completed.
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        magnetometer@35 {
+            compatible = "ti,tmag5273";
+            reg = <0x35>;
+            #io-channel-cells = <1>;
+            ti,angle-measurement = "x-z";
+            vcc-supply = <&vcc3v3>;
+        };
+    };
+...
MAINTAINERS +6 -0
index cf0f18502372..ea7acec52f8b 100644
@@ -20613,6 +20613,12 @@ L:	[email protected] (moderated for non-subscribers)
 S:	Odd Fixes
 F:	sound/soc/codecs/tas571x*
 
+TI TMAG5273 MAGNETOMETER DRIVER
+M:	Gerald Loacker <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/magnetometer/ti,tmag5273.yaml
+
 TI TRF7970A NFC DRIVER
 M:	Mark Greer <[email protected]>
 L:	[email protected]
Signature · 1 line
2.37.2
author maintainer
#4 lore raw

PATCH v4 3/3 iio: magnetometer: add ti tmag5273 driver

In reply to Gerald Loacker

Add support for TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor. Additionally to temperature and magnetic X, Y and Z-axes the angle and magnitude are reported.
The sensor is operating in continuous measurement mode and changes to sleep mode if not used for 5 seconds.

Datasheet: https://www.ti.com/lit/gpn/tmag5273

---
Changes in v4:
- Renamed struct iio_val_int_plus_micro members
- Simplified tmag5273_write_scale()
- Removed unnecessary '!= 0'
- Fixed regmap max_register
- Simplified tmag5273_read_device_property()
- Fixed some line breaks

Changes in v3:
- Added include <linux/bitfield.h>
| Reported-by: kernel test robot <[email protected]>
- Added include <linux/bits.h>
- Removed <asm/unaligned.h>
- Added missing "static const" for tmag5273_avg_table
- Documented Device ID
- Fixed index of tmag5273_scale definition
- Clarify TMAG5273_MAG_CH_EN_X_Y_Z as an index
- Removed unnecessary print
- Introduced tmag5273_write_scale() and tmag5273_write_osr() helper functions
- Use of match_string()
- Format

Changes in v2:
- Implemented suggestions from review and cleaned up probe function. This results in changes all over the tmag5273.c code.

 MAINTAINERS                         |   1 +
 drivers/iio/magnetometer/Kconfig    |  12 +
 drivers/iio/magnetometer/Makefile   |   2 +
 drivers/iio/magnetometer/tmag5273.c | 731 ++++++++++++++++++++++++++++
 4 files changed, 746 insertions(+)
 create mode 100644 drivers/iio/magnetometer/tmag5273.c
MAINTAINERS +1 -0
index ea7acec52f8b..9d20b5780051 100644
@@ -20618,6 +20618,7 @@ M:	Gerald Loacker <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/magnetometer/ti,tmag5273.yaml
+F:	drivers/iio/magnetometer/tmag5273.c
 
 TI TRF7970A NFC DRIVER
 M:	Mark Greer <[email protected]>
drivers/iio/magnetometer/Kconfig +12 -0
index b91fc5e6a26e..467819335588 100644
@@ -208,6 +208,18 @@ config SENSORS_RM3100_SPI
 	  To compile this driver as a module, choose M here: the module
 	  will be called rm3100-spi.
 
+config TI_TMAG5273
+	tristate "TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  Say Y here to add support for the TI TMAG5273 Low-Power
+	  Linear 3D 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 tmag5273.
+
 config YAMAHA_YAS530
 	tristate "Yamaha YAS530 family of 3-Axis Magnetometers (I2C)"
 	depends on I2C
drivers/iio/magnetometer/Makefile +2 -0
index b9f45b7fafc3..b1c784ea71c8 100644
@@ -29,4 +29,6 @@ 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_TI_TMAG5273)		+= tmag5273.o
+
 obj-$(CONFIG_YAMAHA_YAS530)		+= yamaha-yas530.o
drivers/iio/magnetometer/tmag5273.c new file +731 -0

731 lines not on this page · raw message

Signature · 1 line
2.37.2
#5 lore raw

Re: [PATCH v4 3/3] iio: magnetometer: add ti tmag5273 driver

Quoted 5 lines · Gerald Loacker

Add support for TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor. Additionally to temperature and magnetic X, Y and Z-axes the angle and magnitude are reported.
The sensor is operating in continuous measurement mode and changes to sleep mode if not used for 5 seconds.

...

Quoted 12 lines
drivers/iio/magnetometer/tmag5273.c new file +12 -0
+static int tmag5273_get_measure(struct tmag5273_data *data, s16 *t, s16 *x,
+				s16 *y, s16 *z, u16 *angle, u16 *magnitude)
+{
+	unsigned int status, val;
+	__be16 reg_data[4];
+	int ret;
+
+	mutex_lock(&data->lock);
+
+	/*
+	 * Max. conversion time is 2425 us in 32x averaging mode for all three
+	 * channels. Since we are in continuous measurement mode, a measurement

drivers/iio/magnetometer/tmag5273.c line 177

Quoted 10 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+	 * may already be there, so poll for completed measurement with
+	 * timeout.
+	 */
+	ret = regmap_read_poll_timeout(data->map, TMAG5273_CONV_STATUS, status,
+				       status & TMAG5273_CONV_STATUS_COMPLETE,
+				       100, 10000);
+	if (ret) {
 
+		dev_err_probe(data->dev, ret,
+			      "timeout waiting for measurement\n");

Is it called from ->probe()? I don't think so...

Quoted 32 lines
drivers/iio/magnetometer/tmag5273.c new file +32 -0
+		goto out_unlock;
+	}
+
+	ret = regmap_bulk_read(data->map, TMAG5273_T_MSB_RESULT, reg_data,
+			       sizeof(reg_data));
+	if (ret)
+		goto out_unlock;
+	*t = be16_to_cpu(reg_data[0]);
+	*x = be16_to_cpu(reg_data[1]);
+	*y = be16_to_cpu(reg_data[2]);
+	*z = be16_to_cpu(reg_data[3]);
+
+	ret = regmap_bulk_read(data->map, TMAG5273_ANGLE_RESULT_MSB,
+			       &reg_data[0], sizeof(reg_data[0]));
+	if (ret)
+		goto out_unlock;
+	/*
+	 * angle has 9 bits integer value and 4 bits fractional part
+	 * 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0
+	 * 0  0  0  a  a  a  a  a  a  a  a  a  f  f  f  f
+	 */
+	*angle = be16_to_cpu(reg_data[0]);
+
+	ret = regmap_read(data->map, TMAG5273_MAGNITUDE_RESULT, &val);
+	if (ret < 0)
+		goto out_unlock;
+	*magnitude = val;
+
+out_unlock:
+	mutex_unlock(&data->lock);
+	return ret;
+}

...

drivers/iio/magnetometer/tmag5273.c line 476

Quoted 5 lines
drivers/iio/magnetometer/tmag5273.c new file +5 -0
+static const struct iio_info tmag5273_info = {
+	.read_avail = &tmag5273_read_avail,
+	.read_raw = &tmag5273_read_raw,
+	.write_raw = &tmag5273_write_raw,
+};

Functions when being assigned are already pointers, no?

...

drivers/iio/magnetometer/tmag5273.c line 511

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	ret = match_string(tmag5273_angle_names,
+			   ARRAY_SIZE(tmag5273_angle_names), str);
+	if (ret < 0)
+		dev_warn(dev, "unexpected read angle-measurement property: %s\n", str);
		dev_warn(dev, "unexpected value in angle-measurement property: %s\n", str);

?

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+	else
+		data->angle_measurement = ret;

...

drivers/iio/magnetometer/tmag5273.c line 573

Quoted 1 line
drivers/iio/magnetometer/tmag5273.c new file +1 -0
+		snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version);

Thinking more about this format, perhaps

		snprintf(data->name, sizeof(data->name), "tmag5273x-v%1u", data->version);

?

...

drivers/iio/magnetometer/tmag5273.c line 693

Quoted 10 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+static int tmag5273_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tmag5273_data *data = iio_priv(indio_dev);
+	int ret;
 
+	/*
+	 * Time to go to stand-by mode from sleep mode is 50us
+	 * typically. During this time no I2C access is possible.
+	 */

Shouldn't be this comment closer to usleep_range()?

Quoted 8 lines
drivers/iio/magnetometer/tmag5273.c new file +8 -0
+	tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
+	usleep_range(80, 200);
+	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
+	if (ret)
+		dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret));
+
+	return ret;
+}

...

drivers/iio/magnetometer/tmag5273.c line 704

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops, tmag5273_runtime_suspend,
+				 tmag5273_runtime_resume, NULL);

I would logically split it like:

static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops,

				 tmag5273_runtime_suspend, tmag5273_runtime_resume, NULL);

or like:

static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops,

				 tmag5273_runtime_suspend, tmag5273_runtime_resume,
				 NULL);

or like:

static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops,

				 tmag5273_runtime_suspend,
				 tmag5273_runtime_resume,
				 NULL);

Depending on your preferences.

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

Re: [PATCH v4 3/3] iio: magnetometer: add ti tmag5273 driver

Quoted 1 line · Andy Shevchenko

On Tue, Nov 29, 2022 at 07:45:40AM +0100, Gerald Loacker wrote:

...

Quoted 11 lines

drivers/iio/magnetometer/tmag5273.c line 511

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	ret = match_string(tmag5273_angle_names,
+			   ARRAY_SIZE(tmag5273_angle_names), str);
+	if (ret < 0)
+		dev_warn(dev, "unexpected read angle-measurement property: %s\n", str);
		dev_warn(dev, "unexpected value in angle-measurement property: %s\n", str);

?

drivers/iio/magnetometer/tmag5273.c line 513

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+	else
+		data->angle_measurement = ret;

Actually remembering Rob's statement that Linux should not validate device tree, this can be written rather as:

	ret = match_string(tmag5273_angle_names,
			   ARRAY_SIZE(tmag5273_angle_names), str);
	if (ret >= 0)
		data->angle_measurement = ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#7 lore raw

Re: [PATCH v4 3/3] iio: magnetometer: add ti tmag5273 driver

In reply to Andy Shevchenko

Am 29.11.2022 um 17:37 schrieb Andy Shevchenko:

Quoted 9 lines · Gerald Loacker
Quoted 5 lines

Add support for TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor. Additionally to temperature and magnetic X, Y and Z-axes the angle and magnitude are reported.
The sensor is operating in continuous measurement mode and changes to sleep mode if not used for 5 seconds.

...

Quoted 7 lines

drivers/iio/magnetometer/tmag5273.c line 573

Quoted 1 line
drivers/iio/magnetometer/tmag5273.c new file +1 -0
+		snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version);

Thinking more about this format, perhaps

		snprintf(data->name, sizeof(data->name), "tmag5273x-v%1u", data->version);

?

I'd prefer to keep this as it's related to the orderable part number, e.g. TMAG5273A1 / TMAG5273A2.

Quoted 9 lines

...

Quoted 5 lines
drivers/iio/magnetometer/tmag5273.c new file +5 -0
+static int tmag5273_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tmag5273_data *data = iio_priv(indio_dev);
+	int ret;
Quoted 7 lines

drivers/iio/magnetometer/tmag5273.c line 557

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	/*
+	 * Time to go to stand-by mode from sleep mode is 50us
+	 * typically. During this time no I2C access is possible.
+	 */

Shouldn't be this comment closer to usleep_range()?

Ok, I'll clarify this and also introduce a wake-up function, as this is used two times in a similar way.

Quoted 8 lines

drivers/iio/magnetometer/tmag5273.c line 701

Quoted 8 lines
drivers/iio/magnetometer/tmag5273.c new file +8 -0
+	tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
+	usleep_range(80, 200);
+	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
+	if (ret)
+		dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret));
+
+	return ret;
+}

Regards,
Gerald

#8 lore raw

Re: [PATCH v4 3/3] iio: magnetometer: add ti tmag5273 driver

Quoted 2 lines · Gerald Loacker

Am 29.11.2022 um 17:37 schrieb Andy Shevchenko:

Quoted 1 line · Gerald Loacker

...

Quoted 10 lines
Quoted 7 lines

drivers/iio/magnetometer/tmag5273.c line 573

Quoted 1 line
drivers/iio/magnetometer/tmag5273.c new file +1 -0
+		snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version);

Thinking more about this format, perhaps

		snprintf(data->name, sizeof(data->name), "tmag5273x-v%1u", data->version);

?

I'd prefer to keep this as it's related to the orderable part number, e.g. TMAG5273A1 / TMAG5273A2.

I see, can you add a comment above to clarify the chosen format?

Signature · 2 lines
With Best Regards,
Andy Shevchenko