rtc-max6900.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * rtc class driver for the Maxim MAX6900 chip
  4. *
  5. * Copyright (c) 2007 MontaVista, Software, Inc.
  6. *
  7. * Author: Dale Farnsworth <[email protected]>
  8. *
  9. * based on previously existing rtc class drivers
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/bcd.h>
  14. #include <linux/rtc.h>
  15. #include <linux/delay.h>
  16. /*
  17. * register indices
  18. */
  19. #define MAX6900_REG_SC 0 /* seconds 00-59 */
  20. #define MAX6900_REG_MN 1 /* minutes 00-59 */
  21. #define MAX6900_REG_HR 2 /* hours 00-23 */
  22. #define MAX6900_REG_DT 3 /* day of month 00-31 */
  23. #define MAX6900_REG_MO 4 /* month 01-12 */
  24. #define MAX6900_REG_DW 5 /* day of week 1-7 */
  25. #define MAX6900_REG_YR 6 /* year 00-99 */
  26. #define MAX6900_REG_CT 7 /* control */
  27. /* register 8 is undocumented */
  28. #define MAX6900_REG_CENTURY 9 /* century */
  29. #define MAX6900_REG_LEN 10
  30. #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
  31. #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
  32. /*
  33. * register read/write commands
  34. */
  35. #define MAX6900_REG_CONTROL_WRITE 0x8e
  36. #define MAX6900_REG_CENTURY_WRITE 0x92
  37. #define MAX6900_REG_CENTURY_READ 0x93
  38. #define MAX6900_REG_RESERVED_READ 0x96
  39. #define MAX6900_REG_BURST_WRITE 0xbe
  40. #define MAX6900_REG_BURST_READ 0xbf
  41. #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
  42. static struct i2c_driver max6900_driver;
  43. static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
  44. {
  45. u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
  46. u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
  47. struct i2c_msg msgs[4] = {
  48. {
  49. .addr = client->addr,
  50. .flags = 0, /* write */
  51. .len = sizeof(reg_burst_read),
  52. .buf = reg_burst_read}
  53. ,
  54. {
  55. .addr = client->addr,
  56. .flags = I2C_M_RD,
  57. .len = MAX6900_BURST_LEN,
  58. .buf = buf}
  59. ,
  60. {
  61. .addr = client->addr,
  62. .flags = 0, /* write */
  63. .len = sizeof(reg_century_read),
  64. .buf = reg_century_read}
  65. ,
  66. {
  67. .addr = client->addr,
  68. .flags = I2C_M_RD,
  69. .len = sizeof(buf[MAX6900_REG_CENTURY]),
  70. .buf = &buf[MAX6900_REG_CENTURY]
  71. }
  72. };
  73. int rc;
  74. rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  75. if (rc != ARRAY_SIZE(msgs)) {
  76. dev_err(&client->dev, "%s: register read failed\n", __func__);
  77. return -EIO;
  78. }
  79. return 0;
  80. }
  81. static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
  82. {
  83. u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
  84. struct i2c_msg century_msgs[1] = {
  85. {
  86. .addr = client->addr,
  87. .flags = 0, /* write */
  88. .len = sizeof(i2c_century_buf),
  89. .buf = i2c_century_buf}
  90. };
  91. u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
  92. struct i2c_msg burst_msgs[1] = {
  93. {
  94. .addr = client->addr,
  95. .flags = 0, /* write */
  96. .len = sizeof(i2c_burst_buf),
  97. .buf = i2c_burst_buf}
  98. };
  99. int rc;
  100. /*
  101. * We have to make separate calls to i2c_transfer because of
  102. * the need to delay after each write to the chip. Also,
  103. * we write the century byte first, since we set the write-protect
  104. * bit as part of the burst write.
  105. */
  106. i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
  107. rc = i2c_transfer(client->adapter, century_msgs,
  108. ARRAY_SIZE(century_msgs));
  109. if (rc != ARRAY_SIZE(century_msgs))
  110. goto write_failed;
  111. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  112. memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
  113. rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
  114. if (rc != ARRAY_SIZE(burst_msgs))
  115. goto write_failed;
  116. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  117. return 0;
  118. write_failed:
  119. dev_err(&client->dev, "%s: register write failed\n", __func__);
  120. return -EIO;
  121. }
  122. static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. int rc;
  126. u8 regs[MAX6900_REG_LEN];
  127. rc = max6900_i2c_read_regs(client, regs);
  128. if (rc < 0)
  129. return rc;
  130. tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
  131. tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
  132. tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
  133. tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
  134. tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
  135. tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
  136. bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
  137. tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
  138. return 0;
  139. }
  140. static int max6900_i2c_clear_write_protect(struct i2c_client *client)
  141. {
  142. return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
  143. }
  144. static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
  145. {
  146. struct i2c_client *client = to_i2c_client(dev);
  147. u8 regs[MAX6900_REG_LEN];
  148. int rc;
  149. rc = max6900_i2c_clear_write_protect(client);
  150. if (rc < 0)
  151. return rc;
  152. regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
  153. regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
  154. regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
  155. regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
  156. regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
  157. regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
  158. regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
  159. regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
  160. /* set write protect */
  161. regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
  162. rc = max6900_i2c_write_regs(client, regs);
  163. if (rc < 0)
  164. return rc;
  165. return 0;
  166. }
  167. static const struct rtc_class_ops max6900_rtc_ops = {
  168. .read_time = max6900_rtc_read_time,
  169. .set_time = max6900_rtc_set_time,
  170. };
  171. static int max6900_probe(struct i2c_client *client)
  172. {
  173. struct rtc_device *rtc;
  174. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  175. return -ENODEV;
  176. rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
  177. &max6900_rtc_ops, THIS_MODULE);
  178. if (IS_ERR(rtc))
  179. return PTR_ERR(rtc);
  180. i2c_set_clientdata(client, rtc);
  181. return 0;
  182. }
  183. static const struct i2c_device_id max6900_id[] = {
  184. { "max6900", 0 },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(i2c, max6900_id);
  188. static struct i2c_driver max6900_driver = {
  189. .driver = {
  190. .name = "rtc-max6900",
  191. },
  192. .probe_new = max6900_probe,
  193. .id_table = max6900_id,
  194. };
  195. module_i2c_driver(max6900_driver);
  196. MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
  197. MODULE_AUTHOR("Dale Farnsworth <[email protected]>");
  198. MODULE_LICENSE("GPL");