rtc-isl12022.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An I2C driver for the Intersil ISL 12022
  4. *
  5. * Author: Roman Fietze <[email protected]>
  6. *
  7. * Based on the Philips PCF8563 RTC
  8. * by Alessandro Zummo <[email protected]>.
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/bcd.h>
  12. #include <linux/rtc.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regmap.h>
  19. /* ISL register offsets */
  20. #define ISL12022_REG_SC 0x00
  21. #define ISL12022_REG_MN 0x01
  22. #define ISL12022_REG_HR 0x02
  23. #define ISL12022_REG_DT 0x03
  24. #define ISL12022_REG_MO 0x04
  25. #define ISL12022_REG_YR 0x05
  26. #define ISL12022_REG_DW 0x06
  27. #define ISL12022_REG_SR 0x07
  28. #define ISL12022_REG_INT 0x08
  29. /* ISL register bits */
  30. #define ISL12022_HR_MIL (1 << 7) /* military or 24 hour time */
  31. #define ISL12022_SR_LBAT85 (1 << 2)
  32. #define ISL12022_SR_LBAT75 (1 << 1)
  33. #define ISL12022_INT_WRTC (1 << 6)
  34. static struct i2c_driver isl12022_driver;
  35. struct isl12022 {
  36. struct rtc_device *rtc;
  37. struct regmap *regmap;
  38. };
  39. /*
  40. * In the routines that deal directly with the isl12022 hardware, we use
  41. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  42. */
  43. static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
  44. {
  45. struct isl12022 *isl12022 = dev_get_drvdata(dev);
  46. struct regmap *regmap = isl12022->regmap;
  47. uint8_t buf[ISL12022_REG_INT + 1];
  48. int ret;
  49. ret = regmap_bulk_read(regmap, ISL12022_REG_SC, buf, sizeof(buf));
  50. if (ret)
  51. return ret;
  52. if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
  53. dev_warn(dev,
  54. "voltage dropped below %u%%, "
  55. "date and time is not reliable.\n",
  56. buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
  57. }
  58. dev_dbg(dev,
  59. "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  60. "mday=%02x, mon=%02x, year=%02x, wday=%02x, "
  61. "sr=%02x, int=%02x",
  62. __func__,
  63. buf[ISL12022_REG_SC],
  64. buf[ISL12022_REG_MN],
  65. buf[ISL12022_REG_HR],
  66. buf[ISL12022_REG_DT],
  67. buf[ISL12022_REG_MO],
  68. buf[ISL12022_REG_YR],
  69. buf[ISL12022_REG_DW],
  70. buf[ISL12022_REG_SR],
  71. buf[ISL12022_REG_INT]);
  72. tm->tm_sec = bcd2bin(buf[ISL12022_REG_SC] & 0x7F);
  73. tm->tm_min = bcd2bin(buf[ISL12022_REG_MN] & 0x7F);
  74. tm->tm_hour = bcd2bin(buf[ISL12022_REG_HR] & 0x3F);
  75. tm->tm_mday = bcd2bin(buf[ISL12022_REG_DT] & 0x3F);
  76. tm->tm_wday = buf[ISL12022_REG_DW] & 0x07;
  77. tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
  78. tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
  79. dev_dbg(dev, "%s: %ptR\n", __func__, tm);
  80. return 0;
  81. }
  82. static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
  83. {
  84. struct isl12022 *isl12022 = dev_get_drvdata(dev);
  85. struct regmap *regmap = isl12022->regmap;
  86. int ret;
  87. uint8_t buf[ISL12022_REG_DW + 1];
  88. dev_dbg(dev, "%s: %ptR\n", __func__, tm);
  89. /* Ensure the write enable bit is set. */
  90. ret = regmap_update_bits(regmap, ISL12022_REG_INT,
  91. ISL12022_INT_WRTC, ISL12022_INT_WRTC);
  92. if (ret)
  93. return ret;
  94. /* hours, minutes and seconds */
  95. buf[ISL12022_REG_SC] = bin2bcd(tm->tm_sec);
  96. buf[ISL12022_REG_MN] = bin2bcd(tm->tm_min);
  97. buf[ISL12022_REG_HR] = bin2bcd(tm->tm_hour) | ISL12022_HR_MIL;
  98. buf[ISL12022_REG_DT] = bin2bcd(tm->tm_mday);
  99. /* month, 1 - 12 */
  100. buf[ISL12022_REG_MO] = bin2bcd(tm->tm_mon + 1);
  101. /* year and century */
  102. buf[ISL12022_REG_YR] = bin2bcd(tm->tm_year % 100);
  103. buf[ISL12022_REG_DW] = tm->tm_wday & 0x07;
  104. return regmap_bulk_write(isl12022->regmap, ISL12022_REG_SC,
  105. buf, sizeof(buf));
  106. }
  107. static const struct rtc_class_ops isl12022_rtc_ops = {
  108. .read_time = isl12022_rtc_read_time,
  109. .set_time = isl12022_rtc_set_time,
  110. };
  111. static const struct regmap_config regmap_config = {
  112. .reg_bits = 8,
  113. .val_bits = 8,
  114. .use_single_write = true,
  115. };
  116. static int isl12022_probe(struct i2c_client *client)
  117. {
  118. struct isl12022 *isl12022;
  119. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  120. return -ENODEV;
  121. isl12022 = devm_kzalloc(&client->dev, sizeof(struct isl12022),
  122. GFP_KERNEL);
  123. if (!isl12022)
  124. return -ENOMEM;
  125. dev_set_drvdata(&client->dev, isl12022);
  126. isl12022->regmap = devm_regmap_init_i2c(client, &regmap_config);
  127. if (IS_ERR(isl12022->regmap)) {
  128. dev_err(&client->dev, "regmap allocation failed\n");
  129. return PTR_ERR(isl12022->regmap);
  130. }
  131. isl12022->rtc = devm_rtc_allocate_device(&client->dev);
  132. if (IS_ERR(isl12022->rtc))
  133. return PTR_ERR(isl12022->rtc);
  134. isl12022->rtc->ops = &isl12022_rtc_ops;
  135. isl12022->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  136. isl12022->rtc->range_max = RTC_TIMESTAMP_END_2099;
  137. return devm_rtc_register_device(isl12022->rtc);
  138. }
  139. #ifdef CONFIG_OF
  140. static const struct of_device_id isl12022_dt_match[] = {
  141. { .compatible = "isl,isl12022" }, /* for backward compat., don't use */
  142. { .compatible = "isil,isl12022" },
  143. { },
  144. };
  145. MODULE_DEVICE_TABLE(of, isl12022_dt_match);
  146. #endif
  147. static const struct i2c_device_id isl12022_id[] = {
  148. { "isl12022", 0 },
  149. { }
  150. };
  151. MODULE_DEVICE_TABLE(i2c, isl12022_id);
  152. static struct i2c_driver isl12022_driver = {
  153. .driver = {
  154. .name = "rtc-isl12022",
  155. #ifdef CONFIG_OF
  156. .of_match_table = of_match_ptr(isl12022_dt_match),
  157. #endif
  158. },
  159. .probe_new = isl12022_probe,
  160. .id_table = isl12022_id,
  161. };
  162. module_i2c_driver(isl12022_driver);
  163. MODULE_AUTHOR("[email protected]");
  164. MODULE_DESCRIPTION("ISL 12022 RTC driver");
  165. MODULE_LICENSE("GPL");