rtc-max6900.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /* register 8 is undocumented */
  32. #define MAX6900_REG_CENTURY 9 /* century */
  33. #define MAX6900_REG_LEN 10
  34. #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
  35. #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
  36. /*
  37. * register read/write commands
  38. */
  39. #define MAX6900_REG_CONTROL_WRITE 0x8e
  40. #define MAX6900_REG_CENTURY_WRITE 0x92
  41. #define MAX6900_REG_CENTURY_READ 0x93
  42. #define MAX6900_REG_RESERVED_READ 0x96
  43. #define MAX6900_REG_BURST_WRITE 0xbe
  44. #define MAX6900_REG_BURST_READ 0xbf
  45. #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
  46. #define MAX6900_I2C_ADDR 0xa0
  47. static unsigned short normal_i2c[] = {
  48. MAX6900_I2C_ADDR >> 1,
  49. I2C_CLIENT_END
  50. };
  51. I2C_CLIENT_INSMOD; /* defines addr_data */
  52. static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
  53. static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
  54. {
  55. u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
  56. u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
  57. struct i2c_msg msgs[4] = {
  58. {
  59. .addr = client->addr,
  60. .flags = 0, /* write */
  61. .len = sizeof(reg_burst_read),
  62. .buf = reg_burst_read
  63. },
  64. {
  65. .addr = client->addr,
  66. .flags = I2C_M_RD,
  67. .len = MAX6900_BURST_LEN,
  68. .buf = buf
  69. },
  70. {
  71. .addr = client->addr,
  72. .flags = 0, /* write */
  73. .len = sizeof(reg_century_read),
  74. .buf = reg_century_read
  75. },
  76. {
  77. .addr = client->addr,
  78. .flags = I2C_M_RD,
  79. .len = sizeof(buf[MAX6900_REG_CENTURY]),
  80. .buf = &buf[MAX6900_REG_CENTURY]
  81. }
  82. };
  83. int rc;
  84. rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  85. if (rc != ARRAY_SIZE(msgs)) {
  86. dev_err(&client->dev, "%s: register read failed\n",
  87. __FUNCTION__);
  88. return -EIO;
  89. }
  90. return 0;
  91. }
  92. static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
  93. {
  94. u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
  95. struct i2c_msg century_msgs[1] = {
  96. {
  97. .addr = client->addr,
  98. .flags = 0, /* write */
  99. .len = sizeof(i2c_century_buf),
  100. .buf = i2c_century_buf
  101. }
  102. };
  103. u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
  104. struct i2c_msg burst_msgs[1] = {
  105. {
  106. .addr = client->addr,
  107. .flags = 0, /* write */
  108. .len = sizeof(i2c_burst_buf),
  109. .buf = i2c_burst_buf
  110. }
  111. };
  112. int rc;
  113. /*
  114. * We have to make separate calls to i2c_transfer because of
  115. * the need to delay after each write to the chip. Also,
  116. * we write the century byte first, since we set the write-protect
  117. * bit as part of the burst write.
  118. */
  119. i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
  120. rc = i2c_transfer(client->adapter, century_msgs,
  121. ARRAY_SIZE(century_msgs));
  122. if (rc != ARRAY_SIZE(century_msgs))
  123. goto write_failed;
  124. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  125. memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
  126. rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
  127. if (rc != ARRAY_SIZE(burst_msgs))
  128. goto write_failed;
  129. msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
  130. return 0;
  131. write_failed:
  132. dev_err(&client->dev, "%s: register write failed\n",
  133. __FUNCTION__);
  134. return -EIO;
  135. }
  136. static int max6900_i2c_validate_client(struct i2c_client *client)
  137. {
  138. u8 regs[MAX6900_REG_LEN];
  139. u8 zero_mask[] = {
  140. 0x80, /* seconds */
  141. 0x80, /* minutes */
  142. 0x40, /* hours */
  143. 0xc0, /* day of month */
  144. 0xe0, /* month */
  145. 0xf8, /* day of week */
  146. 0x00, /* year */
  147. 0x7f, /* control */
  148. };
  149. int i;
  150. int rc;
  151. int reserved;
  152. reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
  153. if (reserved != 0x07)
  154. return -ENODEV;
  155. rc = max6900_i2c_read_regs(client, regs);
  156. if (rc < 0)
  157. return rc;
  158. for (i = 0; i < ARRAY_SIZE(zero_mask); ++i) {
  159. if (regs[i] & zero_mask[i])
  160. return -ENODEV;
  161. }
  162. return 0;
  163. }
  164. static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  165. {
  166. int rc;
  167. u8 regs[MAX6900_REG_LEN];
  168. rc = max6900_i2c_read_regs(client, regs);
  169. if (rc < 0)
  170. return rc;
  171. tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]);
  172. tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]);
  173. tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f);
  174. tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]);
  175. tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1;
  176. tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) +
  177. BCD2BIN(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
  178. tm->tm_wday = BCD2BIN(regs[MAX6900_REG_DW]);
  179. return 0;
  180. }
  181. static int max6900_i2c_clear_write_protect(struct i2c_client *client)
  182. {
  183. int rc;
  184. rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
  185. if (rc < 0) {
  186. dev_err(&client->dev, "%s: control register write failed\n",
  187. __FUNCTION__);
  188. return -EIO;
  189. }
  190. return 0;
  191. }
  192. static int max6900_i2c_set_time(struct i2c_client *client,
  193. struct rtc_time const *tm)
  194. {
  195. u8 regs[MAX6900_REG_LEN];
  196. int rc;
  197. rc = max6900_i2c_clear_write_protect(client);
  198. if (rc < 0)
  199. return rc;
  200. regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec);
  201. regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min);
  202. regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour);
  203. regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday);
  204. regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1);
  205. regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday);
  206. regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year % 100);
  207. regs[MAX6900_REG_CENTURY] = BIN2BCD((tm->tm_year + 1900) / 100);
  208. /* set write protect */
  209. regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
  210. rc = max6900_i2c_write_regs(client, regs);
  211. if (rc < 0)
  212. return rc;
  213. return 0;
  214. }
  215. static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
  216. {
  217. return max6900_i2c_read_time(to_i2c_client(dev), tm);
  218. }
  219. static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
  220. {
  221. return max6900_i2c_set_time(to_i2c_client(dev), tm);
  222. }
  223. static int max6900_attach_adapter(struct i2c_adapter *adapter)
  224. {
  225. return i2c_probe(adapter, &addr_data, max6900_probe);
  226. }
  227. static int max6900_detach_client(struct i2c_client *client)
  228. {
  229. struct rtc_device *const rtc = i2c_get_clientdata(client);
  230. if (rtc)
  231. rtc_device_unregister(rtc);
  232. return i2c_detach_client(client);
  233. }
  234. static struct i2c_driver max6900_driver = {
  235. .driver = {
  236. .name = DRV_NAME,
  237. },
  238. .id = I2C_DRIVERID_MAX6900,
  239. .attach_adapter = max6900_attach_adapter,
  240. .detach_client = max6900_detach_client,
  241. };
  242. static const struct rtc_class_ops max6900_rtc_ops = {
  243. .read_time = max6900_rtc_read_time,
  244. .set_time = max6900_rtc_set_time,
  245. };
  246. static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind)
  247. {
  248. int rc = 0;
  249. struct i2c_client *client = NULL;
  250. struct rtc_device *rtc = NULL;
  251. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  252. rc = -ENODEV;
  253. goto failout;
  254. }
  255. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  256. if (client == NULL) {
  257. rc = -ENOMEM;
  258. goto failout;
  259. }
  260. client->addr = addr;
  261. client->adapter = adapter;
  262. client->driver = &max6900_driver;
  263. strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
  264. if (kind < 0) {
  265. rc = max6900_i2c_validate_client(client);
  266. if (rc < 0)
  267. goto failout;
  268. }
  269. rc = i2c_attach_client(client);
  270. if (rc < 0)
  271. goto failout;
  272. dev_info(&client->dev,
  273. "chip found, driver version " DRV_VERSION "\n");
  274. rtc = rtc_device_register(max6900_driver.driver.name,
  275. &client->dev,
  276. &max6900_rtc_ops, THIS_MODULE);
  277. if (IS_ERR(rtc)) {
  278. rc = PTR_ERR(rtc);
  279. goto failout_detach;
  280. }
  281. i2c_set_clientdata(client, rtc);
  282. return 0;
  283. failout_detach:
  284. i2c_detach_client(client);
  285. failout:
  286. kfree(client);
  287. return rc;
  288. }
  289. static int __init max6900_init(void)
  290. {
  291. return i2c_add_driver(&max6900_driver);
  292. }
  293. static void __exit max6900_exit(void)
  294. {
  295. i2c_del_driver(&max6900_driver);
  296. }
  297. MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
  298. MODULE_LICENSE("GPL");
  299. MODULE_VERSION(DRV_VERSION);
  300. module_init(max6900_init);
  301. module_exit(max6900_exit);