From: "lianfeng.ouyang" <lianfeng.ouyang@starfivetech.com>
To: Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
Griffin Kroah-Hartman <griffin@kroah.com>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Rob Herring <robh@kernel.org>, Conor Dooley <conor+dt@kernel.org>,
Philipp Zabel <p.zabel@pengutronix.de>
Cc: linux-leds@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org,
Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
Subject: [PATCH 2/2] leds: Add StarFive JHB100 heartbeat LED driver
Date: Fri, 4 Dec 2026 10:07:02 +0800 [thread overview]
Message-ID: <20261204020702.3787-3-lianfeng.ouyang@starfivetech.com> (raw)
In-Reply-To: <20261204020702.3787-1-lianfeng.ouyang@starfivetech.com>
From: Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
Add a driver for the hardware heartbeat generator of the StarFive JHB100
SoC. The block drives a single LED with a programmable pulse width and
period:
on time = 40 ms + PULSE_DURATION * 16 / clock rate
period = 80 ms + NORMAL_PERIOD * 32 / clock rate
With a 25 MHz clock this gives an on time of 40..711 ms and a period of
80..1422 ms.
The hardware uses this period only while it sees bus activity from the
CPUs. Otherwise it uses a second "abnormal" period, which the driver
does not touch.
The hardware can only blink, so the LED is registered with a maximum
brightness of 1. Setting the brightness to 1 runs the heartbeat with the
last applied timing, and 0 stops it.
blink_set() uses the hardware timing. Requests are clamped to the
supported range and the resulting delays are written back, as the LED
core expects. An unspecified request (0/0) uses 500 ms on and 500 ms
off. If only one delay is zero, -EINVAL is returned so that the LED core
sets a plain brightness instead.
"default-state" is supported. With "keep", the timing of a heartbeat
that is already running is taken over.
The clock rate is locked, because the programmed counters are only valid
for the rate they were calculated with.
Signed-off-by: Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
---
MAINTAINERS | 6 +
drivers/leds/Kconfig | 12 ++
drivers/leds/Makefile | 1 +
drivers/leds/leds-starfive-jhb100.c | 286 ++++++++++++++++++++++++++++
4 files changed, 305 insertions(+)
create mode 100644 drivers/leds/leds-starfive-jhb100.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..1ecdf7ddb29e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26881,6 +26881,12 @@ S: Supported
F: Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100-intc.yaml
F: drivers/irqchip/irq-starfive-jhb100-intc.c
+STARFIVE JHB100 HEARTBEAT LED DRIVER
+M: Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
+S: Supported
+F: Documentation/devicetree/bindings/leds/starfive,jhb100-leds.yaml
+F: drivers/leds/leds-starfive-jhb100.c
+
STARFIVE JHB100 PINCTRL DRIVERS
M: Changhuang Liang <changhuang.liang@starfivetech.com>
M: Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 7ec2c8d78542..1cadecea3f8b 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -1001,6 +1001,18 @@ config LEDS_ST1202
Say Y to enable support for LEDs connected to LED1202
LED driver chips accessed via the I2C bus.
+config LEDS_STARFIVE_JHB100
+ tristate "Heartbeat LED support for StarFive JHB100"
+ depends on LEDS_CLASS
+ depends on HAS_IOMEM
+ depends on ARCH_STARFIVE || COMPILE_TEST
+ help
+ This option enables support for the hardware heartbeat LED
+ controller found on the StarFive JHB100 SoC.
+
+ To compile this driver as a module, choose M here: the module
+ will be called leds-starfive-jhb100.
+
config LEDS_LTC3220
tristate "LED Driver for Analog Devices Inc. LTC3220"
depends on I2C && LEDS_CLASS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 4d4b089156e2..89d13dce37fe 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -89,6 +89,7 @@ obj-$(CONFIG_LEDS_QNAP_MCU) += leds-qnap-mcu.o
obj-$(CONFIG_LEDS_REGULATOR) += leds-regulator.o
obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o
obj-$(CONFIG_LEDS_ST1202) += leds-st1202.o
+obj-$(CONFIG_LEDS_STARFIVE_JHB100) += leds-starfive-jhb100.o
obj-$(CONFIG_LEDS_SUN50I_A100) += leds-sun50i-a100.o
obj-$(CONFIG_LEDS_SUNFIRE) += leds-sunfire.o
obj-$(CONFIG_LEDS_SYSCON) += leds-syscon.o
diff --git a/drivers/leds/leds-starfive-jhb100.c b/drivers/leds/leds-starfive-jhb100.c
new file mode 100644
index 000000000000..a28614028034
--- /dev/null
+++ b/drivers/leds/leds-starfive-jhb100.c
@@ -0,0 +1,286 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * StarFive JHB100 heartbeat LED controller driver
+ *
+ * Copyright (C) 2025 StarFive Technology Co., Ltd.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/container_of.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/leds.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/reset.h>
+#include <linux/spinlock.h>
+#include <linux/time64.h>
+#include <linux/types.h>
+
+#define HEARTBEAT_SYSCFG0 0x0
+#define HEARTBEAT_SYSCFG0_ENABLE BIT(0)
+#define HEARTBEAT_SYSCFG0_PULSE_DURATION_MASK GENMASK(20, 1)
+#define HEARTBEAT_SYSCFG0_PULSE_DURATION_MAX \
+ FIELD_MAX(HEARTBEAT_SYSCFG0_PULSE_DURATION_MASK)
+#define HEARTBEAT_SYSCFG1 0x4
+#define HEARTBEAT_SYSCFG1_NORMAL_PERIOD_MASK GENMASK(19, 0)
+#define HEARTBEAT_SYSCFG1_NORMAL_PERIOD_MAX \
+ FIELD_MAX(HEARTBEAT_SYSCFG1_NORMAL_PERIOD_MASK)
+#define HEARTBEAT_SYSCFG2 0x8
+
+/*
+ * Timing model, with "rate" being the heartbeat clock rate in Hz and
+ * PULSE_DURATION / NORMAL_PERIOD the register fields:
+ *
+ * on time [ms] = 40 + PULSE_DURATION * 16 * 1000 / rate
+ * period [ms] = 80 + NORMAL_PERIOD * 32 * 1000 / rate
+ *
+ * The normal period is only used while the hardware sees bus activity from
+ * the CPUs. Without activity it uses the abnormal period in SYSCFG2 (base
+ * 1280 ms, divider 512) instead, which this driver does not touch.
+ */
+#define HEARTBEAT_PULSE_DURATION_BASE_MS 40UL
+#define HEARTBEAT_PULSE_DURATION_CLK_DIV 16
+#define HEARTBEAT_NORMAL_PERIOD_BASE_MS 80UL
+#define HEARTBEAT_NORMAL_PERIOD_CLK_DIV 32
+
+/* Used for an unspecified blink request and as the probe-time timing */
+#define HEARTBEAT_DEFAULT_ON_MS 500
+#define HEARTBEAT_DEFAULT_OFF_MS 500
+
+struct jhb100_led_cnt {
+ u32 pulse_duration;
+ u32 normal_period;
+};
+
+struct jhb100_led {
+ void __iomem *base;
+ unsigned long rate;
+ spinlock_t lock; /* protects cnt and the registers */
+ struct jhb100_led_cnt cnt; /* last applied timing */
+ struct led_classdev cdev;
+};
+
+static struct jhb100_led *to_jhb100_led(struct led_classdev *cdev)
+{
+ return container_of(cdev, struct jhb100_led, cdev);
+}
+
+/* Longest time in ms that @base plus @max counts of a /@div counter give */
+static u64 jhb100_led_max_ms(const struct jhb100_led *led, u64 base, u32 max,
+ u32 div)
+{
+ return base + div64_ul((u64)max * div * MSEC_PER_SEC, led->rate);
+}
+
+/* @ms must be within [@base, jhb100_led_max_ms()] */
+static u32 jhb100_led_ms_to_cnt(const struct jhb100_led *led, u64 ms, u64 base,
+ u32 div)
+{
+ return div_u64((ms - base) * led->rate, div * MSEC_PER_SEC);
+}
+
+/*
+ * Convert a non-zero on/off request in ms into counter values. The request is
+ * clamped to what the hardware can do and the result is written back.
+ */
+static void jhb100_led_calc(const struct jhb100_led *led,
+ unsigned long *on, unsigned long *off,
+ struct jhb100_led_cnt *cnt)
+{
+ u64 max_on, max_period, on_ms, off_ms, period_ms;
+
+ max_on = jhb100_led_max_ms(led, HEARTBEAT_PULSE_DURATION_BASE_MS,
+ HEARTBEAT_SYSCFG0_PULSE_DURATION_MAX,
+ HEARTBEAT_PULSE_DURATION_CLK_DIV);
+ max_period = jhb100_led_max_ms(led, HEARTBEAT_NORMAL_PERIOD_BASE_MS,
+ HEARTBEAT_SYSCFG1_NORMAL_PERIOD_MAX,
+ HEARTBEAT_NORMAL_PERIOD_CLK_DIV);
+
+ /*
+ * max_period is always larger than max_on, so the room left for the
+ * off time cannot underflow, and on_ms + off_ms cannot wrap.
+ */
+ on_ms = clamp(*on, HEARTBEAT_PULSE_DURATION_BASE_MS, max_on);
+ off_ms = min(*off, max_period - on_ms);
+ period_ms = max(on_ms + off_ms, HEARTBEAT_NORMAL_PERIOD_BASE_MS);
+
+ cnt->pulse_duration =
+ jhb100_led_ms_to_cnt(led, on_ms,
+ HEARTBEAT_PULSE_DURATION_BASE_MS,
+ HEARTBEAT_PULSE_DURATION_CLK_DIV);
+ cnt->normal_period =
+ jhb100_led_ms_to_cnt(led, period_ms,
+ HEARTBEAT_NORMAL_PERIOD_BASE_MS,
+ HEARTBEAT_NORMAL_PERIOD_CLK_DIV);
+
+ *on = on_ms;
+ *off = period_ms - on_ms;
+}
+
+/* Apply @cnt (or the last applied timing if NULL) and start/stop the LED */
+static void jhb100_led_control(struct jhb100_led *led,
+ const struct jhb100_led_cnt *cnt, bool enable)
+{
+ unsigned long flags;
+ u32 ctrl;
+
+ spin_lock_irqsave(&led->lock, flags);
+
+ if (cnt)
+ led->cnt = *cnt;
+
+ ctrl = FIELD_PREP(HEARTBEAT_SYSCFG0_PULSE_DURATION_MASK,
+ led->cnt.pulse_duration);
+
+ if (enable)
+ ctrl |= HEARTBEAT_SYSCFG0_ENABLE;
+
+ /* Program the period before (re)enabling with the new pulse width */
+ writel(led->cnt.normal_period, led->base + HEARTBEAT_SYSCFG1);
+ writel(ctrl, led->base + HEARTBEAT_SYSCFG0);
+
+ spin_unlock_irqrestore(&led->lock, flags);
+}
+
+static int jhb100_led_blink_set(struct led_classdev *cdev,
+ unsigned long *delay_on,
+ unsigned long *delay_off)
+{
+ struct jhb100_led *led = to_jhb100_led(cdev);
+ struct jhb100_led_cnt cnt;
+
+ if (!*delay_on && !*delay_off) {
+ *delay_on = HEARTBEAT_DEFAULT_ON_MS;
+ *delay_off = HEARTBEAT_DEFAULT_OFF_MS;
+ }
+
+ /* Never on / never off: let the LED core set a plain brightness */
+ if (!*delay_on || !*delay_off)
+ return -EINVAL;
+
+ jhb100_led_calc(led, delay_on, delay_off, &cnt);
+ jhb100_led_control(led, &cnt, true);
+
+ return 0;
+}
+
+static enum led_brightness jhb100_led_brightness_get(struct led_classdev *cdev)
+{
+ struct jhb100_led *led = to_jhb100_led(cdev);
+
+ return !!(readl(led->base + HEARTBEAT_SYSCFG0) &
+ HEARTBEAT_SYSCFG0_ENABLE);
+}
+
+/*
+ * The hardware can only blink: brightness = 1 runs the heartbeat with the
+ * last applied timing, brightness = 0 stops it.
+ */
+static void jhb100_led_brightness_set(struct led_classdev *cdev,
+ enum led_brightness value)
+{
+ jhb100_led_control(to_jhb100_led(cdev), NULL, value);
+}
+
+static int jhb100_led_probe(struct platform_device *pdev)
+{
+ unsigned long def_on = HEARTBEAT_DEFAULT_ON_MS;
+ unsigned long def_off = HEARTBEAT_DEFAULT_OFF_MS;
+ struct device *dev = &pdev->dev;
+ struct led_init_data init_data = { .fwnode = dev_fwnode(dev) };
+ struct reset_control *rst;
+ struct jhb100_led *led;
+ struct clk *clk;
+ u32 ctrl;
+ int ret;
+
+ led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
+ if (!led)
+ return -ENOMEM;
+
+ led->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(led->base))
+ return PTR_ERR(led->base);
+
+ spin_lock_init(&led->lock);
+
+ clk = devm_clk_get_enabled(dev, NULL);
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk),
+ "Failed to get clock\n");
+
+ rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);
+ if (IS_ERR(rst))
+ return dev_err_probe(dev, PTR_ERR(rst),
+ "Failed to get reset\n");
+
+ /* The programmed timings are only valid for the current clock rate */
+ ret = devm_clk_rate_exclusive_get(dev, clk);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to lock clock rate\n");
+
+ led->rate = clk_get_rate(clk);
+ if (!led->rate)
+ return dev_err_probe(dev, -EINVAL, "Invalid clock rate\n");
+
+ jhb100_led_calc(led, &def_on, &def_off, &led->cnt);
+
+ led->cdev.max_brightness = 1;
+ led->cdev.brightness_get = jhb100_led_brightness_get;
+ led->cdev.brightness_set = jhb100_led_brightness_set;
+ led->cdev.blink_set = jhb100_led_blink_set;
+
+ switch (led_init_default_state_get(init_data.fwnode)) {
+ case LEDS_DEFSTATE_ON:
+ jhb100_led_brightness_set(&led->cdev, 1);
+ break;
+ case LEDS_DEFSTATE_KEEP:
+ /* Keep the timing of a heartbeat that is already running */
+ ctrl = readl(led->base + HEARTBEAT_SYSCFG0);
+ if (ctrl & HEARTBEAT_SYSCFG0_ENABLE) {
+ led->cnt.pulse_duration =
+ FIELD_GET(HEARTBEAT_SYSCFG0_PULSE_DURATION_MASK,
+ ctrl);
+ led->cnt.normal_period =
+ FIELD_GET(HEARTBEAT_SYSCFG1_NORMAL_PERIOD_MASK,
+ readl(led->base + HEARTBEAT_SYSCFG1));
+ }
+ break;
+ default:
+ jhb100_led_brightness_set(&led->cdev, 0);
+ break;
+ }
+
+ ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register LED\n");
+
+ return 0;
+}
+
+static const struct of_device_id jhb100_led_dt_ids[] = {
+ { .compatible = "starfive,jhb100-leds" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, jhb100_led_dt_ids);
+
+static struct platform_driver jhb100_led_driver = {
+ .driver = {
+ .name = "jhb100-leds",
+ .of_match_table = jhb100_led_dt_ids,
+ },
+ .probe = jhb100_led_probe,
+};
+module_platform_driver(jhb100_led_driver);
+
+MODULE_AUTHOR("Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>");
+MODULE_DESCRIPTION("StarFive JHB100 heartbeat LED driver");
+MODULE_LICENSE("GPL");
--
2.43.0
next prev parent reply other threads:[~2026-12-04 2:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 2:07 [PATCH 0/2] leds: Add StarFive JHB100 heartbeat LED driver lianfeng.ouyang
2026-12-04 2:07 ` [PATCH 1/2] dt-bindings: leds: Add StarFive JHB100 heartbeat LED controller lianfeng.ouyang
2026-12-04 2:07 ` lianfeng.ouyang [this message]
2026-12-04 2:17 ` [PATCH 0/2] leds: Add StarFive JHB100 heartbeat LED driver sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204020702.3787-3-lianfeng.ouyang@starfivetech.com \
--to=lianfeng.ouyang@starfivetech.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=griffin@kroah.com \
--cc=krzk+dt@kernel.org \
--cc=lee@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=p.zabel@pengutronix.de \
--cc=pavel@kernel.org \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox