|
| 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