Skip to contentSkip to conversation

iio: accel: add mount matrix support to 18 accelerometer drivers

PATCH In review He Xin 2 participants 20 messages Dec 4

Subsystems (7)
lore
Share & tools
lore

He Xin

author
#0 lore raw

v3 cover iio: accel: add mount matrix support to 18 accelerometer drivers

These drivers ignore the mount matrix described by the firmware, so userspace gets raw sensor axes on any board whose sensor is not mounted axis-aligned with the display. A udev/hwdb override does not cover it either: it keys off the sensor modalias and DMI, which do not tell such boards apart.

Each patch follows the pattern of the drivers that already support this: try the ACPI ROTM method first, fall back to the "mount-matrix" property (devicetree, or _DSD on ACPI platforms), and keep the result in the driver's private data so it can be exposed through IIO_MOUNT_MATRIX(). The patches remain independent of each other.

The ACPI tables on the platform this was tested on are part of the board firmware rather than a local SSDT overlay, and that firmware provides "mount-matrix" through _DSD today. Adding the ROTM method to it later does not require touching these drivers again.

Andy suggested on the mxc6255 patch that the matrix belongs in the IIO core rather than in every driver. I kept the per-driver shape because that is what kxcjk-1013, bmc150-accel, mxc4005, bmi323 and st_lsm6dsx already do; moving parsing and storage into the core, together with those five, is a separate change and I can send it as a follow-up.

One behaviour change: a board whose firmware describes the property with the wrong number of entries now fails to probe with -EINVAL instead of being ignored, which is what the drivers that already support the property do. A board that provides neither ROTM nor the property keeps the identity matrix.

Not converted here, because the change would not have the same shape:
adis16201, adis16209 and sca3000 list their channels as array literals, adxl313_core and fxls8962af-core keep the probe in the bus wrappers, and mma9551/mma9553 differ in both. hid-sensor-accel-3d, ssp_accel_sensor and cros_ec_accel_legacy take the orientation from the HID/SSP/EC side rather than from a firmware property.

Changes in v3:
- read the matrix through iio_read_acpi_mount_matrix("ROTM") first and fall back to the "mount-matrix" property, as kxcjk-1013, bmc150-accel, mxc4005, bmi323 and st_lsm6dsx already do
- drop the Reviewed-by carried in v2: the probe path changed, so the series needs another look

Changes in v2:
- move stk8ba50 to patch 1
- place the mount matrix before the __aligned(IIO_DMA_MINALIGN) members in adxl345, adxl355, adxl367, adxl380, bma220, bmi088 and sca3300, so the DMA buffers keep their own cacheline
- add the missing blank line after the mount matrix read in probe
- add the @orientation kernel-doc entry for msa311 and sca3300
- shorten the stk8ba50 test note to the behaviour observed on the device
- v1 lost its threading because the sending MTA rewrote Message-IDs while leaving References untouched; v2 is posted as a new series

Build tested with allmodconfig: drivers/iio/accel, 71 objects, no warnings; checkpatch.pl is silent in default and in --strict mode. Only stk8ba50 was tested on hardware; the other 17 are compile tested.

---
He Xin (18):
iio: accel: stk8ba50: support mount matrix
iio: accel: adxl345_core: support mount matrix
iio: accel: adxl355_core: support mount matrix
iio: accel: adxl367: support mount matrix
iio: accel: adxl372: support mount matrix
iio: accel: adxl380: support mount matrix
iio: accel: bma220_core: support mount matrix
iio: accel: bmi088-accel-core: support mount matrix
iio: accel: da280: support mount matrix
iio: accel: da311: support mount matrix
iio: accel: dmard06: support mount matrix
iio: accel: dmard09: support mount matrix
iio: accel: dmard10: support mount matrix
iio: accel: mma7455_core: support mount matrix
iio: accel: msa311: support mount matrix
iio: accel: mxc6255: support mount matrix
iio: accel: sca3300: support mount matrix
iio: accel: stk8312: support mount matrix

 drivers/iio/accel/adxl345_core.c      | 23 +++++++++++++++++++++++
 drivers/iio/accel/adxl355_core.c      | 23 +++++++++++++++++++++++
 drivers/iio/accel/adxl367.c           | 23 +++++++++++++++++++++++
 drivers/iio/accel/adxl372.c           | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/adxl380.c           | 22 ++++++++++++++++++++++
 drivers/iio/accel/bma220_core.c       | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/bmi088-accel-core.c | 23 +++++++++++++++++++++++
 drivers/iio/accel/da280.c             | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/da311.c             | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/dmard06.c           | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/dmard09.c           | 23 +++++++++++++++++++++++
 drivers/iio/accel/dmard10.c           | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/mma7455_core.c      | 23 +++++++++++++++++++++++
 drivers/iio/accel/msa311.c            | 24 ++++++++++++++++++++++++
 drivers/iio/accel/mxc6255.c           | 23 +++++++++++++++++++++++
 drivers/iio/accel/sca3300.c           | 28 ++++++++++++++++++++++++++++
 drivers/iio/accel/stk8312.c           | 27 +++++++++++++++++++++++++++
 drivers/iio/accel/stk8ba50.c          | 23 +++++++++++++++++++++++
 18 files changed, 447 insertions(+)

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261130-work-ab0d96ec614e

Best regards,

Signature · 1 line
He Xin <[email protected]>
author
#1 lore raw

PATCH v3 1/18 iio: accel: stk8ba50: support mount matrix

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

Tested on a tablet whose sensor is mounted rotated 180 degrees about X:
without the patch in_accel_mount_matrix is absent, so iio-sensor-proxy has to fall back to a udev/hwdb override; with it the matrix from _DSD is reported and the orientation comes out correct.

---
 drivers/iio/accel/stk8ba50.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/stk8ba50.c +23 -0
index 6c1e286c0a..f64ed8d3d6 100644
@@ -102,10 +102,26 @@ struct stk8ba50_data {
 		s16 chans[3];
 		aligned_s64 timetamp;
 	} scan;
+	struct iio_mount_matrix orientation;
+};
+
+static const struct iio_mount_matrix *
+stk8ba50_get_mount_matrix(const struct iio_dev *indio_dev,
+			  const struct iio_chan_spec *chan)
+{
+	struct stk8ba50_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info stk8ba50_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, stk8ba50_get_mount_matrix),
+	{ }
 };
 
 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = stk8ba50_ext_info,					\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -395,6 +411,13 @@ static int stk8ba50_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 	i2c_set_clientdata(client, indio_dev);
 	mutex_init(&data->lock);
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH v3 2/18 iio: accel: adxl345_core: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/adxl345_core.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/adxl345_core.c +23 -0
index 6c9080d88c..98885c3e94 100644
@@ -203,6 +203,7 @@ struct adxl345_state {
 	u32 tap_latent_us;
 	u32 tap_window_us;
 
+	struct iio_mount_matrix orientation;
 	__le16 fifo_buf[ADXL345_DIRS * ADXL345_FIFO_SIZE + 1] __aligned(IIO_DMA_MINALIGN);
 };
 
@@ -248,8 +249,23 @@ static const struct iio_event_spec adxl345_events[] = {
 	},
 };
 
+static const struct iio_mount_matrix *
+adxl345_core_get_mount_matrix(const struct iio_dev *indio_dev,
+			      const struct iio_chan_spec *chan)
+{
+	struct adxl345_state *st = iio_priv(indio_dev);
+
+	return &st->orientation;
+}
+
+static const struct iio_chan_spec_ext_info adxl345_core_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, adxl345_core_get_mount_matrix),
+	{ }
+};
+
 #define ADXL345_CHANNEL(index, reg, axis) {					\
 	.type = IIO_ACCEL,						\
+	.ext_info = adxl345_core_ext_info,				\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
 	.address = (reg),						\
@@ -1902,6 +1918,13 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 		return -ENOMEM;
 
 	st = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &st->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &st->orientation);
+		if (ret)
+			return ret;
+	}
+
 	st->regmap = regmap;
 	st->info = device_get_match_data(dev);
 	if (!st->info)
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH v3 3/18 iio: accel: adxl355_core: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/adxl355_core.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/adxl355_core.c +23 -0
index 080c9f6f9a..78fac75811 100644
@@ -228,6 +228,7 @@ struct adxl355_data {
 	int calibbias[3];
 	int adxl355_hpf_3db_table[7][2];
 	struct iio_trigger *dready_trig;
+	struct iio_mount_matrix orientation;
 	union {
 		u8 transf_buf[3];
 		struct {
@@ -707,8 +708,23 @@ static irqreturn_t adxl355_trigger_handler(int irq, void *p)
 	return IRQ_HANDLED;
 }
 
+static const struct iio_mount_matrix *
+adxl355_core_get_mount_matrix(const struct iio_dev *indio_dev,
+			      const struct iio_chan_spec *chan)
+{
+	struct adxl355_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info adxl355_core_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, adxl355_core_get_mount_matrix),
+	{ }
+};
+
 #define ADXL355_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = adxl355_core_ext_info,				\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -792,6 +808,13 @@ int adxl355_core_probe(struct device *dev, struct regmap *regmap,
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->regmap = regmap;
 	data->dev = dev;
 	data->op_mode = ADXL355_STANDBY;
Signature · 1 line
2.53.0
author
#4 lore raw

PATCH v3 4/18 iio: accel: adxl367: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/adxl367.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/adxl367.c +23 -0
index 026db73057..1f11f09a44 100644
@@ -180,6 +180,7 @@ struct adxl367_state {
 	unsigned int	fifo_set_size;
 	unsigned int	fifo_watermark;
 
+	struct iio_mount_matrix orientation;
 	__be16		fifo_buf[ADXL367_FIFO_SIZE] __aligned(IIO_DMA_MINALIGN);
 	__be16		sample_buf;
 	u8		act_threshold_buf[2];
@@ -1336,8 +1337,23 @@ static const struct iio_event_spec adxl367_events[] = {
 	},
 };
 
+static const struct iio_mount_matrix *
+adxl367_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct adxl367_state *st = iio_priv(indio_dev);
+
+	return &st->orientation;
+}
+
+static const struct iio_chan_spec_ext_info adxl367_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, adxl367_get_mount_matrix),
+	{ }
+};
+
 #define ADXL367_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = adxl367_ext_info,					\
 	.address = (reg),						\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -1477,6 +1493,13 @@ int adxl367_probe(struct device *dev, const struct adxl367_ops *ops,
 		return -ENOMEM;
 
 	st = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &st->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &st->orientation);
+		if (ret)
+			return ret;
+	}
+
 	st->dev = dev;
 	st->regmap = regmap;
 	st->context = context;
Signature · 1 line
2.53.0
author
#5 lore raw

PATCH v3 5/18 iio: accel: adxl372: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/adxl372.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/adxl372.c +27 -0
index e375d068a3..becec9a491 100644
@@ -315,8 +315,18 @@ static const struct iio_event_spec adxl372_events[] = {
 	},
 };
 
+static const struct iio_mount_matrix *
+adxl372_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info adxl372_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, adxl372_get_mount_matrix),
+	{ }
+};
+
 #define ADXL372_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = adxl372_ext_info,					\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -368,8 +378,18 @@ struct adxl372_state {
 	__be16				fifo_buf[ADXL372_FIFO_SIZE];
 	bool				peak_fifo_mode_en;
 	struct mutex			threshold_m; /* lock for threshold */
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+adxl372_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct adxl372_state *st = iio_priv(indio_dev);
+
+	return &st->orientation;
+}
+
 static const unsigned long adxl372_channel_masks[] = {
 	BIT(0), BIT(1), BIT(2),
 	BIT(0) | BIT(1),
@@ -1292,6 +1312,13 @@ int adxl372_probe(struct device *dev, struct regmap *regmap,
 		return -ENOMEM;
 
 	st = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &st->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &st->orientation);
+		if (ret)
+			return ret;
+	}
+
 	dev_set_drvdata(dev, indio_dev);
 
 	st->dev = dev;
Signature · 1 line
2.53.0
author
#6 lore raw

PATCH v3 6/18 iio: accel: adxl380: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/adxl380.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/iio/accel/adxl380.c +22 -0
index 03b1403035..435ba9d997 100644
@@ -223,6 +223,7 @@ struct adxl380_state {
 	int lpf_tbl[4];
 	int hpf_tbl[7][2];
 
+	struct iio_mount_matrix orientation;
 	__be16 fifo_buf[ADXL380_FIFO_SAMPLES] __aligned(IIO_DMA_MINALIGN);
 };
 
@@ -1785,8 +1786,23 @@ static const struct iio_event_spec adxl380_events[] = {
 	},
 };
 
+static const struct iio_mount_matrix *
+adxl380_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct adxl380_state *st = iio_priv(indio_dev);
+
+	return &st->orientation;
+}
+
+static const struct iio_chan_spec_ext_info adxl380_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, adxl380_get_mount_matrix),
+	{ }
+};
+
 #define ADXL380_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = adxl380_ext_info,					\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -1978,6 +1994,12 @@ int adxl380_probe(struct device *dev, struct regmap *regmap,
 
 	st = iio_priv(indio_dev);
 
+	if (!iio_read_acpi_mount_matrix(dev, &st->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &st->orientation);
+		if (ret)
+			return ret;
+	}
+
 	st->dev = dev;
 	st->regmap = regmap;
 	st->chip_info = chip_info;
Signature · 1 line
2.53.0
author
#7 lore raw

PATCH v3 7/18 iio: accel: bma220_core: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/bma220_core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/bma220_core.c +27 -0
index b83abfdd84..150d8db2c4 100644
@@ -94,8 +94,18 @@
 #define BMA220_COF_64Hz				0x4
 #define BMA220_COF_32Hz				0x5
 
+static const struct iio_mount_matrix *
+bma220_core_get_mount_matrix(const struct iio_dev *indio_dev,
+			     const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info bma220_core_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, bma220_core_get_mount_matrix),
+	{ }
+};
+
 #define BMA220_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = bma220_core_ext_info,				\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -130,6 +140,7 @@ struct bma220_data {
 	u8 lpf_3dB_freq_idx;
 	u8 range_idx;
 	struct iio_trigger *trig;
+	struct iio_mount_matrix orientation;
 	struct {
 		s8 chans[3];
 		/* Ensure timestamp is naturally aligned. */
@@ -137,6 +148,15 @@ struct bma220_data {
 	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
+static const struct iio_mount_matrix *
+bma220_core_get_mount_matrix(const struct iio_dev *indio_dev,
+			     const struct iio_chan_spec *chan)
+{
+	struct bma220_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
 static const struct iio_chan_spec bma220_channels[] = {
 	BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
 	BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
@@ -508,6 +528,13 @@ int bma220_common_probe(struct device *dev, struct regmap *regmap, int irq)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->regmap = regmap;
 
 	ret = bma220_init(dev, data);
Signature · 1 line
2.53.0
author
#8 lore raw

PATCH v3 8/18 iio: accel: bmi088-accel-core: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/bmi088-accel-core.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/bmi088-accel-core.c +23 -0
index c7da90af0d..33ec4f72b3 100644
@@ -125,6 +125,7 @@ struct bmi088_accel_chip_info {
 struct bmi088_accel_data {
 	struct regmap *regmap;
 	const struct bmi088_accel_chip_info *chip_info;
+	struct iio_mount_matrix orientation;
 	u8 buffer[2] __aligned(IIO_DMA_MINALIGN); /* shared DMA safe buffer */
 };
 
@@ -433,8 +434,23 @@ static int bmi088_accel_write_raw(struct iio_dev *indio_dev,
 	}
 }
 
+static const struct iio_mount_matrix *
+bmi088_accel_core_get_mount_matrix(const struct iio_dev *indio_dev,
+				   const struct iio_chan_spec *chan)
+{
+	struct bmi088_accel_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info bmi088_accel_core_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, bmi088_accel_core_get_mount_matrix),
+	{ }
+};
+
 #define BMI088_ACCEL_CHANNEL(_axis) { \
 	.type = IIO_ACCEL, \
+	.ext_info = bmi088_accel_core_ext_info,\
 	.modified = 1, \
 	.channel2 = IIO_MOD_##_axis, \
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
@@ -555,6 +571,13 @@ int bmi088_accel_core_probe(struct device *dev, struct regmap *regmap,
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	dev_set_drvdata(dev, indio_dev);
 
 	data->regmap = regmap;
Signature · 1 line
2.53.0
author
#9 lore raw

PATCH v3 9/18 iio: accel: da280: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/da280.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/da280.c +27 -0
index 15acbe1c2c..20c34938ed 100644
@@ -30,8 +30,18 @@
 
 static const int da280_nscale = 2395019;
 
+static const struct iio_mount_matrix *
+da280_get_mount_matrix(const struct iio_dev *indio_dev,
+		       const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info da280_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, da280_get_mount_matrix),
+	{ }
+};
+
 #define DA280_CHANNEL(reg, axis) {	\
 	.type = IIO_ACCEL,	\
+	.ext_info = da280_ext_info,\
 	.address = reg,	\
 	.modified = 1,	\
 	.channel2 = IIO_MOD_##axis,	\
@@ -52,8 +62,18 @@ struct da280_match_data {
 
 struct da280_data {
 	struct i2c_client *client;
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+da280_get_mount_matrix(const struct iio_dev *indio_dev,
+		       const struct iio_chan_spec *chan)
+{
+	struct da280_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
 static int da280_enable(struct i2c_client *client, bool enable)
 {
 	u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
@@ -119,6 +139,13 @@ static int da280_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 
 	indio_dev->info = &da280_info;
Signature · 1 line
2.53.0
author
#10 lore raw

PATCH v3 10/18 iio: accel: da311: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/da311.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/da311.c +27 -0
index dcb69c8eaf..beee8bf508 100644
@@ -77,8 +77,18 @@
 
 static const int da311_nscale = 9580078;
 
+static const struct iio_mount_matrix *
+da311_get_mount_matrix(const struct iio_dev *indio_dev,
+		       const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info da311_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, da311_get_mount_matrix),
+	{ }
+};
+
 #define DA311_CHANNEL(reg, axis) {	\
 	.type = IIO_ACCEL,	\
+	.ext_info = da311_ext_info,\
 	.address = reg,	\
 	.modified = 1,	\
 	.channel2 = IIO_MOD_##axis,	\
@@ -95,8 +105,18 @@ static const struct iio_chan_spec da311_channels[] = {
 
 struct da311_data {
 	struct i2c_client *client;
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+da311_get_mount_matrix(const struct iio_dev *indio_dev,
+		       const struct iio_chan_spec *chan)
+{
+	struct da311_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
 static int da311_register_mask_write(struct i2c_client *client, u16 addr,
 				     u8 mask, u8 data)
 {
@@ -232,6 +252,13 @@ static int da311_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 
 	indio_dev->info = &da311_info;
Signature · 1 line
2.53.0
author
#11 lore raw

PATCH v3 11/18 iio: accel: dmard06: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/dmard06.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/dmard06.c +27 -0
index 82816fa443..b7b152cdc7 100644
@@ -34,9 +34,19 @@
 #define DMARD06_MODE_NORMAL		0x27
 #define DMARD06_MODE_POWERDOWN		0x00
 
+static const struct iio_mount_matrix *
+dmard06_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info dmard06_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, dmard06_get_mount_matrix),
+	{ }
+};
+
 /* Device channels */
 #define DMARD06_ACCEL_CHANNEL(_axis, _reg) {			\
 	.type = IIO_ACCEL,					\
+	.ext_info = dmard06_ext_info,				\
 	.address = _reg,					\
 	.channel2 = IIO_MOD_##_axis,				\
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
@@ -54,8 +64,18 @@
 struct dmard06_data {
 	struct i2c_client *client;
 	u8 chip_id;
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+dmard06_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct dmard06_data *dmard06 = iio_priv(indio_dev);
+
+	return &dmard06->orientation;
+}
+
 static const struct iio_chan_spec dmard06_channels[] = {
 	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
 	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
@@ -140,6 +160,13 @@ static int dmard06_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	dmard06 = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &dmard06->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &dmard06->orientation);
+		if (ret)
+			return ret;
+	}
+
 	dmard06->client = client;
 
 	ret = i2c_smbus_read_byte_data(dmard06->client, DMARD06_CHIP_ID_REG);
Signature · 1 line
2.53.0
author
#12 lore raw

PATCH v3 12/18 iio: accel: dmard09: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/dmard09.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/dmard09.c +23 -0
index 6f0497ab61..8c308836f0 100644
@@ -30,10 +30,26 @@
 
 struct dmard09_data {
 	struct i2c_client *client;
+	struct iio_mount_matrix orientation;
+};
+
+static const struct iio_mount_matrix *
+dmard09_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct dmard09_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info dmard09_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, dmard09_get_mount_matrix),
+	{ }
 };
 
 #define DMARD09_CHANNEL(_axis, offset) {			\
 	.type = IIO_ACCEL,					\
+	.ext_info = dmard09_ext_info,				\
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
 	.modified = 1,						\
@@ -106,6 +122,13 @@ static int dmard09_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 
 	ret = i2c_smbus_read_byte_data(data->client, DMARD09_REG_CHIPID);
Signature · 1 line
2.53.0
author
#13 lore raw

PATCH v3 13/18 iio: accel: dmard10: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/dmard10.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/dmard10.c +27 -0
index 8ba00fd579..d57fc129ba 100644
@@ -51,8 +51,18 @@
 
 static const int dmard10_nscale = 76640625;
 
+static const struct iio_mount_matrix *
+dmard10_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info dmard10_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, dmard10_get_mount_matrix),
+	{ }
+};
+
 #define DMARD10_CHANNEL(reg, axis) {	\
 	.type = IIO_ACCEL,	\
+	.ext_info = dmard10_ext_info,\
 	.address = reg,	\
 	.modified = 1,	\
 	.channel2 = IIO_MOD_##axis,	\
@@ -68,8 +78,18 @@ static const struct iio_chan_spec dmard10_channels[] = {
 
 struct dmard10_data {
 	struct i2c_client *client;
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+dmard10_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct dmard10_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
 /* Init sequence taken from the android driver */
 static int dmard10_reset(struct i2c_client *client)
 {
@@ -195,6 +215,13 @@ static int dmard10_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 
 	indio_dev->info = &dmard10_info;
Signature · 1 line
2.53.0
author
#14 lore raw

PATCH v3 14/18 iio: accel: mma7455_core: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/mma7455_core.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/mma7455_core.c +23 -0
index a2b5bdf14d..57c1b500bf 100644
@@ -61,6 +61,7 @@ struct mma7455_data {
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan;
+	struct iio_mount_matrix orientation;
 };
 
 static int mma7455_drdy(struct mma7455_data *mma7455)
@@ -209,8 +210,23 @@ static const struct iio_info mma7455_info = {
 	.write_raw = mma7455_write_raw,
 };
 
+static const struct iio_mount_matrix *
+mma7455_core_get_mount_matrix(const struct iio_dev *indio_dev,
+			      const struct iio_chan_spec *chan)
+{
+	struct mma7455_data *mma7455 = iio_priv(indio_dev);
+
+	return &mma7455->orientation;
+}
+
+static const struct iio_chan_spec_ext_info mma7455_core_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mma7455_core_get_mount_matrix),
+	{ }
+};
+
 #define MMA7455_CHANNEL(axis, idx) { \
 	.type = IIO_ACCEL, \
+	.ext_info = mma7455_core_ext_info,\
 	.modified = 1, \
 	.address = MMA7455_REG_##axis##OUTL,\
 	.channel2 = IIO_MOD_##axis, \
@@ -267,6 +283,13 @@ int mma7455_core_probe(struct device *dev, struct regmap *regmap,
 
 	dev_set_drvdata(dev, indio_dev);
 	mma7455 = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &mma7455->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(dev, &mma7455->orientation);
+		if (ret)
+			return ret;
+	}
+
 	mma7455->regmap = regmap;
 
 	indio_dev->info = &mma7455_info;
Signature · 1 line
2.53.0
author
#15 lore raw

PATCH v3 15/18 iio: accel: msa311: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/msa311.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/iio/accel/msa311.c +24 -0
index caf9b1b639..611c29fe8f 100644
@@ -351,6 +351,7 @@ static const struct regmap_config msa311_regmap_config = {
  * @chip_name: Chip name in the format "msa311-%02x" % partid
  * @new_data_trig: Optional NEW_DATA interrupt driven trigger used
  *                 to notify external consumers a new sample is ready
+ * @orientation: mount matrix describing the sensor mounting orientation
  */
 struct msa311_priv {
 	struct regmap *regs;
@@ -361,6 +362,7 @@ struct msa311_priv {
 	char *chip_name;
 
 	struct iio_trigger *new_data_trig;
+	struct iio_mount_matrix orientation;
 };
 
 enum msa311_si {
@@ -370,8 +372,23 @@ enum msa311_si {
 	MSA311_SI_TIMESTAMP,
 };
 
+static const struct iio_mount_matrix *
+msa311_get_mount_matrix(const struct iio_dev *indio_dev,
+			const struct iio_chan_spec *chan)
+{
+	struct msa311_priv *msa311 = iio_priv(indio_dev);
+
+	return &msa311->orientation;
+}
+
+static const struct iio_chan_spec_ext_info msa311_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, msa311_get_mount_matrix),
+	{ }
+};
+
 #define MSA311_ACCEL_CHANNEL(axis) {                                        \
 	.type = IIO_ACCEL,                                                  \
+	.ext_info = msa311_ext_info,					    \
 	.modified = 1,                                                      \
 	.channel2 = IIO_MOD_##axis,                                         \
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                       \
@@ -1149,6 +1166,13 @@ static int msa311_probe(struct i2c_client *i2c)
 		return -ENOMEM;
 
 	msa311 = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(dev, &msa311->orientation, "ROTM")) {
+		err = iio_read_mount_matrix(dev, &msa311->orientation);
+		if (err)
+			return err;
+	}
+
 	msa311->dev = dev;
 	i2c_set_clientdata(i2c, indio_dev);
 
Signature · 1 line
2.53.0
author
#16 lore raw

PATCH v3 16/18 iio: accel: mxc6255: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/mxc6255.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iio/accel/mxc6255.c +23 -0
index a9f41cf27a..e14ca1509a 100644
@@ -42,6 +42,7 @@ enum mxc6255_axis {
 struct mxc6255_data {
 	struct i2c_client *client;
 	struct regmap *regmap;
+	struct iio_mount_matrix orientation;
 };
 
 static int mxc6255_read_raw(struct iio_dev *indio_dev,
@@ -76,8 +77,23 @@ static const struct iio_info mxc6255_info = {
 	.read_raw	= mxc6255_read_raw,
 };
 
+static const struct iio_mount_matrix *
+mxc6255_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct mxc6255_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info mxc6255_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mxc6255_get_mount_matrix),
+	{ }
+};
+
 #define MXC6255_CHANNEL(_axis, reg) {				\
 	.type = IIO_ACCEL,					\
+	.ext_info = mxc6255_ext_info,				\
 	.modified = 1,						\
 	.channel2 = IIO_MOD_##_axis,				\
 	.address = reg,						\
@@ -130,6 +146,13 @@ static int mxc6255_probe(struct i2c_client *client)
 	}
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	i2c_set_clientdata(client, indio_dev);
 	data->client = client;
 	data->regmap = regmap;
Signature · 1 line
2.53.0
author
#17 lore raw

PATCH v3 17/18 iio: accel: sca3300: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/sca3300.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/accel/sca3300.c +28 -0
index 67582cc341..a5eeded322 100644
@@ -56,8 +56,18 @@ enum sca3300_scan_indexes {
 	SCA3300_SCAN_MAX
 };
 
+static const struct iio_mount_matrix *
+sca3300_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info sca3300_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, sca3300_get_mount_matrix),
+	{ }
+};
+
 #define SCA3300_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = sca3300_ext_info,					\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -184,15 +194,26 @@ struct sca3300_chip_info {
  * @chip: Sensor chip specific information
  * @txbuf: Transmit buffer
  * @rxbuf: Receive buffer
+ * @orientation: mount matrix describing the sensor mounting orientation
  */
 struct sca3300_data {
 	struct spi_device *spi;
 	struct mutex lock;
 	const struct sca3300_chip_info *chip;
+	struct iio_mount_matrix orientation;
 	u8 txbuf[4] __aligned(IIO_DMA_MINALIGN);
 	u8 rxbuf[4];
 };
 
+static const struct iio_mount_matrix *
+sca3300_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct sca3300_data *sca_data = iio_priv(indio_dev);
+
+	return &sca_data->orientation;
+}
+
 static const struct sca3300_chip_info sca3300_chip_tbl[] = {
 	{
 		.name = "sca3300",
@@ -619,6 +640,13 @@ static int sca3300_probe(struct spi_device *spi)
 		return -ENOMEM;
 
 	sca_data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&spi->dev, &sca_data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&spi->dev, &sca_data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	mutex_init(&sca_data->lock);
 	sca_data->spi = spi;
 
Signature · 1 line
2.53.0
author
#18 lore raw

PATCH v3 18/18 iio: accel: stk8312: support mount matrix

In reply to He Xin

The driver ignores the mount matrix described by the firmware (ACPI _DSD "mount-matrix" or the devicetree property), so the orientation reported to userspace is wrong whenever the sensor is not mounted axis-aligned with the display.

Read the mount matrix at probe time and expose it through IIO_MOUNT_MATRIX(), as several other accelerometer drivers already do.

---
 drivers/iio/accel/stk8312.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/iio/accel/stk8312.c +27 -0
index 50840c3440..f44198b53b 100644
@@ -78,8 +78,18 @@ static const struct {
 	{12, 500000}, {6, 250000}, {3, 125000}
 };
 
+static const struct iio_mount_matrix *
+stk8312_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan);
+
+static const struct iio_chan_spec_ext_info stk8312_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, stk8312_get_mount_matrix),
+	{ }
+};
+
 #define STK8312_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
+	.ext_info = stk8312_ext_info,					\
 	.address = reg,							\
 	.modified = 1,							\
 	.channel2 = IIO_MOD_##axis,					\
@@ -115,8 +125,18 @@ struct stk8312_data {
 		s8 chans[3];
 		aligned_s64 timestamp;
 	} scan;
+	struct iio_mount_matrix orientation;
 };
 
+static const struct iio_mount_matrix *
+stk8312_get_mount_matrix(const struct iio_dev *indio_dev,
+			 const struct iio_chan_spec *chan)
+{
+	struct stk8312_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
 
 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
@@ -516,6 +536,13 @@ static int stk8312_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	data->client = client;
 	i2c_set_clientdata(client, indio_dev);
 	mutex_init(&data->lock);
Signature · 1 line
2.53.0
#19 lore raw
In reply to He Xin
Quoted 27 lines · On Fri, Dec 04, 2026 at 07:12:51PM +0800, He Xin via B4 Relay wrote:

These drivers ignore the mount matrix described by the firmware, so userspace gets raw sensor axes on any board whose sensor is not mounted axis-aligned with the display. A udev/hwdb override does not cover it either: it keys off the sensor modalias and DMI, which do not tell such boards apart.

Each patch follows the pattern of the drivers that already support this: try the ACPI ROTM method first, fall back to the "mount-matrix" property (devicetree, or _DSD on ACPI platforms), and keep the result in the driver's private data so it can be exposed through IIO_MOUNT_MATRIX(). The patches remain independent of each other.

The ACPI tables on the platform this was tested on are part of the board firmware rather than a local SSDT overlay, and that firmware provides "mount-matrix" through _DSD today. Adding the ROTM method to it later does not require touching these drivers again.

Andy suggested on the mxc6255 patch that the matrix belongs in the IIO core rather than in every driver. I kept the per-driver shape because that is what kxcjk-1013, bmc150-accel, mxc4005, bmi323 and st_lsm6dsx already do; moving parsing and storage into the core, together with those five, is a separate change and I can send it as a follow-up.

One behaviour change: a board whose firmware describes the property with the wrong number of entries now fails to probe with -EINVAL instead of being ignored, which is what the drivers that already support the property do. A board that provides neither ROTM nor the property keeps the identity matrix.

Quoted 6 lines

Not converted here, because the change would not have the same shape:
adis16201, adis16209 and sca3000 list their channels as array literals, adxl313_core and fxls8962af-core keep the probe in the bus wrappers, and mma9551/mma9553 differ in both. hid-sensor-accel-3d, ssp_accel_sensor and cros_ec_accel_legacy take the orientation from the HID/SSP/EC side rather than from a firmware property.

The v2 was not settled yet. Please, read v2 replies in act accordingly.

With such a series you need to wait at least 24h from the last reply on the previous version and at least 1w from the previous round.

Signature · 2 lines
With Best Regards,
Andy Shevchenko