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