Skip to contentSkip to conversation

add ti tmag5273 driver

PATCH New Gerald Loacker 4 participants 14 messages Nov 25, 2022 – Dec 3, 2022

Subsystems (3)
lore
Share & tools
lore

Gerald Loacker

author maintainer
#0 lore raw

v2 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 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 declarations 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           | 736 ++++++++++++++++++
 include/linux/iio/iio.h                       |  15 +
 6 files changed, 847 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 v3 1/3 iio: add struct declarations for iio types

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

---
 include/linux/iio/iio.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/iio/iio.h +15 -0
index f0ec8a5e5a7a..eaf6727445a6 100644
@@ -383,6 +383,21 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev);
 
 struct iio_trigger; /* forward declaration */
 
+struct iio_val_int_plus_micro {
+	int val_int;
+	int val_micro;
+};
+
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;
+	int val_micro_db;
+};
+
 /**
  * struct iio_info - constant information about device
  * @event_attrs:	event control attributes
Signature · 1 line
2.37.2
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Quoted 2 lines · Gerald Loacker

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

And thank you for doing this!

(one comment below)

Quoted 15 lines
---
 include/linux/iio/iio.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/iio/iio.h +2 -0
index f0ec8a5e5a7a..eaf6727445a6 100644
@@ -383,6 +383,21 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev);
 
 struct iio_trigger; /* forward declaration */
 
+struct iio_val_int_plus_micro {
+	int val_int;

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 397

Quoted 10 lines
include/linux/iio/iio.h +10 -0
+	int val_micro;
+};
+
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;

int val_int_db; ?

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 399

Quoted 2 lines
include/linux/iio/iio.h +2 -0
+	int val_micro_db;
+};

Actually why can't we simply do

typedef iio_val_int_plus_micro_db iio_val_int_plus_micro;

?

Quoted 6 lines
include/linux/iio/iio.h +0 -0
 /**
  * struct iio_info - constant information about device
  * @event_attrs:	event control attributes
Signature · 1 line
2.37.2
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Quoted 1 line · Andy Shevchenko

On Fri, Nov 25, 2022 at 09:35:24AM +0100, Gerald Loacker wrote:

...

Quoted 4 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 389

Quoted 4 lines
include/linux/iio/iio.h +4 -0
+struct iio_val_int_plus_micro {
+	int val_int;
+	int val_micro;
+};

Thinking more about naming, why not drop val_ completely?

	int integer;
	int micro;

?

Quoted 18 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 397

Quoted 7 lines
include/linux/iio/iio.h +7 -0
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;

int val_int_db; ?

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 399

Quoted 2 lines
include/linux/iio/iio.h +2 -0
+	int val_micro_db;
+};

Actually why can't we simply do

typedef iio_val_int_plus_micro_db iio_val_int_plus_micro;

?

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

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

Quoted 12 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

...

Quoted 4 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 389

Quoted 4 lines
include/linux/iio/iio.h +4 -0
+struct iio_val_int_plus_micro {
+	int val_int;
+	int val_micro;
+};

Thinking more about naming, why not drop val_ completely?

Quoted 5 lines
	int integer;
	int micro;

?

Yes, this sounds good to me. I think of adding only

	typedef struct {
		int integer;
		int micro;
	} iio_val_int_plus_micro;

for now, and one can add similar structures when needed, like

	typedef struct {
		int integer;
		int nano;
	} iio_val_int_plus_nano;

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

If you think it's better to add them all, I can do that, of course.

Quoted 19 lines
Quoted 18 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 397

Quoted 7 lines
include/linux/iio/iio.h +7 -0
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;

int val_int_db; ?

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 399

Quoted 2 lines
include/linux/iio/iio.h +2 -0
+	int val_micro_db;
+};

Actually why can't we simply do

typedef iio_val_int_plus_micro_db iio_val_int_plus_micro;

?

#5 lore raw

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Quoted 3 lines · Gerald Loacker

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

...

Quoted 19 lines
Quoted 11 lines
Quoted 4 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 389

Quoted 4 lines
include/linux/iio/iio.h +4 -0
+struct iio_val_int_plus_micro {
+	int val_int;
+	int val_micro;
+};

Thinking more about naming, why not drop val_ completely?

	int integer;
	int micro;

?

Yes, this sounds good to me. I think of adding only

	typedef struct {
		int integer;
		int micro;
	} iio_val_int_plus_micro;
Quoted 6 lines

for now, and one can add similar structures when needed, like

	typedef struct {
		int integer;
		int nano;
	} iio_val_int_plus_nano;

It's a rule to use _t for typedef:s in the kernel. That's why
I suggested to leave struct definition and only typedef the same structures (existing) to new names (if needed).

Quoted 3 lines

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

This is better as explained above.

Quoted 20 lines

If you think it's better to add them all, I can do that, of course.

Quoted 18 lines
Quoted 18 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 397

Quoted 7 lines
include/linux/iio/iio.h +7 -0
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;

int val_int_db; ?

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 399

Quoted 2 lines
include/linux/iio/iio.h +2 -0
+	int val_micro_db;
+};

Actually why can't we simply do

typedef iio_val_int_plus_micro_db iio_val_int_plus_micro;

?

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

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Hi Gerald, Andy,

Quoted 17 lines · Andy Shevchenko
Quoted 3 lines · Gerald Loacker

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

...

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 389

Quoted 4 lines
include/linux/iio/iio.h +4 -0
+struct iio_val_int_plus_micro {
+	int val_int;
+	int val_micro;
+};

Thinking more about naming, why not drop val_ completely?

	int integer;
	int micro;
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

?

Yes, this sounds good to me. I think of adding only

	typedef struct {
		int integer;
		int micro;
	} iio_val_int_plus_micro;

I think we actually want

struct iio_val_int_plus_micro {
	int integer;
	int micro;
};

here, right?

Quoted 6 lines
Quoted 6 lines

for now, and one can add similar structures when needed, like

	typedef struct {
		int integer;
		int nano;
	} iio_val_int_plus_nano;

+1 for introducing things when they are actually used.

Quoted 3 lines

It's a rule to use _t for typedef:s in the kernel. That's why
I suggested to leave struct definition and only typedef the same structures (existing) to new names (if needed).

Andy, excuse our ignorance but we are not sure how this typedef approach is supposed to look like...

Quoted 3 lines
Quoted 3 lines

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

... because

#include <stdio.h>

struct iio_val_int_plus_micro {
	int integer;
	int micro;
};

typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

int main()
{
struct iio_val_int_plus_micro a = { .integer = 100, .micro = 10, }; struct iio_val_int_plus_micro_db b = { .integer = 20, .micro = 10, }; return 0;
}

won't compile.

Quoted 3 lines

This is better as explained above.

Quoted 1 line

If you think it's better to add them all, I can do that, of course.

Anyway, seeing that only struct iio_val_int_plus_micro is used at the moment, I believe the best path forward is to introduce only this struct and move on.

Best regards,
Michael

Quoted 19 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 397

Quoted 7 lines
include/linux/iio/iio.h +7 -0
+struct iio_val_int_plus_nano {
+	int val_int;
+	int val_nano;
+};
+
+struct iio_val_int_plus_micro_db {
+	int val_int;

int val_int_db; ?

include/linux/iio/iio.h s64 iio_get_time_ns(const struct iio_dev *indio_dev); · line 399

Quoted 2 lines
include/linux/iio/iio.h +2 -0
+	int val_micro_db;
+};

Actually why can't we simply do

typedef iio_val_int_plus_micro_db iio_val_int_plus_micro;

?

#7 lore raw

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Quoted 3 lines · Michael Riesch
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

...

Quoted 22 lines
Quoted 3 lines

It's a rule to use _t for typedef:s in the kernel. That's why
I suggested to leave struct definition and only typedef the same structures (existing) to new names (if needed).

Andy, excuse our ignorance but we are not sure how this typedef approach is supposed to look like...

Quoted 3 lines
Quoted 3 lines

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

... because

#include <stdio.h>

struct iio_val_int_plus_micro {
	int integer;
	int micro;
};

typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

Quoted 8 lines

int main()
{
struct iio_val_int_plus_micro a = { .integer = 100, .micro = 10, }; struct iio_val_int_plus_micro_db b = { .integer = 20, .micro = 10, }; return 0;
}

won't compile.

I see. Thanks for pointing this out.

Then the question is why do we need the two same structures with different names?

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

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Hi Andy,

Quoted 36 lines · Andy Shevchenko
Quoted 3 lines · Michael Riesch
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

...

Quoted 29 lines
Quoted 3 lines

It's a rule to use _t for typedef:s in the kernel. That's why
I suggested to leave struct definition and only typedef the same structures (existing) to new names (if needed).

Andy, excuse our ignorance but we are not sure how this typedef approach is supposed to look like...

Quoted 3 lines
Quoted 3 lines

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

... because

#include <stdio.h>

struct iio_val_int_plus_micro {
	int integer;
	int micro;
};

typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

int main()
{
struct iio_val_int_plus_micro a = { .integer = 100, .micro = 10, }; struct iio_val_int_plus_micro_db b = { .integer = 20, .micro = 10, }; return 0;
}

Quoted 6 lines
Quoted 1 line

won't compile.

I see. Thanks for pointing this out.

Then the question is why do we need the two same structures with different names?

Most probably we don't need "struct iio_val_int_plus_micro_db" at all since IIO_VAL_INT_PLUS_MICRO_DB and IIO_VAL_INT_PLUS_MICRO get the same treatment in industrialio-core.c. At least it should not be introduced in the scope of this series. In the end this is up to whoever writes the first driver using the common data structures and IIO_VAL_INT_PLUS_MICRO_DB.

Best regards,
Michael

maintainer
#9 lore raw

Re: [PATCH v3 1/3] iio: add struct declarations for iio types

Quoted 43 lines · On Mon, 28 Nov 2022 15:26:51 +0100 Michael Riesch…

Hi Andy,

Quoted 40 lines · Andy Shevchenko
Quoted 3 lines · Michael Riesch
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Gerald Loacker

Am 25.11.2022 um 12:01 schrieb Andy Shevchenko:

...

Quoted 22 lines
Quoted 3 lines

It's a rule to use _t for typedef:s in the kernel. That's why
I suggested to leave struct definition and only typedef the same structures (existing) to new names (if needed).

Andy, excuse our ignorance but we are not sure how this typedef approach is supposed to look like...

Quoted 3 lines
Quoted 3 lines

or

	typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;  

... because

#include <stdio.h>

struct iio_val_int_plus_micro {
	int integer;
	int micro;
};

typedef iio_val_int_plus_micro iio_val_int_plus_micro_db;

Quoted 8 lines

int main()
{
struct iio_val_int_plus_micro a = { .integer = 100, .micro = 10, }; struct iio_val_int_plus_micro_db b = { .integer = 20, .micro = 10, }; return 0;
}

won't compile.

I see. Thanks for pointing this out.

Quoted 8 lines
Quoted 2 lines

Then the question is why do we need the two same structures with different names?

Most probably we don't need "struct iio_val_int_plus_micro_db" at all since IIO_VAL_INT_PLUS_MICRO_DB and IIO_VAL_INT_PLUS_MICRO get the same treatment in industrialio-core.c. At least it should not be introduced in the scope of this series. In the end this is up to whoever writes the first driver using the common data structures and IIO_VAL_INT_PLUS_MICRO_DB.

They get same treatment today because we don't attempt to deal with IIO_VAL_INT_PLUS_MICRO_DB in conjunction with any of the analog circuit type front ends yet. Mind you, even though the handling in iio-rescale.c will be different if anyone ever adds support for the DB form (I shudder at the maths of combining this with other scale factors), I don't see the possibility meaning we need a different structure.

Jonathan

Quoted 4 lines

Best regards,
Michael

author maintainer
#10 lore raw

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

In reply to Gerald Loacker

Add bindings for TI TMAG5273.

---
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
#11 lore raw

PATCH v3 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 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 | 736 ++++++++++++++++++++++++++++
 4 files changed, 751 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 +736 -0

736 lines not on this page · raw message

Signature · 1 line
2.37.2
#12 lore raw

Re: [PATCH v3 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.

Much better now, my comments below.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int tmag5273_write_scale(struct tmag5273_data *data, int scale_micro)
+{

What about

	u32 mask;

drivers/iio/magnetometer/tmag5273.c line 241

Quoted 9 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(tmag5273_scale[0]); i++) {
+		if (tmag5273_scale[data->version][i].val_micro == scale_micro)
+			break;
+	}
+	if (i == ARRAY_SIZE(tmag5273_scale[0]))
+		return -EINVAL;
+	data->scale_index = i;
	if (data->scale_index == MAGN_RANGE_LOW)
		mask = 0;
	else
		mask = TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK;

drivers/iio/magnetometer/tmag5273.c line 245

Quoted 3 lines
drivers/iio/magnetometer/tmag5273.c new file +3 -0
+	return regmap_update_bits(data->map,
+		TMAG5273_SENSOR_CONFIG_2,
+		TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK,
		mask);

drivers/iio/magnetometer/tmag5273.c line 248

Quoted 3 lines
drivers/iio/magnetometer/tmag5273.c new file +3 -0
+		data->scale_index == MAGN_RANGE_LOW ? 0 :
+			TMAG5273_Z_RANGE_MASK |
+			TMAG5273_X_Y_RANGE_MASK);

?

Quoted 1 line

+}

...

drivers/iio/magnetometer/tmag5273.c line 382

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +3 -0
+		switch (chan->type) {
+		case IIO_MAGN:
 
+			if (val != 0)

if (val)

Quoted 5 lines
drivers/iio/magnetometer/tmag5273.c new file +5 -0
+				return -EINVAL;
+			return tmag5273_write_scale(data, val2);
+		default:
+			return -EINVAL;
+		}

...

drivers/iio/magnetometer/tmag5273.c line 484

Quoted 5 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+static const struct regmap_config tmag5273_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
 
+	.max_register = 0xff,

Does it indeed have 256 registers?

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+	.volatile_reg = tmag5273_volatile_reg,
+};

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+static void tmag5273_read_device_property(struct tmag5273_data *data)
+{
	struct device *dev = data->dev;

drivers/iio/magnetometer/tmag5273.c line 499

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	const char *str;
+	int ret;
+
+	data->angle_measurement = TMAG5273_ANGLE_EN_X_Y;
	ret = device_property_read_string(dev, "ti,angle-measurement", &str);
	if (ret)
		return;

drivers/iio/magnetometer/tmag5273.c line 509

Quoted 9 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+	if (!device_property_read_string(data->dev, "ti,angle-measurement", &str)) {
+		ret = match_string(tmag5273_angle_names,
+				   ARRAY_SIZE(tmag5273_angle_names), str);
+		if (ret < 0)
+			dev_warn(data->dev,
+				 "unexpected read angle-measurement property: %s\n", str);
+		else
+			data->angle_measurement = ret;
+	}
	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);
	else
		data->angle_measurement = ret;
Quoted 1 line

+}

...

drivers/iio/magnetometer/tmag5273.c line 559

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+		return dev_err_probe(data->dev, ret,
+				     "failed to power on device\n");

I would leave on one line (only 84 characters long).

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return dev_err_probe(data->dev, ret,
+				     "failed to read device ID\n");

Ditto.

...

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	switch (data->devid) {
+	case TMAG5273_MANUFACTURER_ID:
+		snprintf(data->name, sizeof(data->name), "tmag5273x%1u",
+			 data->version);

drivers/iio/magnetometer/tmag5273.c line 582

Quoted 10 lines
drivers/iio/magnetometer/tmag5273.c new file +10 -0
+		if (data->version < 1 || data->version > 2)
+			dev_warn(data->dev, "Unsupported device %s\n",
+				 data->name);
+		break;
+	default:
+		dev_warn(data->dev, "Unknown device ID 0x%x\n", data->devid);
+		break;
+	}
+
+	return 0;

'break;':s above can be replaced by direct 'return 0;':s. It's up to you.

...

drivers/iio/magnetometer/tmag5273.c line 600

Quoted 4 lines
drivers/iio/magnetometer/tmag5273.c new file +4 -0
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return dev_err_probe(dev, -ENOMEM,
+				     "IIO device allocation failed\n");

We don't print ENOMEM error messages in the drivers, core does this for us. Otherwise you have to explain why this message is so important.

...

drivers/iio/magnetometer/tmag5273.c line 633

Quoted 9 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+	/*
+	 * Register powerdown deferred callback which suspends the chip
+	 * after module unloaded.
+	 *
+	 * TMAG5273 should be in SUSPEND mode in the two cases:
+	 * 1) When driver is loaded, but we do not have any data or
+	 *    configuration requests to it (we are solving it using
+	 *    autosuspend feature).
+	 * 2) When driver is unloaded and device is not used (devm action is

Something with indentation of this or other lines.

Quoted 2 lines
(quoted diff) +2 -0
+	 *    used in this case).
+	 */

...

drivers/iio/magnetometer/tmag5273.c line 639

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+		return dev_err_probe(dev, ret,
+				     "failed to add powerdown action\n");

One line?

...

drivers/iio/magnetometer/tmag5273.c line 684

Quoted 2 lines
drivers/iio/magnetometer/tmag5273.c new file +2 -0
+		dev_err(dev, "failed to power off device (%pe)\n",
+			ERR_PTR(ret));

Ditto.

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

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

Quoted 1 line · Andy Shevchenko

On Fri, Nov 25, 2022 at 09:35:26AM +0100, Gerald Loacker wrote:

...

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int tmag5273_write_scale(struct tmag5273_data *data, int scale_micro)
+{

What about

	u32 mask;

After looking again, I guess it should be

	u32 value;
Quoted 28 lines

drivers/iio/magnetometer/tmag5273.c line 241

Quoted 9 lines
drivers/iio/magnetometer/tmag5273.c new file +9 -0
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(tmag5273_scale[0]); i++) {
+		if (tmag5273_scale[data->version][i].val_micro == scale_micro)
+			break;
+	}
+	if (i == ARRAY_SIZE(tmag5273_scale[0]))
+		return -EINVAL;
+	data->scale_index = i;
	if (data->scale_index == MAGN_RANGE_LOW)
		mask = 0;
	else
		mask = TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK;

drivers/iio/magnetometer/tmag5273.c line 245

Quoted 3 lines
drivers/iio/magnetometer/tmag5273.c new file +3 -0
+	return regmap_update_bits(data->map,
+		TMAG5273_SENSOR_CONFIG_2,
+		TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK,
		mask);

drivers/iio/magnetometer/tmag5273.c line 248

Quoted 3 lines
drivers/iio/magnetometer/tmag5273.c new file +3 -0
+		data->scale_index == MAGN_RANGE_LOW ? 0 :
+			TMAG5273_Z_RANGE_MASK |
+			TMAG5273_X_Y_RANGE_MASK);

?

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko