sandbox_rtc.c 2.4 KB

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