1 // SPDX-License-Identifier: GPL-2.0+
2 // Keyboard backlight LED driver for ChromeOS
3 //
4 // Copyright (C) 2012 Google, Inc.
5 
6 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_data/cros_ec_commands.h>
15 #include <linux/platform_data/cros_ec_proto.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
19 
20 struct keyboard_led {
21 	struct led_classdev cdev;
22 	struct cros_ec_device *ec;
23 };
24 
25 /**
26  * struct keyboard_led_drvdata - keyboard LED driver data.
27  * @init:			Init function.
28  * @brightness_get:		Get LED brightness level.
29  * @brightness_set:		Set LED brightness level.  Must not sleep.
30  * @brightness_set_blocking:	Set LED brightness level.  It can block the
31  *				caller for the time required for accessing a
32  *				LED device register
33  * @max_brightness:		Maximum brightness.
34  *
35  * See struct led_classdev in include/linux/leds.h for more details.
36  */
37 struct keyboard_led_drvdata {
38 	int (*init)(struct platform_device *pdev);
39 
40 	enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
41 
42 	void (*brightness_set)(struct led_classdev *led_cdev,
43 			       enum led_brightness brightness);
44 	int (*brightness_set_blocking)(struct led_classdev *led_cdev,
45 				       enum led_brightness brightness);
46 
47 	enum led_brightness max_brightness;
48 };
49 
50 #define KEYBOARD_BACKLIGHT_MAX 100
51 
52 #ifdef CONFIG_ACPI
53 
54 /* Keyboard LED ACPI Device must be defined in firmware */
55 #define ACPI_KEYBOARD_BACKLIGHT_DEVICE	"\\_SB.KBLT"
56 #define ACPI_KEYBOARD_BACKLIGHT_READ	ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC"
57 #define ACPI_KEYBOARD_BACKLIGHT_WRITE	ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM"
58 
keyboard_led_set_brightness_acpi(struct led_classdev * cdev,enum led_brightness brightness)59 static void keyboard_led_set_brightness_acpi(struct led_classdev *cdev,
60 					     enum led_brightness brightness)
61 {
62 	union acpi_object param;
63 	struct acpi_object_list input;
64 	acpi_status status;
65 
66 	param.type = ACPI_TYPE_INTEGER;
67 	param.integer.value = brightness;
68 	input.count = 1;
69 	input.pointer = &param;
70 
71 	status = acpi_evaluate_object(NULL, ACPI_KEYBOARD_BACKLIGHT_WRITE,
72 				      &input, NULL);
73 	if (ACPI_FAILURE(status))
74 		dev_err(cdev->dev, "Error setting keyboard LED value: %d\n",
75 			status);
76 }
77 
78 static enum led_brightness
keyboard_led_get_brightness_acpi(struct led_classdev * cdev)79 keyboard_led_get_brightness_acpi(struct led_classdev *cdev)
80 {
81 	unsigned long long brightness;
82 	acpi_status status;
83 
84 	status = acpi_evaluate_integer(NULL, ACPI_KEYBOARD_BACKLIGHT_READ,
85 				       NULL, &brightness);
86 	if (ACPI_FAILURE(status)) {
87 		dev_err(cdev->dev, "Error getting keyboard LED value: %d\n",
88 			status);
89 		return -EIO;
90 	}
91 
92 	return brightness;
93 }
94 
keyboard_led_init_acpi(struct platform_device * pdev)95 static int keyboard_led_init_acpi(struct platform_device *pdev)
96 {
97 	acpi_handle handle;
98 	acpi_status status;
99 
100 	/* Look for the keyboard LED ACPI Device */
101 	status = acpi_get_handle(ACPI_ROOT_OBJECT,
102 				 ACPI_KEYBOARD_BACKLIGHT_DEVICE,
103 				 &handle);
104 	if (ACPI_FAILURE(status)) {
105 		dev_err(&pdev->dev, "Unable to find ACPI device %s: %d\n",
106 			ACPI_KEYBOARD_BACKLIGHT_DEVICE, status);
107 		return -ENXIO;
108 	}
109 
110 	return 0;
111 }
112 
113 static const struct keyboard_led_drvdata keyboard_led_drvdata_acpi = {
114 	.init = keyboard_led_init_acpi,
115 	.brightness_set = keyboard_led_set_brightness_acpi,
116 	.brightness_get = keyboard_led_get_brightness_acpi,
117 	.max_brightness = KEYBOARD_BACKLIGHT_MAX,
118 };
119 
120 #endif /* CONFIG_ACPI */
121 
122 #if IS_ENABLED(CONFIG_CROS_EC)
123 
124 static int
keyboard_led_set_brightness_ec_pwm(struct led_classdev * cdev,enum led_brightness brightness)125 keyboard_led_set_brightness_ec_pwm(struct led_classdev *cdev,
126 				   enum led_brightness brightness)
127 {
128 	struct {
129 		struct cros_ec_command msg;
130 		struct ec_params_pwm_set_keyboard_backlight params;
131 	} __packed buf;
132 	struct ec_params_pwm_set_keyboard_backlight *params = &buf.params;
133 	struct cros_ec_command *msg = &buf.msg;
134 	struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);
135 
136 	memset(&buf, 0, sizeof(buf));
137 
138 	msg->command = EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT;
139 	msg->outsize = sizeof(*params);
140 
141 	params->percent = brightness;
142 
143 	return cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
144 }
145 
146 static enum led_brightness
keyboard_led_get_brightness_ec_pwm(struct led_classdev * cdev)147 keyboard_led_get_brightness_ec_pwm(struct led_classdev *cdev)
148 {
149 	struct {
150 		struct cros_ec_command msg;
151 		struct ec_response_pwm_get_keyboard_backlight resp;
152 	} __packed buf;
153 	struct ec_response_pwm_get_keyboard_backlight *resp = &buf.resp;
154 	struct cros_ec_command *msg = &buf.msg;
155 	struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);
156 	int ret;
157 
158 	memset(&buf, 0, sizeof(buf));
159 
160 	msg->command = EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT;
161 	msg->insize = sizeof(*resp);
162 
163 	ret = cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
164 	if (ret < 0)
165 		return ret;
166 
167 	return resp->percent;
168 }
169 
keyboard_led_init_ec_pwm(struct platform_device * pdev)170 static int keyboard_led_init_ec_pwm(struct platform_device *pdev)
171 {
172 	struct keyboard_led *keyboard_led = platform_get_drvdata(pdev);
173 
174 	keyboard_led->ec = dev_get_drvdata(pdev->dev.parent);
175 	if (!keyboard_led->ec) {
176 		dev_err(&pdev->dev, "no parent EC device\n");
177 		return -EINVAL;
178 	}
179 
180 	return 0;
181 }
182 
183 static const __maybe_unused struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {
184 	.init = keyboard_led_init_ec_pwm,
185 	.brightness_set_blocking = keyboard_led_set_brightness_ec_pwm,
186 	.brightness_get = keyboard_led_get_brightness_ec_pwm,
187 	.max_brightness = KEYBOARD_BACKLIGHT_MAX,
188 };
189 
190 #else /* IS_ENABLED(CONFIG_CROS_EC) */
191 
192 static const __maybe_unused struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {};
193 
194 #endif /* IS_ENABLED(CONFIG_CROS_EC) */
195 
keyboard_led_probe(struct platform_device * pdev)196 static int keyboard_led_probe(struct platform_device *pdev)
197 {
198 	const struct keyboard_led_drvdata *drvdata;
199 	struct keyboard_led *keyboard_led;
200 	int error;
201 
202 	drvdata = device_get_match_data(&pdev->dev);
203 	if (!drvdata)
204 		return -EINVAL;
205 
206 	keyboard_led = devm_kzalloc(&pdev->dev, sizeof(*keyboard_led), GFP_KERNEL);
207 	if (!keyboard_led)
208 		return -ENOMEM;
209 	platform_set_drvdata(pdev, keyboard_led);
210 
211 	if (drvdata->init) {
212 		error = drvdata->init(pdev);
213 		if (error)
214 			return error;
215 	}
216 
217 	keyboard_led->cdev.name = "chromeos::kbd_backlight";
218 	keyboard_led->cdev.flags |= LED_CORE_SUSPENDRESUME;
219 	keyboard_led->cdev.max_brightness = drvdata->max_brightness;
220 	keyboard_led->cdev.brightness_set = drvdata->brightness_set;
221 	keyboard_led->cdev.brightness_set_blocking = drvdata->brightness_set_blocking;
222 	keyboard_led->cdev.brightness_get = drvdata->brightness_get;
223 
224 	error = devm_led_classdev_register(&pdev->dev, &keyboard_led->cdev);
225 	if (error)
226 		return error;
227 
228 	return 0;
229 }
230 
231 #ifdef CONFIG_ACPI
232 static const struct acpi_device_id keyboard_led_acpi_match[] = {
233 	{ "GOOG0002", (kernel_ulong_t)&keyboard_led_drvdata_acpi },
234 	{ }
235 };
236 MODULE_DEVICE_TABLE(acpi, keyboard_led_acpi_match);
237 #endif
238 
239 #ifdef CONFIG_OF
240 static const struct of_device_id keyboard_led_of_match[] = {
241 	{
242 		.compatible = "google,cros-kbd-led-backlight",
243 		.data = &keyboard_led_drvdata_ec_pwm,
244 	},
245 	{}
246 };
247 MODULE_DEVICE_TABLE(of, keyboard_led_of_match);
248 #endif
249 
250 static struct platform_driver keyboard_led_driver = {
251 	.driver		= {
252 		.name	= "chromeos-keyboard-leds",
253 		.acpi_match_table = ACPI_PTR(keyboard_led_acpi_match),
254 		.of_match_table = of_match_ptr(keyboard_led_of_match),
255 	},
256 	.probe		= keyboard_led_probe,
257 };
258 module_platform_driver(keyboard_led_driver);
259 
260 MODULE_AUTHOR("Simon Que <sque@chromium.org>");
261 MODULE_DESCRIPTION("ChromeOS Keyboard backlight LED Driver");
262 MODULE_LICENSE("GPL");
263 MODULE_ALIAS("platform:chromeos-keyboard-leds");
264