cros_kbd_led_backlight.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Keyboard backlight LED driver for Chrome OS.
  3. *
  4. * Copyright (C) 2012 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/acpi.h>
  17. #include <linux/leds.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. /* Keyboard LED ACPI Device must be defined in firmware */
  26. #define ACPI_KEYBOARD_BACKLIGHT_DEVICE "\\_SB.KBLT"
  27. #define ACPI_KEYBOARD_BACKLIGHT_READ ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC"
  28. #define ACPI_KEYBOARD_BACKLIGHT_WRITE ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM"
  29. #define ACPI_KEYBOARD_BACKLIGHT_MAX 100
  30. static void keyboard_led_set_brightness(struct led_classdev *cdev,
  31. enum led_brightness brightness)
  32. {
  33. union acpi_object param;
  34. struct acpi_object_list input;
  35. acpi_status status;
  36. param.type = ACPI_TYPE_INTEGER;
  37. param.integer.value = brightness;
  38. input.count = 1;
  39. input.pointer = &param;
  40. status = acpi_evaluate_object(NULL, ACPI_KEYBOARD_BACKLIGHT_WRITE,
  41. &input, NULL);
  42. if (ACPI_FAILURE(status))
  43. dev_err(cdev->dev, "Error setting keyboard LED value: %d\n",
  44. status);
  45. }
  46. static enum led_brightness
  47. keyboard_led_get_brightness(struct led_classdev *cdev)
  48. {
  49. unsigned long long brightness;
  50. acpi_status status;
  51. status = acpi_evaluate_integer(NULL, ACPI_KEYBOARD_BACKLIGHT_READ,
  52. NULL, &brightness);
  53. if (ACPI_FAILURE(status)) {
  54. dev_err(cdev->dev, "Error getting keyboard LED value: %d\n",
  55. status);
  56. return -EIO;
  57. }
  58. return brightness;
  59. }
  60. static int keyboard_led_probe(struct platform_device *pdev)
  61. {
  62. struct led_classdev *cdev;
  63. acpi_handle handle;
  64. acpi_status status;
  65. int error;
  66. /* Look for the keyboard LED ACPI Device */
  67. status = acpi_get_handle(ACPI_ROOT_OBJECT,
  68. ACPI_KEYBOARD_BACKLIGHT_DEVICE,
  69. &handle);
  70. if (ACPI_FAILURE(status)) {
  71. dev_err(&pdev->dev, "Unable to find ACPI device %s: %d\n",
  72. ACPI_KEYBOARD_BACKLIGHT_DEVICE, status);
  73. return -ENXIO;
  74. }
  75. cdev = devm_kzalloc(&pdev->dev, sizeof(*cdev), GFP_KERNEL);
  76. if (!cdev)
  77. return -ENOMEM;
  78. cdev->name = "chromeos::kbd_backlight";
  79. cdev->max_brightness = ACPI_KEYBOARD_BACKLIGHT_MAX;
  80. cdev->flags |= LED_CORE_SUSPENDRESUME;
  81. cdev->brightness_set = keyboard_led_set_brightness;
  82. cdev->brightness_get = keyboard_led_get_brightness;
  83. error = devm_led_classdev_register(&pdev->dev, cdev);
  84. if (error)
  85. return error;
  86. return 0;
  87. }
  88. static const struct acpi_device_id keyboard_led_id[] = {
  89. { "GOOG0002", 0 },
  90. { }
  91. };
  92. MODULE_DEVICE_TABLE(acpi, keyboard_led_id);
  93. static struct platform_driver keyboard_led_driver = {
  94. .driver = {
  95. .name = "chromeos-keyboard-leds",
  96. .acpi_match_table = ACPI_PTR(keyboard_led_id),
  97. },
  98. .probe = keyboard_led_probe,
  99. };
  100. module_platform_driver(keyboard_led_driver);
  101. MODULE_AUTHOR("Simon Que <sque@chromium.org>");
  102. MODULE_DESCRIPTION("ChromeOS Keyboard backlight LED Driver");
  103. MODULE_LICENSE("GPL");
  104. MODULE_ALIAS("platform:chromeos-keyboard-leds");