1 // SPDX-License-Identifier: GPL-2.0
3 * Keyboard backlight LED driver for the Wilco Embedded Controller
5 * Copyright 2019 Google LLC
7 * Since the EC will never change the backlight level of its own accord,
8 * we don't need to implement a brightness_get() method.
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/leds.h>
14 #include <linux/platform_data/wilco-ec.h>
15 #include <linux/slab.h>
17 #define WILCO_EC_COMMAND_KBBL 0x75
18 #define WILCO_KBBL_MODE_FLAG_PWM BIT(1) /* Set brightness by percent. */
19 #define WILCO_KBBL_DEFAULT_BRIGHTNESS 0
21 struct wilco_keyboard_leds
{
22 struct wilco_ec_device
*ec
;
23 struct led_classdev keyboard
;
26 enum wilco_kbbl_subcommand
{
27 WILCO_KBBL_SUBCMD_GET_FEATURES
= 0x00,
28 WILCO_KBBL_SUBCMD_GET_STATE
= 0x01,
29 WILCO_KBBL_SUBCMD_SET_STATE
= 0x02,
33 * struct wilco_keyboard_leds_msg - Message to/from EC for keyboard LED control.
34 * @command: Always WILCO_EC_COMMAND_KBBL.
35 * @status: Set by EC to 0 on success, 0xFF on failure.
36 * @subcmd: One of enum wilco_kbbl_subcommand.
37 * @reserved3: Should be 0.
38 * @mode: Bit flags for used mode, we want to use WILCO_KBBL_MODE_FLAG_PWM.
39 * @reserved5to8: Should be 0.
40 * @percent: Brightness in 0-100. Only meaningful in PWM mode.
41 * @reserved10to15: Should be 0.
43 struct wilco_keyboard_leds_msg
{
54 /* Send a request, get a response, and check that the response is good. */
55 static int send_kbbl_msg(struct wilco_ec_device
*ec
,
56 struct wilco_keyboard_leds_msg
*request
,
57 struct wilco_keyboard_leds_msg
*response
)
59 struct wilco_ec_message msg
;
62 memset(&msg
, 0, sizeof(msg
));
63 msg
.type
= WILCO_EC_MSG_LEGACY
;
64 msg
.request_data
= request
;
65 msg
.request_size
= sizeof(*request
);
66 msg
.response_data
= response
;
67 msg
.response_size
= sizeof(*response
);
69 ret
= wilco_ec_mailbox(ec
, &msg
);
72 "Failed sending keyboard LEDs command: %d\n", ret
);
79 static int set_kbbl(struct wilco_ec_device
*ec
, enum led_brightness brightness
)
81 struct wilco_keyboard_leds_msg request
;
82 struct wilco_keyboard_leds_msg response
;
85 memset(&request
, 0, sizeof(request
));
86 request
.command
= WILCO_EC_COMMAND_KBBL
;
87 request
.subcmd
= WILCO_KBBL_SUBCMD_SET_STATE
;
88 request
.mode
= WILCO_KBBL_MODE_FLAG_PWM
;
89 request
.percent
= brightness
;
91 ret
= send_kbbl_msg(ec
, &request
, &response
);
95 if (response
.status
) {
97 "EC reported failure sending keyboard LEDs command: %d\n",
105 static int kbbl_exist(struct wilco_ec_device
*ec
, bool *exists
)
107 struct wilco_keyboard_leds_msg request
;
108 struct wilco_keyboard_leds_msg response
;
111 memset(&request
, 0, sizeof(request
));
112 request
.command
= WILCO_EC_COMMAND_KBBL
;
113 request
.subcmd
= WILCO_KBBL_SUBCMD_GET_FEATURES
;
115 ret
= send_kbbl_msg(ec
, &request
, &response
);
119 *exists
= response
.status
!= 0xFF;
125 * kbbl_init() - Initialize the state of the keyboard backlight.
126 * @ec: EC device to talk to.
128 * Gets the current brightness, ensuring that the BIOS already initialized the
129 * backlight to PWM mode. If not in PWM mode, then the current brightness is
130 * meaningless, so set the brightness to WILCO_KBBL_DEFAULT_BRIGHTNESS.
132 * Return: Final brightness of the keyboard, or negative error code on failure.
134 static int kbbl_init(struct wilco_ec_device
*ec
)
136 struct wilco_keyboard_leds_msg request
;
137 struct wilco_keyboard_leds_msg response
;
140 memset(&request
, 0, sizeof(request
));
141 request
.command
= WILCO_EC_COMMAND_KBBL
;
142 request
.subcmd
= WILCO_KBBL_SUBCMD_GET_STATE
;
144 ret
= send_kbbl_msg(ec
, &request
, &response
);
148 if (response
.status
) {
150 "EC reported failure sending keyboard LEDs command: %d\n",
155 if (response
.mode
& WILCO_KBBL_MODE_FLAG_PWM
)
156 return response
.percent
;
158 ret
= set_kbbl(ec
, WILCO_KBBL_DEFAULT_BRIGHTNESS
);
162 return WILCO_KBBL_DEFAULT_BRIGHTNESS
;
165 static int wilco_keyboard_leds_set(struct led_classdev
*cdev
,
166 enum led_brightness brightness
)
168 struct wilco_keyboard_leds
*wkl
=
169 container_of(cdev
, struct wilco_keyboard_leds
, keyboard
);
170 return set_kbbl(wkl
->ec
, brightness
);
173 int wilco_keyboard_leds_init(struct wilco_ec_device
*ec
)
175 struct wilco_keyboard_leds
*wkl
;
179 ret
= kbbl_exist(ec
, &leds_exist
);
182 "Failed checking keyboard LEDs support: %d\n", ret
);
188 wkl
= devm_kzalloc(ec
->dev
, sizeof(*wkl
), GFP_KERNEL
);
193 wkl
->keyboard
.name
= "platform::kbd_backlight";
194 wkl
->keyboard
.max_brightness
= 100;
195 wkl
->keyboard
.flags
= LED_CORE_SUSPENDRESUME
;
196 wkl
->keyboard
.brightness_set_blocking
= wilco_keyboard_leds_set
;
200 wkl
->keyboard
.brightness
= ret
;
202 return devm_led_classdev_register(ec
->dev
, &wkl
->keyboard
);