rtc-max6900.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_NAME "max6900"
  19. #define DRV_VERSION "0.1"
  20. /*
  21. * register indices
  22. */
  23. #define MAX6900_REG_SC 0 /* seconds 00-59 */
  24. #define MAX6900_REG_MN 1 /* minutes 00-59 */
  25. #define MAX6900_REG_HR 2 /* hours 00-23 */
  26. #define MAX6900_REG_DT 3 /* day of month 00-31 */
  27. #define MAX6900_REG_MO 4 /* month 01-12 */
  28. #define MAX6900_REG_DW 5 /* day of week 1-7 */
  29. #define MAX6900_REG_YR 6 /* year 00-99 */
  30. #define MAX6900_REG_CT 7 /* control */
  31. #define MAX6900_REG_LEN 8
  32. #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
  33. /*
  34. * register read/write commands
  35. */
  36. #define MAX6900_REG_CONTROL_WRITE 0x8e
  37. #define MAX6900_REG_BURST_READ 0xbf
  38. #define MAX6900_REG_BURST_WRITE 0xbe
  39. #define MAX6900_REG_RESERVED_READ 0x96
  40. #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
  41. #define MAX6900_I2C_ADDR 0xa0
  42. static unsigned short normal_i2c[] = {
  43. MAX6900_I2C_ADDR >> 1,
  44. I2C_CLIENT_END
  45. };
  46. I2C_CLIENT_INSMOD; /* defines addr_data */
  47. static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
  48. static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
  49. {
  50. u8 reg_addr[1] = { MAX6900_REG_BURST_READ };
  51. struct i2c_msg msgs[2] = {
  52. {
  53. .addr = client->addr,
  54. .flags = 0, /* write */
  55. .len = sizeof(reg_addr),
  56. .buf = reg_addr
  57. },
  58. {
  59. .addr = client->addr,
  60. .flags = I2C_M_RD,
  61. .len = MAX6900_REG_LEN,
  62. .buf = buf
  63. }
  64. };
  65. int rc;
  66. rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  67. if (rc != ARRAY_SIZE(msgs)) {
  68. dev_err(&client->dev, "%s: register read failed\n",
  69. __FUNCTION__);
  70. return -EIO;
  71. }
  72. return 0;
  73. }
  74. static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
  75. {
  76. u8 i2c_buf[MAX6900_REG_LEN + 1] = { MAX6900_REG_BURST_WRITE };
  77. struct i2c_msg msgs[1] = {
  78. {
  79. .addr = client->addr,
  80. .flags = 0, /* write */
  81. .len = MAX6900_REG_LEN + 1,
  82. .buf = i2c_buf
  83. }
  84. };
  85. int rc;
  86. memcpy(&i2c_buf[1], buf, MAX6900_REG_LEN);
  87. rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  88. if (rc != ARRAY_SIZE(msgs)) {
  89. dev_err(&client->dev, "%s: register write failed\n",
  90. __FUNCTION__);
  91. return -EIO;
  92. }
  93. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  94. return 0;
  95. }
  96. static int max6900_i2c_validate_client(struct i2c_client *client)
  97. {
  98. u8 regs[MAX6900_REG_LEN];
  99. u8 zero_mask[MAX6900_REG_LEN] = {
  100. 0x80, /* seconds */
  101. 0x80, /* minutes */
  102. 0x40, /* hours */
  103. 0xc0, /* day of month */
  104. 0xe0, /* month */
  105. 0xf8, /* day of week */
  106. 0x00, /* year */
  107. 0x7f, /* control */
  108. };
  109. int i;
  110. int rc;
  111. int reserved;
  112. reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
  113. if (reserved != 0x07)
  114. return -ENODEV;
  115. rc = max6900_i2c_read_regs(client, regs);
  116. if (rc < 0)
  117. return rc;
  118. for (i = 0; i < MAX6900_REG_LEN; ++i) {
  119. if (regs[i] & zero_mask[i])
  120. return -ENODEV;
  121. }
  122. return 0;
  123. }
  124. static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  125. {
  126. int rc;
  127. u8 regs[MAX6900_REG_LEN];
  128. rc = max6900_i2c_read_regs(client, regs);
  129. if (rc < 0)
  130. return rc;
  131. tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]);
  132. tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]);
  133. tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f);
  134. tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]);
  135. tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1;
  136. tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) + 100;
  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. int rc;
  143. rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
  144. if (rc < 0) {
  145. dev_err(&client->dev, "%s: control register write failed\n",
  146. __FUNCTION__);
  147. return -EIO;
  148. }
  149. return 0;
  150. }
  151. static int max6900_i2c_set_time(struct i2c_client *client,
  152. struct rtc_time const *tm)
  153. {
  154. u8 regs[MAX6900_REG_LEN];
  155. int rc;
  156. rc = max6900_i2c_clear_write_protect(client);
  157. if (rc < 0)
  158. return rc;
  159. regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec);
  160. regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min);
  161. regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour);
  162. regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday);
  163. regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1);
  164. regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year - 100);
  165. regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday);
  166. regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; /* set write protect */
  167. rc = max6900_i2c_write_regs(client, regs);
  168. if (rc < 0)
  169. return rc;
  170. return 0;
  171. }
  172. static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
  173. {
  174. return max6900_i2c_read_time(to_i2c_client(dev), tm);
  175. }
  176. static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
  177. {
  178. return max6900_i2c_set_time(to_i2c_client(dev), tm);
  179. }
  180. static int max6900_attach_adapter(struct i2c_adapter *adapter)
  181. {
  182. return i2c_probe(adapter, &addr_data, max6900_probe);
  183. }
  184. static int max6900_detach_client(struct i2c_client *client)
  185. {
  186. struct rtc_device *const rtc = i2c_get_clientdata(client);
  187. if (rtc)
  188. rtc_device_unregister(rtc);
  189. return i2c_detach_client(client);
  190. }
  191. static struct i2c_driver max6900_driver = {
  192. .driver = {
  193. .name = DRV_NAME,
  194. },
  195. .id = I2C_DRIVERID_MAX6900,
  196. .attach_adapter = max6900_attach_adapter,
  197. .detach_client = max6900_detach_client,
  198. };
  199. static const struct rtc_class_ops max6900_rtc_ops = {
  200. .read_time = max6900_rtc_read_time,
  201. .set_time = max6900_rtc_set_time,
  202. };
  203. static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind)
  204. {
  205. int rc = 0;
  206. struct i2c_client *client = NULL;
  207. struct rtc_device *rtc = NULL;
  208. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  209. rc = -ENODEV;
  210. goto failout;
  211. }
  212. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  213. if (client == NULL) {
  214. rc = -ENOMEM;
  215. goto failout;
  216. }
  217. client->addr = addr;
  218. client->adapter = adapter;
  219. client->driver = &max6900_driver;
  220. strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
  221. if (kind < 0) {
  222. rc = max6900_i2c_validate_client(client);
  223. if (rc < 0)
  224. goto failout;
  225. }
  226. rc = i2c_attach_client(client);
  227. if (rc < 0)
  228. goto failout;
  229. dev_info(&client->dev,
  230. "chip found, driver version " DRV_VERSION "\n");
  231. rtc = rtc_device_register(max6900_driver.driver.name,
  232. &client->dev,
  233. &max6900_rtc_ops, THIS_MODULE);
  234. if (IS_ERR(rtc)) {
  235. rc = PTR_ERR(rtc);
  236. goto failout_detach;
  237. }
  238. i2c_set_clientdata(client, rtc);
  239. return 0;
  240. failout_detach:
  241. i2c_detach_client(client);
  242. failout:
  243. kfree(client);
  244. return rc;
  245. }
  246. static int __init max6900_init(void)
  247. {
  248. return i2c_add_driver(&max6900_driver);
  249. }
  250. static void __exit max6900_exit(void)
  251. {
  252. i2c_del_driver(&max6900_driver);
  253. }
  254. MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
  255. MODULE_LICENSE("GPL");
  256. MODULE_VERSION(DRV_VERSION);
  257. module_init(max6900_init);
  258. module_exit(max6900_exit);