rtc-max6900.c 6.2 KB

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