rtc-rx8581.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * An I2C driver for the Epson RX8581 RTC
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
  12. * Copyright 2005-06 Tower Technologies
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/bcd.h>
  17. #include <linux/regmap.h>
  18. #include <linux/rtc.h>
  19. #include <linux/log2.h>
  20. #define RX8581_REG_SC 0x00 /* Second in BCD */
  21. #define RX8581_REG_MN 0x01 /* Minute in BCD */
  22. #define RX8581_REG_HR 0x02 /* Hour in BCD */
  23. #define RX8581_REG_DW 0x03 /* Day of Week */
  24. #define RX8581_REG_DM 0x04 /* Day of Month in BCD */
  25. #define RX8581_REG_MO 0x05 /* Month in BCD */
  26. #define RX8581_REG_YR 0x06 /* Year in BCD */
  27. #define RX8581_REG_RAM 0x07 /* RAM */
  28. #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/
  29. #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */
  30. #define RX8581_REG_ADM 0x0A
  31. #define RX8581_REG_ADW 0x0A
  32. #define RX8581_REG_TMR0 0x0B
  33. #define RX8581_REG_TMR1 0x0C
  34. #define RX8581_REG_EXT 0x0D /* Extension Register */
  35. #define RX8581_REG_FLAG 0x0E /* Flag Register */
  36. #define RX8581_REG_CTRL 0x0F /* Control Register */
  37. /* Flag Register bit definitions */
  38. #define RX8581_FLAG_UF 0x20 /* Update */
  39. #define RX8581_FLAG_TF 0x10 /* Timer */
  40. #define RX8581_FLAG_AF 0x08 /* Alarm */
  41. #define RX8581_FLAG_VLF 0x02 /* Voltage Low */
  42. /* Control Register bit definitions */
  43. #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */
  44. #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
  45. #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
  46. #define RX8581_CTRL_STOP 0x02 /* STOP bit */
  47. #define RX8581_CTRL_RESET 0x01 /* RESET bit */
  48. struct rx8581 {
  49. struct regmap *regmap;
  50. struct rtc_device *rtc;
  51. };
  52. /*
  53. * In the routines that deal directly with the rx8581 hardware, we use
  54. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  55. */
  56. static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
  57. {
  58. struct i2c_client *client = to_i2c_client(dev);
  59. unsigned char date[7];
  60. unsigned int data;
  61. int err;
  62. struct rx8581 *rx8581 = i2c_get_clientdata(client);
  63. /* First we ensure that the "update flag" is not set, we read the
  64. * time and date then re-read the "update flag". If the update flag
  65. * has been set, we know that the time has changed during the read so
  66. * we repeat the whole process again.
  67. */
  68. err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
  69. if (err < 0)
  70. return err;
  71. if (data & RX8581_FLAG_VLF) {
  72. dev_warn(dev,
  73. "low voltage detected, date/time is not reliable.\n");
  74. return -EINVAL;
  75. }
  76. do {
  77. /* If update flag set, clear it */
  78. if (data & RX8581_FLAG_UF) {
  79. err = regmap_write(rx8581->regmap, RX8581_REG_FLAG,
  80. data & ~RX8581_FLAG_UF);
  81. if (err < 0)
  82. return err;
  83. }
  84. /* Now read time and date */
  85. err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
  86. sizeof(date));
  87. if (err < 0)
  88. return err;
  89. /* Check flag register */
  90. err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
  91. if (err < 0)
  92. return err;
  93. } while (data & RX8581_FLAG_UF);
  94. dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  95. "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
  96. __func__,
  97. date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
  98. tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
  99. tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
  100. tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
  101. tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
  102. tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
  103. tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  104. tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
  105. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  106. "mday=%d, mon=%d, year=%d, wday=%d\n",
  107. __func__,
  108. tm->tm_sec, tm->tm_min, tm->tm_hour,
  109. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  110. return 0;
  111. }
  112. static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
  113. {
  114. struct i2c_client *client = to_i2c_client(dev);
  115. int err;
  116. unsigned char buf[7];
  117. struct rx8581 *rx8581 = i2c_get_clientdata(client);
  118. dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
  119. "mday=%d, mon=%d, year=%d, wday=%d\n",
  120. __func__,
  121. tm->tm_sec, tm->tm_min, tm->tm_hour,
  122. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  123. /* hours, minutes and seconds */
  124. buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
  125. buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
  126. buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
  127. buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
  128. /* month, 1 - 12 */
  129. buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
  130. /* year and century */
  131. buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
  132. buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
  133. /* Stop the clock */
  134. err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
  135. RX8581_CTRL_STOP, RX8581_CTRL_STOP);
  136. if (err < 0)
  137. return err;
  138. /* write register's data */
  139. err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
  140. buf, sizeof(buf));
  141. if (err < 0)
  142. return err;
  143. /* get VLF and clear it */
  144. err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
  145. RX8581_FLAG_VLF, 0);
  146. if (err < 0)
  147. return err;
  148. /* Restart the clock */
  149. return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
  150. RX8581_CTRL_STOP, 0);
  151. }
  152. static const struct rtc_class_ops rx8581_rtc_ops = {
  153. .read_time = rx8581_rtc_read_time,
  154. .set_time = rx8581_rtc_set_time,
  155. };
  156. static int rx8581_probe(struct i2c_client *client,
  157. const struct i2c_device_id *id)
  158. {
  159. struct rx8581 *rx8581;
  160. static const struct regmap_config config = {
  161. .reg_bits = 8,
  162. .val_bits = 8,
  163. .max_register = 0xf,
  164. };
  165. dev_dbg(&client->dev, "%s\n", __func__);
  166. rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
  167. if (!rx8581)
  168. return -ENOMEM;
  169. i2c_set_clientdata(client, rx8581);
  170. rx8581->regmap = devm_regmap_init_i2c(client, &config);
  171. if (IS_ERR(rx8581->regmap))
  172. return PTR_ERR(rx8581->regmap);
  173. rx8581->rtc = devm_rtc_allocate_device(&client->dev);
  174. if (IS_ERR(rx8581->rtc))
  175. return PTR_ERR(rx8581->rtc);
  176. rx8581->rtc->ops = &rx8581_rtc_ops;
  177. rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  178. rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
  179. rx8581->rtc->start_secs = 0;
  180. rx8581->rtc->set_start_time = true;
  181. return rtc_register_device(rx8581->rtc);
  182. }
  183. static const struct i2c_device_id rx8581_id[] = {
  184. { "rx8581", 0 },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(i2c, rx8581_id);
  188. static const struct of_device_id rx8581_of_match[] = {
  189. { .compatible = "epson,rx8581" },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(of, rx8581_of_match);
  193. static struct i2c_driver rx8581_driver = {
  194. .driver = {
  195. .name = "rtc-rx8581",
  196. .of_match_table = of_match_ptr(rx8581_of_match),
  197. },
  198. .probe = rx8581_probe,
  199. .id_table = rx8581_id,
  200. };
  201. module_i2c_driver(rx8581_driver);
  202. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  203. MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
  204. MODULE_LICENSE("GPL");