industrialio-acpi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IIO ACPI helper functions */
  3. #include <linux/acpi.h>
  4. #include <linux/device.h>
  5. #include <linux/export.h>
  6. #include <linux/iio/iio.h>
  7. #include <linux/sprintf.h>
  8. /**
  9. * iio_read_acpi_mount_matrix() - Read accelerometer mount matrix info from ACPI
  10. * @dev: Device structure
  11. * @orientation: iio_mount_matrix struct to fill
  12. * @acpi_method: ACPI method name to read the matrix from, usually "ROTM"
  13. *
  14. * Try to read the mount-matrix by calling the specified method on the device's
  15. * ACPI firmware-node. If the device has no ACPI firmware-node; or the method
  16. * does not exist then this will fail silently. This expects the method to
  17. * return data in the ACPI "ROTM" format defined by Microsoft:
  18. * https://learn.microsoft.com/en-us/windows-hardware/drivers/sensors/sensors-acpi-entries
  19. * This is a Microsoft extension and not part of the official ACPI spec.
  20. * The method name is configurable because some dual-accel setups define 2 mount
  21. * matrices in a single ACPI device using separate "ROMK" and "ROMS" methods.
  22. *
  23. * Returns: true if the matrix was successfully, false otherwise.
  24. */
  25. bool iio_read_acpi_mount_matrix(struct device *dev,
  26. struct iio_mount_matrix *orientation,
  27. char *acpi_method)
  28. {
  29. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  30. char *str;
  31. union acpi_object *obj, *elements;
  32. acpi_handle handle;
  33. acpi_status status;
  34. int i, j, val[3];
  35. bool ret = false;
  36. handle = ACPI_HANDLE(dev);
  37. if (!handle)
  38. return false;
  39. if (!acpi_has_method(handle, acpi_method))
  40. return false;
  41. status = acpi_evaluate_object(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. obj = buffer.pointer;
  47. if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3) {
  48. dev_err(dev, "Unknown ACPI mount matrix package format\n");
  49. goto out_free_buffer;
  50. }
  51. elements = obj->package.elements;
  52. for (i = 0; i < 3; i++) {
  53. if (elements[i].type != ACPI_TYPE_STRING) {
  54. dev_err(dev, "Unknown ACPI mount matrix element format\n");
  55. goto out_free_buffer;
  56. }
  57. str = elements[i].string.pointer;
  58. if (sscanf(str, "%d %d %d", &val[0], &val[1], &val[2]) != 3) {
  59. dev_err(dev, "Incorrect ACPI mount matrix string format\n");
  60. goto out_free_buffer;
  61. }
  62. for (j = 0; j < 3; j++) {
  63. switch (val[j]) {
  64. case -1: str = "-1"; break;
  65. case 0: str = "0"; break;
  66. case 1: str = "1"; break;
  67. default:
  68. dev_err(dev, "Invalid value in ACPI mount matrix: %d\n", val[j]);
  69. goto out_free_buffer;
  70. }
  71. orientation->rotation[i * 3 + j] = str;
  72. }
  73. }
  74. ret = true;
  75. out_free_buffer:
  76. kfree(buffer.pointer);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL_GPL(iio_read_acpi_mount_matrix);
  80. /**
  81. * iio_get_acpi_device_name_and_data() - Return ACPI device instance name and driver data
  82. * @dev: Device structure
  83. * @data: Optional pointer to return driver data
  84. *
  85. * When device was enumerated by ACPI ID matching, the user might
  86. * want to set description for the physical chip. In such cases
  87. * the ACPI device instance name might be used. This call may be
  88. * performed to retrieve this information.
  89. *
  90. * NOTE: This helper function exists only for backward compatibility,
  91. * do not use in a new code!
  92. *
  93. * Returns: ACPI device instance name or %NULL.
  94. */
  95. const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
  96. {
  97. const struct acpi_device_id *id;
  98. acpi_handle handle;
  99. handle = ACPI_HANDLE(dev);
  100. if (!handle)
  101. return NULL;
  102. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  103. if (!id)
  104. return NULL;
  105. if (data)
  106. *data = (const void *)id->driver_data;
  107. return dev_name(dev);
  108. }
  109. EXPORT_SYMBOL_GPL(iio_get_acpi_device_name_and_data);