sandbox_rtc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <i2c.h>
  9. #include <rtc.h>
  10. #include <asm/rtc.h>
  11. #include <dm/acpi.h>
  12. #define REG_COUNT 0x80
  13. static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
  14. {
  15. u8 buf[7];
  16. int ret;
  17. ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf));
  18. if (ret < 0)
  19. return ret;
  20. time->tm_sec = buf[REG_SEC - REG_SEC];
  21. time->tm_min = buf[REG_MIN - REG_SEC];
  22. time->tm_hour = buf[REG_HOUR - REG_SEC];
  23. time->tm_mday = buf[REG_MDAY - REG_SEC];
  24. time->tm_mon = buf[REG_MON - REG_SEC];
  25. time->tm_year = buf[REG_YEAR - REG_SEC] + 1900;
  26. time->tm_wday = buf[REG_WDAY - REG_SEC];
  27. return 0;
  28. }
  29. static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
  30. {
  31. u8 buf[7];
  32. int ret;
  33. buf[REG_SEC - REG_SEC] = time->tm_sec;
  34. buf[REG_MIN - REG_SEC] = time->tm_min;
  35. buf[REG_HOUR - REG_SEC] = time->tm_hour;
  36. buf[REG_MDAY - REG_SEC] = time->tm_mday;
  37. buf[REG_MON - REG_SEC] = time->tm_mon;
  38. buf[REG_YEAR - REG_SEC] = time->tm_year - 1900;
  39. buf[REG_WDAY - REG_SEC] = time->tm_wday;
  40. ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf));
  41. if (ret < 0)
  42. return ret;
  43. return 0;
  44. }
  45. static int sandbox_rtc_reset(struct udevice *dev)
  46. {
  47. return dm_i2c_reg_write(dev, REG_RESET, 0);
  48. }
  49. static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
  50. {
  51. return dm_i2c_reg_read(dev, reg);
  52. }
  53. static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
  54. {
  55. return dm_i2c_reg_write(dev, reg, val);
  56. }
  57. #if CONFIG_IS_ENABLED(ACPIGEN)
  58. static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name)
  59. {
  60. return acpi_copy_name(out_name, "RTCC");
  61. }
  62. struct acpi_ops sandbox_rtc_acpi_ops = {
  63. .get_name = sandbox_rtc_get_name,
  64. };
  65. #endif
  66. static const struct rtc_ops sandbox_rtc_ops = {
  67. .get = sandbox_rtc_get,
  68. .set = sandbox_rtc_set,
  69. .reset = sandbox_rtc_reset,
  70. .read8 = sandbox_rtc_read8,
  71. .write8 = sandbox_rtc_write8,
  72. };
  73. static const struct udevice_id sandbox_rtc_ids[] = {
  74. { .compatible = "sandbox-rtc" },
  75. { }
  76. };
  77. U_BOOT_DRIVER(rtc_sandbox) = {
  78. .name = "rtc-sandbox",
  79. .id = UCLASS_RTC,
  80. .of_match = sandbox_rtc_ids,
  81. .ops = &sandbox_rtc_ops,
  82. ACPI_OPS_PTR(&sandbox_rtc_acpi_ops)
  83. };