From nobody Tue Feb 10 04:02:43 2026 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 646B7EB64DC for ; Sat, 24 Jun 2023 08:43:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232845AbjFXIms (ORCPT ); Sat, 24 Jun 2023 04:42:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231514AbjFXImj (ORCPT ); Sat, 24 Jun 2023 04:42:39 -0400 Received: from 6.mo563.mail-out.ovh.net (6.mo563.mail-out.ovh.net [46.105.55.200]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5E56E5B for ; Sat, 24 Jun 2023 01:42:37 -0700 (PDT) Received: from director1.derp.mail-out.ovh.net (director1.derp.mail-out.ovh.net [51.68.80.175]) by mo563.mail-out.ovh.net (Postfix) with ESMTPS id 8CC8D22EFC; Sat, 24 Jun 2023 08:42:35 +0000 (UTC) Received: from director1.derp.mail-out.ovh.net (director1.derp.mail-out.ovh.net. [127.0.0.1]) by director1.derp.mail-out.ovh.net (inspect_sender_mail_agent) with SMTP for ; Sat, 24 Jun 2023 08:42:35 +0000 (UTC) Received: from pro2.mail.ovh.net (unknown [10.108.4.220]) by director1.derp.mail-out.ovh.net (Postfix) with ESMTPS id 344422011E3; Sat, 24 Jun 2023 08:42:35 +0000 (UTC) Received: from traphandler.com (88.161.25.233) by DAG1EX1.emp2.local (172.16.2.1) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 24 Jun 2023 10:42:34 +0200 From: Jean-Jacques Hiblot To: , , , , CC: , , , Jean-Jacques Hiblot Subject: [PATCH v10 5/5] leds: Add a multicolor LED driver to group monochromatic LEDs Date: Sat, 24 Jun 2023 10:42:17 +0200 Message-ID: <20230624084217.3079205-6-jjhiblot@traphandler.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230624084217.3079205-1-jjhiblot@traphandler.com> References: <20230624084217.3079205-1-jjhiblot@traphandler.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [88.161.25.233] X-ClientProxiedBy: DAG2EX1.emp2.local (172.16.2.11) To DAG1EX1.emp2.local (172.16.2.1) X-Ovh-Tracer-Id: 12428527598681602523 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvhedrgeegjedgtdejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucenucfjughrpefhvfevufffkffojghfggfgtghisehtkeertdertddtnecuhfhrohhmpeflvggrnhdqlfgrtghquhgvshcujfhisghlohhtuceojhhjhhhisghlohhtsehtrhgrphhhrghnughlvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeduteevleevvefggfdvueffffejhfehheeuiedtgedtjeeghfehueduudegfeefueenucfkpheptddrtddrtddrtddpkeekrdduiedurddvhedrvdeffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhhouggvpehsmhhtphhouhhtpdhhvghlohepughirhgvtghtohhruddruggvrhhprdhmrghilhdqohhuthdrohhvhhdrnhgvthdpihhnvghtpedtrddtrddtrddtpdhmrghilhhfrhhomhepjhhjhhhisghlohhtsehtrhgrphhhrghnughlvghrrdgtohhmpdhnsggprhgtphhtthhopedupdhrtghpthhtoheplhhinhhugidqlhgvughssehvghgvrhdrkhgvrhhnvghlrdhorhhgpdfovfetjfhoshhtpehmohehieef Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Grouping multiple monochrome LEDs into a multicolor LED device has a few benefits over handling the group in user-space: - The state of the LEDs relative to each other is consistent. In other words, if 2 threads competes to set the LED to green and red, the end-result cannot be black or yellow. - The multicolor LED as a whole can be driven through the sysfs LED interface. Signed-off-by: Jean-Jacques Hiblot Reviewed-by: Lee Jones --- drivers/leds/rgb/Kconfig | 12 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-group-multicolor.c | 165 +++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 drivers/leds/rgb/leds-group-multicolor.c diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig index 360c8679c6e2..183bccc06cf3 100644 --- a/drivers/leds/rgb/Kconfig +++ b/drivers/leds/rgb/Kconfig @@ -2,6 +2,18 @@ =20 if LEDS_CLASS_MULTICOLOR =20 +config LEDS_GROUP_MULTICOLOR + tristate "LEDs group multi-color support" + depends on OF || COMPILE_TEST + help + This option enables support for monochrome LEDs that are grouped + into multicolor LEDs which is useful in the case where LEDs of + different colors are physically grouped in a single multi-color LED + and driven by a controller that doesn't have multi-color support. + + To compile this driver as a module, choose M here: the module + will be called leds-group-multicolor. + config LEDS_PWM_MULTICOLOR tristate "PWM driven multi-color LED Support" depends on PWM diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile index 8c01daf63f61..c11cc56384e7 100644 --- a/drivers/leds/rgb/Makefile +++ b/drivers/leds/rgb/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 =20 +obj-$(CONFIG_LEDS_GROUP_MULTICOLOR) +=3D leds-group-multicolor.o obj-$(CONFIG_LEDS_PWM_MULTICOLOR) +=3D leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) +=3D leds-qcom-lpg.o obj-$(CONFIG_LEDS_MT6370_RGB) +=3D leds-mt6370-rgb.o diff --git a/drivers/leds/rgb/leds-group-multicolor.c b/drivers/leds/rgb/le= ds-group-multicolor.c new file mode 100644 index 000000000000..df3e41a6272b --- /dev/null +++ b/drivers/leds/rgb/leds-group-multicolor.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Multi-color LED built with monochromatic LED devices + * + * This driver groups several monochromatic LED devices in a single multic= olor LED device. + * + * Compared to handling this grouping in user-space, the benefits are: + * - The state of the monochromatic LED relative to each other is always c= onsistent. + * - The sysfs interface of the LEDs can be used for the group as a whole. + * + * Copyright 2023 Jean-Jacques Hiblot + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +struct leds_multicolor { + struct led_classdev_mc mc_cdev; + struct led_classdev **monochromatics; +}; + +static int leds_gmc_set(struct led_classdev *cdev, enum led_brightness bri= ghtness) +{ + struct led_classdev_mc *mc_cdev =3D lcdev_to_mccdev(cdev); + struct leds_multicolor *priv =3D container_of(mc_cdev, struct leds_multic= olor, mc_cdev); + const unsigned int group_max_brightness =3D mc_cdev->led_cdev.max_brightn= ess; + int i; + + for (i =3D 0; i < mc_cdev->num_colors; i++) { + struct led_classdev *mono =3D priv->monochromatics[i]; + const unsigned int mono_max_brightness =3D mono->max_brightness; + unsigned int intensity =3D mc_cdev->subled_info[i].intensity; + int mono_brightness; + + /* + * Scale the brightness according to relative intensity of the + * color AND the max brightness of the monochromatic LED. + */ + mono_brightness =3D DIV_ROUND_CLOSEST(brightness * intensity * mono_max_= brightness, + group_max_brightness * group_max_brightness); + + led_set_brightness(mono, mono_brightness); + } + + return 0; +} + +static void restore_sysfs_write_access(void *data) +{ + struct led_classdev *led_cdev =3D data; + + mutex_lock(&led_cdev->led_access); + led_sysfs_enable(led_cdev); + mutex_unlock(&led_cdev->led_access); +} + +static int leds_gmc_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct led_init_data init_data =3D {}; + struct led_classdev *cdev; + struct mc_subled *subled; + struct leds_multicolor *priv; + unsigned int max_brightness =3D 0; + int i, ret, count =3D 0; + + priv =3D devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + for (;;) { + struct led_classdev *led_cdev; + + led_cdev =3D devm_of_led_get_optional(dev, count); + if (IS_ERR(led_cdev)) + return dev_err_probe(dev, PTR_ERR(led_cdev), "Unable to get LED #%d", + count); + if (!led_cdev) + break; + + priv->monochromatics =3D devm_krealloc_array(dev, priv->monochromatics, + count + 1, sizeof(*priv->monochromatics), + GFP_KERNEL); + if (!priv->monochromatics) + return -ENOMEM; + + priv->monochromatics[count] =3D led_cdev; + + max_brightness =3D max(max_brightness, led_cdev->max_brightness); + + count++; + } + + subled =3D devm_kcalloc(dev, count, sizeof(*subled), GFP_KERNEL); + if (!subled) + return -ENOMEM; + priv->mc_cdev.subled_info =3D subled; + + for (i =3D 0; i < count; i++) { + struct led_classdev *led_cdev =3D priv->monochromatics[i]; + + subled[i].color_index =3D led_cdev->color; + + /* Configure the LED intensity to its maximum */ + subled[i].intensity =3D max_brightness; + } + + /* Initialise the multicolor's LED class device */ + cdev =3D &priv->mc_cdev.led_cdev; + cdev->flags =3D LED_CORE_SUSPENDRESUME; + cdev->brightness_set_blocking =3D leds_gmc_set; + cdev->max_brightness =3D max_brightness; + cdev->color =3D LED_COLOR_ID_MULTI; + priv->mc_cdev.num_colors =3D count; + + init_data.fwnode =3D dev_fwnode(dev); + ret =3D devm_led_classdev_multicolor_register_ext(dev, &priv->mc_cdev, &i= nit_data); + if (ret) + return dev_err_probe(dev, ret, "failed to register multicolor LED for %s= .\n", + cdev->name); + + ret =3D leds_gmc_set(cdev, cdev->brightness); + if (ret) + return dev_err_probe(dev, ret, "failed to set LED value for %s.", cdev->= name); + + for (i =3D 0; i < count; i++) { + struct led_classdev *led_cdev =3D priv->monochromatics[i]; + + /* Make monochromatic LED sysfs interface read-only */ + mutex_lock(&led_cdev->led_access); + led_sysfs_disable(led_cdev); + mutex_unlock(&led_cdev->led_access); + + /* Restore sysfs access when the multicolor LED is released */ + devm_add_action_or_reset(dev, restore_sysfs_write_access, led_cdev); + } + + return 0; +} + +static const struct of_device_id of_leds_group_multicolor_match[] =3D { + { .compatible =3D "leds-group-multicolor" }, + {} +}; +MODULE_DEVICE_TABLE(of, of_leds_group_multicolor_match); + +static struct platform_driver leds_group_multicolor_driver =3D { + .probe =3D leds_gmc_probe, + .driver =3D { + .name =3D "leds_group_multicolor", + .of_match_table =3D of_leds_group_multicolor_match, + } +}; +module_platform_driver(leds_group_multicolor_driver); + +MODULE_AUTHOR("Jean-Jacques Hiblot "); +MODULE_DESCRIPTION("LEDs group multicolor driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:leds-group-multicolor"); --=20 2.34.1