chromeos_tbmc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <[email protected]>
  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. acpi_pm_wakeup_event(&adev->dev);
  42. switch (event) {
  43. case 0x80:
  44. chromeos_tbmc_query_switch(adev, adev->driver_data);
  45. break;
  46. default:
  47. dev_err(&adev->dev, "Unexpected event: 0x%08X\n", event);
  48. }
  49. }
  50. static int chromeos_tbmc_open(struct input_dev *idev)
  51. {
  52. struct acpi_device *adev = input_get_drvdata(idev);
  53. return chromeos_tbmc_query_switch(adev, idev);
  54. }
  55. static int chromeos_tbmc_add(struct acpi_device *adev)
  56. {
  57. struct input_dev *idev;
  58. struct device *dev = &adev->dev;
  59. int ret;
  60. idev = devm_input_allocate_device(dev);
  61. if (!idev)
  62. return -ENOMEM;
  63. idev->name = "Tablet Mode Switch";
  64. idev->phys = acpi_device_hid(adev);
  65. idev->id.bustype = BUS_HOST;
  66. idev->id.version = 1;
  67. idev->id.product = 0;
  68. idev->open = chromeos_tbmc_open;
  69. input_set_drvdata(idev, adev);
  70. adev->driver_data = idev;
  71. input_set_capability(idev, EV_SW, SW_TABLET_MODE);
  72. ret = input_register_device(idev);
  73. if (ret) {
  74. dev_err(dev, "cannot register input device\n");
  75. return ret;
  76. }
  77. device_init_wakeup(dev, true);
  78. return 0;
  79. }
  80. static const struct acpi_device_id chromeos_tbmc_acpi_device_ids[] = {
  81. { ACPI_DRV_NAME, 0 },
  82. { }
  83. };
  84. MODULE_DEVICE_TABLE(acpi, chromeos_tbmc_acpi_device_ids);
  85. static SIMPLE_DEV_PM_OPS(chromeos_tbmc_pm_ops, NULL,
  86. chromeos_tbmc_resume);
  87. static struct acpi_driver chromeos_tbmc_driver = {
  88. .name = DRV_NAME,
  89. .class = DRV_NAME,
  90. .ids = chromeos_tbmc_acpi_device_ids,
  91. .ops = {
  92. .add = chromeos_tbmc_add,
  93. .notify = chromeos_tbmc_notify,
  94. },
  95. .drv.pm = &chromeos_tbmc_pm_ops,
  96. };
  97. module_acpi_driver(chromeos_tbmc_driver);
  98. MODULE_LICENSE("GPL v2");
  99. MODULE_DESCRIPTION("ChromeOS ACPI tablet switch driver");