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);
4745struct 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
7376struct 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 )
0 commit comments