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 8B5B3C83010 for ; Fri, 27 Jun 2025 14:57:46 +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=V+Ir8AKnKTGrLi5MZFChXFHxWZZA4GsFRGmSG4/v8tk=; b=uhrYAzK/Re2rBG Vv+z20+p05U1aXX/ZM/aeMRzZeU+F9V20N7fEl6o3hF+XwPOqqcfZBvPm55Hj1Y2PDg8VoTBI+Rs7 2YgxDsmm2/GiRbueI6bI/AL/Xfa9HfD4dzml5MA7cl3Xwma2TH4VdnZd/olRs5UMOhgDpEhKea3Jt vt8KVLgfgvE0iUCF37ipeYqoWKtKZ7u4sJ5+J2RQdi+JkzMwc/U7kYJMakg6UiRy2edsGWPzedw5B FUBx3kTIEGBhQPuj5kuh7AA0zEJaEU+TajfocfM6e6xeL63P9jtFB6C/yy9YKRxeOeTUH4n0/0bvQ Smvqx3Zb/Cy+Z6f0G2UA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uVAWI-0000000EyfY-2J8A; Fri, 27 Jun 2025 14:57:38 +0000 Received: from mail-qk1-x72d.google.com ([2607:f8b0:4864:20::72d]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uV9z6-0000000EtxO-41cD for linux-riscv@lists.infradead.org; Fri, 27 Jun 2025 14:23:22 +0000 Received: by mail-qk1-x72d.google.com with SMTP id af79cd13be357-7d3f5796755so211044885a.1 for ; Fri, 27 Jun 2025 07:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1751034200; x=1751639000; 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=GtlkaFHjvmTy5RrCm3lhHAt4uNxWGS/x5o/PAIyMoV4=; b=tkKsoRYOW5q1MNHDLcv1meAhFIUHAK5crAfqYUU+CsmvOc2TtJqO3pC+vJo5SA1vXy KPvSdIOKNO9NHAVPePNgKbNfsGAOw+KWyOlnFrQN2b5uw2W2UHkUoGlsC8I3lOp27edS 3TOBM5VETps7b8OYdSyw2p21V1KG3g/q26euTgmmx9Us5aEo1aNL/Vt8G4HrjSgRtgT4 D6zm++BQa7mT188gl6xrIgEmjfYQaisghaWAf8JaRAJEvRLbUrYtXf1scG6ZVC7PZhdi qPfJRzDPtuu0DnOMaeG2243Ul/nLsp9RWRGWdGi894ezSaC6zJ8EER3EX55GbiJCmwke xsJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751034200; x=1751639000; 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=PTxVSvShRSeWlnGS6psxPGyUDqADEAbh+B2g0g9UYhWDPI1u+gJnDZaXXM7LgjybAl lh5vdCB8B06sc1kiqzsp5IFDihR35C1O5A2Vv0YrgEmTscSlaZ8UOCH05ZZaIOE2yhOa tWv0jcyBK71syZ9dQD7oOEeSOr+zhXzGFnXbWbxfYLFbImmg9M+ZC5/rw1sYNll41qtw OfZDlrDFXAPR0jZzMH+IjUCWRjkApQS/7FzpTZazleoOfWGbgtLrky97ncuhWW0MLFAC WbL+aJQIkVDZUR7pquywZ96QszXPqalvOrLXQYuZQLqLG5A1TsIQypgNb8E3uRgra4Mv w1Ug== X-Forwarded-Encrypted: i=1; AJvYcCW7/jK/21C5HCzxOfOkC4rL4H0v3AUsJ77PceG/nqyxcBjRZWAIybc8xPGDwTFzmVIMmwxGJnGz1x43Ag==@lists.infradead.org X-Gm-Message-State: AOJu0YydKjnABdzK4PnbFED3nhobV6j9kB8Jak0xXHh38cu/SlBvJGwm Xz1P2CYYGDgp24NlgO6pRy7jey3t+m7HiE9xGCDtxaCogXV6UK9c64yOFF/9BLgfsBk= X-Gm-Gg: ASbGnctp2BNy5XmR7BOA1OiVymTmXej2hliDX+tMlOb95wWLwB08PjfV4KV65l+MUGW PUJ9l6fSQMAFue6V9fd+phNb0Knk9jioDyRyqIe0crlU+xpaV/G6/4lna9oTqEmz17SDtiMdDX5 hJxwYOidjWgiwgIr5L8GEiOuNB6DdREub+rT2fFFqfbwOjecmz1287sOrbN919XYRMm76DapBQI iB7u3aQl0L6IFl8MhQeeKIjrIIVlRRTh1UMtkF2jI9Ee02kfizhei+u+qfAt/OcbS9SDuBIZRt2 u0vkTtTAMjfMs+mbstU/vDJ611/6eUHAA7++GqJM0rw3GJ3Zk0cevOlTgReiV+ZlfpBi3ZG5PMg 55p1GEghZTz2WV2Xs0Pth4fP5uJrSkfgfFQU= X-Google-Smtp-Source: AGHT+IEgrYKhOCKiOsFX0gYQTF47XaE9uXp1Sdf+EyZSPFtjULtMu+TN2F343Xkf+TDAeAcNg0SfPA== X-Received: by 2002:a05:620a:4406:b0:7c7:739d:5cea with SMTP id af79cd13be357-7d443979080mr551276885a.35.1751034199591; Fri, 27 Jun 2025 07:23:19 -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 af79cd13be357-7d44323b4c4sm135708785a.106.2025.06.27.07.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jun 2025 07:23:19 -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 v6 5/8] rtc: spacemit: support the SpacemiT P1 RTC Date: Fri, 27 Jun 2025 09:23:04 -0500 Message-ID: <20250627142309.1444135-6-elder@riscstar.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250627142309.1444135-1-elder@riscstar.com> References: <20250627142309.1444135-1-elder@riscstar.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250627_072321_005297_3598AE28 X-CRM114-Status: GOOD ( 26.23 ) 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 --- 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. +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) += 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..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 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 -ENODEV; /* 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.45.2 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv