From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 33AE3CA0FE7 for ; Mon, 25 Aug 2025 17:53:48 +0000 (UTC) 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=eTbEOXtQ8yFK06eoivRanqPLBHsJes6BbFK1zRmpwoA=; b=hwtqaw/WouShlD 9KF+LSL39gNvvIX2LBkSTmEjzQQ2F1o0IWeyAQvSlQ/SVll7IJzTafyX1StguVVg3WOtn3uc0Disd AIyALOUtMQBkA7HLI4mLaZ9sd4JytBYsAxmreNgmCH7xn8Bg6nPB8c7ARDhU3vlL4c9Ng5+wLucXs yxRJqbzvXQZyppYJFO983VYegT+i1tex2ih5eOW3q9wRZPU4aVWCI+6bPMwS3CwJTL9+FLw/HnrCd DzU5opE5181cfVxkWfm7Z52yDGZ+ClmroxwWLTRdpglDlP2i2gpdPo19lFU//Y5bdveotBZsPXOPk bPizA4eFJ+SMRVRjnpGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uqbNc-00000008sNm-1uKg; Mon, 25 Aug 2025 17:53:16 +0000 Received: from mail-io1-f49.google.com ([209.85.166.49]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uqasZ-00000008otf-0pKq for linux-riscv@lists.infradead.org; Mon, 25 Aug 2025 17:21:12 +0000 Received: by mail-io1-f49.google.com with SMTP id ca18e2360f4ac-88432cbdb8bso51565939f.0 for ; Mon, 25 Aug 2025 10:21:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1756142470; x=1756747270; darn=lists.infradead.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=skQxKpPrKNdpCm7T4rAP4k9qKdZRbQN/uiLC1I5fUpk=; b=oqfsdKH3+PN+JPeI2bVTFnnCX+b+O6V9K4fwOlg7a/1Haan8LntaQvERXLPMJHRVLR BkdTUKq7qUvJFLHgsj/juaukrUpCj00PdT4BhVg16eDYfDjP1dk4mI4md2lug9NVLTBv pt+euGr8kT3VsYWahIdoLqP9ZYs0lTnfrxSqj8sCrOKWeewIkaK53gHEmu2VPMJfNTTh JGwv0i5splwyN32TqU/isH4zHWn3GyFhwrFAr0+PF2ysneCS4DzgGDdc8hTCfhzcaAAr BZmekajiM33Y9zPIW5gg2tw+vJz5Dt8J/LSBhVknUK6aXp1cyxGsls4oXf9asAndty9q yG1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756142470; x=1756747270; 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=skQxKpPrKNdpCm7T4rAP4k9qKdZRbQN/uiLC1I5fUpk=; b=LhhBcdEvhyxHk4OWAR6aPp3Hau+PZ4RqWr9S7svGPivLlxVzCzsJwLNY2dpMJu2sYB Q+bqATwgzzm6YaHAXRa5JqxHGADMFrNO1Y5sifbtvVItavoZf/YAla5QuSnNZgNsPe79 6VMRJot3HE/C8QJccIJ6qzsaBXeS8ARvEeBU5i4xa8J3gvy3X7kXiX0+45lKyQlhkAad xLQHxnl3V0Vb3i+QcyAfTmxo2VeQPWVHrb1k2F6cQn0SdRy3w3y7zwdCmrjpQL0Z4egb 7W6boDugFmzOuyMCL1g/vdlvYCopUSW55aeNhR7cACFoiCsXyoDkI5vTwPWHaBZQ++FF xOBA== X-Forwarded-Encrypted: i=1; AJvYcCUPX1PVCNRiC5TrpvzJUNr+AOAj2SnOi7yBE83Ivt+wYfyCCZqnuMi7xA5c6X3Eubdglecs5ksqvnE+vA==@lists.infradead.org X-Gm-Message-State: AOJu0Yy8Rrc46B3EgDNsJZeplZkbeytYxWg1usRp6vQpiRbGLpSAc93N 6y8HWdso57avqEkWPzF3pKu/2vj/0EjLlZp1X3u3WDxkJWJB2KR94FFsoPu/ew7aaiY= X-Gm-Gg: ASbGncsqRYffeP8TQHY7AMzqYUOLnh/gTPf3oqvAx2JBkGBnjdJWBmtNy1aNUcOFHyt eo2a8pZvvMd29YBXHrMtS+tY3K3NgRYA7IRlyRvC712Yi2GA/mk45W49It7KKbzyQHk2h1qkK3e ySrUVLy5ddZCXjrgjT2xsE1XqfJdjq6TZlVcLm8qSwELa4HWQ4uE89tMsUTebXLoqLMz9gxrpDS PcxUELLZI9EXjiTRDWa+LMaBbTy2Juivo2ZGt/qqukfk7zPMDyVqn3P0mXRZ4lW00xxM4Uq3nG8 teQNgAy/esHZ91NBOoc4E7TCKDwPIZm782lyssP4Ccoh9EarevOQjkgZ2lZnm6JaZmOKxA+wZRd z62evet45znb/nK8V5t6U90gFFrYsg9xo2xQUepoUJyXAopQywlLju6S7NA76ySabUQ== X-Google-Smtp-Source: AGHT+IGjHL42eoyIr7EDD3bK1scXRSzcjEoKPKkj9mEjGPeObhHR0X7TrKov0WB3dntdIdKwAdG/pw== X-Received: by 2002:a05:6602:14c9:b0:884:47f0:b89f with SMTP id ca18e2360f4ac-886bd0f2960mr1623157639f.3.1756142469773; Mon, 25 Aug 2025 10:21:09 -0700 (PDT) Received: from zippy.localdomain (c-75-72-117-212.hsd1.mn.comcast.net. [75.72.117.212]) by smtp.gmail.com with ESMTPSA id ca18e2360f4ac-886e60c4737sm76275439f.26.2025.08.25.10.21.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Aug 2025 10:21:09 -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, linux.amoon@gmail.com, 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 v13 4/7] rtc: spacemit: support the SpacemiT P1 RTC Date: Mon, 25 Aug 2025 12:20:53 -0500 Message-ID: <20250825172057.163883-5-elder@riscstar.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250825172057.163883-1-elder@riscstar.com> References: <20250825172057.163883-1-elder@riscstar.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250825_102111_238563_937CBABE X-CRM114-Status: GOOD ( 26.33 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 64f6e9756aff4..31d355b103d43 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. +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 789bddfea99d8..f41fdb4fabae9 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -171,6 +171,7 @@ obj-$(CONFIG_RTC_DRV_SD2405AL) += rtc-sd2405al.o obj-$(CONFIG_RTC_DRV_SD3078) += rtc-sd3078.o obj-$(CONFIG_RTC_DRV_SH) += rtc-sh.o obj-$(CONFIG_RTC_DRV_SNVS) += rtc-snvs.o +obj-$(CONFIG_RTC_DRV_SPACEMIT_P1) += rtc-spacemit-p1.o obj-$(CONFIG_RTC_DRV_SPEAR) += rtc-spear.o obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o obj-$(CONFIG_RTC_DRV_STK17TA8) += 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..43ab62494bb4b --- /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 reserved. + */ + +#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 = dev_get_drvdata(dev); + struct regmap *regmap = p1->regmap; + u32 count = RTC_READ_TRIES; + u8 seconds; + u8 time[6]; + int ret; + + if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) + return -EINVAL; /* RTC is disabled */ + + ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time)); + if (ret) + return ret; + + do { + seconds = time[0]; + ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time)); + if (ret) + return ret; + } while (time[0] != seconds && --count); + + if (!count) + return -EIO; /* Unable to get a consistent result */ + + t->tm_sec = time[0] & GENMASK(5, 0); + t->tm_min = time[1] & GENMASK(5, 0); + t->tm_hour = time[2] & GENMASK(4, 0); + t->tm_mday = (time[3] & GENMASK(4, 0)) + 1; + t->tm_mon = time[4] & GENMASK(3, 0); + t->tm_year = (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 = dev_get_drvdata(dev); + struct regmap *regmap = p1->regmap; + u8 time[6]; + int ret; + + time[0] = t->tm_sec; + time[1] = t->tm_min; + time[2] = t->tm_hour; + time[3] = t->tm_mday - 1; + time[4] = t->tm_mon; + time[5] = t->tm_year - 100; + + /* Disable the RTC to update; re-enable again when done */ + ret = regmap_clear_bits(regmap, RTC_CTRL, RTC_EN); + if (ret) + return ret; + + /* If something goes wrong, leave the RTC disabled */ + ret = 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 = { + .read_time = p1_rtc_read_time, + .set_time = p1_rtc_set_time, +}; + +static int p1_rtc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct rtc_device *rtc; + struct p1_rtc *p1; + + p1 = devm_kzalloc(dev, sizeof(*p1), GFP_KERNEL); + if (!p1) + return -ENOMEM; + dev_set_drvdata(dev, p1); + + p1->regmap = dev_get_regmap(dev->parent, NULL); + if (!p1->regmap) + return dev_err_probe(dev, -ENODEV, "failed to get regmap\n"); + + rtc = devm_rtc_allocate_device(dev); + if (IS_ERR(rtc)) + return dev_err_probe(dev, PTR_ERR(rtc), + "error allocating device\n"); + p1->rtc = rtc; + + rtc->ops = &p1_rtc_class_ops; + rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; + rtc->range_max = 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 = { + .probe = p1_rtc_probe, + .driver = { + .name = MOD_NAME, + }, +}; + +module_platform_driver(p1_rtc_driver); + +MODULE_DESCRIPTION("SpacemiT P1 RTC driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:" MOD_NAME); -- 2.48.1 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv