industrialio-acpi.c 2.6 KB

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