rtc-max6900.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #define DRV_VERSION "0.2"
  19. /*
  20. * register indices
  21. */
  22. #define MAX6900_REG_SC 0 /* seconds 00-59 */
  23. #define MAX6900_REG_MN 1 /* minutes 00-59 */
  24. #define MAX6900_REG_HR 2 /* hours 00-23 */
  25. #define MAX6900_REG_DT 3 /* day of month 00-31 */
  26. #define MAX6900_REG_MO 4 /* month 01-12 */
  27. #define MAX6900_REG_DW 5 /* day of week 1-7 */
  28. #define MAX6900_REG_YR 6 /* year 00-99 */
  29. #define MAX6900_REG_CT 7 /* control */
  30. /* register 8 is undocumented */
  31. #define MAX6900_REG_CENTURY 9 /* century */
  32. #define MAX6900_REG_LEN 10
  33. #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
  34. #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
  35. /*
  36. * register read/write commands
  37. */
  38. #define MAX6900_REG_CONTROL_WRITE 0x8e
  39. #define MAX6900_REG_CENTURY_WRITE 0x92
  40. #define MAX6900_REG_CENTURY_READ 0x93
  41. #define MAX6900_REG_RESERVED_READ 0x96
  42. #define MAX6900_REG_BURST_WRITE 0xbe
  43. #define MAX6900_REG_BURST_READ 0xbf
  44. #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
  45. static struct i2c_driver max6900_driver;
  46. static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
  47. {
  48. u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
  49. u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
  50. struct i2c_msg msgs[4] = {
  51. {
  52. .addr = client->addr,
  53. .flags = 0, /* write */
  54. .len = sizeof(reg_burst_read),
  55. .buf = reg_burst_read}
  56. ,
  57. {
  58. .addr = client->addr,
  59. .flags = I2C_M_RD,
  60. .len = MAX6900_BURST_LEN,
  61. .buf = buf}
  62. ,
  63. {
  64. .addr = client->addr,
  65. .flags = 0, /* write */
  66. .len = sizeof(reg_century_read),
  67. .buf = reg_century_read}
  68. ,
  69. {
  70. .addr = client->addr,
  71. .flags = I2C_M_RD,
  72. .len = sizeof(buf[MAX6900_REG_CENTURY]),
  73. .buf = &buf[MAX6900_REG_CENTURY]
  74. }
  75. };
  76. int rc;
  77. rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  78. if (rc != ARRAY_SIZE(msgs)) {
  79. dev_err(&client->dev, "%s: register read failed\n", __func__);
  80. return -EIO;
  81. }
  82. return 0;
  83. }
  84. static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
  85. {
  86. u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
  87. struct i2c_msg century_msgs[1] = {
  88. {
  89. .addr = client->addr,
  90. .flags = 0, /* write */
  91. .len = sizeof(i2c_century_buf),
  92. .buf = i2c_century_buf}
  93. };
  94. u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
  95. struct i2c_msg burst_msgs[1] = {
  96. {
  97. .addr = client->addr,
  98. .flags = 0, /* write */
  99. .len = sizeof(i2c_burst_buf),
  100. .buf = i2c_burst_buf}
  101. };
  102. int rc;
  103. /*
  104. * We have to make separate calls to i2c_transfer because of
  105. * the need to delay after each write to the chip. Also,
  106. * we write the century byte first, since we set the write-protect
  107. * bit as part of the burst write.
  108. */
  109. i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
  110. rc = i2c_transfer(client->adapter, century_msgs,
  111. ARRAY_SIZE(century_msgs));
  112. if (rc != ARRAY_SIZE(century_msgs))
  113. goto write_failed;
  114. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  115. memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
  116. rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
  117. if (rc != ARRAY_SIZE(burst_msgs))
  118. goto write_failed;
  119. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  120. return 0;
  121. write_failed:
  122. dev_err(&client->dev, "%s: register write failed\n", __func__);
  123. return -EIO;
  124. }
  125. static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  126. {
  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 rtc_valid_tm(tm);
  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
  147. max6900_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
  148. {
  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 int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
  170. {
  171. return max6900_i2c_read_time(to_i2c_client(dev), tm);
  172. }
  173. static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
  174. {
  175. return max6900_i2c_set_time(to_i2c_client(dev), tm);
  176. }
  177. static const struct rtc_class_ops max6900_rtc_ops = {
  178. .read_time = max6900_rtc_read_time,
  179. .set_time = max6900_rtc_set_time,
  180. };
  181. static int
  182. max6900_probe(struct i2c_client *client, const struct i2c_device_id *id)
  183. {
  184. struct rtc_device *rtc;
  185. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  186. return -ENODEV;
  187. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  188. rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
  189. &max6900_rtc_ops, THIS_MODULE);
  190. if (IS_ERR(rtc))
  191. return PTR_ERR(rtc);
  192. i2c_set_clientdata(client, rtc);
  193. return 0;
  194. }
  195. static struct i2c_device_id max6900_id[] = {
  196. { "max6900", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(i2c, max6900_id);
  200. static struct i2c_driver max6900_driver = {
  201. .driver = {
  202. .name = "rtc-max6900",
  203. },
  204. .probe = max6900_probe,
  205. .id_table = max6900_id,
  206. };
  207. module_i2c_driver(max6900_driver);
  208. MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
  209. MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
  210. MODULE_LICENSE("GPL");
  211. MODULE_VERSION(DRV_VERSION);