Skip to content

Commit 7cd8603

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. Reviewed-by: Neal Gompa <neal@gompa.dev> Signed-off-by: Hector Martin <marcan@marcan.st> Signed-off-by: Sven Peter <sven@kernel.org> Signed-off-by: James Calligeros <jcalligeros99@gmail.com>
1 parent 43f9a8d commit 7cd8603

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ F: drivers/nvmem/apple-spmi-nvmem.c
24202420
F: drivers/pinctrl/pinctrl-apple-gpio.c
24212421
F: drivers/power/reset/macsmc-reboot.c
24222422
F: drivers/pwm/pwm-apple.c
2423+
F: drivers/rtc/rtc-macsmc.c
24232424
F: drivers/soc/apple/*
24242425
F: drivers/spi/spi-apple.c
24252426
F: drivers/spmi/spmi-apple-controller.c

drivers/rtc/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,17 @@ config RTC_DRV_WILCO_EC
20682068
This can also be built as a module. If so, the module will
20692069
be named "rtc_wilco_ec".
20702070

2071+
config RTC_DRV_MACSMC
2072+
tristate "Apple Mac System Management Controller RTC"
2073+
depends on MFD_MACSMC
2074+
help
2075+
If you say yes here you get support for RTC functions
2076+
inside Apple SPMI PMUs accessed through the SoC's
2077+
System Management Controller
2078+
2079+
To compile this driver as a module, choose M here: the
2080+
module will be called rtc-macsmc.
2081+
20712082
config RTC_DRV_MSC313
20722083
tristate "MStar MSC313 RTC"
20732084
depends on ARCH_MSTARV7 || COMPILE_TEST

drivers/rtc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
9393
obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o
9494
obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o
9595
obj-$(CONFIG_RTC_DRV_MA35D1) += rtc-ma35d1.o
96+
obj-$(CONFIG_RTC_DRV_MACSMC) += rtc-macsmc.o
9697
obj-$(CONFIG_RTC_DRV_MAX31335) += rtc-max31335.o
9798
obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
9899
obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o

drivers/rtc/rtc-macsmc.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 < 0)
42+
return ret;
43+
if (ret != RTC_BYTES)
44+
return -EIO;
45+
46+
p_off = nvmem_cell_read(rtc->rtc_offset, &len);
47+
if (IS_ERR(p_off))
48+
return PTR_ERR(p_off);
49+
if (len < RTC_BYTES) {
50+
kfree(p_off);
51+
return -EIO;
52+
}
53+
54+
memcpy(&off, p_off, RTC_BYTES);
55+
kfree(p_off);
56+
57+
/* Sign extend from 48 to 64 bits, then arithmetic shift right 15 bits to get seconds */
58+
now = sign_extend64(ctr + off, RTC_BITS - 1) >> RTC_SEC_SHIFT;
59+
rtc_time64_to_tm(now, tm);
60+
61+
return ret;
62+
}
63+
64+
static int macsmc_rtc_set_time(struct device *dev, struct rtc_time *tm)
65+
{
66+
struct macsmc_rtc *rtc = dev_get_drvdata(dev);
67+
u64 ctr = 0, off = 0;
68+
int ret;
69+
70+
ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
71+
if (ret < 0)
72+
return ret;
73+
if (ret != RTC_BYTES)
74+
return -EIO;
75+
76+
/* This sets the offset such that the set second begins now */
77+
off = (rtc_tm_to_time64(tm) << RTC_SEC_SHIFT) - ctr;
78+
return nvmem_cell_write(rtc->rtc_offset, &off, RTC_BYTES);
79+
}
80+
81+
static const struct rtc_class_ops macsmc_rtc_ops = {
82+
.read_time = macsmc_rtc_get_time,
83+
.set_time = macsmc_rtc_set_time,
84+
};
85+
86+
static int macsmc_rtc_probe(struct platform_device *pdev)
87+
{
88+
struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
89+
struct macsmc_rtc *rtc;
90+
91+
/*
92+
* MFD will probe this device even without a node in the device tree,
93+
* thus bail out early if the SMC on the current machines does not
94+
* support RTC and has no node in the device tree.
95+
*/
96+
if (!pdev->dev.of_node)
97+
return -ENODEV;
98+
99+
rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
100+
if (!rtc)
101+
return -ENOMEM;
102+
103+
rtc->dev = &pdev->dev;
104+
rtc->smc = smc;
105+
106+
rtc->rtc_offset = devm_nvmem_cell_get(&pdev->dev, "rtc_offset");
107+
if (IS_ERR(rtc->rtc_offset))
108+
return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_offset),
109+
"Failed to get rtc_offset NVMEM cell\n");
110+
111+
rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
112+
if (IS_ERR(rtc->rtc_dev))
113+
return PTR_ERR(rtc->rtc_dev);
114+
115+
rtc->rtc_dev->ops = &macsmc_rtc_ops;
116+
rtc->rtc_dev->range_min = S64_MIN >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
117+
rtc->rtc_dev->range_max = S64_MAX >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
118+
119+
platform_set_drvdata(pdev, rtc);
120+
121+
return devm_rtc_register_device(rtc->rtc_dev);
122+
}
123+
124+
static const struct of_device_id macsmc_rtc_of_table[] = {
125+
{ .compatible = "apple,smc-rtc", },
126+
{}
127+
};
128+
MODULE_DEVICE_TABLE(of, macsmc_rtc_of_table);
129+
130+
static struct platform_driver macsmc_rtc_driver = {
131+
.driver = {
132+
.name = "macsmc-rtc",
133+
.of_match_table = macsmc_rtc_of_table,
134+
},
135+
.probe = macsmc_rtc_probe,
136+
};
137+
module_platform_driver(macsmc_rtc_driver);
138+
139+
MODULE_LICENSE("Dual MIT/GPL");
140+
MODULE_DESCRIPTION("Apple SMC RTC driver");
141+
MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");

0 commit comments

Comments
 (0)