From nobody Thu Sep 11 14:15:25 2025 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6587D2F5318 for ; Thu, 10 Jul 2025 17:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752169881; cv=none; b=T2rNt5HAhtmDIuQY95igr4Vmt5f5gNF+vF/g5EAaETaRLvWowbOvMZSq9hyoj/Hj/IdOao3zF5Lzkoteh8n4H1h9I0dpiMeBLE5lELJqUdBNmvPlCgYzWaEKWt9N+hKCS6dFun3HIy7we4U4yWqOynx6Ys1flKyjEc8ya2y/CE4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752169881; c=relaxed/simple; bh=zclW5evbD26kO18GZgybKpcCRCtcM25Tzi+558CiH3U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dZjnAc4m/j7YM17pMfP3+xWuyAX/FVh+QZooGSpt98c0BbRV8chE60CGAtkZfw4icWH3zN6W7+KW89+WWYQjhlTWFd7meRUhwCzJpinnl242iWuRnNmLQU7ZLFLR7tKt4K+p802bb8rDiRbppTbcn4hfP+lD34nZReGDqftCst0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b=eBjYek0V; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b="eBjYek0V" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-7d7cf6efc2fso133174785a.3 for ; Thu, 10 Jul 2025 10:51:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1752169878; x=1752774678; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GtlkaFHjvmTy5RrCm3lhHAt4uNxWGS/x5o/PAIyMoV4=; b=eBjYek0VShEgOIAofNKMWInyjhRqyb3uXtz3pXAeRyP9HNB9+Zj5I0KSmhsBuEyv3T JlWfZAX1EeWkoaKBwIBNttEXxr1xoZ43lFgT/c3rrIUJCmWekO8aqHZ+Q6A/D/F4M4f0 OpwoknWmYZGqaqunO2uIeR5WXQw3ilM5Ok1ViPMM4hqw4mwFGlEUSd4XIlznTl9RfsWf 6KCPWEULC1VLN+ocBHmpQr3WMlE2aLXS8b1yh9XtjbUXtbbiJcouIJGxzzT62byAJ/9L 1+zqcSnG8uu/4KBaPhgduaqUiMTuDd2hl8jCUP85Jt4kFEcedyn3ElRVSKxnr5HdQWBQ uZ5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752169878; x=1752774678; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GtlkaFHjvmTy5RrCm3lhHAt4uNxWGS/x5o/PAIyMoV4=; b=O/K69YOoFJKVWS64hOMQ6Dw/3AIurJoqXe+PqKj9RDjE3YWnT8cxAkyScC7A0BFagi 0eqYlLKLFtEsXqwotE9sdVFlY6SUyuWGC8SGheTQHTrEPonSZ05FZS0JlkV2oGohItZY AqY3AU80vB8a8+MUa54dkyxNl1T1yDRYdPeNPVGgxfBuXMbL+QqCMXcsMi4MtTQN3VOY a63MIn+2SRKimRafB1w6O9BA1BngidqbsGh3vGHTdvdHZ3HWKZ3MRqR0S9UUakX9IjHk UsIrVDSRpXtLiO4H7rPOyfJ1JXuQc52INNomy4FZsHUgF/I9E4zT3N2qNsH+EBCTUI7X 7C4w== X-Forwarded-Encrypted: i=1; AJvYcCUPwA3hNE6HV0oeV1gCbxzUhLaXz9Bhl8PATNKTuQCzjljyQ4kOa1TZEOt8dUukKShzZr/WLd4Ng8+9eQk=@vger.kernel.org X-Gm-Message-State: AOJu0YyXJvTCWCmkaoo8wQAo+kY67J63gxiFeIpx1MjLrpAjst0aXBif EBhOtnazPSA7PXNBi7PKS7+jcsW27QlE7Z9PsihcUpT/NE/cTg/cD6nONJ+WjNdX24E= X-Gm-Gg: ASbGncu1Gi1sFrIsV0aKp99V3Q3ywGag3wONlZe+qxDVBcj36ACH77IwJIT8xzxbuH8 FbcFdpkYUNIWwgTQ4k+j4tDNo5enBqS0OtEE0l05LvRXO4aS50198yRGmm+v3AfxQdO+8fb/Kdy eIyj51OQC1xkI7yG0HmZJewjHCAUL76z0m4TJz0XCDP9s804ZaMUTBStHIApF7NdBiWgRi34Mhp 954tDUSC2Z4Ym5ZGPEW/DhPBf02HovVhLNUIgbu7hilF4E164kR86uvd8iJ7pWz2iRKLMFBjchB LShC7pVw/YhhkOs0aJxskxhoXX57mrJ1lRsls+CqFAqNdd5nfP9m8cqyHZGZPAC/r7RzN0TbEGZ n4ezMsI53CeSOJn0asralHTyA983ZWDkeYMk= X-Google-Smtp-Source: AGHT+IE9P5qhuz4/xVWrdokjRO9CWefv8mgSYB/yTUYLe+8ex96dPbyhfL7QdTydlepbu+Nm7SZbPg== X-Received: by 2002:a05:620a:4693:b0:7d6:f963:8764 with SMTP id af79cd13be357-7ddec93519dmr67843885a.34.1752169878272; Thu, 10 Jul 2025 10:51:18 -0700 (PDT) Received: from localhost.localdomain (c-73-228-159-35.hsd1.mn.comcast.net. [73.228.159.35]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4a9edeee300sm11165941cf.73.2025.07.10.10.51.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jul 2025 10:51:17 -0700 (PDT) From: Alex Elder To: lee@kernel.org, lgirdwood@gmail.com, broonie@kernel.org, alexandre.belloni@bootlin.com, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org Cc: mat.jonczyk@o2.pl, dlan@gentoo.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, troymitchell988@gmail.com, guodong@riscstar.com, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-riscv@lists.infradead.org, spacemit@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v8 5/8] rtc: spacemit: support the SpacemiT P1 RTC Date: Thu, 10 Jul 2025 12:51:03 -0500 Message-ID: <20250710175107.1280221-6-elder@riscstar.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250710175107.1280221-1-elder@riscstar.com> References: <20250710175107.1280221-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for the RTC found in the SpacemiT P1 PMIC. Initially only setting and reading the time are supported. The PMIC is implemented as a multi-function device. This RTC is probed based on this driver being named in a MFD cell in the simple MFD I2C driver. Signed-off-by: Alex Elder Acked-by: Alexandre Belloni --- drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-spacemit-p1.c | 167 ++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 drivers/rtc/rtc-spacemit-p1.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 9aec922613cec..93620f2c9b29c 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -406,6 +406,16 @@ config RTC_DRV_MAX77686 This driver can also be built as a module. If so, the module will be called rtc-max77686. =20 +config RTC_DRV_SPACEMIT_P1 + tristate "SpacemiT P1 RTC" + depends on ARCH_SPACEMIT || COMPILE_TEST + select MFD_SPACEMIT_P1 + default ARCH_SPACEMIT + help + Enable support for the RTC function in the SpacemiT P1 PMIC. + This driver can also be built as a module, which will be called + "spacemit-p1-rtc". + config RTC_DRV_NCT3018Y tristate "Nuvoton NCT3018Y" depends on OF diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 4619aa2ac4697..a24ff6ad5ca58 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -171,6 +171,7 @@ obj-$(CONFIG_RTC_DRV_SD2405AL) +=3D rtc-sd2405al.o obj-$(CONFIG_RTC_DRV_SD3078) +=3D rtc-sd3078.o obj-$(CONFIG_RTC_DRV_SH) +=3D rtc-sh.o obj-$(CONFIG_RTC_DRV_SNVS) +=3D rtc-snvs.o +obj-$(CONFIG_RTC_DRV_SPACEMIT_P1) +=3D rtc-spacemit-p1.o obj-$(CONFIG_RTC_DRV_SPEAR) +=3D rtc-spear.o obj-$(CONFIG_RTC_DRV_STARFIRE) +=3D rtc-starfire.o obj-$(CONFIG_RTC_DRV_STK17TA8) +=3D rtc-stk17ta8.o diff --git a/drivers/rtc/rtc-spacemit-p1.c b/drivers/rtc/rtc-spacemit-p1.c new file mode 100644 index 0000000000000..5f1bcca00549c --- /dev/null +++ b/drivers/rtc/rtc-spacemit-p1.c @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for the RTC found in the SpacemiT P1 PMIC + * + * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reser= ved. + */ + +#include +#include +#include +#include +#include +#include + +#define MOD_NAME "spacemit-p1-rtc" + +/* + * Six consecutive 1-byte registers hold the seconds, minutes, hours, + * day-of-month, month, and year (respectively). + * + * The range of values in these registers is: + * seconds 0-59 + * minutes 0-59 + * hours 0-59 + * day 0-30 (struct tm is 1-31) + * month 0-11 + * year years since 2000 (struct tm is since 1900) + * + * Note that the day and month must be converted after reading and + * before writing. + */ +#define RTC_TIME 0x0d /* Offset of the seconds register */ + +#define RTC_CTRL 0x1d +#define RTC_EN BIT(2) + +/* Number of attempts to read a consistent time stamp before giving up */ +#define RTC_READ_TRIES 20 /* At least 1 */ + +struct p1_rtc { + struct regmap *regmap; + struct rtc_device *rtc; +}; + +/* + * The P1 hardware documentation states that the register values are + * latched to ensure a consistent time snapshot within the registers, + * but these are in fact unstable due to a bug in the hardware design. + * So we loop until we get two identical readings. + */ +static int p1_rtc_read_time(struct device *dev, struct rtc_time *t) +{ + struct p1_rtc *p1 =3D dev_get_drvdata(dev); + struct regmap *regmap =3D p1->regmap; + u32 count =3D RTC_READ_TRIES; + u8 seconds; + u8 time[6]; + int ret; + + if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) + return -ENODEV; /* RTC is disabled */ + + ret =3D regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time)); + if (ret) + return ret; + + do { + seconds =3D time[0]; + ret =3D regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time)); + if (ret) + return ret; + } while (time[0] !=3D seconds && --count); + + if (!count) + return -EIO; /* Unable to get a consistent result */ + + t->tm_sec =3D time[0] & GENMASK(5, 0); + t->tm_min =3D time[1] & GENMASK(5, 0); + t->tm_hour =3D time[2] & GENMASK(4, 0); + t->tm_mday =3D (time[3] & GENMASK(4, 0)) + 1; + t->tm_mon =3D time[4] & GENMASK(3, 0); + t->tm_year =3D (time[5] & GENMASK(5, 0)) + 100; + + return 0; +} + +/* + * The P1 hardware documentation states that values in the registers are + * latched so when written they represent a consistent time snapshot. + * Nevertheless, this is not guaranteed by the implementation, so we must + * disable the RTC while updating it. + */ +static int p1_rtc_set_time(struct device *dev, struct rtc_time *t) +{ + struct p1_rtc *p1 =3D dev_get_drvdata(dev); + struct regmap *regmap =3D p1->regmap; + u8 time[6]; + int ret; + + time[0] =3D t->tm_sec; + time[1] =3D t->tm_min; + time[2] =3D t->tm_hour; + time[3] =3D t->tm_mday - 1; + time[4] =3D t->tm_mon; + time[5] =3D t->tm_year - 100; + + /* Disable the RTC to update; re-enable again when done */ + ret =3D regmap_clear_bits(regmap, RTC_CTRL, RTC_EN); + if (ret) + return ret; + + /* If something goes wrong, leave the RTC disabled */ + ret =3D regmap_bulk_write(regmap, RTC_TIME, time, sizeof(time)); + if (ret) + return ret; + + return regmap_set_bits(regmap, RTC_CTRL, RTC_EN); +} + +static const struct rtc_class_ops p1_rtc_class_ops =3D { + .read_time =3D p1_rtc_read_time, + .set_time =3D p1_rtc_set_time, +}; + +static int p1_rtc_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct rtc_device *rtc; + struct p1_rtc *p1; + + p1 =3D devm_kzalloc(dev, sizeof(*p1), GFP_KERNEL); + if (!p1) + return -ENOMEM; + dev_set_drvdata(dev, p1); + + p1->regmap =3D dev_get_regmap(dev->parent, NULL); + if (!p1->regmap) + return dev_err_probe(dev, -ENODEV, "failed to get regmap\n"); + + rtc =3D devm_rtc_allocate_device(dev); + if (IS_ERR(rtc)) + return dev_err_probe(dev, PTR_ERR(rtc), + "error allocating device\n"); + p1->rtc =3D rtc; + + rtc->ops =3D &p1_rtc_class_ops; + rtc->range_min =3D RTC_TIMESTAMP_BEGIN_2000; + rtc->range_max =3D RTC_TIMESTAMP_END_2063; + + clear_bit(RTC_FEATURE_ALARM, rtc->features); + clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features); + + return devm_rtc_register_device(rtc); +} + +static struct platform_driver p1_rtc_driver =3D { + .probe =3D p1_rtc_probe, + .driver =3D { + .name =3D MOD_NAME, + }, +}; + +module_platform_driver(p1_rtc_driver); + +MODULE_DESCRIPTION("SpacemiT P1 RTC driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:" MOD_NAME); --=20 2.45.2