rtc-wilco-ec.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC interface for Wilco Embedded Controller with R/W abilities
  4. *
  5. * Copyright 2018 Google LLC
  6. *
  7. * The corresponding platform device is typically registered in
  8. * drivers/platform/chrome/wilco_ec/core.c
  9. */
  10. #include <linux/bcd.h>
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/platform_data/wilco-ec.h>
  16. #include <linux/rtc.h>
  17. #include <linux/timekeeping.h>
  18. #define EC_COMMAND_CMOS 0x7c
  19. #define EC_CMOS_TOD_WRITE 0x02
  20. #define EC_CMOS_TOD_READ 0x08
  21. /* Message sent to the EC to request the current time. */
  22. struct ec_rtc_read_request {
  23. u8 command;
  24. u8 reserved;
  25. u8 param;
  26. } __packed;
  27. static struct ec_rtc_read_request read_rq = {
  28. .command = EC_COMMAND_CMOS,
  29. .param = EC_CMOS_TOD_READ,
  30. };
  31. /**
  32. * struct ec_rtc_read_response - Format of RTC returned by EC.
  33. * @reserved: Unused byte
  34. * @second: Second value (0..59)
  35. * @minute: Minute value (0..59)
  36. * @hour: Hour value (0..23)
  37. * @day: Day value (1..31)
  38. * @month: Month value (1..12)
  39. * @year: Year value (full year % 100)
  40. * @century: Century value (full year / 100)
  41. *
  42. * All values are presented in binary (not BCD).
  43. */
  44. struct ec_rtc_read_response {
  45. u8 reserved;
  46. u8 second;
  47. u8 minute;
  48. u8 hour;
  49. u8 day;
  50. u8 month;
  51. u8 year;
  52. u8 century;
  53. } __packed;
  54. /**
  55. * struct ec_rtc_write_request - Format of RTC sent to the EC.
  56. * @command: Always EC_COMMAND_CMOS
  57. * @reserved: Unused byte
  58. * @param: Always EC_CMOS_TOD_WRITE
  59. * @century: Century value (full year / 100)
  60. * @year: Year value (full year % 100)
  61. * @month: Month value (1..12)
  62. * @day: Day value (1..31)
  63. * @hour: Hour value (0..23)
  64. * @minute: Minute value (0..59)
  65. * @second: Second value (0..59)
  66. * @weekday: Day of the week (0=Saturday)
  67. *
  68. * All values are presented in BCD.
  69. */
  70. struct ec_rtc_write_request {
  71. u8 command;
  72. u8 reserved;
  73. u8 param;
  74. u8 century;
  75. u8 year;
  76. u8 month;
  77. u8 day;
  78. u8 hour;
  79. u8 minute;
  80. u8 second;
  81. u8 weekday;
  82. } __packed;
  83. static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
  84. {
  85. struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
  86. struct ec_rtc_read_response rtc;
  87. struct wilco_ec_message msg;
  88. int ret;
  89. memset(&msg, 0, sizeof(msg));
  90. msg.type = WILCO_EC_MSG_LEGACY;
  91. msg.request_data = &read_rq;
  92. msg.request_size = sizeof(read_rq);
  93. msg.response_data = &rtc;
  94. msg.response_size = sizeof(rtc);
  95. ret = wilco_ec_mailbox(ec, &msg);
  96. if (ret < 0)
  97. return ret;
  98. tm->tm_sec = rtc.second;
  99. tm->tm_min = rtc.minute;
  100. tm->tm_hour = rtc.hour;
  101. tm->tm_mday = rtc.day;
  102. tm->tm_mon = rtc.month - 1;
  103. tm->tm_year = rtc.year + (rtc.century * 100) - 1900;
  104. /* Ignore other tm fields, man rtc says userspace shouldn't use them. */
  105. if (rtc_valid_tm(tm)) {
  106. dev_err(dev, "Time from RTC is invalid: %ptRr\n", tm);
  107. return -EIO;
  108. }
  109. return 0;
  110. }
  111. static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
  112. {
  113. struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
  114. struct ec_rtc_write_request rtc;
  115. struct wilco_ec_message msg;
  116. int year = tm->tm_year + 1900;
  117. /*
  118. * Convert from 0=Sunday to 0=Saturday for the EC
  119. * We DO need to set weekday because the EC controls battery charging
  120. * schedules that depend on the day of the week.
  121. */
  122. int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1;
  123. int ret;
  124. rtc.command = EC_COMMAND_CMOS;
  125. rtc.param = EC_CMOS_TOD_WRITE;
  126. rtc.century = bin2bcd(year / 100);
  127. rtc.year = bin2bcd(year % 100);
  128. rtc.month = bin2bcd(tm->tm_mon + 1);
  129. rtc.day = bin2bcd(tm->tm_mday);
  130. rtc.hour = bin2bcd(tm->tm_hour);
  131. rtc.minute = bin2bcd(tm->tm_min);
  132. rtc.second = bin2bcd(tm->tm_sec);
  133. rtc.weekday = bin2bcd(wday);
  134. memset(&msg, 0, sizeof(msg));
  135. msg.type = WILCO_EC_MSG_LEGACY;
  136. msg.request_data = &rtc;
  137. msg.request_size = sizeof(rtc);
  138. ret = wilco_ec_mailbox(ec, &msg);
  139. if (ret < 0)
  140. return ret;
  141. return 0;
  142. }
  143. static const struct rtc_class_ops wilco_ec_rtc_ops = {
  144. .read_time = wilco_ec_rtc_read,
  145. .set_time = wilco_ec_rtc_write,
  146. };
  147. static int wilco_ec_rtc_probe(struct platform_device *pdev)
  148. {
  149. struct rtc_device *rtc;
  150. rtc = devm_rtc_allocate_device(&pdev->dev);
  151. if (IS_ERR(rtc))
  152. return PTR_ERR(rtc);
  153. rtc->ops = &wilco_ec_rtc_ops;
  154. /* EC only supports this century */
  155. rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  156. rtc->range_max = RTC_TIMESTAMP_END_2099;
  157. rtc->owner = THIS_MODULE;
  158. return rtc_register_device(rtc);
  159. }
  160. static struct platform_driver wilco_ec_rtc_driver = {
  161. .driver = {
  162. .name = "rtc-wilco-ec",
  163. },
  164. .probe = wilco_ec_rtc_probe,
  165. };
  166. module_platform_driver(wilco_ec_rtc_driver);
  167. MODULE_ALIAS("platform:rtc-wilco-ec");
  168. MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
  169. MODULE_LICENSE("GPL v2");
  170. MODULE_DESCRIPTION("Wilco EC RTC driver");