WIP FPC-III support
[linux/fpc-iii.git] / drivers / platform / chrome / cros_kbd_led_backlight.c
blobaa409f0201fb1bedf2445b2784ed7a42f230bbf5
1 // SPDX-License-Identifier: GPL-2.0+
2 // Keyboard backlight LED driver for ChromeOS
3 //
4 // Copyright (C) 2012 Google, Inc.
6 #include <linux/acpi.h>
7 #include <linux/leds.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 /* Keyboard LED ACPI Device must be defined in firmware */
17 #define ACPI_KEYBOARD_BACKLIGHT_DEVICE "\\_SB.KBLT"
18 #define ACPI_KEYBOARD_BACKLIGHT_READ ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC"
19 #define ACPI_KEYBOARD_BACKLIGHT_WRITE ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM"
21 #define ACPI_KEYBOARD_BACKLIGHT_MAX 100
23 static void keyboard_led_set_brightness(struct led_classdev *cdev,
24 enum led_brightness brightness)
26 union acpi_object param;
27 struct acpi_object_list input;
28 acpi_status status;
30 param.type = ACPI_TYPE_INTEGER;
31 param.integer.value = brightness;
32 input.count = 1;
33 input.pointer = &param;
35 status = acpi_evaluate_object(NULL, ACPI_KEYBOARD_BACKLIGHT_WRITE,
36 &input, NULL);
37 if (ACPI_FAILURE(status))
38 dev_err(cdev->dev, "Error setting keyboard LED value: %d\n",
39 status);
42 static enum led_brightness
43 keyboard_led_get_brightness(struct led_classdev *cdev)
45 unsigned long long brightness;
46 acpi_status status;
48 status = acpi_evaluate_integer(NULL, ACPI_KEYBOARD_BACKLIGHT_READ,
49 NULL, &brightness);
50 if (ACPI_FAILURE(status)) {
51 dev_err(cdev->dev, "Error getting keyboard LED value: %d\n",
52 status);
53 return -EIO;
56 return brightness;
59 static int keyboard_led_probe(struct platform_device *pdev)
61 struct led_classdev *cdev;
62 acpi_handle handle;
63 acpi_status status;
64 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;
76 cdev = devm_kzalloc(&pdev->dev, sizeof(*cdev), GFP_KERNEL);
77 if (!cdev)
78 return -ENOMEM;
80 cdev->name = "chromeos::kbd_backlight";
81 cdev->max_brightness = ACPI_KEYBOARD_BACKLIGHT_MAX;
82 cdev->flags |= LED_CORE_SUSPENDRESUME;
83 cdev->brightness_set = keyboard_led_set_brightness;
84 cdev->brightness_get = keyboard_led_get_brightness;
86 error = devm_led_classdev_register(&pdev->dev, cdev);
87 if (error)
88 return error;
90 return 0;
93 static const struct acpi_device_id keyboard_led_id[] = {
94 { "GOOG0002", 0 },
95 { }
97 MODULE_DEVICE_TABLE(acpi, keyboard_led_id);
99 static struct platform_driver keyboard_led_driver = {
100 .driver = {
101 .name = "chromeos-keyboard-leds",
102 .acpi_match_table = ACPI_PTR(keyboard_led_id),
104 .probe = keyboard_led_probe,
106 module_platform_driver(keyboard_led_driver);
108 MODULE_AUTHOR("Simon Que <sque@chromium.org>");
109 MODULE_DESCRIPTION("ChromeOS Keyboard backlight LED Driver");
110 MODULE_LICENSE("GPL");
111 MODULE_ALIAS("platform:chromeos-keyboard-leds");