From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=139.219.17.133 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796350046; b=DCiuQThIJoyRlY8reLoca1dQQzv55qlfsVRvS01CfAnWDxuKSTLU/TY2kfJr6B5nX1DF QrLUgwxDNhuAtiQZrPCu144LDCK7XsBpGK/Wnw26p7SAsdJhQPMs7jcvtMI3LIIGarErB 88cpKppibK2n7kZVeQ/58LFNVX32YStayAL8aofu/OKgeqHOQEpDie09m9tiFZshNaUJW aGR45vtHEwqRLOR0wZyVtSviCwZ3kFdbSRPF/BJKN4VesoZZZJ+wY9+ncIQbZ5x4eYs6X rbTIA1DSCMcyXVn6fKv3rwJMt+Kp79TD3TJti1jcsw0bHK1PnA7EXNIuE+BM52nDt4A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796350046; h=DMARC-Filter:Received:Received:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:Content-Transfer-Encoding: Content-Type:X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=b4+wU0Esz2zpOUAbf5xNwYDitQZns0uf45y884+hCAA=; b=ZZLtQ6YbvfnwWH5qSKhImVSLWyzoiTdEhBV5d/ITJ/rwQlzDmhg0wxivYi9EHXB59bA/ ruEIx5fGORW/585w0ZNnwo2wG1YKOdUBkfZBlsnNowGgxjtViQwL8S+vHIGnALsg2K4I3 5oz8BQ2yrlRuqI/l0/Zjf+iowdetFsPqnw2COcE61xj+xDODmpvS1eHrPbeJF7mKpCnuD D2hQcVUMLPcNmlxlRrjnesF5PlS2rHfva7+m5CAKCj8hpIzDXbK2n9XDAHHYfQ7G7Vhim e3hPG5fo1NgsldhnTPYsCFc/yx8zq8MnIvfSBS0G1IGt7MY//mmFTJr+T2JR4DT4PFg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=starfivetech.com; spf=pass smtp.mailfrom=starfivetech.com; arc=pass header.oldest-pass=0 smtp.remote-ip=139.219.17.133 Received: from CHN02-BJS-obe.outbound.protection.partner.outlook.cn (unknown [139.219.17.133]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F11A7606A339; Fri, 4 Dec 2026 02:07:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F11A7606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=starfivetech.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starfivetech.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CKMk625UUpa/Q4Mfpiy8YUCGescynHf4eA0VPnp49OnaGlXm1Hq7eWnKbFqDuhAhRj3ABoagk4TDuMrDPRFpvlFXqyNFX8TanvzptNRIVoDulVxITF1dgbU7qqPzDnrhMlLes9Ku/KFGC7Z/gn1zYUNoy3YdowSw4wJ/5Y+UAM0fsLCxGJd68qI8Pq6D7GsW+vPIpFy/yYu302q5uq84CpgMfwv2cTn7qfvIH1hcAT37u7ZyHSSHlvU+JJzIVuEDKezkCYVFlC1uQfclzNRfNsWbl86dSKrcYwxnqeQtwPrwm0OTPOmsdTMBaa47zSwKdL7XR0X+NtTStkUuyB0imA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=b4+wU0Esz2zpOUAbf5xNwYDitQZns0uf45y884+hCAA=; b=H1X3zNUYr4E8DfINll+7VAoeuQTDSzWXCjY9EcaL1mN3piLXFtZBXYtyGSbtOthw1VI/gb4N1ZEMY3X5j5ZujSsPo5rldtOVsu1K07Wxv8jDG7R6RIQE1Gxnbp+b3w0NR7USUki1RWj3NjUCSMbfpoZa1i0JaLDVJSEbDbyQn78s5zQ908lNAaZ6cdJ2IbmsWOt07m7Ufjfsk89fI73Jm9jewk9+TNubRL686AOV94bAh2gRBBwaF1SJFMSx2rj/cK7kdYuC3sMo9bmq9xQmPav3H6uA0MGglozkT1C+LOmIQOkT4nKd1RJ1EIyC4CXRzPNA5xfz8tM7TTKdQkqxUg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=starfivetech.com; dmarc=pass action=none header.from=starfivetech.com; dkim=pass header.d=starfivetech.com; arc=none Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=starfivetech.com; Received: from BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:1f::11) by BJSPR01MB0498.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:10::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.639.17; Fri, 4 Dec 2026 02:07:12 +0000 Received: from BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn ([fe80::d75f:eae6:f1d7:efe9]) by BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn ([fe80::d75f:eae6:f1d7:efe9%6]) with mapi id 15.21.0639.017; Fri, 4 Dec 2026 02:07:12 +0000 From: "lianfeng.ouyang" To: Lee Jones , Pavel Machek , Griffin Kroah-Hartman , Krzysztof Kozlowski , Rob Herring , Conor Dooley , Philipp Zabel Cc: linux-leds@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Lianfeng Ouyang Subject: [PATCH 2/2] leds: Add StarFive JHB100 heartbeat LED driver Date: Fri, 4 Dec 2026 10:07:02 +0800 Message-Id: <20261204020702.3787-3-lianfeng.ouyang@starfivetech.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204020702.3787-1-lianfeng.ouyang@starfivetech.com> References: <20261204020702.3787-1-lianfeng.ouyang@starfivetech.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BJSPR01CA0010.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:c::22) To BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:1f::11) X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BJSPR01MB0756:EE_|BJSPR01MB0498:EE_ X-MS-Office365-Filtering-Correlation-Id: df79d54b-2daa-48c9-1433-08df51ab4f41 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|52116014|7416014|366016|23010399003|1800799024|261009223027099003|22082099003|18002099003|10067099003|261026121527099003|56012099006|261102123227099003|261021123027099003|261028144727099003|261022075727099003|261022105027099003|261021121427099003|261026140227099003|38350700014; X-Microsoft-Antispam-Message-Info: XKlloA/umDInJaoXFu1FmMUWog+IzaoTiV+aQdoj4o/774/3Ut5kGbatYTO7g61mXic9bo78UV5qNnsIUvAKqXs2JvbWa9BLSCIihQ1PeuaRFskaBgODfzwzqrb/E8eU8dgU4MMF7gDE1aVdSu/W5xEYUBqhC1Bj1L09MwKfKwVYtHFOaZR7XwOvzyIca+vqV2Mbxzu2Tco9svckFg5jPwTAi6CVDPUPUxFzsZDseoZ9nM0JcO1OvdAoOp5phNALhdxYbBNu8gtmO3sP9/Cl70WMN5jvCTNOTt6QMcKeqMvyiMuFNuFFUCuohog7fZl4LENsGZRsNC2Gx3meV6w02Oadzd5n9P75i2ZFLLJsropk3mOKe+ArivKDBSkiKTWPKFO71c59X8bGGyFyU3BFFeUoIWnqp8hjrgngv0mtyClHgsij/1DynDS7mo47a/LU9O3LiYYD9A8H4mgOnAk7SYKmln/qfDuy+XpduinqZ09xRy305+5/41jsGUtZlOVmUdGI490/hu03eGaXvoAi4JlHsFkGwBug896R6oS8oJJ5BwmQzDoksFrb4oyBOmq6 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn;PTR:;CAT:NONE;SFS:(13230040)(376014)(52116014)(7416014)(366016)(23010399003)(1800799024)(261009223027099003)(22082099003)(18002099003)(10067099003)(261026121527099003)(56012099006)(261102123227099003)(261021123027099003)(261028144727099003)(261022075727099003)(261022105027099003)(261021121427099003)(261026140227099003)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?9Pk6tEVZ6Wli4O1bDDJd8jWYUZa7ImQmcake6tbDQS3vCVi+lt4Nj5ao1pQ/?= =?us-ascii?Q?HcvCi/0/3y2b6T/PtDI9PBqOK6iYhFAGMZ3vA73O9Ii4O64RuRZUIL5sjYjj?= =?us-ascii?Q?SwyjSmwSmhUH/P9Yvt6uz2nWKH4bxKmw+21wwWpT2MLKSL87sw+NQV9+pFDh?= =?us-ascii?Q?IV0y/o2NSDv4hc5Humwurh0xLrB2UKeJdG9ye5bd/SGaI85U/u7mA4tAm989?= =?us-ascii?Q?LeyIpT9TR+vjgA000ruvp0Hl+tDkMDyeAq9yr3fjdbMnaWdsi+ym6vT+wl9a?= =?us-ascii?Q?wMWRNJu2JJhJ6XSbucBd9DfwAOCF6iOjcALWEF2VRydDZJmS4yG1o7P4PsSe?= =?us-ascii?Q?pP/EybFwuYudZhHTg/NmTEcebHPvdTmfYPCmUSDVf3rjre1OvaKLDbMi5ttQ?= =?us-ascii?Q?ERGXkZDBgMMdKJQ1+T+bBhzs8nGjgTUgrmuycYQ/S3817cc2fwspSl1vwYjV?= =?us-ascii?Q?zC8OmG1+TsZZg6FQe7eN3L+Tat2iD+TctQFirUP0fTNpcUZowU6REQV2AYn4?= =?us-ascii?Q?RI67DdMtCbk/+ro3pVJOkS6e4HCPYu1nTAjO5Mmr5NyeOHlezCB4NH7HZ1W4?= =?us-ascii?Q?isTP5bJTKcZmG1XwKl1zJe29zSlzoYkbS2zwAAFbGmeIG/rZS5zA7V9k5YlJ?= =?us-ascii?Q?uhi+J2MRQLN3+kG1IWy9u/umVqvaW9DrwMzjpcj/H/GZIQTOzlqUj4s5ucXy?= =?us-ascii?Q?enFdpoXGakMmNzRI3Bmdmw8MrAF+Yit20usllDyI8pL3eOohWRK+DzsmwMeD?= =?us-ascii?Q?hDmmM6VDEQPGFx4480jZL6lrFwA2C6Bz3QQFJkBBXpd/RMkppQsp0n9H1VmK?= =?us-ascii?Q?CSDLiinK7RtKG4U5ntK9Z+ETv4pd5Dd/4BtH18qMyg3vSUDrlmtfXHrJDs02?= =?us-ascii?Q?xey0XDkjXTa6mGZI1UunnEFxhLRn3x4gCeL0ppbtkbnYmLP+I8u7834nv2bw?= =?us-ascii?Q?uRf1t4KgNeNfNcvueZnhypBJeYQ2UKZhwC+AS+ru6QKZ6oEx/zSwzkOgmvk5?= =?us-ascii?Q?ZAJ7jj1zr8ZTrTZsi+lpic3E/95vW15QmPyhT/Llpz29SLF6PVgMIvSRtOEw?= =?us-ascii?Q?qG7GLaIpZIQG/ZTWrLbO7rzZAi4lTn9lI7WM4ZnRF5DHus64LmHVmUcJjM0V?= =?us-ascii?Q?1VLD8QcH6YLtdqZAFkA54xGSwhIz4C+T3BbYwMBNaYiJI6mwrokFlZx+ziCl?= =?us-ascii?Q?PhPpiC0GGmOLDeUXtvDlOetKsL+SFQgruEqCWT2xgyMJevkhHkxj0En6QpGB?= =?us-ascii?Q?APLrSgYTowu4B1tLwBy1buuSvrQ6u3w+e7DfBYKYdMbvy2bHQPEWPMDPotsj?= =?us-ascii?Q?Le6W/gRQEkCEGuv95RVxu6H65UQJ8gmslIAiInrJUbk0U/Kw8g1KAZRewodI?= =?us-ascii?Q?cVzpual0Ev6lvq8y8DI49ne2MTt+2snTp+3oyU+l1Nc7s+qrYtzeIWXJskoB?= =?us-ascii?Q?YT/R9xXyCxAVihNyKON6DIqDhxFcdFdUuvjkmPzO55m8+EKZGKXcGXoMZpVT?= =?us-ascii?Q?rDAKNEY/REao2SK2WGxZK74Fr8Db3tZuOmZw/KQXZo98IkoKNpYZcZtEQ6Nt?= =?us-ascii?Q?dtU4+UdDeobH/Jy/Fv5qxI2sEUTfaFHrkNzDFtSJgiaBEmwbaiaHNsKnbCX6?= =?us-ascii?Q?qP0tnV7jLQ8OBzdBYmqInkH5/VY5U9YQogFH8BJAMyl2K2MpA5bW73g5SjAH?= =?us-ascii?Q?KoUU/9YH4Y/+GErOEPRh/uFSX4BdrFxnDg3ctqO36+8un9KXLx8mlY5KxKo8?= =?us-ascii?Q?1SsWqok7H7AqftFGTSj8LxZDlxXTUPA=3D?= X-OriginatorOrg: starfivetech.com X-MS-Exchange-CrossTenant-Network-Message-Id: df79d54b-2daa-48c9-1433-08df51ab4f41 X-MS-Exchange-CrossTenant-AuthSource: BJSPR01MB0756.CHNPR01.prod.partner.outlook.cn X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 02:07:12.2192 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 06fe3fa3-1221-43d3-861b-5a4ee687a85c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yGlor2TqHCqscFKisSwfCtNs66shqaGb2RI3Y25InSEJeQbLlW+gU07QeUfaDAnucSdQSY9uqvba4J5oYW/shRULiwbIAhQq1A/+Mok6kb9DKoeum3s4unzg0uANnkFs X-MS-Exchange-Transport-CrossTenantHeadersStamped: BJSPR01MB0498 From: Lianfeng Ouyang 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 --- 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 +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 M: Lianfeng Ouyang 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 "); +MODULE_DESCRIPTION("StarFive JHB100 heartbeat LED driver"); +MODULE_LICENSE("GPL"); -- 2.43.0