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