Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Link: https://lore.kernel.org/r/20240425125754.76010-2-hdegoede@redhat.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Hans de Goede and committed by
Jonathan Cameron
02eae0bb 79df437b

+99
+1
drivers/iio/Makefile
··· 7 7 industrialio-y := industrialio-core.o industrialio-event.o inkern.o 8 8 industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o 9 9 industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o 10 + industrialio-$(CONFIG_ACPI) += industrialio-acpi.o 10 11 11 12 obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o 12 13 obj-$(CONFIG_IIO_GTS_HELPER) += industrialio-gts-helper.o
+85
drivers/iio/industrialio-acpi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* IIO ACPI helper functions */ 3 + 4 + #include <linux/acpi.h> 5 + #include <linux/dev_printk.h> 6 + #include <linux/iio/iio.h> 7 + #include <linux/sprintf.h> 8 + 9 + /** 10 + * iio_read_acpi_mount_matrix() - Read accelerometer mount matrix info from ACPI 11 + * @dev: Device structure 12 + * @orientation: iio_mount_matrix struct to fill 13 + * @acpi_method: ACPI method name to read the matrix from, usually "ROTM" 14 + * 15 + * Try to read the mount-matrix by calling the specified method on the device's 16 + * ACPI firmware-node. If the device has no ACPI firmware-node; or the method 17 + * does not exist then this will fail silently. This expects the method to 18 + * return data in the ACPI "ROTM" format defined by Microsoft: 19 + * https://learn.microsoft.com/en-us/windows-hardware/drivers/sensors/sensors-acpi-entries 20 + * This is a Microsoft extension and not part of the official ACPI spec. 21 + * The method name is configurable because some dual-accel setups define 2 mount 22 + * matrices in a single ACPI device using separate "ROMK" and "ROMS" methods. 23 + * 24 + * Returns: true if the matrix was successfully, false otherwise. 25 + */ 26 + bool iio_read_acpi_mount_matrix(struct device *dev, 27 + struct iio_mount_matrix *orientation, 28 + char *acpi_method) 29 + { 30 + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 31 + struct acpi_device *adev = ACPI_COMPANION(dev); 32 + char *str; 33 + union acpi_object *obj, *elements; 34 + acpi_status status; 35 + int i, j, val[3]; 36 + bool ret = false; 37 + 38 + if (!adev || !acpi_has_method(adev->handle, acpi_method)) 39 + return false; 40 + 41 + status = acpi_evaluate_object(adev->handle, acpi_method, NULL, &buffer); 42 + if (ACPI_FAILURE(status)) { 43 + dev_err(dev, "Failed to get ACPI mount matrix: %d\n", status); 44 + return false; 45 + } 46 + 47 + obj = buffer.pointer; 48 + if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3) { 49 + dev_err(dev, "Unknown ACPI mount matrix package format\n"); 50 + goto out_free_buffer; 51 + } 52 + 53 + elements = obj->package.elements; 54 + for (i = 0; i < 3; i++) { 55 + if (elements[i].type != ACPI_TYPE_STRING) { 56 + dev_err(dev, "Unknown ACPI mount matrix element format\n"); 57 + goto out_free_buffer; 58 + } 59 + 60 + str = elements[i].string.pointer; 61 + if (sscanf(str, "%d %d %d", &val[0], &val[1], &val[2]) != 3) { 62 + dev_err(dev, "Incorrect ACPI mount matrix string format\n"); 63 + goto out_free_buffer; 64 + } 65 + 66 + for (j = 0; j < 3; j++) { 67 + switch (val[j]) { 68 + case -1: str = "-1"; break; 69 + case 0: str = "0"; break; 70 + case 1: str = "1"; break; 71 + default: 72 + dev_err(dev, "Invalid value in ACPI mount matrix: %d\n", val[j]); 73 + goto out_free_buffer; 74 + } 75 + orientation->rotation[i * 3 + j] = str; 76 + } 77 + } 78 + 79 + ret = true; 80 + 81 + out_free_buffer: 82 + kfree(buffer.pointer); 83 + return ret; 84 + } 85 + EXPORT_SYMBOL_GPL(iio_read_acpi_mount_matrix);
+13
include/linux/iio/iio.h
··· 788 788 } 789 789 #endif 790 790 791 + #ifdef CONFIG_ACPI 792 + bool iio_read_acpi_mount_matrix(struct device *dev, 793 + struct iio_mount_matrix *orientation, 794 + char *acpi_method); 795 + #else 796 + static inline bool iio_read_acpi_mount_matrix(struct device *dev, 797 + struct iio_mount_matrix *orientation, 798 + char *acpi_method) 799 + { 800 + return false; 801 + } 802 + #endif 803 + 791 804 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals); 792 805 793 806 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,