Skip to content

Commit 0603c50

Browse files
marcanjannau
authored andcommitted
rtc: Add new rtc-macsmc driver for Apple Silicon Macs
Apple Silicon Macs (M1, etc.) have an RTC that is part of the PMU IC, but most of the PMU functionality is abstracted out by the SMC. On T600x machines, the RTC counter must be accessed via the SMC to get full functionality, and it seems likely that future machines will move towards making SMC handle all RTC functionality. The SMC RTC counter access is implemented on all current machines as of the time of this writing, on firmware 12.x. However, the RTC offset (needed to set the time) is still only accessible via direct PMU access. To handle this, we expose the RTC offset as an NVMEM cell from the SPMI PMU device node, and this driver consumes that cell and uses it to compute/set the current time. Alarm functionality is not yet implemented. This would also go via the PMU today, but could change in the future. Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent dea6880 commit 0603c50

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

drivers/rtc/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,4 +2043,17 @@ config RTC_DRV_SSD202D
20432043
This driver can also be built as a module, if so, the module
20442044
will be called "rtc-ssd20xd".
20452045

2046+
config RTC_DRV_MACSMC
2047+
tristate "Apple Mac SMC RTC"
2048+
depends on ARCH_APPLE || COMPILE_TEST
2049+
depends on APPLE_SMC
2050+
depends on OF
2051+
default ARCH_APPLE
2052+
help
2053+
If you say yes here you get support for RTC functions
2054+
inside Apple SPMI PMUs.
2055+
2056+
To compile this driver as a module, choose M here: the
2057+
module will be called rtc-macsmc.
2058+
20462059
endif # RTC_CLASS

drivers/rtc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
8989
obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o
9090
obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o
9191
obj-$(CONFIG_RTC_DRV_MA35D1) += rtc-ma35d1.o
92+
obj-$(CONFIG_RTC_DRV_MACSMC) += rtc-macsmc.o
9293
obj-$(CONFIG_RTC_DRV_MAX31335) += rtc-max31335.o
9394
obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
9495
obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o

drivers/rtc/rtc-macsmc.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
/*
3+
* Apple SMC RTC driver
4+
* Copyright The Asahi Linux Contributors
5+
*/
6+
7+
#include <linux/bitops.h>
8+
#include <linux/mfd/core.h>
9+
#include <linux/mfd/macsmc.h>
10+
#include <linux/module.h>
11+
#include <linux/nvmem-consumer.h>
12+
#include <linux/of.h>
13+
#include <linux/platform_device.h>
14+
#include <linux/rtc.h>
15+
#include <linux/slab.h>
16+
17+
/* 48-bit RTC */
18+
#define RTC_BYTES 6
19+
#define RTC_BITS (8 * RTC_BYTES)
20+
21+
/* 32768 Hz clock */
22+
#define RTC_SEC_SHIFT 15
23+
24+
struct macsmc_rtc {
25+
struct device *dev;
26+
struct apple_smc *smc;
27+
struct rtc_device *rtc_dev;
28+
struct nvmem_cell *rtc_offset;
29+
};
30+
31+
static int macsmc_rtc_get_time(struct device *dev, struct rtc_time *tm)
32+
{
33+
struct macsmc_rtc *rtc = dev_get_drvdata(dev);
34+
u64 ctr = 0, off = 0;
35+
time64_t now;
36+
void *p_off;
37+
size_t len;
38+
int ret;
39+
40+
ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
41+
if (ret != RTC_BYTES)
42+
return ret < 0 ? ret : -EIO;
43+
44+
p_off = nvmem_cell_read(rtc->rtc_offset, &len);
45+
if (IS_ERR(p_off))
46+
return PTR_ERR(p_off);
47+
if (len < RTC_BYTES) {
48+
kfree(p_off);
49+
return -EIO;
50+
}
51+
52+
memcpy(&off, p_off, RTC_BYTES);
53+
kfree(p_off);
54+
55+
/* Sign extend from 48 to 64 bits, then arithmetic shift right 15 bits to get seconds */
56+
now = sign_extend64(ctr + off, RTC_BITS - 1) >> RTC_SEC_SHIFT;
57+
rtc_time64_to_tm(now, tm);
58+
59+
return ret;
60+
}
61+
62+
static int macsmc_rtc_set_time(struct device *dev, struct rtc_time *tm)
63+
{
64+
struct macsmc_rtc *rtc = dev_get_drvdata(dev);
65+
u64 ctr = 0, off = 0;
66+
int ret;
67+
68+
ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
69+
if (ret != RTC_BYTES)
70+
return ret < 0 ? ret : -EIO;
71+
72+
/* This sets the offset such that the set second begins now */
73+
off = (rtc_tm_to_time64(tm) << RTC_SEC_SHIFT) - ctr;
74+
return nvmem_cell_write(rtc->rtc_offset, &off, RTC_BYTES);
75+
}
76+
77+
static const struct rtc_class_ops macsmc_rtc_ops = {
78+
.read_time = macsmc_rtc_get_time,
79+
.set_time = macsmc_rtc_set_time,
80+
};
81+
82+
static int macsmc_rtc_probe(struct platform_device *pdev)
83+
{
84+
struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
85+
struct macsmc_rtc *rtc;
86+
87+
/* Ignore devices without this functionality */
88+
if (!apple_smc_key_exists(smc, SMC_KEY(CLKM)))
89+
return -ENODEV;
90+
91+
rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
92+
if (!rtc)
93+
return -ENOMEM;
94+
95+
rtc->dev = &pdev->dev;
96+
rtc->smc = smc;
97+
98+
pdev->dev.of_node = of_get_child_by_name(pdev->dev.parent->of_node, "rtc");
99+
100+
rtc->rtc_offset = devm_nvmem_cell_get(&pdev->dev, "rtc_offset");
101+
if (IS_ERR(rtc->rtc_offset))
102+
return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_offset),
103+
"Failed to get rtc_offset NVMEM cell\n");
104+
105+
rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
106+
if (IS_ERR(rtc->rtc_dev))
107+
return PTR_ERR(rtc->rtc_dev);
108+
109+
rtc->rtc_dev->ops = &macsmc_rtc_ops;
110+
rtc->rtc_dev->range_min = S64_MIN >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
111+
rtc->rtc_dev->range_max = S64_MAX >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
112+
113+
platform_set_drvdata(pdev, rtc);
114+
115+
return devm_rtc_register_device(rtc->rtc_dev);
116+
}
117+
118+
static struct platform_driver macsmc_rtc_driver = {
119+
.driver = {
120+
.name = "macsmc-rtc",
121+
.owner = THIS_MODULE,
122+
},
123+
.probe = macsmc_rtc_probe,
124+
};
125+
module_platform_driver(macsmc_rtc_driver);
126+
127+
MODULE_LICENSE("Dual MIT/GPL");
128+
MODULE_DESCRIPTION("Apple SMC RTC driver");
129+
MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
130+
MODULE_ALIAS("platform:macsmc-rtc");

0 commit comments

Comments
 (0)