drivers/rtc/rtc-macsmc.c
Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-macsmc.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/rtc/rtc-macsmc.c- Extension
.c- Size
- 3518 bytes
- Lines
- 141
- Domain
- Driver Families
- Bucket
- drivers/rtc
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitops.hlinux/mfd/macsmc.hlinux/module.hlinux/nvmem-consumer.hlinux/of.hlinux/platform_device.hlinux/rtc.hlinux/slab.h
Detected Declarations
struct macsmc_rtcfunction macsmc_rtc_get_timefunction macsmc_rtc_set_timefunction macsmc_rtc_probe
Annotated Snippet
struct macsmc_rtc {
struct device *dev;
struct apple_smc *smc;
struct rtc_device *rtc_dev;
struct nvmem_cell *rtc_offset;
};
static int macsmc_rtc_get_time(struct device *dev, struct rtc_time *tm)
{
struct macsmc_rtc *rtc = dev_get_drvdata(dev);
u64 ctr = 0, off = 0;
time64_t now;
void *p_off;
size_t len;
int ret;
ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
if (ret < 0)
return ret;
if (ret != RTC_BYTES)
return -EIO;
p_off = nvmem_cell_read(rtc->rtc_offset, &len);
if (IS_ERR(p_off))
return PTR_ERR(p_off);
if (len < RTC_BYTES) {
kfree(p_off);
return -EIO;
}
memcpy(&off, p_off, RTC_BYTES);
kfree(p_off);
/* Sign extend from 48 to 64 bits, then arithmetic shift right 15 bits to get seconds */
now = sign_extend64(ctr + off, RTC_BITS - 1) >> RTC_SEC_SHIFT;
rtc_time64_to_tm(now, tm);
return ret;
}
static int macsmc_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
struct macsmc_rtc *rtc = dev_get_drvdata(dev);
u64 ctr = 0, off = 0;
int ret;
ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
if (ret < 0)
return ret;
if (ret != RTC_BYTES)
return -EIO;
/* This sets the offset such that the set second begins now */
off = (rtc_tm_to_time64(tm) << RTC_SEC_SHIFT) - ctr;
return nvmem_cell_write(rtc->rtc_offset, &off, RTC_BYTES);
}
static const struct rtc_class_ops macsmc_rtc_ops = {
.read_time = macsmc_rtc_get_time,
.set_time = macsmc_rtc_set_time,
};
static int macsmc_rtc_probe(struct platform_device *pdev)
{
struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
struct macsmc_rtc *rtc;
/*
* MFD will probe this device even without a node in the device tree,
* thus bail out early if the SMC on the current machines does not
* support RTC and has no node in the device tree.
*/
if (!pdev->dev.of_node)
return -ENODEV;
rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
if (!rtc)
return -ENOMEM;
rtc->dev = &pdev->dev;
rtc->smc = smc;
rtc->rtc_offset = devm_nvmem_cell_get(&pdev->dev, "rtc_offset");
if (IS_ERR(rtc->rtc_offset))
return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_offset),
"Failed to get rtc_offset NVMEM cell\n");
rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
if (IS_ERR(rtc->rtc_dev))
return PTR_ERR(rtc->rtc_dev);
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/mfd/macsmc.h`, `linux/module.h`, `linux/nvmem-consumer.h`, `linux/of.h`, `linux/platform_device.h`, `linux/rtc.h`, `linux/slab.h`.
- Detected declarations: `struct macsmc_rtc`, `function macsmc_rtc_get_time`, `function macsmc_rtc_set_time`, `function macsmc_rtc_probe`.
- Atlas domain: Driver Families / drivers/rtc.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.