Skip to content

Commit 78c0df7

Browse files
committed
sensor: icm4268x: Add support for ICM42686 variant
Now this driver supports both ICM42688 and ICM42686. Tested with read-decode as well as streaming mode. Signed-off-by: Luis Ubieda <luisf@croxel.com>
1 parent 9bd3e92 commit 78c0df7

File tree

10 files changed

+475
-337
lines changed

10 files changed

+475
-337
lines changed

drivers/sensor/tdk/icm4268x/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
menuconfig ICM4268X
99
bool "ICM4268X Six-Axis Motion Tracking Device"
1010
default y
11-
depends on DT_HAS_INVENSENSE_ICM42688_ENABLED
11+
depends on DT_HAS_INVENSENSE_ICM4268X_ENABLED
1212
select SPI
1313
select RTIO_WORKQ if SENSOR_ASYNC_API
1414
help

drivers/sensor/tdk/icm4268x/icm4268x.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ static int icm4268x_attr_set(const struct device *dev, enum sensor_channel chan,
150150
if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
151151
new_config.accel_odr = icm4268x_accel_hz_to_reg(val->val1);
152152
} else if (attr == SENSOR_ATTR_FULL_SCALE) {
153-
new_config.accel_fs = icm4268x_accel_fs_to_reg(sensor_ms2_to_g(val));
153+
new_config.accel_fs = icm4268x_accel_fs_to_reg(sensor_ms2_to_g(val),
154+
data->cfg.variant);
154155
} else {
155156
LOG_ERR("Unsupported attribute");
156157
res = -ENOTSUP;
@@ -163,7 +164,8 @@ static int icm4268x_attr_set(const struct device *dev, enum sensor_channel chan,
163164
if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
164165
new_config.gyro_odr = icm4268x_gyro_odr_to_reg(val->val1);
165166
} else if (attr == SENSOR_ATTR_FULL_SCALE) {
166-
new_config.gyro_fs = icm4268x_gyro_fs_to_reg(sensor_rad_to_degrees(val));
167+
new_config.gyro_fs = icm4268x_gyro_fs_to_reg(sensor_rad_to_degrees(val),
168+
data->cfg.variant);
167169
} else {
168170
LOG_ERR("Unsupported attribute");
169171
res = -EINVAL;
@@ -232,7 +234,7 @@ static int icm4268x_attr_get(const struct device *dev, enum sensor_channel chan,
232234
if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
233235
icm4268x_accel_reg_to_hz(cfg->accel_odr, val);
234236
} else if (attr == SENSOR_ATTR_FULL_SCALE) {
235-
icm4268x_accel_reg_to_fs(cfg->accel_fs, val);
237+
icm4268x_accel_reg_to_fs(cfg->accel_fs, data->cfg.variant, val);
236238
} else {
237239
LOG_ERR("Unsupported attribute");
238240
res = -EINVAL;
@@ -245,7 +247,7 @@ static int icm4268x_attr_get(const struct device *dev, enum sensor_channel chan,
245247
if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
246248
icm4268x_gyro_reg_to_odr(cfg->gyro_odr, val);
247249
} else if (attr == SENSOR_ATTR_FULL_SCALE) {
248-
icm4268x_gyro_reg_to_fs(cfg->gyro_fs, val);
250+
icm4268x_gyro_reg_to_fs(cfg->gyro_fs, data->cfg.variant, val);
249251
} else {
250252
LOG_ERR("Unsupported attribute");
251253
res = -EINVAL;
@@ -341,6 +343,10 @@ void icm4268x_unlock(const struct device *dev)
341343

342344
#define ICM42688_DT_CONFIG_INIT(inst) \
343345
{ \
346+
COND_CODE_1(DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42688), \
347+
(.variant = ICM4268X_VARIANT_ICM42688,), ( \
348+
COND_CODE_1(DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42686), \
349+
(.variant = ICM4268X_VARIANT_ICM42686,), ()))) \
344350
.accel_pwr_mode = DT_INST_PROP(inst, accel_pwr_mode), \
345351
.accel_fs = DT_INST_PROP(inst, accel_fs), \
346352
.accel_odr = DT_INST_PROP(inst, accel_odr), \
@@ -372,11 +378,25 @@ void icm4268x_unlock(const struct device *dev)
372378
.spi_iodev = &icm4268x_spi_iodev_##inst,)) \
373379
};
374380

381+
/** The rest of the Device-tree configuration is validated in the YAML
382+
* file. This outlier comes from the fact we're sharing the dts-properties
383+
* across variants, and ICM42686 has an extra enum for the accel-fs.
384+
*/
385+
#define ICM42688_BUILD_CONFIG_VALIDATION(inst) \
386+
BUILD_ASSERT((DT_INST_PROP(inst, accel_fs) >= ICM42688_DT_ACCEL_FS_16) && \
387+
(DT_INST_PROP(inst, accel_fs) <= ICM42688_DT_ACCEL_FS_2), \
388+
"Invalid accel-fs configured for ICM42688. Please revisit DTS config-set");
389+
375390
#define ICM4268X_INIT(inst) \
376391
\
377-
BUILD_ASSERT(DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42688), \
392+
BUILD_ASSERT(DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42688) || \
393+
DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42686), \
378394
"Please define additional compatible property to dts node: " \
379-
"<invensense,icm42688>"); \
395+
"<invensense,icm42688> or <invensense,icm42686>"); \
396+
\
397+
\
398+
COND_CODE_1(DT_INST_NODE_HAS_COMPAT(inst, invensense_icm42688), \
399+
(ICM42688_BUILD_CONFIG_VALIDATION(inst)), ()); \
380400
\
381401
ICM4268X_DEFINE_DATA(inst); \
382402
\

0 commit comments

Comments
 (0)