Skip to content

Commit 2bf61e5

Browse files
yidingkartben
authored andcommitted
drivers: sensor: Fix TMAG5273/TMAG3001 by adding a TMAG3001 compatible
Fixes issue introduced in #76460 The previous code attempted to detect whether TMAG5273 or TMAG3001 was connected based on DEVICE ID register. This doesn't work as the bits that denote the version on one part are undefined on the other part, and cannot be relied on to be zero. This commit adds a TMAG3001 compatible which (for now) is just derived from the TMAG5273 compatible. This allows TMAG3001 to be specified directly in the DT. The driver code is updated to support both compatibles. Signed-off-by: Yiding Jia <[email protected]>
1 parent cc042a6 commit 2bf61e5

File tree

6 files changed

+99
-43
lines changed

6 files changed

+99
-43
lines changed

drivers/sensor/ti/tmag5273/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
config TMAG5273
77
bool "TMAG5273/TMAG3001 3D Hall-Effect Sensor"
88
default y
9-
depends on DT_HAS_TI_TMAG5273_ENABLED
9+
depends on DT_HAS_TI_TMAG5273_ENABLED || DT_HAS_TI_TMAG3001_ENABLED
1010
select I2C
1111
select GPIO
1212
imply CRC

drivers/sensor/ti/tmag5273/tmag5273.c

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#define DT_DRV_COMPAT ti_tmag5273
8-
97
#include "tmag5273.h"
108

119
#include <stdint.h>
@@ -47,6 +45,11 @@ LOG_MODULE_REGISTER(TMAG5273, CONFIG_SENSOR_LOG_LEVEL);
4745
struct tmag5273_config {
4846
struct i2c_dt_spec i2c;
4947

48+
enum {
49+
TMAG5273_PART,
50+
TMAG3001_PART
51+
} part;
52+
5053
uint8_t mag_channel;
5154
uint8_t axis;
5255
bool temperature;
@@ -71,8 +74,8 @@ struct tmag5273_config {
7174
};
7275

7376
struct tmag5273_data {
74-
uint8_t version; /** version as given by the sensor */
75-
uint16_t conversion_time_us; /** time for one conversion */
77+
enum tmag5273_version version; /** version as given by the sensor */
78+
uint16_t conversion_time_us; /** time for one conversion */
7679

7780
int16_t x_sample; /** measured B-field @x-axis */
7881
int16_t y_sample; /** measured B-field @y-axis */
@@ -1152,7 +1155,26 @@ static int tmag5273_init(const struct device *dev)
11521155
return -EIO;
11531156
}
11541157

1155-
drv_data->version = regdata & TMAG5273_VER_MSK;
1158+
switch (drv_cfg->part) {
1159+
case TMAG5273_PART:
1160+
drv_data->version = regdata & TMAG5273_VER_MSK;
1161+
break;
1162+
case TMAG3001_PART:
1163+
drv_data->version = regdata & TMAG3001_VER_MSK;
1164+
break;
1165+
default:
1166+
__ASSERT(false, "invalid part %d", drv_cfg->part);
1167+
}
1168+
switch (drv_data->version) {
1169+
case TMAG5273_VER_TMAG5273X1:
1170+
case TMAG5273_VER_TMAG5273X2:
1171+
case TMAG5273_VER_TMAG3001X1:
1172+
case TMAG5273_VER_TMAG3001X2:
1173+
break;
1174+
default:
1175+
LOG_ERR("unsupported version %d", drv_data->version);
1176+
return -EIO;
1177+
}
11561178

11571179
/* magnetic measurement range based on version, apply correct one */
11581180
if (drv_cfg->meas_range == TMAG5273_DT_AXIS_RANGE_LOW) {
@@ -1217,33 +1239,38 @@ static DEVICE_API(sensor, tmag5273_driver_api) = {
12171239
: 0)
12181240

12191241
/** Instantiation macro */
1220-
#define TMAG5273_DEFINE(inst) \
1221-
BUILD_ASSERT(IS_ENABLED(CONFIG_CRC) || (DT_INST_PROP(inst, crc_enabled) == 0), \
1242+
#define TMAG5273_DEFINE(inst, compat, _part) \
1243+
BUILD_ASSERT(IS_ENABLED(CONFIG_CRC) || (DT_PROP(DT_INST(inst, compat), crc_enabled) == 0), \
12221244
"CRC support necessary"); \
1223-
BUILD_ASSERT(!DT_INST_PROP(inst, trigger_conversion_via_int) || \
1224-
DT_INST_NODE_HAS_PROP(inst, int_gpios), \
1245+
BUILD_ASSERT(!DT_PROP(DT_INST(inst, compat), trigger_conversion_via_int) || \
1246+
DT_NODE_HAS_PROP(DT_INST(inst, compat), int_gpios), \
12251247
"trigger-conversion-via-int requires int-gpios to be defined"); \
1226-
static const struct tmag5273_config tmag5273_driver_cfg##inst = { \
1227-
.i2c = I2C_DT_SPEC_INST_GET(inst), \
1228-
.mag_channel = DT_INST_PROP(inst, axis), \
1229-
.axis = (TMAG5273_DT_X_AXIS_BIT(DT_INST_PROP(inst, axis)) | \
1230-
TMAG5273_DT_Y_AXIS_BIT(DT_INST_PROP(inst, axis)) | \
1231-
TMAG5273_DT_Z_AXIS_BIT(DT_INST_PROP(inst, axis))), \
1232-
.temperature = DT_INST_PROP(inst, temperature), \
1233-
.meas_range = DT_INST_PROP(inst, range), \
1234-
.temperature_coefficient = DT_INST_PROP(inst, temperature_coefficient), \
1235-
.angle_magnitude_axis = DT_INST_PROP(inst, angle_magnitude_axis), \
1236-
.ch_mag_gain_correction = DT_INST_PROP(inst, ch_mag_gain_correction), \
1237-
.operation_mode = DT_INST_PROP(inst, operation_mode), \
1238-
.averaging = DT_INST_PROP(inst, average_mode), \
1239-
.trigger_conv_via_int = DT_INST_PROP(inst, trigger_conversion_via_int), \
1240-
.low_noise_mode = DT_INST_PROP(inst, low_noise), \
1241-
.ignore_diag_fail = DT_INST_PROP(inst, ignore_diag_fail), \
1242-
.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0}), \
1243-
IF_ENABLED(CONFIG_CRC, (.crc_enabled = DT_INST_PROP(inst, crc_enabled),))}; \
1244-
static struct tmag5273_data tmag5273_driver_data##inst; \
1245-
SENSOR_DEVICE_DT_INST_DEFINE(inst, tmag5273_init, NULL, &tmag5273_driver_data##inst, \
1246-
&tmag5273_driver_cfg##inst, POST_KERNEL, \
1247-
CONFIG_SENSOR_INIT_PRIORITY, &tmag5273_driver_api);
1248-
1249-
DT_INST_FOREACH_STATUS_OKAY(TMAG5273_DEFINE)
1248+
static const struct tmag5273_config compat##_driver_cfg##inst = { \
1249+
.i2c = I2C_DT_SPEC_GET(DT_INST(inst, compat)), \
1250+
.part = _part, \
1251+
.mag_channel = DT_PROP(DT_INST(inst, compat), axis), \
1252+
.axis = (TMAG5273_DT_X_AXIS_BIT(DT_PROP(DT_INST(inst, compat), axis)) | \
1253+
TMAG5273_DT_Y_AXIS_BIT(DT_PROP(DT_INST(inst, compat), axis)) | \
1254+
TMAG5273_DT_Z_AXIS_BIT(DT_PROP(DT_INST(inst, compat), axis))), \
1255+
.temperature = DT_PROP(DT_INST(inst, compat), temperature), \
1256+
.meas_range = DT_PROP(DT_INST(inst, compat), range), \
1257+
.temperature_coefficient = \
1258+
DT_PROP(DT_INST(inst, compat), temperature_coefficient), \
1259+
.angle_magnitude_axis = DT_PROP(DT_INST(inst, compat), angle_magnitude_axis), \
1260+
.ch_mag_gain_correction = DT_PROP(DT_INST(inst, compat), ch_mag_gain_correction), \
1261+
.operation_mode = DT_PROP(DT_INST(inst, compat), operation_mode), \
1262+
.averaging = DT_PROP(DT_INST(inst, compat), average_mode), \
1263+
.trigger_conv_via_int = \
1264+
DT_PROP(DT_INST(inst, compat), trigger_conversion_via_int), \
1265+
.low_noise_mode = DT_PROP(DT_INST(inst, compat), low_noise), \
1266+
.ignore_diag_fail = DT_PROP(DT_INST(inst, compat), ignore_diag_fail), \
1267+
.int_gpio = GPIO_DT_SPEC_GET_OR(DT_INST(inst, compat), int_gpios, {0}), \
1268+
IF_ENABLED(CONFIG_CRC, \
1269+
(.crc_enabled = DT_PROP(DT_INST(inst, compat), crc_enabled),))}; \
1270+
static struct tmag5273_data compat##_driver_data##inst; \
1271+
SENSOR_DEVICE_DT_DEFINE(DT_INST(inst, compat), tmag5273_init, NULL, \
1272+
&compat##_driver_data##inst, &compat##_driver_cfg##inst, \
1273+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &tmag5273_driver_api);
1274+
1275+
DT_COMPAT_FOREACH_STATUS_OKAY_VARGS(ti_tmag5273, TMAG5273_DEFINE, TMAG5273_PART)
1276+
DT_COMPAT_FOREACH_STATUS_OKAY_VARGS(ti_tmag3001, TMAG5273_DEFINE, TMAG3001_PART)

drivers/sensor/ti/tmag5273/tmag5273.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@
194194
#define TMAG5273_I2C_ADDRESS_UPDATE_ENABLE (1 << TMAG5273_I2C_ADDRESS_UPDATE_EN_POS)
195195

196196
/* Register DEVICE_ID */
197-
#define TMAG5273_VER_POS 0
198-
#define TMAG3001_VER_POS 2
199-
#define TMAG5273_VER_MSK GENMASK(3, 0)
200-
201-
#define TMAG5273_VER_TMAG5273X1 (1 << TMAG5273_VER_POS)
202-
#define TMAG5273_VER_TMAG5273X2 (2 << TMAG5273_VER_POS)
203-
#define TMAG5273_VER_TMAG3001X1 (0 << TMAG3001_VER_POS)
204-
#define TMAG5273_VER_TMAG3001X2 (2 << TMAG3001_VER_POS)
197+
#define TMAG5273_VER_POS 0
198+
#define TMAG3001_VER_POS 2
199+
#define TMAG5273_VER_MSK GENMASK(1, 0)
200+
#define TMAG3001_VER_MSK GENMASK(3, 2)
201+
202+
enum tmag5273_version {
203+
TMAG5273_VER_TMAG5273X1 = 1 << TMAG5273_VER_POS,
204+
TMAG5273_VER_TMAG5273X2 = 2 << TMAG5273_VER_POS,
205+
TMAG5273_VER_TMAG3001X1 = 0 << TMAG3001_VER_POS,
206+
TMAG5273_VER_TMAG3001X2 = 2 << TMAG3001_VER_POS,
207+
};
205208

206209
/* Register CONV_STATUS */
207210
#define TMAG5273_SET_COUNT_POS 5

dts/bindings/sensor/ti,tmag3001.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
description: |
3+
TMAG3001 Low-Power 3D Linear and Angle Hall-Effect Sensor With I2C Interface
4+
and Wake Up Detection in WCSP.
5+
6+
See the specification for the default I2C address.
7+
8+
The specification of the sensor can be found at:
9+
https://www.ti.com/lit/ds/symlink/tmag3001.pdf
10+
11+
Currently, this driver shares implementation with tmag5273. When setting the
12+
enum properties in a .dts or .dtsi file you may include
13+
`zephyr/dt-bindings/sensor/tmag5273.h` and use the macros defined there.
14+
15+
compatible: "ti,tmag3001"
16+
17+
include: ["ti,tmag5273.yaml"]

dts/bindings/sensor/ti,tmag5273.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ description: |
88
99
The specification of the sensor can be found at:
1010
https://www.ti.com/lit/ds/symlink/tmag5273.pdf
11-
https://www.ti.com/lit/ds/symlink/tmag3001.pdf
1211
1312
When setting the enum properties in a .dts or .dtsi file you may
1413
include tmag5273.h and use the macros defined there.

tests/drivers/build_all/sensor/i2c.dtsi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,3 +1201,13 @@ test_i2c_lis2duxs12: lis2duxs12@a7 {
12011201
power-mode = <LIS2DUX12_OPER_MODE_HIGH_FREQUENCY>;
12021202
status = "okay";
12031203
};
1204+
1205+
test_i2c_tmag3001: tmag3001@a8 {
1206+
compatible = "ti,tmag3001";
1207+
status = "okay";
1208+
reg = <0xa8>;
1209+
int-gpios = <&test_gpio 15 1>;
1210+
1211+
operation-mode = <TMAG5273_DT_OPER_MODE_CONTINUOUS>;
1212+
angle-magnitude-axis = <TMAG5273_DT_ANGLE_MAG_XY>;
1213+
};

0 commit comments

Comments
 (0)