[PATCH v2 0/3] iio: lsm303dlh-magn: endianness + boot-time fullscale selection

From: Herman van Hazendonk

Date: Tue Jun 16 2026 - 09:09:26 EST


This series fixes two independent issues that together prevent the
LSM303DLH magnetometer from delivering usable readings out of the
box on at least the HP TouchPad (apq8060), and adds a small generic
extension to the ST sensors device-tree binding to allow boards to
declare a non-default initial full-scale.

PATCH 1/3 fixes st_sensors_core's read_axis_data() helper to honour
the channel's declared scan_type.endianness. The helper has
unconditionally decoded multi-byte results as little-endian since
it was introduced. Every other in-tree ST sensor declares IIO_LE
and was unaffected, but the LSM303DLH / LSM303DLHC / LSM303DLM
magnetometers all bind st_magn_16bit_channels (IIO_BE) and publish
their X / Y / Z words as big-endian pairs (high byte at the lower
register address, 0x03 / 0x05 / 0x07). The mismatch swapped the
high and low bytes of every magnetometer sample on these three
parts. The fix only affects the IIO_BE branch; existing IIO_LE
consumers are untouched.

PATCH 2/3 adds an optional st,fullscale-milligauss device-tree
property to the ST sensors binding. The driver core hardcodes
fs_avl[0] (the highest-sensitivity range) as the starting
full-scale, which is the right default for a desk-noise floor but
leaves no margin for boards that pick up DC bias from nearby PCB
structures. The property is scoped to magnetometer compatibles
via per-family enum clauses so DTSes with a misspelled value, or
that put the property on accel/gyro/pressure or fixed-FS magn
nodes, fail dt_binding_check rather than being silently no-op'd at
runtime.

PATCH 3/3 parses st,fullscale-milligauss in the magnetometer
common probe and selects the matching fs_avl entry. The LSM303DLH
on the HP TouchPad picks up enough DC bias from the surrounding
power planes that the chip-default +/-1.3 G range saturates the X
axis to the chip's 0xF000 overflow sentinel on every sample, while
Y and Z fall within range. Empirically any fs_avl >= 1 (+/-1.9 G
and up) works; on tenderloin the appropriate value is 2500 mg
(+/-2.5 G).

PATCH 1/3 is a standalone bug fix and is now Cc'd to stable;
PATCHES 2/3 and 3/3 form a unit.

Changes since v1
~~~~~~~~~~~~~~~~

PATCH 1/3 (endianness):
- Restructure around a single u32 tmp + one trailing
sign_extend32(tmp, BYTES_TO_BITS(byte_for_channel) - 1), drop
the (s16) / (s32) casts (Andy Shevchenko).
- Make byte_for_channel == 0 || >= 4 an explicit -EINVAL return
(no in-tree caller hits this, but the prior code silently left
*data uninitialised).
- Add Fixes: 23491b513bcd ("iio:common: Add STMicroelectronics
common library") and Cc: stable@xxxxxxxxxxxxxxx (Jonathan
Cameron). The bug has been present since the helper was
introduced in 2013.
- Spell out that the fix changes in_magn_*_raw decoding for
LSM303DLHC and LSM303DLM too (same IIO_BE channel set), not
only LSM303DLH.

PATCH 2/3 (binding):
- Rename st,fullscale-mg to st,fullscale-milligauss. "-mg" is
the DT unit-suffix convention but already names milli-g in
the accelerometer parts of this same binding file; spelling
milligauss out keeps the unit unambiguous if a similar
tunable is ever added for accel/gyro/pressure.
- Scope the property to magnetometer compatibles via per-family
allOf:if-then enum clauses:
LSM303DLH/DLHC/DLM: enum [1300, 1900, 2500, 4000, 4700,
5600, 8100]
LIS3MDL/LSM9DS1/LSM303C: enum [4000, 8000, 12000, 16000]
LSM303AGR/LIS2MDL/IIS2MDC (fixed FS): rejected outright
everything else (accel/gyro/pressure/IMU): rejected outright
- Drop the "(or analogous engineering units for other sensor
families that may grow this property in the future)" hand-wave
from the description (Jonathan Cameron); the property is now
positively bound to magnetometers only.
- Drop the overstated "userspace cannot recover without racing
the driver" wording; document the actual probe-time window
(the in-tree IIO consumers cache the saturation sentinel
before any UDEV rule fires).
- Add an in-file maintenance comment before the catch-all NOT
clause so future contributors who add a new magnetometer
compatible know all four clauses must be updated together.

PATCH 3/3 (driver):
- Honour the rename to st,fullscale-milligauss.
- Restructure per Andy: const char *propname at the top,
device_property_present() pre-check, device_property_read_u32()
error path with explicit return.
- Gate the parse block on mdata->sensor_settings->fs.addr != 0
as defence in depth; the binding already rejects the property
on fixed-FS magnetometers, the gate keeps the code path
self-contained against stale DTBs.

To: Jonathan Cameron <jic23@xxxxxxxxxx>
To: David Lechner <dlechner@xxxxxxxxxxxx>
To: Nuno Sá <nuno.sa@xxxxxxxxxx>
To: Andy Shevchenko <andy@xxxxxxxxxx>
To: Nathan Chancellor <nathan@xxxxxxxxxx>
To: Nick Desaulniers <nick.desaulniers+lkml@xxxxxxxxx>
To: Bill Wendling <morbo@xxxxxxxxxx>
To: Justin Stitt <justinstitt@xxxxxxxxxx>
To: Denis Ciocca <denis.ciocca@xxxxxxxxx>
To: Lars-Peter Clausen <lars@xxxxxxxxxx>
To: Rob Herring <robh@xxxxxxxxxx>
To: Krzysztof Kozlowski <krzk+dt@xxxxxxxxxx>
To: Conor Dooley <conor+dt@xxxxxxxxxx>
To: Denis Ciocca <denis.ciocca@xxxxxx>
To: Linus Walleij <linusw@xxxxxxxxxx>
Cc: linux-iio@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: llvm@xxxxxxxxxxxxxxx
Cc: devicetree@xxxxxxxxxxxxxxx
v1: https://lore.kernel.org/linux-iio/cover.1780652883.git.github.com@xxxxxxxxxx/

base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260616-submit-iio-lsm303dlh-magn-fixes-48153433b301
---
Herman van Hazendonk (3):
iio: common: st_sensors: honour channel endianness in read_axis_data
dt-bindings: iio: st,st-sensors: add st,fullscale-milligauss
iio: magnetometer: st_magn: honour st,fullscale-milligauss DT property

.../devicetree/bindings/iio/st,st-sensors.yaml | 71 ++++++++++++++++++++++
drivers/iio/common/st_sensors/st_sensors_core.c | 23 +++++--
drivers/iio/magnetometer/st_magn_core.c | 32 ++++++++++
3 files changed, 120 insertions(+), 6 deletions(-)
---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260616-submit-iio-lsm303dlh-magn-fixes-48153433b301

Best regards,
--
Herman van Hazendonk <github.com@xxxxxxxxxx>