From patchwork Thu Apr 13 15:18:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 673035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F5F1C77B61 for ; Thu, 13 Apr 2023 15:19:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231560AbjDMPTB (ORCPT ); Thu, 13 Apr 2023 11:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231504AbjDMPS6 (ORCPT ); Thu, 13 Apr 2023 11:18:58 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94D8B9027 for ; Thu, 13 Apr 2023 08:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681399096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Bim9aFxJ1jiUJxY00YpYsm2y07+62n6FaYn2V6vbnTQ=; b=fkb5n/w1xWP8SqNf/ZNDFuZojwIqDZixirkkITKw7ydQE3mS2ul+9f+NYrHtiMluU+Bxqk fVdcyEKuSw/6AtZBacXq+yJUDuTBzaZ4N+hebI1bbrGJ+OL2HITabvO3/yG6l7PX/XM/O3 n8F7aR8ebi8o7HnpzLXuQt8qBAFOq+k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-582-tL4dSMluP5-CM5Od2hJ5SQ-1; Thu, 13 Apr 2023 11:18:13 -0400 X-MC-Unique: tL4dSMluP5-CM5Od2hJ5SQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A42C6885620; Thu, 13 Apr 2023 15:18:12 +0000 (UTC) Received: from x1.nl (unknown [10.39.192.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 66D5B141511D; Thu, 13 Apr 2023 15:18:11 +0000 (UTC) From: Hans de Goede To: Pavel Machek , Lee Jones , Yauhen Kharuzhy Cc: Hans de Goede , linux-leds@vger.kernel.org Subject: [PATCH 1/5] leds: Add Intel Cherry Trail Whiskey Cove PMIC LED driver Date: Thu, 13 Apr 2023 17:18:04 +0200 Message-Id: <20230413151808.20900-2-hdegoede@redhat.com> In-Reply-To: <20230413151808.20900-1-hdegoede@redhat.com> References: <20230413151808.20900-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org From: Yauhen Kharuzhy Add support for LEDs connected to the Intel Cherry Trail Whiskey Cove PMIC. Charger and general-purpose leds are supported. Hardware blinking is implemented, breathing is not. This driver was tested with Lenovo Yoga Book notebook. Changes by Hans de Goede (in response to review of v2): - Since the PMIC is connected to the battery any changes we make to the LED settings are permanent, even surviving reboot / poweroff. Save LED1 register settings on probe() and if auto-/hw-control was enabled on probe() restore the settings on remove() and shutdown(). - Delay switching LED1 to software control mode to first brightness write. - Use dynamically allocated drvdata instead of a global drvdata variable. - Ensure the LED is on when activating blinking. - Fix CHT_WC_LED_EFF_BREATHING val ((3 << 1) rather then BIT(3)). Link: https://lore.kernel.org/r/20190212205901.13037-2-jekhor@gmail.com Signed-off-by: Yauhen Kharuzhy Co-developed-by: Hans de Goede Signed-off-by: Hans de Goede --- drivers/leds/Kconfig | 11 + drivers/leds/Makefile | 1 + drivers/leds/leds-cht-wcove.c | 368 ++++++++++++++++++++++++++++++++++ 3 files changed, 380 insertions(+) create mode 100644 drivers/leds/leds-cht-wcove.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 9dbce09eabac..90835716f14a 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -122,6 +122,17 @@ config LEDS_BCM6358 This option enables support for LEDs connected to the BCM6358 LED HW controller accessed via MMIO registers. +config LEDS_CHT_WCOVE + tristate "LED support for Intel Cherry Trail Whiskey Cove PMIC" + depends on LEDS_CLASS + depends on INTEL_SOC_PMIC_CHTWC + help + This option enables support for charger and general purpose LEDs + connected to the Intel Cherrytrail Whiskey Cove PMIC. + + To compile this driver as a module, choose M here: the module + will be called leds-cht-wcove. + config LEDS_CPCAP tristate "LED Support for Motorola CPCAP" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index d30395d11fd8..78b5b69f9c54 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.o obj-$(CONFIG_LEDS_BCM6358) += leds-bcm6358.o obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.o obj-$(CONFIG_LEDS_BLINKM) += leds-blinkm.o +obj-$(CONFIG_LEDS_CHT_WCOVE) += leds-cht-wcove.o obj-$(CONFIG_LEDS_CLEVO_MAIL) += leds-clevo-mail.o obj-$(CONFIG_LEDS_COBALT_QUBE) += leds-cobalt-qube.o obj-$(CONFIG_LEDS_COBALT_RAQ) += leds-cobalt-raq.o diff --git a/drivers/leds/leds-cht-wcove.c b/drivers/leds/leds-cht-wcove.c new file mode 100644 index 000000000000..06447804d050 --- /dev/null +++ b/drivers/leds/leds-cht-wcove.c @@ -0,0 +1,368 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for LEDs connected to the Intel Cherry Trail Whiskey Cove PMIC + * + * Copyright 2019 Yauhen Kharuzhy + * Copyright 2023 Hans de Goede + * + * Based on Lenovo Yoga Book Android kernel sources + */ + +#include +#include +#include +#include +#include +#include +#include + +#define CHT_WC_LED1_CTRL 0x5e1f +#define CHT_WC_LED1_FSM 0x5e20 +#define CHT_WC_LED1_PWM 0x5e21 + +#define CHT_WC_LED2_CTRL 0x4fdf +#define CHT_WC_LED2_FSM 0x4fe0 +#define CHT_WC_LED2_PWM 0x4fe1 + +/* HW or SW control of charging led */ +#define CHT_WC_LED1_SWCTL BIT(0) +#define CHT_WC_LED1_ON BIT(1) + +#define CHT_WC_LED2_ON BIT(0) +#define CHT_WC_LED_I_MA2_5 (2 << 2) +/* LED current limit */ +#define CHT_WC_LED_I_MASK GENMASK(3, 2) + +#define CHT_WC_LED_F_1_4_HZ (0 << 4) +#define CHT_WC_LED_F_1_2_HZ (1 << 4) +#define CHT_WC_LED_F_1_HZ (2 << 4) +#define CHT_WC_LED_F_2_HZ (3 << 4) +#define CHT_WC_LED_F_MASK GENMASK(5, 4) + +#define CHT_WC_LED_EFF_OFF (0 << 1) +#define CHT_WC_LED_EFF_ON (1 << 1) +#define CHT_WC_LED_EFF_BLINKING (2 << 1) +#define CHT_WC_LED_EFF_BREATHING (3 << 1) +#define CHT_WC_LED_EFF_MASK GENMASK(2, 1) + +#define CHT_WC_LED_COUNT 2 + +struct cht_wc_led_regs { + /* Register addresses */ + u16 ctrl; + u16 fsm; + u16 pwm; + /* Mask + values for turning the LED on/off */ + u8 on_off_mask; + u8 on_val; + u8 off_val; +}; + +struct cht_wc_led_saved_regs { + unsigned int ctrl; + unsigned int fsm; + unsigned int pwm; +}; + +struct cht_wc_led { + struct led_classdev cdev; + const struct cht_wc_led_regs *regs; + struct regmap *regmap; + struct mutex mutex; +}; + +struct cht_wc_leds { + struct cht_wc_led leds[CHT_WC_LED_COUNT]; + /* Saved LED1 initial register values */ + struct cht_wc_led_saved_regs led1_initial_regs; +}; + +static const struct cht_wc_led_regs cht_wc_led_regs[CHT_WC_LED_COUNT] = { + { + .ctrl = CHT_WC_LED1_CTRL, + .fsm = CHT_WC_LED1_FSM, + .pwm = CHT_WC_LED1_PWM, + .on_off_mask = CHT_WC_LED1_SWCTL | CHT_WC_LED1_ON, + .on_val = CHT_WC_LED1_SWCTL | CHT_WC_LED1_ON, + .off_val = CHT_WC_LED1_SWCTL, + }, + { + .ctrl = CHT_WC_LED2_CTRL, + .fsm = CHT_WC_LED2_FSM, + .pwm = CHT_WC_LED2_PWM, + .on_off_mask = CHT_WC_LED2_ON, + .on_val = CHT_WC_LED2_ON, + .off_val = 0, + }, +}; + +static const char * const cht_wc_leds_names[CHT_WC_LED_COUNT] = { + "cht-wc::" LED_FUNCTION_CHARGING, + "cht-wc::" LED_FUNCTION_INDICATOR, +}; + +static int cht_wc_leds_brightness_set(struct led_classdev *cdev, + enum led_brightness value) +{ + struct cht_wc_led *led = container_of(cdev, struct cht_wc_led, cdev); + int ret; + + mutex_lock(&led->mutex); + + if (!value) { + ret = regmap_update_bits(led->regmap, led->regs->ctrl, + led->regs->on_off_mask, led->regs->off_val); + if (ret) + dev_err(cdev->dev, "Failed to turn off: %d\n", ret); + + /* Disable HW blinking */ + ret = regmap_update_bits(led->regmap, led->regs->fsm, + CHT_WC_LED_EFF_MASK, CHT_WC_LED_EFF_ON); + if (ret < 0) + dev_err(cdev->dev, "Failed to update LED FSM reg: %d\n", ret); + } else { + ret = regmap_write(led->regmap, led->regs->pwm, value); + if (ret) + dev_err(cdev->dev, "Failed to set brightness: %d\n", ret); + + ret = regmap_update_bits(led->regmap, led->regs->ctrl, + led->regs->on_off_mask, led->regs->on_val); + if (ret) + dev_err(cdev->dev, "Failed to turn on: %d\n", ret); + } + + mutex_unlock(&led->mutex); + + return ret; +} + +enum led_brightness cht_wc_leds_brightness_get(struct led_classdev *cdev) +{ + struct cht_wc_led *led = container_of(cdev, struct cht_wc_led, cdev); + unsigned int val; + int ret; + + mutex_lock(&led->mutex); + + ret = regmap_read(led->regmap, led->regs->ctrl, &val); + if (ret < 0) { + dev_err(cdev->dev, "Failed to read LED CTRL reg: %d\n", ret); + ret = LED_OFF; + goto done; + } + + val &= led->regs->on_off_mask; + if (val != led->regs->on_val) { + ret = LED_OFF; + goto done; + } + + ret = regmap_read(led->regmap, led->regs->pwm, &val); + if (ret < 0) { + dev_err(cdev->dev, "Failed to read LED PWM reg: %d\n", ret); + ret = LED_ON; + goto done; + } + + ret = val; +done: + mutex_unlock(&led->mutex); + + return ret; +} + +/* Return blinking period for given CTRL reg value */ +static unsigned long cht_wc_leds_get_period(int ctrl) +{ + ctrl &= CHT_WC_LED_F_MASK; + + switch (ctrl) { + case CHT_WC_LED_F_1_4_HZ: + return 1000 * 4; + case CHT_WC_LED_F_1_2_HZ: + return 1000 * 2; + case CHT_WC_LED_F_1_HZ: + return 1000; + case CHT_WC_LED_F_2_HZ: + return 1000 / 2; + }; + + return 0; +} + +/* + * Find suitable hardware blink mode for given period. + * period < 750 ms - select 2 HZ + * 750 ms <= period < 1500 ms - select 1 HZ + * 1500 ms <= period < 3000 ms - select 1/2 HZ + * 3000 ms <= period < 5000 ms - select 1/4 HZ + * 5000 ms <= period - return -1 + */ +static int cht_wc_leds_find_freq(unsigned long period) +{ + if (period < 750) + return CHT_WC_LED_F_2_HZ; + else if (period < 1500) + return CHT_WC_LED_F_1_HZ; + else if (period < 3000) + return CHT_WC_LED_F_1_2_HZ; + else if (period < 5000) + return CHT_WC_LED_F_1_4_HZ; + else + return -1; +} + +static int cht_wc_leds_blink_set(struct led_classdev *cdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct cht_wc_led *led = container_of(cdev, struct cht_wc_led, cdev); + unsigned int ctrl; + int ret; + + mutex_lock(&led->mutex); + + if (!*delay_on && !*delay_off) + *delay_on = *delay_off = 1000; + + ctrl = cht_wc_leds_find_freq(*delay_on + *delay_off); + if (ctrl < 0) { + /* Disable HW blinking */ + ret = regmap_update_bits(led->regmap, led->regs->fsm, + CHT_WC_LED_EFF_MASK, CHT_WC_LED_EFF_ON); + if (ret < 0) + dev_err(cdev->dev, "Failed to update LED FSM reg: %d\n", ret); + + /* Fallback to software timer */ + *delay_on = *delay_off = 0; + ret = -EINVAL; + goto done; + } + + ret = regmap_update_bits(led->regmap, led->regs->fsm, + CHT_WC_LED_EFF_MASK, CHT_WC_LED_EFF_BLINKING); + if (ret < 0) + dev_err(cdev->dev, "Failed to update LED FSM reg: %d\n", ret); + + /* Set the frequency and make sure the LED is on */ + ret = regmap_update_bits(led->regmap, led->regs->ctrl, + CHT_WC_LED_F_MASK | led->regs->on_off_mask, + ctrl | led->regs->on_val); + if (ret < 0) + dev_err(cdev->dev, "Failed to update LED CTRL reg: %d\n", ret); + + *delay_off = *delay_on = cht_wc_leds_get_period(ctrl) / 2; + +done: + mutex_unlock(&led->mutex); + + return ret; +} + +static int cht_wc_led_save_regs(struct cht_wc_led *led, + struct cht_wc_led_saved_regs *saved_regs) +{ + int ret; + + ret = regmap_read(led->regmap, led->regs->ctrl, &saved_regs->ctrl); + if (ret < 0) + return ret; + + ret = regmap_read(led->regmap, led->regs->fsm, &saved_regs->fsm); + if (ret < 0) + return ret; + + return regmap_read(led->regmap, led->regs->pwm, &saved_regs->pwm); +} + +static void cht_wc_led_restore_regs(struct cht_wc_led *led, + const struct cht_wc_led_saved_regs *saved_regs) +{ + regmap_write(led->regmap, led->regs->ctrl, saved_regs->ctrl); + regmap_write(led->regmap, led->regs->fsm, saved_regs->fsm); + regmap_write(led->regmap, led->regs->pwm, saved_regs->pwm); +} + +static int cht_wc_leds_probe(struct platform_device *pdev) +{ + struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent); + struct cht_wc_leds *leds; + int ret; + int i; + + leds = devm_kzalloc(&pdev->dev, sizeof(*leds), GFP_KERNEL); + if (!leds) + return -ENOMEM; + + /* + * LED1 might be in hw-controlled mode when this driver gets loaded; and + * since the PMIC is always powered by the battery any changes made are + * permanent. Save LED1 regs to restore them on remove() or shutdown(). + */ + leds->leds[0].regs = &cht_wc_led_regs[0]; + leds->leds[0].regmap = pmic->regmap; + ret = cht_wc_led_save_regs(&leds->leds[0], &leds->led1_initial_regs); + if (ret < 0) + return ret; + + for (i = 0; i < CHT_WC_LED_COUNT; i++) { + struct cht_wc_led *led = &leds->leds[i]; + + led->regs = &cht_wc_led_regs[i]; + led->regmap = pmic->regmap; + mutex_init(&led->mutex); + led->cdev.name = cht_wc_leds_names[i]; + led->cdev.brightness_set_blocking = cht_wc_leds_brightness_set; + led->cdev.brightness_get = cht_wc_leds_brightness_get; + led->cdev.blink_set = cht_wc_leds_blink_set; + led->cdev.max_brightness = 255; + + ret = led_classdev_register(&pdev->dev, &led->cdev); + if (ret < 0) + return ret; + } + + platform_set_drvdata(pdev, leds); + return 0; +} + +static void cht_wc_leds_remove(struct platform_device *pdev) +{ + struct cht_wc_leds *leds = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < CHT_WC_LED_COUNT; i++) + led_classdev_unregister(&leds->leds[i].cdev); + + /* Restore LED1 regs if hw-control was active, else leave LED1 off. */ + if (!(leds->led1_initial_regs.ctrl & CHT_WC_LED1_SWCTL)) + cht_wc_led_restore_regs(&leds->leds[0], &leds->led1_initial_regs); +} + +static void cht_wc_leds_disable(struct platform_device *pdev) +{ + struct cht_wc_leds *leds = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < CHT_WC_LED_COUNT; i++) + cht_wc_leds_brightness_set(&leds->leds[i].cdev, LED_OFF); + + /* Restore LED1 regs if hw-control was active, else leave LED1 off. */ + if (!(leds->led1_initial_regs.ctrl & CHT_WC_LED1_SWCTL)) + cht_wc_led_restore_regs(&leds->leds[0], &leds->led1_initial_regs); +} + +static struct platform_driver cht_wc_leds_driver = { + .probe = cht_wc_leds_probe, + .remove_new = cht_wc_leds_remove, + .shutdown = cht_wc_leds_disable, + .driver = { + .name = "cht_wcove_leds", + }, +}; +module_platform_driver(cht_wc_leds_driver); + +MODULE_ALIAS("platform:cht_wcove_leds"); +MODULE_DESCRIPTION("Intel Cherry Trail Whiskey Cove PMIC LEDs driver"); +MODULE_AUTHOR("Yauhen Kharuzhy "); +MODULE_LICENSE("GPL"); From patchwork Thu Apr 13 15:18:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 673036 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AFDFC77B61 for ; Thu, 13 Apr 2023 15:18:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231367AbjDMPS4 (ORCPT ); Thu, 13 Apr 2023 11:18:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229938AbjDMPS4 (ORCPT ); Thu, 13 Apr 2023 11:18:56 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A88FE7D8E for ; Thu, 13 Apr 2023 08:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681399097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nJ47cVJlHnKJwn62y6tFjxdmA7al47zHicuz/WaXQ8o=; b=GDE+hGISw25mJuxS8BaM9A4BS7th/j0RL/ieCk9NznjlChflvMIbVgpWfz1d9PMYTJhPwj pS6R8asYSN05THABa9ELlwKEJ79uFHbK6eBPCTGriHAPlYVoNtY2/sYzA78AReAu7WgQk+ wmPoNsCSdwsqEdcgw0kwE/Q6x7sUZTc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-636-x33vrFJIPIihpLOvwaxW3w-1; Thu, 13 Apr 2023 11:18:14 -0400 X-MC-Unique: x33vrFJIPIihpLOvwaxW3w-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1D8CD1C00ABD; Thu, 13 Apr 2023 15:18:14 +0000 (UTC) Received: from x1.nl (unknown [10.39.192.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB2FE141511D; Thu, 13 Apr 2023 15:18:12 +0000 (UTC) From: Hans de Goede To: Pavel Machek , Lee Jones , Yauhen Kharuzhy Cc: Hans de Goede , linux-leds@vger.kernel.org Subject: [PATCH 2/5] leds: cht-wcove: Add suspend/resume handling Date: Thu, 13 Apr 2023 17:18:05 +0200 Message-Id: <20230413151808.20900-3-hdegoede@redhat.com> In-Reply-To: <20230413151808.20900-1-hdegoede@redhat.com> References: <20230413151808.20900-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org When LED1 is showing the tablet is charging and then the device gets suspended followed by unplugging the charger, then it will incorrectly still show it is charging. To avoid this turn both LEDs off on suspend, just like the PMIC always turns them off when the tablet is powered off (even if the tablet is charging). If hw-control is supported for LED1, then restore the initial hw-control settings to let the hw control LED1 while suspended. To restore the state the LEDs had before suspending, save it before turning the LEDs off and restore it on resume. Signed-off-by: Hans de Goede Acked-by: Pavel Machek --- drivers/leds/leds-cht-wcove.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/leds/leds-cht-wcove.c b/drivers/leds/leds-cht-wcove.c index 06447804d050..2d968ddd18c5 100644 --- a/drivers/leds/leds-cht-wcove.c +++ b/drivers/leds/leds-cht-wcove.c @@ -15,6 +15,7 @@ #include #include #include +#include #define CHT_WC_LED1_CTRL 0x5e1f #define CHT_WC_LED1_FSM 0x5e20 @@ -69,6 +70,7 @@ struct cht_wc_led { const struct cht_wc_led_regs *regs; struct regmap *regmap; struct mutex mutex; + struct cht_wc_led_saved_regs saved_regs; }; struct cht_wc_leds { @@ -352,12 +354,43 @@ static void cht_wc_leds_disable(struct platform_device *pdev) cht_wc_led_restore_regs(&leds->leds[0], &leds->led1_initial_regs); } +/* On suspend save current settings and turn LEDs off */ +static int cht_wc_leds_suspend(struct device *dev) +{ + struct cht_wc_leds *leds = dev_get_drvdata(dev); + int i, ret; + + for (i = 0; i < CHT_WC_LED_COUNT; i++) { + ret = cht_wc_led_save_regs(&leds->leds[i], &leds->leds[i].saved_regs); + if (ret < 0) + return ret; + } + + cht_wc_leds_disable(to_platform_device(dev)); + return 0; +} + +/* On resume restore the saveds settings */ +static int cht_wc_leds_resume(struct device *dev) +{ + struct cht_wc_leds *leds = dev_get_drvdata(dev); + int i; + + for (i = 0; i < CHT_WC_LED_COUNT; i++) + cht_wc_led_restore_regs(&leds->leds[i], &leds->leds[i].saved_regs); + + return 0; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(cht_wc_leds_pm, cht_wc_leds_suspend, cht_wc_leds_resume); + static struct platform_driver cht_wc_leds_driver = { .probe = cht_wc_leds_probe, .remove_new = cht_wc_leds_remove, .shutdown = cht_wc_leds_disable, .driver = { .name = "cht_wcove_leds", + .pm = pm_sleep_ptr(&cht_wc_leds_pm), }, }; module_platform_driver(cht_wc_leds_driver); From patchwork Thu Apr 13 15:18:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 673725 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE8F4C77B6E for ; Thu, 13 Apr 2023 15:19:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231526AbjDMPTF (ORCPT ); Thu, 13 Apr 2023 11:19:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229999AbjDMPTE (ORCPT ); Thu, 13 Apr 2023 11:19:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA532903B for ; Thu, 13 Apr 2023 08:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681399098; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w56AS1tZ6os14W041FUHUjU+xMJeZy2xcFKJl82QFmg=; b=bO/B5OZpwXyNmbQMkdC4bLfAO7MnoV42KDgy9U5YI4hB+08STj+wHwNvq+pqcYSGB3v2bt uRXYIbprZjn3SesuJFmCchbsKlVn5tFPDbnSGPoJsZz4FJka33JT2zPQqNEEWBQH4pk1C/ q4N9ngQHWwBSRorE7SZwg/cxebA5s+4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-219-TDJcA7iGP9uxRuurUBjyXQ-1; Thu, 13 Apr 2023 11:18:16 -0400 X-MC-Unique: TDJcA7iGP9uxRuurUBjyXQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A54B8996F5; Thu, 13 Apr 2023 15:18:16 +0000 (UTC) Received: from x1.nl (unknown [10.39.192.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 78861141511D; Thu, 13 Apr 2023 15:18:14 +0000 (UTC) From: Hans de Goede To: Pavel Machek , Lee Jones , Yauhen Kharuzhy Cc: Hans de Goede , linux-leds@vger.kernel.org, Jacek Anaszewski Subject: [PATCH 3/5] leds: cht-wcove: Add support for breathing mode use hw_pattern sysfs API Date: Thu, 13 Apr 2023 17:18:06 +0200 Message-Id: <20230413151808.20900-4-hdegoede@redhat.com> In-Reply-To: <20230413151808.20900-1-hdegoede@redhat.com> References: <20230413151808.20900-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org The hw-blinking of the LED controller in the Whiskey Cove PMIC can also be used for a hw-breathing effect. As discussed during review of v2 of the submission of the new leds-cht-wcove driver, the LED subsystem already supports breathing mode on several other LED controllers using the hw_pattern interface. Implement a pattern_set callback to implement breathing mode modelled after the breathing mode supported by the SC27xx breathing light and Crane EL15203000 LED drivers. The Whiskey Cove PMIC's breathing mode is closer to the EL15203000 one then to the SC27xx one since it does not support staying high / low for a specific time, it only supports rise and fall times. As such the supported hw_pattern and the documentation for this is almost a 1:1 copy of the pattern/docs for the EL15203000 breathing mode. Suggested-by: Jacek Anaszewski Link: https://lore.kernel.org/all/6beed61c-1fc6-6525-e873-a8978f5fbffb@gmail.com/ Signed-off-by: Hans de Goede --- Documentation/leds/index.rst | 1 + Documentation/leds/leds-cht-wcove.rst | 29 ++++++++++++++++++ drivers/leds/leds-cht-wcove.c | 42 ++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 Documentation/leds/leds-cht-wcove.rst diff --git a/Documentation/leds/index.rst b/Documentation/leds/index.rst index b9ca081fac71..c92612271e25 100644 --- a/Documentation/leds/index.rst +++ b/Documentation/leds/index.rst @@ -17,6 +17,7 @@ LEDs uleds leds-blinkm + leds-cht-wcove leds-el15203000 leds-lm3556 leds-lp3944 diff --git a/Documentation/leds/leds-cht-wcove.rst b/Documentation/leds/leds-cht-wcove.rst new file mode 100644 index 000000000000..fa79d8fd7ef8 --- /dev/null +++ b/Documentation/leds/leds-cht-wcove.rst @@ -0,0 +1,29 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=========================================================== +Kernel driver for Intel Cherry Trail Whiskey Cove PMIC LEDs +=========================================================== + +/sys/class/leds//hw_pattern +-------------------------------- + +Specify a hardware pattern for the Whiskey Cove PMIC LEDs. + +The only supported pattern is hardware breathing mode:: + + "0 2000 1 2000" + + ^ + | + Max-| --- + | / \ + | / \ + | / \ / + | / \ / + Min-|- --- + | + 0------2------4--> time (sec) + +The rise and fall times must be the same value. +Supported values are 2000, 1000, 500 and 250 for +breathing frequencies of 1/4, 1/2, 1 and 2 Hz. diff --git a/drivers/leds/leds-cht-wcove.c b/drivers/leds/leds-cht-wcove.c index 2d968ddd18c5..2b6c6f39d406 100644 --- a/drivers/leds/leds-cht-wcove.c +++ b/drivers/leds/leds-cht-wcove.c @@ -214,9 +214,10 @@ static int cht_wc_leds_find_freq(unsigned long period) return -1; } -static int cht_wc_leds_blink_set(struct led_classdev *cdev, - unsigned long *delay_on, - unsigned long *delay_off) +static int cht_wc_leds_set_effect(struct led_classdev *cdev, + unsigned long *delay_on, + unsigned long *delay_off, + u8 effect) { struct cht_wc_led *led = container_of(cdev, struct cht_wc_led, cdev); unsigned int ctrl; @@ -242,7 +243,7 @@ static int cht_wc_leds_blink_set(struct led_classdev *cdev, } ret = regmap_update_bits(led->regmap, led->regs->fsm, - CHT_WC_LED_EFF_MASK, CHT_WC_LED_EFF_BLINKING); + CHT_WC_LED_EFF_MASK, effect); if (ret < 0) dev_err(cdev->dev, "Failed to update LED FSM reg: %d\n", ret); @@ -261,6 +262,37 @@ static int cht_wc_leds_blink_set(struct led_classdev *cdev, return ret; } +static int cht_wc_leds_blink_set(struct led_classdev *cdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + return cht_wc_leds_set_effect(cdev, delay_on, delay_off, CHT_WC_LED_EFF_BLINKING); +} + +static int cht_wc_leds_pattern_set(struct led_classdev *cdev, + struct led_pattern *pattern, + u32 len, int repeat) +{ + unsigned long delay_off, delay_on; + + if (repeat > 0 || len != 2 || + pattern[0].brightness != LED_OFF || pattern[1].brightness != LED_ON || + pattern[0].delta_t != pattern[1].delta_t || + (pattern[0].delta_t != 250 && pattern[0].delta_t != 500 && + pattern[0].delta_t != 1000 && pattern[0].delta_t != 2000)) + return -EINVAL; + + delay_off = pattern[0].delta_t; + delay_on = pattern[1].delta_t; + + return cht_wc_leds_set_effect(cdev, &delay_on, &delay_off, CHT_WC_LED_EFF_BREATHING); +} + +static int cht_wc_leds_pattern_clear(struct led_classdev *cdev) +{ + return cht_wc_leds_brightness_set(cdev, LED_OFF); +} + static int cht_wc_led_save_regs(struct cht_wc_led *led, struct cht_wc_led_saved_regs *saved_regs) { @@ -317,6 +349,8 @@ static int cht_wc_leds_probe(struct platform_device *pdev) led->cdev.brightness_set_blocking = cht_wc_leds_brightness_set; led->cdev.brightness_get = cht_wc_leds_brightness_get; led->cdev.blink_set = cht_wc_leds_blink_set; + led->cdev.pattern_set = cht_wc_leds_pattern_set; + led->cdev.pattern_clear = cht_wc_leds_pattern_clear; led->cdev.max_brightness = 255; ret = led_classdev_register(&pdev->dev, &led->cdev); From patchwork Thu Apr 13 15:18:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 673724 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93074C77B61 for ; Thu, 13 Apr 2023 15:19:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231289AbjDMPTm (ORCPT ); Thu, 13 Apr 2023 11:19:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230083AbjDMPTk (ORCPT ); Thu, 13 Apr 2023 11:19:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066A29EF7 for ; Thu, 13 Apr 2023 08:18:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681399103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=19DAvj0MIEfSChS6L62Nsvj3SR7juAb8fhzuGqtvJkI=; b=CrMs/bFXGni5ftmo610TnNI9+9Xksh+rKhL5/Vg2gRDkNscsmZwlxEz7gmVLEAT11jXAAS 4i1f9G/BA9o2/fo2wn/JOAEWfNjLwm+kS6z4SrsgMjIURvikxC87tUtuWaXyps4CEfE2Xc iciNJBWS+g9S+TQ8Evz4J8oCtaOne+Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-n8SdJqWwM3Sl2CDbY7zaSg-1; Thu, 13 Apr 2023 11:18:18 -0400 X-MC-Unique: n8SdJqWwM3Sl2CDbY7zaSg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9BF6938149B7; Thu, 13 Apr 2023 15:18:17 +0000 (UTC) Received: from x1.nl (unknown [10.39.192.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BA0C141511D; Thu, 13 Apr 2023 15:18:16 +0000 (UTC) From: Hans de Goede To: Pavel Machek , Lee Jones , Yauhen Kharuzhy Cc: Hans de Goede , linux-leds@vger.kernel.org Subject: [PATCH 4/5] leds: cht-wcove: Set default trigger for charging LED Date: Thu, 13 Apr 2023 17:18:07 +0200 Message-Id: <20230413151808.20900-5-hdegoede@redhat.com> In-Reply-To: <20230413151808.20900-1-hdegoede@redhat.com> References: <20230413151808.20900-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Set a default trigger for the charging LED based on the machine-model as set by the PMIC MFD driver. Signed-off-by: Hans de Goede --- drivers/leds/leds-cht-wcove.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/leds/leds-cht-wcove.c b/drivers/leds/leds-cht-wcove.c index 2b6c6f39d406..597bfbe19cc2 100644 --- a/drivers/leds/leds-cht-wcove.c +++ b/drivers/leds/leds-cht-wcove.c @@ -339,6 +339,25 @@ static int cht_wc_leds_probe(struct platform_device *pdev) if (ret < 0) return ret; + /* Set LED1 default trigger based on machine model */ + switch (pmic->cht_wc_model) { + case INTEL_CHT_WC_GPD_WIN_POCKET: + leds->leds[0].cdev.default_trigger = "max170xx_battery-charging-blink-full-solid"; + break; + case INTEL_CHT_WC_XIAOMI_MIPAD2: + leds->leds[0].cdev.default_trigger = "bq27520-0-charging-blink-full-solid"; + break; + case INTEL_CHT_WC_LENOVO_YOGABOOK1: + leds->leds[0].cdev.default_trigger = "bq27542-0-charging-blink-full-solid"; + break; + case INTEL_CHT_WC_LENOVO_YT3_X90: + leds->leds[0].cdev.default_trigger = "bq27500-1-charging-blink-full-solid"; + break; + default: + dev_warn(&pdev->dev, "Unknown model, no default charging trigger\n"); + break; + } + for (i = 0; i < CHT_WC_LED_COUNT; i++) { struct cht_wc_led *led = &leds->leds[i]; From patchwork Thu Apr 13 15:18:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 673034 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1ED06C77B71 for ; Thu, 13 Apr 2023 15:19:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbjDMPTG (ORCPT ); Thu, 13 Apr 2023 11:19:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229999AbjDMPTF (ORCPT ); Thu, 13 Apr 2023 11:19:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 649C79755 for ; Thu, 13 Apr 2023 08:18:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681399101; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4cEBUnbg1vE7QsUZJZ/ROdFhuT0hQY8iLC6B/T3ZzLk=; b=Kuc3TyP+RPqaYnE+fqppnPuOuN+8HztxrODQZrxXzl7inKFcBM5VPJIyyLx2nmYHvw57fX WURNEIIgYifnShyd6FnIpYhedrS/zr1vJz5B/s/4u53UInvO8QKOA5WVmwAE3p3A46zdVs Tu2IU4R9zVQj6qahmZ4uClmtGGAzMUg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-250-dzd7R9brOrCYpIEzyIjIyw-1; Thu, 13 Apr 2023 11:18:19 -0400 X-MC-Unique: dzd7R9brOrCYpIEzyIjIyw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 02438885626; Thu, 13 Apr 2023 15:18:19 +0000 (UTC) Received: from x1.nl (unknown [10.39.192.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id D734F141511D; Thu, 13 Apr 2023 15:18:17 +0000 (UTC) From: Hans de Goede To: Pavel Machek , Lee Jones , Yauhen Kharuzhy Cc: Hans de Goede , linux-leds@vger.kernel.org Subject: [PATCH 5/5] leds: cht-wcove: Use breathing when LED_INIT_DEFAULT_TRIGGER is set Date: Thu, 13 Apr 2023 17:18:08 +0200 Message-Id: <20230413151808.20900-6-hdegoede@redhat.com> In-Reply-To: <20230413151808.20900-1-hdegoede@redhat.com> References: <20230413151808.20900-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org The desired default behavior of LED1 / the charge LED is breathing while charging and on/solid when full. Since triggers cannot select breathing, blink_set() gets called when charging. Use breathing when the default "charging-blink-full-solid" trigger is used to achieve the desired default behavior. Signed-off-by: Hans de Goede --- drivers/leds/leds-cht-wcove.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/leds/leds-cht-wcove.c b/drivers/leds/leds-cht-wcove.c index 597bfbe19cc2..125e9331348c 100644 --- a/drivers/leds/leds-cht-wcove.c +++ b/drivers/leds/leds-cht-wcove.c @@ -266,7 +266,19 @@ static int cht_wc_leds_blink_set(struct led_classdev *cdev, unsigned long *delay_on, unsigned long *delay_off) { - return cht_wc_leds_set_effect(cdev, delay_on, delay_off, CHT_WC_LED_EFF_BLINKING); + u8 effect = CHT_WC_LED_EFF_BLINKING; + + /* + * The desired default behavior of LED1 / the charge LED is breathing + * while charging and on/solid when full. Since triggers cannot select + * breathing, blink_set() gets called when charging. Use breathing + * when the default "charging-blink-full-solid" trigger is used to + * achieve the desired default behavior. + */ + if (cdev->flags & LED_INIT_DEFAULT_TRIGGER) + effect = CHT_WC_LED_EFF_BREATHING; + + return cht_wc_leds_set_effect(cdev, delay_on, delay_off, effect); } static int cht_wc_leds_pattern_set(struct led_classdev *cdev,