platform/chrome: cros_kbd_led_backlight: support EC PWM backend

EC PWM backend uses EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT and
EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT for setting and getting the brightness
respectively.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
Tested-by: Matthias Kaehlcke <mka@chromium.org>
Link: https://lore.kernel.org/r/20220523090822.3035189-6-tzungbi@kernel.org
This commit is contained in:
Tzung-Bi Shih 2022-05-23 17:08:22 +08:00
parent fd1e8054ff
commit 40f5814374
2 changed files with 99 additions and 16 deletions

View File

@ -139,7 +139,7 @@ config CROS_EC_PROTO
config CROS_KBD_LED_BACKLIGHT config CROS_KBD_LED_BACKLIGHT
tristate "Backlight LED support for Chrome OS keyboards" tristate "Backlight LED support for Chrome OS keyboards"
depends on LEDS_CLASS && ACPI depends on LEDS_CLASS && (ACPI || CROS_EC)
help help
This option enables support for the keyboard backlight LEDs on This option enables support for the keyboard backlight LEDs on
select Chrome OS systems. select Chrome OS systems.

View File

@ -11,10 +11,17 @@
#include <linux/leds.h> #include <linux/leds.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/property.h> #include <linux/property.h>
#include <linux/slab.h> #include <linux/slab.h>
struct keyboard_led {
struct led_classdev cdev;
struct cros_ec_device *ec;
};
/** /**
* struct keyboard_led_drvdata - keyboard LED driver data. * struct keyboard_led_drvdata - keyboard LED driver data.
* @init: Init function. * @init: Init function.
@ -40,6 +47,8 @@ struct keyboard_led_drvdata {
enum led_brightness max_brightness; enum led_brightness max_brightness;
}; };
#define KEYBOARD_BACKLIGHT_MAX 100
#ifdef CONFIG_ACPI #ifdef CONFIG_ACPI
/* Keyboard LED ACPI Device must be defined in firmware */ /* Keyboard LED ACPI Device must be defined in firmware */
@ -47,8 +56,6 @@ struct keyboard_led_drvdata {
#define ACPI_KEYBOARD_BACKLIGHT_READ ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC" #define ACPI_KEYBOARD_BACKLIGHT_READ ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC"
#define ACPI_KEYBOARD_BACKLIGHT_WRITE ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM" #define ACPI_KEYBOARD_BACKLIGHT_WRITE ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM"
#define ACPI_KEYBOARD_BACKLIGHT_MAX 100
static void keyboard_led_set_brightness_acpi(struct led_classdev *cdev, static void keyboard_led_set_brightness_acpi(struct led_classdev *cdev,
enum led_brightness brightness) enum led_brightness brightness)
{ {
@ -107,39 +114,114 @@ static const struct keyboard_led_drvdata keyboard_led_drvdata_acpi = {
.init = keyboard_led_init_acpi, .init = keyboard_led_init_acpi,
.brightness_set = keyboard_led_set_brightness_acpi, .brightness_set = keyboard_led_set_brightness_acpi,
.brightness_get = keyboard_led_get_brightness_acpi, .brightness_get = keyboard_led_get_brightness_acpi,
.max_brightness = ACPI_KEYBOARD_BACKLIGHT_MAX, .max_brightness = KEYBOARD_BACKLIGHT_MAX,
}; };
#endif /* CONFIG_ACPI */ #endif /* CONFIG_ACPI */
#ifdef CONFIG_CROS_EC
static int
keyboard_led_set_brightness_ec_pwm(struct led_classdev *cdev,
enum led_brightness brightness)
{
struct {
struct cros_ec_command msg;
struct ec_params_pwm_set_keyboard_backlight params;
} __packed buf;
struct ec_params_pwm_set_keyboard_backlight *params = &buf.params;
struct cros_ec_command *msg = &buf.msg;
struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);
memset(&buf, 0, sizeof(buf));
msg->command = EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT;
msg->outsize = sizeof(*params);
params->percent = brightness;
return cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
}
static enum led_brightness
keyboard_led_get_brightness_ec_pwm(struct led_classdev *cdev)
{
struct {
struct cros_ec_command msg;
struct ec_response_pwm_get_keyboard_backlight resp;
} __packed buf;
struct ec_response_pwm_get_keyboard_backlight *resp = &buf.resp;
struct cros_ec_command *msg = &buf.msg;
struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);
int ret;
memset(&buf, 0, sizeof(buf));
msg->command = EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT;
msg->insize = sizeof(*resp);
ret = cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
if (ret < 0)
return ret;
return resp->percent;
}
static int keyboard_led_init_ec_pwm(struct platform_device *pdev)
{
struct keyboard_led *keyboard_led = platform_get_drvdata(pdev);
keyboard_led->ec = dev_get_drvdata(pdev->dev.parent);
if (!keyboard_led->ec) {
dev_err(&pdev->dev, "no parent EC device\n");
return -EINVAL;
}
return 0;
}
static const struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {
.init = keyboard_led_init_ec_pwm,
.brightness_set_blocking = keyboard_led_set_brightness_ec_pwm,
.brightness_get = keyboard_led_get_brightness_ec_pwm,
.max_brightness = KEYBOARD_BACKLIGHT_MAX,
};
#else /* CONFIG_CROS_EC */
static const struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {};
#endif /* CONFIG_CROS_EC */
static int keyboard_led_probe(struct platform_device *pdev) static int keyboard_led_probe(struct platform_device *pdev)
{ {
struct led_classdev *cdev;
const struct keyboard_led_drvdata *drvdata; const struct keyboard_led_drvdata *drvdata;
struct keyboard_led *keyboard_led;
int error; int error;
drvdata = device_get_match_data(&pdev->dev); drvdata = device_get_match_data(&pdev->dev);
if (!drvdata) if (!drvdata)
return -EINVAL; return -EINVAL;
keyboard_led = devm_kzalloc(&pdev->dev, sizeof(*keyboard_led), GFP_KERNEL);
if (!keyboard_led)
return -ENOMEM;
platform_set_drvdata(pdev, keyboard_led);
if (drvdata->init) { if (drvdata->init) {
error = drvdata->init(pdev); error = drvdata->init(pdev);
if (error) if (error)
return error; return error;
} }
cdev = devm_kzalloc(&pdev->dev, sizeof(*cdev), GFP_KERNEL); keyboard_led->cdev.name = "chromeos::kbd_backlight";
if (!cdev) keyboard_led->cdev.flags |= LED_CORE_SUSPENDRESUME;
return -ENOMEM; keyboard_led->cdev.max_brightness = drvdata->max_brightness;
keyboard_led->cdev.brightness_set = drvdata->brightness_set;
keyboard_led->cdev.brightness_set_blocking = drvdata->brightness_set_blocking;
keyboard_led->cdev.brightness_get = drvdata->brightness_get;
cdev->name = "chromeos::kbd_backlight"; error = devm_led_classdev_register(&pdev->dev, &keyboard_led->cdev);
cdev->flags |= LED_CORE_SUSPENDRESUME;
cdev->max_brightness = drvdata->max_brightness;
cdev->brightness_set = drvdata->brightness_set;
cdev->brightness_set_blocking = drvdata->brightness_set_blocking;
cdev->brightness_get = drvdata->brightness_get;
error = devm_led_classdev_register(&pdev->dev, cdev);
if (error) if (error)
return error; return error;
@ -158,6 +240,7 @@ MODULE_DEVICE_TABLE(acpi, keyboard_led_acpi_match);
static const struct of_device_id keyboard_led_of_match[] = { static const struct of_device_id keyboard_led_of_match[] = {
{ {
.compatible = "google,cros-kbd-led-backlight", .compatible = "google,cros-kbd-led-backlight",
.data = &keyboard_led_drvdata_ec_pwm,
}, },
{} {}
}; };