Skip to contentSkip to conversation

iio: imu: inv_mpu6050: fix magnetometer scale of Y and Z axes

PATCH Applied Arnav Kapoor 3 participants 3 messages Oct 11 – Oct 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: inv_mpu6050: fix magnetometer scale of Y and Z axes

inv_mpu_magn_get_scale() returns st->magn_raw_to_gauss[chan->address], where magn_raw_to_gauss[] holds one coefficient per axis, computed in inv_magn_init() from the per-axis factory sensitivity adjustment values (ASAX, ASAY, ASAZ) of the AK8975/AK8963 magnetometer.

However INV_MPU9X50_MAGN_CHAN() never sets .address, so it is 0 for all three magnetometer channels. As a result the Y and Z channels report the scale computed from the X axis adjustment value, and userspace scales the Y and Z readings with the wrong per-axis calibration.

Set .address to the axis index (0 for X, 1 for Y, 2 for Z) in the channel macro, matching the order used to fill magn_raw_to_gauss[].

Fixes b1392de06184 ("iio: imu: inv_mpu6050: add MPU925x magnetometer support")
Assisted-by LLM

---
Compile-tested only (W=1); I don't have MPU9150/MPU925x hardware. I checked the built object: before this patch the X, Y and Z magnetometer channels all have .address == 0, after it they have 0, 1 and 2.

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +2 -0
index 38c3f3b398d4..1614f49be66c 100644
@@ -1576,6 +1576,8 @@ static const unsigned long inv_mpu_scan_masks[] = {
 		.type = IIO_MAGN,					\
 		.modified = 1,						\
 		.channel2 = _chan2,					\
+		/* index of the axis in magn_raw_to_gauss[] */		\
+		.address = (_chan2) - IIO_MOD_X,			\
 		.info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) |	\
 				      BIT(IIO_CHAN_INFO_RAW),		\
 		.scan_index = _index,					\
Signature · 1 line
2.53.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 40 lines

inv_mpu_magn_get_scale() returns st->magn_raw_to_gauss[chan->address], where magn_raw_to_gauss[] holds one coefficient per axis, computed in inv_magn_init() from the per-axis factory sensitivity adjustment values (ASAX, ASAY, ASAZ) of the AK8975/AK8963 magnetometer.

However INV_MPU9X50_MAGN_CHAN() never sets .address, so it is 0 for all three magnetometer channels. As a result the Y and Z channels report the scale computed from the X axis adjustment value, and userspace scales the Y and Z readings with the wrong per-axis calibration.

Set .address to the axis index (0 for X, 1 for Y, 2 for Z) in the channel macro, matching the order used to fill magn_raw_to_gauss[].

Fixes b1392de06184 ("iio: imu: inv_mpu6050: add MPU925x magnetometer support")
Assisted-by LLM

---
Compile-tested only (W=1); I don't have MPU9150/MPU925x hardware. I checked the built object: before this patch the X, Y and Z magnetometer channels all have .address == 0, after it they have 0, 1 and 2.

drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 2 ++

1 file changed, 2 insertions(+)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +2 -0
index 38c3f3b398d4..1614f49be66c 100644
@@ -1576,6 +1576,8 @@ static const unsigned long inv_mpu_scan_masks[] = {
 		.type = IIO_MAGN,					\
 		.modified = 1,						\
 		.channel2 = _chan2,					\
+		/* index of the axis in magn_raw_to_gauss[] */		\
+		.address = (_chan2) - IIO_MOD_X,			\
 		.info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) |	\
 				      BIT(IIO_CHAN_INFO_RAW),		\
 		.scan_index = _index,					\
Signature · 1 line
2.53.0

Hello Arnav,

good catch indeed, thanks for the patch.

Thanks,
JB

maintainer Applied
#2 lore raw
Quoted 10 lines · On Mon, 12 Oct 2026 07:50:52 +0000 Jean-Baptiste Maneyrol…
Quoted 10 lines

inv_mpu_magn_get_scale() returns st->magn_raw_to_gauss[chan->address], where magn_raw_to_gauss[] holds one coefficient per axis, computed in inv_magn_init() from the per-axis factory sensitivity adjustment values (ASAX, ASAY, ASAZ) of the AK8975/AK8963 magnetometer.

However INV_MPU9X50_MAGN_CHAN() never sets .address, so it is 0 for all three magnetometer channels. As a result the Y and Z channels report the scale computed from the X axis adjustment value, and userspace scales the Y and Z readings with the wrong per-axis calibration.

Quoted 6 lines
Quoted 6 lines

Set .address to the axis index (0 for X, 1 for Y, 2 for Z) in the channel macro, matching the order used to fill magn_raw_to_gauss[].

Fixes b1392de06184 ("iio: imu: inv_mpu6050: add MPU925x magnetometer support")
Assisted-by LLM

Applied to the fixes-togreg branch of iio.git and marked for stable. Note I'll be rebasing that tree on rc1 before sending a pull request.

Thanks,

Jonathan

Quoted 33 lines
Quoted 24 lines

---
Compile-tested only (W=1); I don't have MPU9150/MPU925x hardware. I checked the built object: before this patch the X, Y and Z magnetometer channels all have .address == 0, after it they have 0, 1 and 2.

drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 2 ++

1 file changed, 2 insertions(+)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +2 -0
index 38c3f3b398d4..1614f49be66c 100644
@@ -1576,6 +1576,8 @@ static const unsigned long inv_mpu_scan_masks[] = {
 		.type = IIO_MAGN,					\
 		.modified = 1,						\
 		.channel2 = _chan2,					\
+		/* index of the axis in magn_raw_to_gauss[] */		\
+		.address = (_chan2) - IIO_MOD_X,			\
 		.info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) |	\
 				      BIT(IIO_CHAN_INFO_RAW),		\
 		.scan_index = _index,					\
Signature · 1 line
2.53.0

Hello Arnav,

good catch indeed, thanks for the patch.

Thanks,
JB