chromeos_tbmc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Driver to detect Tablet Mode for ChromeOS convertible.
  3. //
  4. // Copyright (C) 2017 Google, Inc.
  5. // Author: Gwendal Grignou <gwendal@chromium.org>
  6. //
  7. // On Chromebook using ACPI, this device listens for notification
  8. // from GOOG0006 and issue method TBMC to retrieve the status.
  9. //
  10. // GOOG0006 issues the notification when it receives EC_HOST_EVENT_MODE_CHANGE
  11. // from the EC.
  12. // Method TBMC reads EC_ACPI_MEM_DEVICE_ORIENTATION byte from the shared
  13. // memory region.
  14. #include <linux/acpi.h>
  15. #include <linux/input.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/printk.h>
  19. #define DRV_NAME "chromeos_tbmc"
  20. #define ACPI_DRV_NAME "GOOG0006"
  21. static int chromeos_tbmc_query_switch(struct acpi_device *adev,
  22. struct input_dev *idev)
  23. {
  24. unsigned long long state;
  25. acpi_status status;
  26. status = acpi_evaluate_integer(adev->handle, "TBMC", NULL, &state);
  27. if (ACPI_FAILURE(status))
  28. return -ENODEV;
  29. /* input layer checks if event is redundant */
  30. input_report_switch(idev, SW_TABLET_MODE, state);
  31. input_sync(idev);
  32. return 0;
  33. }
  34. static __maybe_unused int chromeos_tbmc_resume(struct device *dev)
  35. {
  36. struct acpi_device *adev = to_acpi_device(dev);
  37. return chromeos_tbmc_query_switch(adev, adev->driver_data);
  38. }
  39. static void chromeos_tbmc_notify(struct acpi_device *adev, u32 event)
  40. {
  41. switch (event) {
  42. case 0x80:
  43. chromeos_tbmc_query_switch(adev, adev->driver_data);
  44. break;
  45. default:
  46. dev_err(&adev->dev, "Unexpected event: 0x%08X\n", event);
  47. }
  48. }
  49. static int chromeos_tbmc_open(struct input_dev *idev)
  50. {
  51. struct acpi_device *adev = input_get_drvdata(idev);
  52. return chromeos_tbmc_query_switch(adev, idev);
  53. }
  54. static int chromeos_tbmc_add(struct acpi_device *adev)
  55. {
  56. struct input_dev *idev;
  57. struct device *dev = &adev->dev;
  58. int ret;
  59. idev = devm_input_allocate_device(dev);
  60. if (!idev)
  61. return -ENOMEM;
  62. idev->name = "Tablet Mode Switch";
  63. idev->phys = acpi_device_hid(adev);
  64. idev->id.bustype = BUS_HOST;
  65. idev->id.version = 1;
  66. idev->id.product = 0;
  67. idev->open = chromeos_tbmc_open;
  68. input_set_drvdata(idev, adev);
  69. adev->driver_data = idev;
  70. input_set_capability(idev, EV_SW, SW_TABLET_MODE);
  71. ret = input_register_device(idev);
  72. if (ret) {
  73. dev_err(dev, "cannot register input device\n");
  74. return ret;
  75. }
  76. return 0;
  77. }
  78. static const struct acpi_device_id chromeos_tbmc_acpi_device_ids[] = {
  79. { ACPI_DRV_NAME, 0 },
  80. { }
  81. };
  82. MODULE_DEVICE_TABLE(acpi, chromeos_tbmc_acpi_device_ids);
  83. static const SIMPLE_DEV_PM_OPS(chromeos_tbmc_pm_ops, NULL,
  84. chromeos_tbmc_resume);
  85. static struct acpi_driver chromeos_tbmc_driver = {
  86. .name = DRV_NAME,
  87. .class = DRV_NAME,
  88. .ids = chromeos_tbmc_acpi_device_ids,
  89. .ops = {
  90. .add = chromeos_tbmc_add,
  91. .notify = chromeos_tbmc_notify,
  92. },
  93. .drv.pm = &chromeos_tbmc_pm_ops,
  94. };
  95. module_acpi_driver(chromeos_tbmc_driver);
  96. MODULE_LICENSE("GPL v2");
  97. MODULE_DESCRIPTION("ChromeOS ACPI tablet switch driver");