From patchwork Sun Oct 30 18:30:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sander Vanheule X-Patchwork-Id: 1696842 X-Patchwork-Delegate: sander@svanheule.net Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=n4VvxQSA; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=svanheule.net header.i=@svanheule.net header.a=rsa-sha256 header.s=mail1707 header.b=cwjosicA; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N0lN06nvSz23lV for ; Mon, 31 Oct 2022 05:35:44 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=k8tLDk6pApKhUm6/wnGHi3CH9ct9IaLPYhmaD+Y3OBg=; b=n4VvxQSAMaOMWx fgmzxhZK4LQE/AknFQWhmNjHek5Iszd6n/4peRw7h5RnuXNOWkSF5ZlQnfqUxOKPcP+LuYbbV9vAZ 2bm823pBQecx8iAxKFyCrPVQ4CScumJhp9ExTK+30rQNlGuJ0ngd7Rvg4wP+EqFrSvfrhTMwVxlVW FN0CIUK8e7R+hYSqoK+cTrHEMJXyNxNERDmAqVKL0JGkM3QrtDqfD1wLbT3xrY/wDj/V9OFioCT8/ +79nyNgs62kCJwT/01yM4WZieopWypL5n4QMnF5MGUW9D2fzDTik3yk1Z5UocystJ6LtnUWU0KG0l 7Ww/sJOThkCmtIEM/IMQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1opD5z-001nFC-Uy; Sun, 30 Oct 2022 18:31:44 +0000 Received: from polaris.svanheule.net ([2a00:c98:2060:a004:1::200]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1opD5f-001n4I-1j for openwrt-devel@lists.openwrt.org; Sun, 30 Oct 2022 18:31:25 +0000 Received: from terra.. (unknown [IPv6:2a02:a03f:eaf9:8401:aa9f:5d01:1b2a:e3cd]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id BAC9433BF22; Sun, 30 Oct 2022 19:31:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1667154674; 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=DiDZEouN1r9Fb8bo+Hifck+aVrC6qvZmXlkIfAxySO8=; b=cwjosicA8nBGn7HkVvlnQPA6NLnKURmy7jkE1fSpEylo5j0qCKAWhx3+GTz/NkLNAE1APy 1O5UUmJdb9tek4O0z9R0ALZSD/UVV75upoCY8ImOLjN1xJcU4/77l+ZO3F0ftKYkEHXiSB 607Sge/tvp4pK6/ASWTXUbCuGlbW0LVyaw7VSjdqLofwJcRpaBkPwHVYmhHdGmnnMbkB9w mxtV4YLf2j23RursUgPOOHUCUml2qD8/byOC0RoL7Ib5mo/WoAVkDPLx0j4fCRsruitxVq uhnJ96E2yEq5uB+M0eVzDtUX4wHzm4bd+UCzOswUfruOA72TQTwZ6Y8m0N3u1Q== From: Sander Vanheule To: openwrt-devel@lists.openwrt.org, Olliver Schinagl , Alexandru Gagniuc Cc: INAGAKI Hiroshi , Markus Stockhausen , Stijn Segers , Sander Vanheule Subject: [PATCH v3 03/13] realtek: add base register field LED support Date: Sun, 30 Oct 2022 19:30:56 +0100 Message-Id: <7467168728bb6733c30cac4e231fa4dbfbbd7f20.1667154408.git.sander@svanheule.net> X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221030_113123_767346_B1F6B9F4 X-CRM114-Status: GOOD ( 22.13 ) X-Spam-Score: -2.5 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Realtek LED hardware peripherals share some common features. They are typically controlled through a register field, a few bits in size. These allow the LED to be turned off or on, or toggled at one o [...] Content analysis details: (-2.5 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [2a00:c98:2060:a004:1:0:0:200 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Realtek LED hardware peripherals share some common features. They are typically controlled through a register field, a few bits in size. These allow the LED to be turned off or on, or toggled at one of a number of intervals. Support for register field LEDs is added in a new directory, so Realtek LEDs drivers can be grouped together. Signed-off-by: Sander Vanheule --- Changes in v3: - Merge symbol selection updates to ease bisecting --- .../files-5.10/drivers/leds/realtek/Kconfig | 10 ++ .../files-5.10/drivers/leds/realtek/Makefile | 2 + .../drivers/leds/realtek/led-regfield.c | 99 +++++++++++++++++++ .../drivers/leds/realtek/led-regfield.h | 55 +++++++++++ ...s-add-Realtek-LED-hardware-directory.patch | 37 +++++++ target/linux/realtek/rtl838x/config-5.10 | 1 + target/linux/realtek/rtl839x/config-5.10 | 1 + target/linux/realtek/rtl930x/config-5.10 | 1 + target/linux/realtek/rtl931x/config-5.10 | 1 + 9 files changed, 207 insertions(+) create mode 100644 target/linux/realtek/files-5.10/drivers/leds/realtek/Kconfig create mode 100644 target/linux/realtek/files-5.10/drivers/leds/realtek/Makefile create mode 100644 target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.c create mode 100644 target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.h create mode 100644 target/linux/realtek/patches-5.10/202-leds-add-Realtek-LED-hardware-directory.patch diff --git a/target/linux/realtek/files-5.10/drivers/leds/realtek/Kconfig b/target/linux/realtek/files-5.10/drivers/leds/realtek/Kconfig new file mode 100644 index 000000000000..687be63f739c --- /dev/null +++ b/target/linux/realtek/files-5.10/drivers/leds/realtek/Kconfig @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig LEDS_RTL + bool "Realtek LED support" + help + Say Y to enable support for LED peripherals found on Realtek switch + SoCs. + +if LEDS_RTL + +endif # LED_RTL diff --git a/target/linux/realtek/files-5.10/drivers/leds/realtek/Makefile b/target/linux/realtek/files-5.10/drivers/leds/realtek/Makefile new file mode 100644 index 000000000000..125dc45ff2ce --- /dev/null +++ b/target/linux/realtek/files-5.10/drivers/leds/realtek/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_LEDS_RTL) += led-regfield.o diff --git a/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.c b/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.c new file mode 100644 index 000000000000..dd669c94c62e --- /dev/null +++ b/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Base support for simple, monochromatic LEDs, configured by a short register + * field. The register field should allow the LED to be turned on or off, or + * toggled at a predetermined rate with a 50% duty cycle. + */ + +#include +#include + +#include "led-regfield.h" + +static int regfield_led_set_mode(struct regfield_led *led, unsigned int mode) +{ + int err; + + err = regmap_field_write(led->field, mode); + if (!err && led->commit) + led->commit(led); + + return err; +} + +static void regfield_led_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brightness) +{ + struct regfield_led *led = to_regfield_led(led_cdev); + bool turn_off = brightness == 0; + + if ((!led->active_low && turn_off) || (led->active_low && !turn_off)) + regfield_led_set_mode(led, led->modes->off); + else + regfield_led_set_mode(led, led->modes->on); +} + +static enum led_brightness regfield_led_brightness_get(struct led_classdev *led_cdev) +{ + struct regfield_led *led = to_regfield_led(led_cdev); + u32 val = 0; + + regmap_field_read(led->field, &val); + + if ((!led->active_low && (val == led->modes->off)) || + (led->active_low && (val == led->modes->on))) + return 0; + else + return 1; +} + +static int regfield_led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on, + unsigned long *delay_off) +{ + struct regfield_led *led = to_regfield_led(led_cdev); + const struct regfield_led_blink_mode *blink = led->modes->blink; + u32 cycle_ms = *delay_on + *delay_off; + int err; + + if (cycle_ms == 0) + cycle_ms = 500; + + while (blink->toggle_ms && (blink + 1)->toggle_ms) { + /* + * Split at the arithmetic mean of intervals, which compares + * the half cycle interval (cycle_ms / 2) to the mean toggle + * interval ((blink->toggle_ms + (blink + 1)->toggle_ms) / 2). + * Since the (/ 2) is common on both sides, it can be dropped. + */ + if (cycle_ms < (blink->toggle_ms + (blink + 1)->toggle_ms)) + break; + blink++; + } + + err = regfield_led_set_mode(led, blink->mode); + if (err) + return err; + + *delay_on = blink->toggle_ms; + *delay_off = blink->toggle_ms; + + return 0; +} + +int regfield_led_init(struct regfield_led *led, struct regmap_field *field, + struct fwnode_handle *fwnode, const struct regfield_led_modes *modes) +{ + if (IS_ERR_OR_NULL(field) || !modes) + return -EINVAL; + + led->field = field; + led->modes = modes; + led->active_low = fwnode_property_read_bool(fwnode, "active-low"); + + led->cdev.max_brightness = 1; + led->cdev.brightness_set = regfield_led_brightness_set; + led->cdev.brightness_get = regfield_led_brightness_get; + led->cdev.blink_set = regfield_led_blink_set; + + return 0; +} diff --git a/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.h b/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.h new file mode 100644 index 000000000000..f0a7c02514ad --- /dev/null +++ b/target/linux/realtek/files-5.10/drivers/leds/realtek/led-regfield.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef LEDS_REALTEK_LED_REGFIELD_H +#define LEDS_REALTEK_LED_REGFIELD_H + +#include +#include +#include +#include +#include + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0) +#include +#else +#include +#endif + +/* + * Register field LED + * + * Next to being able to turn an LED on or off, Realtek provides LED management + * peripherals with hardware accelerated blinking modes with 50% duty cycle. + */ +struct regfield_led_blink_mode { + u16 toggle_ms; /* Toggle interval in ms */ + u8 mode; /* ASIC mode bits */ +}; + +struct regfield_led_modes { + u8 off; + u8 on; + /* + * List of blink modes. Must be sorted by interval and terminated by an + * entry where regfield_led_blink_mode::toggle_ms equals zero. + */ + struct regfield_led_blink_mode blink[]; +}; + +struct regfield_led { + struct led_classdev cdev; + const struct regfield_led_modes *modes; + struct regmap_field *field; + void (*commit)(struct regfield_led *led); + bool active_low; +}; + +static inline struct regfield_led *to_regfield_led(struct led_classdev *cdev) +{ + return container_of(cdev, struct regfield_led, cdev); +} + +int regfield_led_init(struct regfield_led *led, struct regmap_field *field, + struct fwnode_handle *led_node, const struct regfield_led_modes *modes); + +#endif diff --git a/target/linux/realtek/patches-5.10/202-leds-add-Realtek-LED-hardware-directory.patch b/target/linux/realtek/patches-5.10/202-leds-add-Realtek-LED-hardware-directory.patch new file mode 100644 index 000000000000..4866a09f5783 --- /dev/null +++ b/target/linux/realtek/patches-5.10/202-leds-add-Realtek-LED-hardware-directory.patch @@ -0,0 +1,37 @@ +From d71ec8184236356c50088b00b2417fb142e72bd9 Mon Sep 17 00:00:00 2001 +From: Sander Vanheule +Date: Sun, 10 Jul 2022 11:31:53 +0200 +Subject: leds: add Realtek LED hardware directory +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Realtek LED hardware peripherals share some common features. They are +typically controlled through a register field, a few bits in size. These +allow the LED to be turned off or on, or toggled at one of a number of +intervals. + +Signed-off-by: Sander Vanheule +--- + +--- a/drivers/leds/Kconfig ++++ b/drivers/leds/Kconfig +@@ -944,4 +944,7 @@ config LEDS_UBNT_LEDBAR + comment "LED Triggers" + source "drivers/leds/trigger/Kconfig" + ++comment "Realtek LED drivers" ++source "drivers/leds/realtek/Kconfig" ++ + endif # NEW_LEDS +--- a/drivers/leds/Makefile ++++ b/drivers/leds/Makefile +@@ -107,5 +107,8 @@ obj-$(CONFIG_LEDS_USER) += uleds.o + # LED Triggers + obj-$(CONFIG_LEDS_TRIGGERS) += trigger/ + ++# Realtek LED drivers ++obj-y += realtek/ ++ + # LED Blink + obj-y += blink/ diff --git a/target/linux/realtek/rtl838x/config-5.10 b/target/linux/realtek/rtl838x/config-5.10 index fdfbc2461c1b..574405e05f06 100644 --- a/target/linux/realtek/rtl838x/config-5.10 +++ b/target/linux/realtek/rtl838x/config-5.10 @@ -116,6 +116,7 @@ CONFIG_IRQ_MIPS_CPU=y CONFIG_IRQ_WORK=y CONFIG_JFFS2_ZLIB=y CONFIG_LEDS_GPIO=y +CONFIG_LEDS_RTL=y CONFIG_LEGACY_PTYS=y CONFIG_LEGACY_PTY_COUNT=256 CONFIG_LIBFDT=y diff --git a/target/linux/realtek/rtl839x/config-5.10 b/target/linux/realtek/rtl839x/config-5.10 index 344e9f988209..5bb76371b58e 100644 --- a/target/linux/realtek/rtl839x/config-5.10 +++ b/target/linux/realtek/rtl839x/config-5.10 @@ -109,6 +109,7 @@ CONFIG_IRQ_MIPS_CPU=y CONFIG_IRQ_WORK=y CONFIG_JFFS2_ZLIB=y CONFIG_LEDS_GPIO=y +CONFIG_LEDS_RTL=y CONFIG_LEGACY_PTYS=y CONFIG_LEGACY_PTY_COUNT=256 CONFIG_LIBFDT=y diff --git a/target/linux/realtek/rtl930x/config-5.10 b/target/linux/realtek/rtl930x/config-5.10 index ab31186ae134..e153caadd368 100644 --- a/target/linux/realtek/rtl930x/config-5.10 +++ b/target/linux/realtek/rtl930x/config-5.10 @@ -100,6 +100,7 @@ CONFIG_IRQ_MIPS_CPU=y CONFIG_IRQ_WORK=y CONFIG_JFFS2_ZLIB=y CONFIG_LEDS_GPIO=y +# CONFIG_LEDS_RTL is not set CONFIG_LEGACY_PTYS=y CONFIG_LEGACY_PTY_COUNT=256 CONFIG_LIBFDT=y diff --git a/target/linux/realtek/rtl931x/config-5.10 b/target/linux/realtek/rtl931x/config-5.10 index 00f751ce855a..19ae6c2dce89 100644 --- a/target/linux/realtek/rtl931x/config-5.10 +++ b/target/linux/realtek/rtl931x/config-5.10 @@ -101,6 +101,7 @@ CONFIG_IRQ_MIPS_CPU=y CONFIG_IRQ_WORK=y CONFIG_JFFS2_ZLIB=y CONFIG_LEDS_GPIO=y +# CONFIG_LEDS_RTL is not set CONFIG_LEGACY_PTYS=y CONFIG_LEGACY_PTY_COUNT=256 CONFIG_LIBFDT=y