]> Git Repo - linux.git/blob - drivers/iio/industrialio-acpi.c
x86/kaslr: Expose and use the end of the physical memory address space
[linux.git] / 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);
This page took 0.036515 seconds and 4 git commands to generate.