1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IIO ACPI helper functions */
4 #include <linux/acpi.h>
5 #include <linux/dev_printk.h>
6 #include <linux/iio/iio.h>
7 #include <linux/sprintf.h>
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"
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.
24 * Returns: true if the matrix was successfully, false otherwise.
26 bool iio_read_acpi_mount_matrix(struct device
*dev
,
27 struct iio_mount_matrix
*orientation
,
30 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
31 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
33 union acpi_object
*obj
, *elements
;
38 if (!adev
|| !acpi_has_method(adev
->handle
, acpi_method
))
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
);
48 if (obj
->type
!= ACPI_TYPE_PACKAGE
|| obj
->package
.count
!= 3) {
49 dev_err(dev
, "Unknown ACPI mount matrix package format\n");
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");
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");
66 for (j
= 0; j
< 3; j
++) {
68 case -1: str
= "-1"; break;
69 case 0: str
= "0"; break;
70 case 1: str
= "1"; break;
72 dev_err(dev
, "Invalid value in ACPI mount matrix: %d\n", val
[j
]);
75 orientation
->rotation
[i
* 3 + j
] = str
;
82 kfree(buffer
.pointer
);
85 EXPORT_SYMBOL_GPL(iio_read_acpi_mount_matrix
);