Skip to contentSkip to conversation

iio: accel: Share ACPI ROTM parsing between drivers and add it to mxc4005

PATCH In review Hans de Goede 2 participants 6 messages Apr 25, 2024 – Apr 28, 2024 IIO

lore
Share & tools
lore
#0 lore raw

v3 cover iio: accel: Share ACPI ROTM parsing between drivers and add it to mxc4005

Hi All,

Here is v2 of the patch series to refactor the ACPI ROTM orientation matrix handling in kxcjk-1013 + bmc150-accel to share the code instead of having 2 copies and then also use the shared implementation in the mxc4005 driver since some MXC6655 ACPI firmware nodes also include this.

Changes in v2:
- Add comment about the Microsoft specification for the "ROTM" ACPI method

Changes in v3:
- Add the new iio_read_acpi_mount_matrix() helper to industrialio.ko instead of making it a static inline

Regards,

Hans

Hans de Goede (4):
iio: core: Add iio_read_acpi_mount_matrix() helper function
iio: accel: kxcjk-1013: Use new iio_read_acpi_mount_matrix() helper
iio: bmc150-accel-core: Use iio_read_acpi_mount_matrix() helper
iio: accel: mxc4005: Read orientation matrix from ACPI ROTM method

 drivers/iio/Makefile                  |  1 +
 drivers/iio/accel/bmc150-accel-core.c | 44 +-------------
 drivers/iio/accel/kxcjk-1013.c        | 80 +------------------------
 drivers/iio/accel/mxc4005.c           | 22 +++++++
 drivers/iio/industrialio-acpi.c       | 85 +++++++++++++++++++++++++++
 include/linux/iio/iio.h               | 13 ++++
 6 files changed, 124 insertions(+), 121 deletions(-)
 create mode 100644 drivers/iio/industrialio-acpi.c
Signature · 1 line
2.44.0
#1 lore raw

PATCH v3 1/4 iio: core: Add iio_read_acpi_mount_matrix() helper function

The ACPI "ROTM" rotation matrix parsing code atm is already duplicated between bmc150-accel-core.c and kxcjk-1013.c and a third user of this is coming.

Add an iio_read_acpi_mount_matrix() helper function for this. The 2 existing copies of the code are identical, except that the kxcjk-1013.c has slightly better error logging.

To new helper is a 1:1 copy of the kxcjk-1013.c version, the only change is the addition of a "char *acpi_method" parameter since some bmc150 dual-accel setups (360° hinges with 1 accel in kbd/base + 1 in display) declare both accels in a single ACPI device with 2 different method names for the 2 matrices. This new acpi_method parameter is not "const char *" because the pathname parameter to acpi_evaluate_object() is not const.

The 2 existing copies of this function will be removed in further patches in this series.

---
Changes in v3:
- New patch in v3 of this series.

---
 drivers/iio/Makefile            |  1 +
 drivers/iio/industrialio-acpi.c | 85 +++++++++++++++++++++++++++++++++
 include/linux/iio/iio.h         | 13 +++++
 3 files changed, 99 insertions(+)
 create mode 100644 drivers/iio/industrialio-acpi.c
drivers/iio/Makefile +1 -0
index 0ba0e1521ba4..cb80ef837e84 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_IIO) += industrialio.o
 industrialio-y := industrialio-core.o industrialio-event.o inkern.o
 industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
 industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
+industrialio-$(CONFIG_ACPI) += industrialio-acpi.o
 
 obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o
 obj-$(CONFIG_IIO_GTS_HELPER) += industrialio-gts-helper.o
drivers/iio/industrialio-acpi.c new file +85 -0

85 lines not on this page · raw message

include/linux/iio/iio.h +13 -0
index e370a7bb3300..4ed41e70c86e 100644
@@ -788,6 +788,19 @@ static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
 }
 #endif
 
+#ifdef CONFIG_ACPI
+bool iio_read_acpi_mount_matrix(struct device *dev,
+				struct iio_mount_matrix *matrix,
+				char *method_name);
+#else
+static inline bool iio_read_acpi_mount_matrix(struct device *dev,
+					      struct iio_mount_matrix *matrix,
+					      char *method_name)
+{
+	return false;
+}
+#endif
+
 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
 
 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
Signature · 1 line
2.44.0
#2 lore raw

PATCH v3 2/4 iio: accel: kxcjk-1013: Use new iio_read_acpi_mount_matrix() helper

In reply to Hans de Goede

Replace the duplicate ACPI "ROTM" data parsing code with the new shared iio_read_acpi_mount_matrix() helper.

This also removes the limiting of the "ROTM" mount matrix to only ACPI devices with an ACPI HID (Hardware-ID) of "KIOX000A". If kxcjk-1013 ACPI devices with another HID have a ROTM method that should still be parsed and if the method is not there then iio_read_acpi_mount_matrix() will fail silently.

---
 drivers/iio/accel/kxcjk-1013.c | 80 +---------------------------------
 1 file changed, 1 insertion(+), 79 deletions(-)
drivers/iio/accel/kxcjk-1013.c +1 -79
index 126e8bdd6d0e..8280d2bef0a3 100644
@@ -636,84 +636,6 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	return 0;
 }
 
-#ifdef CONFIG_ACPI
-static bool kxj_acpi_orientation(struct device *dev,
-				 struct iio_mount_matrix *orientation)
-{
-	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-	struct acpi_device *adev = ACPI_COMPANION(dev);
-	char *str;
-	union acpi_object *obj, *elements;
-	acpi_status status;
-	int i, j, val[3];
-	bool ret = false;
-
-	if (!acpi_has_method(adev->handle, "ROTM"))
-		return false;
-
-	status = acpi_evaluate_object(adev->handle, "ROTM", NULL, &buffer);
-	if (ACPI_FAILURE(status)) {
-		dev_err(dev, "Failed to get ACPI mount matrix: %d\n", status);
-		return false;
-	}
-
-	obj = buffer.pointer;
-	if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3) {
-		dev_err(dev, "Unknown ACPI mount matrix package format\n");
-		goto out_free_buffer;
-	}
-
-	elements = obj->package.elements;
-	for (i = 0; i < 3; i++) {
-		if (elements[i].type != ACPI_TYPE_STRING) {
-			dev_err(dev, "Unknown ACPI mount matrix element format\n");
-			goto out_free_buffer;
-		}
-
-		str = elements[i].string.pointer;
-		if (sscanf(str, "%d %d %d", &val[0], &val[1], &val[2]) != 3) {
-			dev_err(dev, "Incorrect ACPI mount matrix string format\n");
-			goto out_free_buffer;
-		}
-
-		for (j = 0; j < 3; j++) {
-			switch (val[j]) {
-			case -1: str = "-1"; break;
-			case 0:  str = "0";  break;
-			case 1:  str = "1";  break;
-			default:
-				dev_err(dev, "Invalid value in ACPI mount matrix: %d\n", val[j]);
-				goto out_free_buffer;
-			}
-			orientation->rotation[i * 3 + j] = str;
-		}
-	}
-
-	ret = true;
-
-out_free_buffer:
-	kfree(buffer.pointer);
-	return ret;
-}
-
-static bool kxj1009_apply_acpi_orientation(struct device *dev,
-					  struct iio_mount_matrix *orientation)
-{
-	struct acpi_device *adev = ACPI_COMPANION(dev);
-
-	if (adev && acpi_dev_hid_uid_match(adev, "KIOX000A", NULL))
-		return kxj_acpi_orientation(dev, orientation);
-
-	return false;
-}
-#else
-static bool kxj1009_apply_acpi_orientation(struct device *dev,
-					  struct iio_mount_matrix *orientation)
-{
-	return false;
-}
-#endif
-
 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data)
 {
 	int ret;
@@ -1544,7 +1466,7 @@ static int kxcjk1013_probe(struct i2c_client *client)
 	} else {
 		data->active_high_intr = true; /* default polarity */
 
-		if (!kxj1009_apply_acpi_orientation(&client->dev, &data->orientation)) {
+		if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
 			ret = iio_read_mount_matrix(&client->dev, &data->orientation);
 			if (ret)
 				return ret;
Signature · 1 line
2.44.0
#3 lore raw

PATCH v3 3/4 iio: bmc150-accel-core: Use iio_read_acpi_mount_matrix() helper

In reply to Hans de Goede

Replace the duplicate ACPI "ROTM" data parsing code with the new shared iio_read_acpi_mount_matrix() helper.

---
 drivers/iio/accel/bmc150-accel-core.c | 44 ++-------------------------
 1 file changed, 2 insertions(+), 42 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +2 -42
index 110591804b4c..ae0cd48a3e29 100644
@@ -386,13 +386,9 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 static bool bmc150_apply_bosc0200_acpi_orientation(struct device *dev,
 						   struct iio_mount_matrix *orientation)
 {
-	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
 	struct acpi_device *adev = ACPI_COMPANION(dev);
-	char *name, *alt_name, *label, *str;
-	union acpi_object *obj, *elements;
-	acpi_status status;
-	int i, j, val[3];
+	char *name, *alt_name, *label;
 
 	if (strcmp(dev_name(dev), "i2c-BOSC0200:base") == 0) {
 		alt_name = "ROMK";
@@ -411,43 +407,7 @@ static bool bmc150_apply_bosc0200_acpi_orientation(struct device *dev,
 		return false;
 	}
 
-	status = acpi_evaluate_object(adev->handle, name, NULL, &buffer);
-	if (ACPI_FAILURE(status)) {
-		dev_warn(dev, "Failed to get ACPI mount matrix: %d\n", status);
-		return false;
-	}
-
-	obj = buffer.pointer;
-	if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3)
-		goto unknown_format;
-
-	elements = obj->package.elements;
-	for (i = 0; i < 3; i++) {
-		if (elements[i].type != ACPI_TYPE_STRING)
-			goto unknown_format;
-
-		str = elements[i].string.pointer;
-		if (sscanf(str, "%d %d %d", &val[0], &val[1], &val[2]) != 3)
-			goto unknown_format;
-
-		for (j = 0; j < 3; j++) {
-			switch (val[j]) {
-			case -1: str = "-1"; break;
-			case 0:  str = "0";  break;
-			case 1:  str = "1";  break;
-			default: goto unknown_format;
-			}
-			orientation->rotation[i * 3 + j] = str;
-		}
-	}
-
-	kfree(buffer.pointer);
-	return true;
-
-unknown_format:
-	dev_warn(dev, "Unknown ACPI mount matrix format, ignoring\n");
-	kfree(buffer.pointer);
-	return false;
+	return iio_read_acpi_mount_matrix(dev, orientation, name);
 }
 
 static bool bmc150_apply_dual250e_acpi_orientation(struct device *dev,
Signature · 1 line
2.44.0
#4 lore raw

PATCH v3 4/4 iio: accel: mxc4005: Read orientation matrix from ACPI ROTM method

In reply to Hans de Goede

Some devices use the semi-standard ACPI "ROTM" method to store the accelerometers orientation matrix.

Add support for this using the new iio_read_acpi_mount_matrix() helper, if the helper fails to read the matrix fall back to iio_read_mount_matrix() which will try to get it from device-properties (devicetree) and if that fails it will fill the matrix with the identity matrix.

---
 drivers/iio/accel/mxc4005.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/iio/accel/mxc4005.c +22 -0
index 9f38d3a08299..72f29e2af6df 100644
@@ -65,6 +65,7 @@ struct mxc4005_data {
 	struct mutex mutex;
 	struct regmap *regmap;
 	struct iio_trigger *dready_trig;
+	struct iio_mount_matrix orientation;
 	/* Ensure timestamp is naturally aligned */
 	struct {
 		__be16 chans[3];
@@ -272,6 +273,20 @@ static int mxc4005_write_raw(struct iio_dev *indio_dev,
 	}
 }
 
+static const struct iio_mount_matrix *
+mxc4005_get_mount_matrix(const struct iio_dev *indio_dev,
+			   const struct iio_chan_spec *chan)
+{
+	struct mxc4005_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info mxc4005_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mxc4005_get_mount_matrix),
+	{ }
+};
+
 static const struct iio_info mxc4005_info = {
 	.read_raw	= mxc4005_read_raw,
 	.write_raw	= mxc4005_write_raw,
@@ -298,6 +313,7 @@ static const unsigned long mxc4005_scan_masks[] = {
 		.shift = 4,					\
 		.endianness = IIO_BE,				\
 	},							\
+	.ext_info = mxc4005_ext_info,				\
 }
 
 static const struct iio_chan_spec mxc4005_channels[] = {
@@ -440,6 +456,12 @@ static int mxc4005_probe(struct i2c_client *client)
 
 	mutex_init(&data->mutex);
 
+	if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
+		ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+		if (ret)
+			return ret;
+	}
+
 	indio_dev->channels = mxc4005_channels;
 	indio_dev->num_channels = ARRAY_SIZE(mxc4005_channels);
 	indio_dev->available_scan_masks = mxc4005_scan_masks;
Signature · 1 line
2.44.0
maintainer
#5 lore raw
In reply to Hans de Goede
Quoted 10 lines · On Thu, 25 Apr 2024 14:57:50 +0200 Hans de Goede <[email protected]> wrote:

Hi All,

Here is v2 of the patch series to refactor the ACPI ROTM orientation matrix handling in kxcjk-1013 + bmc150-accel to share the code instead of having 2 copies and then also use the shared implementation in the mxc4005 driver since some MXC6655 ACPI firmware nodes also include this.

Changes in v2:
- Add comment about the Microsoft specification for the "ROTM" ACPI method

Quoted 7 lines

Changes in v3:
- Add the new iio_read_acpi_mount_matrix() helper to industrialio.ko instead of making it a static inline

Regards,

Hans

LGTM
Applied to the togreg branch of iio.git and pushed out as testing for 0-day to play with it.

Thanks for tidying this up.

Jonathan

Quoted 17 lines

Hans de Goede (4):
iio: core: Add iio_read_acpi_mount_matrix() helper function
iio: accel: kxcjk-1013: Use new iio_read_acpi_mount_matrix() helper
iio: bmc150-accel-core: Use iio_read_acpi_mount_matrix() helper
iio: accel: mxc4005: Read orientation matrix from ACPI ROTM method

 drivers/iio/Makefile                  |  1 +
 drivers/iio/accel/bmc150-accel-core.c | 44 +-------------
 drivers/iio/accel/kxcjk-1013.c        | 80 +------------------------
 drivers/iio/accel/mxc4005.c           | 22 +++++++
 drivers/iio/industrialio-acpi.c       | 85 +++++++++++++++++++++++++++
 include/linux/iio/iio.h               | 13 ++++
 6 files changed, 124 insertions(+), 121 deletions(-)
 create mode 100644 drivers/iio/industrialio-acpi.c