From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender4-op-o12.zoho.com (sender4-op-o12.zoho.com [136.143.188.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34CD63859CB; Fri, 27 Feb 2026 19:07:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.12 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772219241; cv=pass; b=SuXpp5GzAsTIkfkG/CFF4bargoaznwOCaYQWBU2WlExRHRv/s/C4ouMW9Q/t86YFvTf+UU9J0wwRWR/O/7cVpwa6U3mQUnrVDR52n1ssvgzUWJwVZWIW7Aye6Y+OLkm68rAon1aHoaZ7UPiidD1idVD+3sjLAc4RAXpRnLmNh0E= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772219241; c=relaxed/simple; bh=eQs8UtEW4GMqtNXX/FhanPTQXPpVEotyg/qfpWuDQOg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qGbFD/4QohuZuEIOVL6ZDjcEk/oGeZs/aYWNrQnFQSFFiMER2gHW3WMycIIvG1oRSkPk3wR38skg8NgWM8EdjVeq+1/Ezv916FGTlHOLO6xd+a/bTUgmorw4a+NYteupm8A80QROIi6Pr8/FppGgx0tfkPHephcgsWW3DKcHJ1c= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=k16FLxJ3; arc=pass smtp.client-ip=136.143.188.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="k16FLxJ3" ARC-Seal: i=1; a=rsa-sha256; t=1772219223; cv=none; d=zohomail.com; s=zohoarc; b=HnG2q/yS6tFJL4dj04owLkpugAaRjNNPuMOdzgSG5p+xIrLVMVxlpBPYnZiq9UOheY2qb03fcnkQgfjJM18YeowZBg+l17Cy0E+WaTXTpqZTaZ40du3FkOITqNdGU6UrxZIwTbp9LRSOpvcm3k9J5sd9TFbX83ejmz5b+SNQzRo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1772219223; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=GBopvb0XLYM5a5cYSz2Hjnrp4LOHHlYa0YRcHewmDc0=; b=H0AP+gTTbhODpzG9Kc/rBDG3f4nRIQAdpIEaxqPK8a6kS3U1u+Fq3JVQQbKRV32XOQstCzCYFFiMQPHmic6PIGMQPHrSHxFfG0j7y6uaUXL1Pv/lgvGptI+xIiB8K7I3RrAs1t5EVmGlBqtfHn8ZVQz6fjWULNYvgOcN4zgb8oA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1772219223; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=GBopvb0XLYM5a5cYSz2Hjnrp4LOHHlYa0YRcHewmDc0=; b=k16FLxJ3V4lraSO9m45g6elJrcK7ai1ECN73M9+noQq9qg4zCdLcpH1wMVyaq2PF Rii66RSjHklDVImNKtc9dTA44AhIbjHG7UGdkt1UXtvJHH8oiOzCotiDgNMEyTfLpde npP4BEK8q3t1jCu+zeaTHfx4BGWplky0p6ZX+Njyt09oMKeKRJrNJn0jJ7FOyrV/A8E M9KY/OojsSsiMZBe1k2BrtfseFUir6nUAey/SE5lTylT6WqjCSKwL0MAwYmdMN4qx4o yiV5pM7EPVgZLtMu1UPmFyXjHe/DrS5LmQv8+TEe0cGgH6tbrvuG1EDXX+9y07mTMp4 73i3OQh/5g== Received: by mx.zohomail.com with SMTPS id 1772219222496341.97858049630565; Fri, 27 Feb 2026 11:07:02 -0800 (PST) From: Rong Zhang To: Lee Jones , Pavel Machek , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Ike Panhc Cc: Rong Zhang , Vishnu Sankar , vsankar@lenovo.com, linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org Subject: [RFC PATCH 8/9] platform/x86: ideapad-laptop: Decouple HW & cdev brightness for kbd backlight Date: Sat, 28 Feb 2026 03:06:05 +0800 Message-ID: <20260227190617.271388-9-i@rong.moe> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260227190617.271388-1-i@rong.moe> References: <20260227190617.271388-1-i@rong.moe> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External Some recent models come with an ambient light sensor (ALS). On these models, their EC will automatically set the keyboard backlight to an appropriate brightness when the effective "HW brightness" is 3. "HW brightness" can't be perfectly mapped to an LED classdev brightness, but the EC does use this predefined brightness value to represent auto mode. Currently, the code processing keyboard backlight is coupled with LED classdev, making it hard to expose the auto brightness (ALS) mode to the userspace. As the first step toward the goal, decouple HW brightness from LED classdev brightness, and update comments about corresponding backlight modes. Signed-off-by: Rong Zhang --- drivers/platform/x86/lenovo/ideapad-laptop.c | 114 +++++++++++++------ 1 file changed, 80 insertions(+), 34 deletions(-) diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c index ae1ebb071fab0..b9af0294fc933 100644 --- a/drivers/platform/x86/lenovo/ideapad-laptop.c +++ b/drivers/platform/x86/lenovo/ideapad-laptop.c @@ -134,10 +134,31 @@ enum { }; /* - * These correspond to the number of supported states - 1 - * Future keyboard types may need a new system, if there's a collision - * KBD_BL_TRISTATE_AUTO has no way to report or set the auto state - * so it effectively has 3 states, but needs to handle 4 + * The enumeration has two purposes: + * - as an internal identifier for all known types of keyboard backlight + * - as a mandatory parameter of the KBLC command + * + * For each type, the HW brightness values are defined as follows: + * +--------------------------+----------+-----+------+------+ + * | HW brightness | 0 | 1 | 2 | 3 | + * | Type | | | | | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_STANDARD | off | on | N/A | N/A | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_TRISTATE | off | low | high | N/A | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_TRISTATE_AUTO | off | low | high | auto | + * +--------------------------+----------+-----+------+------+ + * + * We map LED classdev brightness for KBD_BL_TRISTATE_AUTO as follows: + * +--------------------------+----------+-----+------+ + * | LED classdev brightness | 0 | 1 | 2 | + * | Operation | | | | + * +--------------------------+----------+-----+------+ + * | Read | off/auto | low | high | + * +--------------------------+----------+-----+------+ + * | Write | off | low | high | + * +--------------------------+----------+-----+------+ */ enum { KBD_BL_STANDARD = 1, @@ -1592,7 +1613,24 @@ static int ideapad_kbd_bl_check_tristate(int type) return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO); } -static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) +static int ideapad_kbd_bl_brightness_parse(struct ideapad_private *priv, + unsigned int hw_brightness) +{ + /* Off, low or high */ + if (hw_brightness <= priv->kbd_bl.led.max_brightness) + return hw_brightness; + + /* Auto, report as off */ + if (hw_brightness == priv->kbd_bl.led.max_brightness + 1) + return 0; + + /* Unknown value */ + dev_warn(&priv->platform_device->dev, + "Unknown keyboard backlight value: %u", hw_brightness); + return -EINVAL; +} + +static int ideapad_kbd_bl_hw_brightness_get(struct ideapad_private *priv) { unsigned long value; int err; @@ -1606,21 +1644,7 @@ static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) if (err) return err; - /* Convert returned value to brightness level */ - value = FIELD_GET(KBD_BL_GET_BRIGHTNESS, value); - - /* Off, low or high */ - if (value <= priv->kbd_bl.led.max_brightness) - return value; - - /* Auto, report as off */ - if (value == priv->kbd_bl.led.max_brightness + 1) - return 0; - - /* Unknown value */ - dev_warn(&priv->platform_device->dev, - "Unknown keyboard backlight value: %lu", value); - return -EINVAL; + return FIELD_GET(KBD_BL_GET_BRIGHTNESS, value); } err = eval_hals(priv->adev->handle, &value); @@ -1630,6 +1654,16 @@ static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) return !!test_bit(HALS_KBD_BL_STATE_BIT, &value); } +static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) +{ + int hw_brightness = ideapad_kbd_bl_hw_brightness_get(priv); + + if (hw_brightness < 0) + return hw_brightness; + + return ideapad_kbd_bl_brightness_parse(priv, hw_brightness); +} + static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev) { struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led); @@ -1637,24 +1671,31 @@ static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_cla return ideapad_kbd_bl_brightness_get(priv); } -static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness) +static int ideapad_kbd_bl_hw_brightness_set(struct ideapad_private *priv, + unsigned int hw_brightness) { - int err; unsigned long value; int type = priv->kbd_bl.type; if (ideapad_kbd_bl_check_tristate(type)) { - if (brightness > priv->kbd_bl.led.max_brightness) - return -EINVAL; - - value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) | + value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, hw_brightness) | FIELD_PREP(KBD_BL_COMMAND_TYPE, type) | KBD_BL_COMMAND_SET; - err = exec_kblc(priv->adev->handle, value); + return exec_kblc(priv->adev->handle, value); } else { - err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF); + value = hw_brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF; + return exec_sals(priv->adev->handle, value); } +} + +static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness) +{ + int err; + if (brightness > priv->kbd_bl.led.max_brightness) + return -EINVAL; + + err = ideapad_kbd_bl_hw_brightness_set(priv, brightness); if (err) return err; @@ -1671,6 +1712,16 @@ static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev, return ideapad_kbd_bl_brightness_set(priv, brightness); } +static void ideapad_kbd_bl_notify_known(struct ideapad_private *priv, unsigned int brightness) +{ + if (brightness == priv->kbd_bl.last_brightness) + return; + + priv->kbd_bl.last_brightness = brightness; + + led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness); +} + static void ideapad_kbd_bl_notify(struct ideapad_private *priv) { int brightness; @@ -1682,12 +1733,7 @@ static void ideapad_kbd_bl_notify(struct ideapad_private *priv) if (brightness < 0) return; - if (brightness == priv->kbd_bl.last_brightness) - return; - - priv->kbd_bl.last_brightness = brightness; - - led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness); + ideapad_kbd_bl_notify_known(priv, brightness); } static int ideapad_kbd_bl_init(struct ideapad_private *priv) -- 2.51.0