s35392a.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SII Semiconductor Corporation S35392A RTC driver.
  4. *
  5. * Copyright (c) 2017, General Electric Company
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <command.h>
  20. #include <common.h>
  21. #include <dm.h>
  22. #include <i2c.h>
  23. #include <linux/bitrev.h>
  24. #include <rtc.h>
  25. #include <linux/delay.h>
  26. #define S35390A_CHIP_ADDR 0x30
  27. #define S35390A_CMD_STATUS1 0x0
  28. #define S35390A_CMD_STATUS2 0x1
  29. #define S35390A_CMD_TIME1 0x2
  30. #define S35390A_CMD_TIME2 0x3
  31. #define S35390A_CMD_INT2_REG1 0x5
  32. #define S35390A_BYTE_YEAR 0
  33. #define S35390A_BYTE_MONTH 1
  34. #define S35390A_BYTE_DAY 2
  35. #define S35390A_BYTE_WDAY 3
  36. #define S35390A_BYTE_HOURS 4
  37. #define S35390A_BYTE_MINS 5
  38. #define S35390A_BYTE_SECS 6
  39. /* flags for STATUS1 */
  40. #define S35390A_FLAG_POC 0x01
  41. #define S35390A_FLAG_BLD 0x02
  42. #define S35390A_FLAG_INT2 0x04
  43. #define S35390A_FLAG_24H 0x40
  44. #define S35390A_FLAG_RESET 0x80
  45. /*
  46. * If either BLD or POC is set, then the chip has lost power long enough for
  47. * the time value to become invalid.
  48. */
  49. #define S35390A_LOW_VOLTAGE (S35390A_FLAG_POC | S35390A_FLAG_BLD)
  50. /*---------------------------------------------------------------------*/
  51. #undef DEBUG_RTC
  52. #ifdef DEBUG_RTC
  53. #define DEBUGR(fmt, args...) printf(fmt, ##args)
  54. #else
  55. #define DEBUGR(fmt, args...)
  56. #endif
  57. /*---------------------------------------------------------------------*/
  58. #ifdef CONFIG_DM_RTC
  59. #define DEV_TYPE struct udevice
  60. #else
  61. /* Local udevice */
  62. struct ludevice {
  63. u8 chip;
  64. };
  65. #define DEV_TYPE struct ludevice
  66. struct ludevice dev;
  67. #endif
  68. #define msleep(a) udelay(a * 1000)
  69. int lowvoltage;
  70. static int s35392a_rtc_reset(DEV_TYPE *dev);
  71. static int s35392a_rtc_read(DEV_TYPE *dev, u8 reg, u8 *buf, int len)
  72. {
  73. int ret;
  74. #ifdef CONFIG_DM_RTC
  75. ret = dm_i2c_read(dev, reg, buf, len);
  76. #else
  77. (void)dev;
  78. ret = i2c_read(S35390A_CHIP_ADDR | reg, 0, -1, buf, len);
  79. #endif
  80. return ret;
  81. }
  82. static int s35392a_rtc_write(DEV_TYPE *dev, u8 reg, u8 *buf, int len)
  83. {
  84. int ret;
  85. #ifdef CONFIG_DM_RTC
  86. ret = dm_i2c_write(dev, reg, buf, len);
  87. #else
  88. (void)dev;
  89. ret = i2c_write(S35390A_CHIP_ADDR | reg, 0, 0, buf, len);
  90. #endif
  91. return ret;
  92. }
  93. static int s35392a_rtc_read8(DEV_TYPE *dev, unsigned int reg)
  94. {
  95. u8 val;
  96. int ret;
  97. ret = s35392a_rtc_read(dev, reg, &val, sizeof(val));
  98. return ret < 0 ? ret : val;
  99. }
  100. static int s35392a_rtc_write8(DEV_TYPE *dev, unsigned int reg, int val)
  101. {
  102. int ret;
  103. u8 lval = val;
  104. ret = s35392a_rtc_write(dev, reg, &lval, sizeof(lval));
  105. return ret < 0 ? ret : 0;
  106. }
  107. static int validate_time(const struct rtc_time *tm)
  108. {
  109. if ((tm->tm_year < 2000) || (tm->tm_year > 2099))
  110. return -EINVAL;
  111. if ((tm->tm_mon < 1) || (tm->tm_mon > 12))
  112. return -EINVAL;
  113. if ((tm->tm_mday < 1) || (tm->tm_mday > 31))
  114. return -EINVAL;
  115. if ((tm->tm_wday < 0) || (tm->tm_wday > 6))
  116. return -EINVAL;
  117. if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
  118. return -EINVAL;
  119. if ((tm->tm_min < 0) || (tm->tm_min > 59))
  120. return -EINVAL;
  121. if ((tm->tm_sec < 0) || (tm->tm_sec > 59))
  122. return -EINVAL;
  123. return 0;
  124. }
  125. void s35392a_rtc_init(DEV_TYPE *dev)
  126. {
  127. int status;
  128. status = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
  129. if (status < 0)
  130. goto error;
  131. DEBUGR("init: S35390A_CMD_STATUS1: 0x%x\n", status);
  132. lowvoltage = status & S35390A_LOW_VOLTAGE ? 1 : 0;
  133. if (status & S35390A_FLAG_POC)
  134. /*
  135. * Do not communicate for 0.5 seconds since the power-on
  136. * detection circuit is in operation.
  137. */
  138. msleep(500);
  139. else if (!lowvoltage)
  140. /*
  141. * If both POC and BLD are unset everything is fine.
  142. */
  143. return;
  144. if (lowvoltage)
  145. printf("RTC low voltage detected\n");
  146. if (!s35392a_rtc_reset(dev))
  147. return;
  148. error:
  149. printf("Error RTC init.\n");
  150. }
  151. /* Get the current time from the RTC */
  152. static int s35392a_rtc_get(DEV_TYPE *dev, struct rtc_time *tm)
  153. {
  154. u8 date[7];
  155. int ret, i;
  156. if (lowvoltage) {
  157. DEBUGR("RTC low voltage detected\n");
  158. return -EINVAL;
  159. }
  160. ret = s35392a_rtc_read(dev, S35390A_CMD_TIME1, date, sizeof(date));
  161. if (ret < 0) {
  162. DEBUGR("Error reading date from RTC\n");
  163. return -EIO;
  164. }
  165. /* This chip returns the bits of each byte in reverse order */
  166. for (i = 0; i < 7; ++i)
  167. date[i] = bitrev8(date[i]);
  168. tm->tm_sec = bcd2bin(date[S35390A_BYTE_SECS]);
  169. tm->tm_min = bcd2bin(date[S35390A_BYTE_MINS]);
  170. tm->tm_hour = bcd2bin(date[S35390A_BYTE_HOURS] & ~S35390A_FLAG_24H);
  171. tm->tm_wday = bcd2bin(date[S35390A_BYTE_WDAY]);
  172. tm->tm_mday = bcd2bin(date[S35390A_BYTE_DAY]);
  173. tm->tm_mon = bcd2bin(date[S35390A_BYTE_MONTH]);
  174. tm->tm_year = bcd2bin(date[S35390A_BYTE_YEAR]) + 2000;
  175. DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  176. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  177. tm->tm_hour, tm->tm_min, tm->tm_sec);
  178. return 0;
  179. }
  180. /* Set the RTC */
  181. static int s35392a_rtc_set(DEV_TYPE *dev, const struct rtc_time *tm)
  182. {
  183. int i, ret;
  184. int status;
  185. u8 date[7];
  186. DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  187. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  188. tm->tm_hour, tm->tm_min, tm->tm_sec);
  189. ret = validate_time(tm);
  190. if (ret < 0)
  191. return -EINVAL;
  192. /* We support only 24h mode */
  193. ret = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
  194. if (ret < 0)
  195. return -EIO;
  196. status = ret;
  197. ret = s35392a_rtc_write8(dev, S35390A_CMD_STATUS1,
  198. status | S35390A_FLAG_24H);
  199. if (ret < 0)
  200. return -EIO;
  201. date[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 2000);
  202. date[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon);
  203. date[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
  204. date[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
  205. date[S35390A_BYTE_HOURS] = bin2bcd(tm->tm_hour);
  206. date[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
  207. date[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
  208. /* This chip expects the bits of each byte to be in reverse order */
  209. for (i = 0; i < 7; ++i)
  210. date[i] = bitrev8(date[i]);
  211. ret = s35392a_rtc_write(dev, S35390A_CMD_TIME1, date, sizeof(date));
  212. if (ret < 0) {
  213. DEBUGR("Error writing date to RTC\n");
  214. return -EIO;
  215. }
  216. /* Now we have time. Reset the low voltage status */
  217. lowvoltage = 0;
  218. return 0;
  219. }
  220. /* Reset the RTC. */
  221. static int s35392a_rtc_reset(DEV_TYPE *dev)
  222. {
  223. int buf;
  224. int ret;
  225. unsigned int initcount = 0;
  226. buf = S35390A_FLAG_RESET;
  227. initialize:
  228. ret = s35392a_rtc_write8(dev, S35390A_CMD_STATUS1, buf);
  229. if (ret < 0)
  230. return -EIO;
  231. ret = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
  232. if (ret < 0)
  233. return -EIO;
  234. buf = ret;
  235. if (!lowvoltage)
  236. lowvoltage = buf & S35390A_LOW_VOLTAGE ? 1 : 0;
  237. if (buf & S35390A_LOW_VOLTAGE) {
  238. /* Try up to five times to reset the chip */
  239. if (initcount < 5) {
  240. ++initcount;
  241. goto initialize;
  242. } else {
  243. return -EIO;
  244. }
  245. }
  246. return 0;
  247. }
  248. #ifndef CONFIG_DM_RTC
  249. int rtc_get(struct rtc_time *tm)
  250. {
  251. return s35392a_rtc_get(&dev, tm);
  252. }
  253. int rtc_set(struct rtc_time *tm)
  254. {
  255. return s35392a_rtc_set(&dev, tm);
  256. }
  257. void rtc_reset(void)
  258. {
  259. s35392a_rtc_reset(&dev);
  260. }
  261. void rtc_init(void)
  262. {
  263. s35392a_rtc_init(&dev);
  264. }
  265. #else
  266. static int s35392a_probe(struct udevice *dev)
  267. {
  268. #if defined(CONFIG_DM_RTC)
  269. /* 3-bit "command", or register, is encoded within the device address.
  270. */
  271. i2c_set_chip_offset_len(dev, 0);
  272. i2c_set_chip_addr_offset_mask(dev, 0x7);
  273. #endif
  274. s35392a_rtc_init(dev);
  275. return 0;
  276. }
  277. static const struct rtc_ops s35392a_rtc_ops = {
  278. .get = s35392a_rtc_get,
  279. .set = s35392a_rtc_set,
  280. .read8 = s35392a_rtc_read8,
  281. .write8 = s35392a_rtc_write8,
  282. .reset = s35392a_rtc_reset,
  283. };
  284. static const struct udevice_id s35392a_rtc_ids[] = {
  285. { .compatible = "sii,s35392a-rtc" },
  286. { .compatible = "sii,s35392a" },
  287. { .compatible = "s35392a" },
  288. { }
  289. };
  290. U_BOOT_DRIVER(s35392a_rtc) = {
  291. .name = "s35392a_rtc",
  292. .id = UCLASS_RTC,
  293. .probe = s35392a_probe,
  294. .of_match = s35392a_rtc_ids,
  295. .ops = &s35392a_rtc_ops,
  296. };
  297. #endif