rtc-macsmc.c 3.4 KB

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