rtc-sd3078.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real Time Clock (RTC) Driver for sd3078
  4. * Copyright (C) 2018 Zoro Li
  5. */
  6. #include <linux/bcd.h>
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include <linux/rtc.h>
  11. #include <linux/slab.h>
  12. #define SD3078_REG_SC 0x00
  13. #define SD3078_REG_MN 0x01
  14. #define SD3078_REG_HR 0x02
  15. #define SD3078_REG_DW 0x03
  16. #define SD3078_REG_DM 0x04
  17. #define SD3078_REG_MO 0x05
  18. #define SD3078_REG_YR 0x06
  19. #define SD3078_REG_CTRL1 0x0f
  20. #define SD3078_REG_CTRL2 0x10
  21. #define SD3078_REG_CTRL3 0x11
  22. #define KEY_WRITE1 0x80
  23. #define KEY_WRITE2 0x04
  24. #define KEY_WRITE3 0x80
  25. #define NUM_TIME_REGS (SD3078_REG_YR - SD3078_REG_SC + 1)
  26. /*
  27. * The sd3078 has write protection
  28. * and we can choose whether or not to use it.
  29. * Write protection is turned off by default.
  30. */
  31. #define WRITE_PROTECT_EN 0
  32. struct sd3078 {
  33. struct rtc_device *rtc;
  34. struct regmap *regmap;
  35. };
  36. /*
  37. * In order to prevent arbitrary modification of the time register,
  38. * when modification of the register,
  39. * the "write" bit needs to be written in a certain order.
  40. * 1. set WRITE1 bit
  41. * 2. set WRITE2 bit
  42. * 3. set WRITE3 bit
  43. */
  44. static void sd3078_enable_reg_write(struct sd3078 *sd3078)
  45. {
  46. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
  47. KEY_WRITE1, KEY_WRITE1);
  48. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
  49. KEY_WRITE2, KEY_WRITE2);
  50. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
  51. KEY_WRITE3, KEY_WRITE3);
  52. }
  53. #if WRITE_PROTECT_EN
  54. /*
  55. * In order to prevent arbitrary modification of the time register,
  56. * we should disable the write function.
  57. * when disable write,
  58. * the "write" bit needs to be clear in a certain order.
  59. * 1. clear WRITE2 bit
  60. * 2. clear WRITE3 bit
  61. * 3. clear WRITE1 bit
  62. */
  63. static void sd3078_disable_reg_write(struct sd3078 *sd3078)
  64. {
  65. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
  66. KEY_WRITE2, 0);
  67. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
  68. KEY_WRITE3, 0);
  69. regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
  70. KEY_WRITE1, 0);
  71. }
  72. #endif
  73. static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. unsigned char hour;
  76. unsigned char rtc_data[NUM_TIME_REGS] = {0};
  77. struct i2c_client *client = to_i2c_client(dev);
  78. struct sd3078 *sd3078 = i2c_get_clientdata(client);
  79. int ret;
  80. ret = regmap_bulk_read(sd3078->regmap, SD3078_REG_SC, rtc_data,
  81. NUM_TIME_REGS);
  82. if (ret < 0) {
  83. dev_err(dev, "reading from RTC failed with err:%d\n", ret);
  84. return ret;
  85. }
  86. tm->tm_sec = bcd2bin(rtc_data[SD3078_REG_SC] & 0x7F);
  87. tm->tm_min = bcd2bin(rtc_data[SD3078_REG_MN] & 0x7F);
  88. /*
  89. * The sd3078 supports 12/24 hour mode.
  90. * When getting time,
  91. * we need to convert the 12 hour mode to the 24 hour mode.
  92. */
  93. hour = rtc_data[SD3078_REG_HR];
  94. if (hour & 0x80) /* 24H MODE */
  95. tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x3F);
  96. else if (hour & 0x20) /* 12H MODE PM */
  97. tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F) + 12;
  98. else /* 12H MODE AM */
  99. tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F);
  100. tm->tm_mday = bcd2bin(rtc_data[SD3078_REG_DM] & 0x3F);
  101. tm->tm_wday = rtc_data[SD3078_REG_DW] & 0x07;
  102. tm->tm_mon = bcd2bin(rtc_data[SD3078_REG_MO] & 0x1F) - 1;
  103. tm->tm_year = bcd2bin(rtc_data[SD3078_REG_YR]) + 100;
  104. return 0;
  105. }
  106. static int sd3078_rtc_set_time(struct device *dev, struct rtc_time *tm)
  107. {
  108. unsigned char rtc_data[NUM_TIME_REGS];
  109. struct i2c_client *client = to_i2c_client(dev);
  110. struct sd3078 *sd3078 = i2c_get_clientdata(client);
  111. int ret;
  112. rtc_data[SD3078_REG_SC] = bin2bcd(tm->tm_sec);
  113. rtc_data[SD3078_REG_MN] = bin2bcd(tm->tm_min);
  114. rtc_data[SD3078_REG_HR] = bin2bcd(tm->tm_hour) | 0x80;
  115. rtc_data[SD3078_REG_DM] = bin2bcd(tm->tm_mday);
  116. rtc_data[SD3078_REG_DW] = tm->tm_wday & 0x07;
  117. rtc_data[SD3078_REG_MO] = bin2bcd(tm->tm_mon) + 1;
  118. rtc_data[SD3078_REG_YR] = bin2bcd(tm->tm_year - 100);
  119. #if WRITE_PROTECT_EN
  120. sd3078_enable_reg_write(sd3078);
  121. #endif
  122. ret = regmap_bulk_write(sd3078->regmap, SD3078_REG_SC, rtc_data,
  123. NUM_TIME_REGS);
  124. if (ret < 0) {
  125. dev_err(dev, "writing to RTC failed with err:%d\n", ret);
  126. return ret;
  127. }
  128. #if WRITE_PROTECT_EN
  129. sd3078_disable_reg_write(sd3078);
  130. #endif
  131. return 0;
  132. }
  133. static const struct rtc_class_ops sd3078_rtc_ops = {
  134. .read_time = sd3078_rtc_read_time,
  135. .set_time = sd3078_rtc_set_time,
  136. };
  137. static const struct regmap_config regmap_config = {
  138. .reg_bits = 8,
  139. .val_bits = 8,
  140. .max_register = 0x11,
  141. };
  142. static int sd3078_probe(struct i2c_client *client,
  143. const struct i2c_device_id *id)
  144. {
  145. int ret;
  146. struct sd3078 *sd3078;
  147. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  148. return -ENODEV;
  149. sd3078 = devm_kzalloc(&client->dev, sizeof(*sd3078), GFP_KERNEL);
  150. if (!sd3078)
  151. return -ENOMEM;
  152. sd3078->regmap = devm_regmap_init_i2c(client, &regmap_config);
  153. if (IS_ERR(sd3078->regmap)) {
  154. dev_err(&client->dev, "regmap allocation failed\n");
  155. return PTR_ERR(sd3078->regmap);
  156. }
  157. i2c_set_clientdata(client, sd3078);
  158. sd3078->rtc = devm_rtc_allocate_device(&client->dev);
  159. if (IS_ERR(sd3078->rtc))
  160. return PTR_ERR(sd3078->rtc);
  161. sd3078->rtc->ops = &sd3078_rtc_ops;
  162. sd3078->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  163. sd3078->rtc->range_max = RTC_TIMESTAMP_END_2099;
  164. ret = rtc_register_device(sd3078->rtc);
  165. if (ret)
  166. return ret;
  167. sd3078_enable_reg_write(sd3078);
  168. return 0;
  169. }
  170. static const struct i2c_device_id sd3078_id[] = {
  171. {"sd3078", 0},
  172. { }
  173. };
  174. MODULE_DEVICE_TABLE(i2c, sd3078_id);
  175. static const struct of_device_id rtc_dt_match[] = {
  176. { .compatible = "whwave,sd3078" },
  177. {},
  178. };
  179. MODULE_DEVICE_TABLE(of, rtc_dt_match);
  180. static struct i2c_driver sd3078_driver = {
  181. .driver = {
  182. .name = "sd3078",
  183. .of_match_table = of_match_ptr(rtc_dt_match),
  184. },
  185. .probe = sd3078_probe,
  186. .id_table = sd3078_id,
  187. };
  188. module_i2c_driver(sd3078_driver);
  189. MODULE_AUTHOR("Dianlong Li <long17.cool@163.com>");
  190. MODULE_DESCRIPTION("SD3078 RTC driver");
  191. MODULE_LICENSE("GPL v2");