sandbox_rtc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #define REG_COUNT 0x80
  12. static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
  13. {
  14. time->tm_sec = dm_i2c_reg_read(dev, REG_SEC);
  15. if (time->tm_sec < 0)
  16. return time->tm_sec;
  17. time->tm_min = dm_i2c_reg_read(dev, REG_MIN);
  18. if (time->tm_min < 0)
  19. return time->tm_min;
  20. time->tm_hour = dm_i2c_reg_read(dev, REG_HOUR);
  21. if (time->tm_hour < 0)
  22. return time->tm_hour;
  23. time->tm_mday = dm_i2c_reg_read(dev, REG_MDAY);
  24. if (time->tm_mday < 0)
  25. return time->tm_mday;
  26. time->tm_mon = dm_i2c_reg_read(dev, REG_MON);
  27. if (time->tm_mon < 0)
  28. return time->tm_mon;
  29. time->tm_year = dm_i2c_reg_read(dev, REG_YEAR);
  30. if (time->tm_year < 0)
  31. return time->tm_year;
  32. time->tm_year += 1900;
  33. time->tm_wday = dm_i2c_reg_read(dev, REG_WDAY);
  34. if (time->tm_wday < 0)
  35. return time->tm_wday;
  36. return 0;
  37. }
  38. static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
  39. {
  40. int ret;
  41. ret = dm_i2c_reg_write(dev, REG_SEC, time->tm_sec);
  42. if (ret < 0)
  43. return ret;
  44. ret = dm_i2c_reg_write(dev, REG_MIN, time->tm_min);
  45. if (ret < 0)
  46. return ret;
  47. ret = dm_i2c_reg_write(dev, REG_HOUR, time->tm_hour);
  48. if (ret < 0)
  49. return ret;
  50. ret = dm_i2c_reg_write(dev, REG_MDAY, time->tm_mday);
  51. if (ret < 0)
  52. return ret;
  53. ret = dm_i2c_reg_write(dev, REG_MON, time->tm_mon);
  54. if (ret < 0)
  55. return ret;
  56. ret = dm_i2c_reg_write(dev, REG_YEAR, time->tm_year - 1900);
  57. if (ret < 0)
  58. return ret;
  59. ret = dm_i2c_reg_write(dev, REG_WDAY, time->tm_wday);
  60. if (ret < 0)
  61. return ret;
  62. return 0;
  63. }
  64. static int sandbox_rtc_reset(struct udevice *dev)
  65. {
  66. return dm_i2c_reg_write(dev, REG_RESET, 0);
  67. }
  68. static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
  69. {
  70. return dm_i2c_reg_read(dev, reg);
  71. }
  72. static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
  73. {
  74. return dm_i2c_reg_write(dev, reg, val);
  75. }
  76. static const struct rtc_ops sandbox_rtc_ops = {
  77. .get = sandbox_rtc_get,
  78. .set = sandbox_rtc_set,
  79. .reset = sandbox_rtc_reset,
  80. .read8 = sandbox_rtc_read8,
  81. .write8 = sandbox_rtc_write8,
  82. };
  83. static const struct udevice_id sandbox_rtc_ids[] = {
  84. { .compatible = "sandbox-rtc" },
  85. { }
  86. };
  87. U_BOOT_DRIVER(rtc_sandbox) = {
  88. .name = "rtc-sandbox",
  89. .id = UCLASS_RTC,
  90. .of_match = sandbox_rtc_ids,
  91. .ops = &sandbox_rtc_ops,
  92. };