rtc-ds1307.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
  3. *
  4. * Copyright (C) 2005 James Chapman (ds1337 core)
  5. * Copyright (C) 2006 David Brownell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/i2c.h>
  15. #include <linux/string.h>
  16. #include <linux/rtc.h>
  17. #include <linux/bcd.h>
  18. /* We can't determine type by probing, but if we expect pre-Linux code
  19. * to have set the chip up as a clock (turning on the oscillator and
  20. * setting the date and time), Linux can ignore the non-clock features.
  21. * That's a natural job for a factory or repair bench.
  22. *
  23. * If the I2C "force" mechanism is used, we assume the chip is a ds1337.
  24. * (Much better would be board-specific tables of I2C devices, along with
  25. * the platform_data drivers would use to sort such issues out.)
  26. */
  27. enum ds_type {
  28. unknown = 0,
  29. ds_1307, /* or ds1338, ... */
  30. ds_1337, /* or ds1339, ... */
  31. ds_1340, /* or st m41t00, ... */
  32. // rs5c372 too? different address...
  33. };
  34. static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
  35. I2C_CLIENT_INSMOD;
  36. /* RTC registers don't differ much, except for the century flag */
  37. #define DS1307_REG_SECS 0x00 /* 00-59 */
  38. # define DS1307_BIT_CH 0x80
  39. #define DS1307_REG_MIN 0x01 /* 00-59 */
  40. #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
  41. # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
  42. # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
  43. #define DS1307_REG_WDAY 0x03 /* 01-07 */
  44. #define DS1307_REG_MDAY 0x04 /* 01-31 */
  45. #define DS1307_REG_MONTH 0x05 /* 01-12 */
  46. # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
  47. #define DS1307_REG_YEAR 0x06 /* 00-99 */
  48. /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  49. * start at 7, and they differ a lot. Only control and status matter for RTC;
  50. * be careful using them.
  51. */
  52. #define DS1307_REG_CONTROL 0x07
  53. # define DS1307_BIT_OUT 0x80
  54. # define DS1307_BIT_SQWE 0x10
  55. # define DS1307_BIT_RS1 0x02
  56. # define DS1307_BIT_RS0 0x01
  57. #define DS1337_REG_CONTROL 0x0e
  58. # define DS1337_BIT_nEOSC 0x80
  59. # define DS1337_BIT_RS2 0x10
  60. # define DS1337_BIT_RS1 0x08
  61. # define DS1337_BIT_INTCN 0x04
  62. # define DS1337_BIT_A2IE 0x02
  63. # define DS1337_BIT_A1IE 0x01
  64. #define DS1337_REG_STATUS 0x0f
  65. # define DS1337_BIT_OSF 0x80
  66. # define DS1337_BIT_A2I 0x02
  67. # define DS1337_BIT_A1I 0x01
  68. #define DS1339_REG_TRICKLE 0x10
  69. struct ds1307 {
  70. u8 reg_addr;
  71. u8 regs[8];
  72. enum ds_type type;
  73. struct i2c_msg msg[2];
  74. struct i2c_client client;
  75. struct rtc_device *rtc;
  76. };
  77. static int ds1307_get_time(struct device *dev, struct rtc_time *t)
  78. {
  79. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  80. int tmp;
  81. /* read the RTC registers all at once */
  82. ds1307->msg[1].flags = I2C_M_RD;
  83. ds1307->msg[1].len = 7;
  84. tmp = i2c_transfer(ds1307->client.adapter, ds1307->msg, 2);
  85. if (tmp != 2) {
  86. dev_err(dev, "%s error %d\n", "read", tmp);
  87. return -EIO;
  88. }
  89. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  90. "read",
  91. ds1307->regs[0], ds1307->regs[1],
  92. ds1307->regs[2], ds1307->regs[3],
  93. ds1307->regs[4], ds1307->regs[5],
  94. ds1307->regs[6]);
  95. t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
  96. t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  97. tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
  98. t->tm_hour = BCD2BIN(tmp);
  99. t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
  100. t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  101. tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
  102. t->tm_mon = BCD2BIN(tmp) - 1;
  103. /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
  104. t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
  105. dev_dbg(dev, "%s secs=%d, mins=%d, "
  106. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  107. "read", t->tm_sec, t->tm_min,
  108. t->tm_hour, t->tm_mday,
  109. t->tm_mon, t->tm_year, t->tm_wday);
  110. return 0;
  111. }
  112. static int ds1307_set_time(struct device *dev, struct rtc_time *t)
  113. {
  114. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  115. int result;
  116. int tmp;
  117. u8 *buf = ds1307->regs;
  118. dev_dbg(dev, "%s secs=%d, mins=%d, "
  119. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  120. "write", t->tm_sec, t->tm_min,
  121. t->tm_hour, t->tm_mday,
  122. t->tm_mon, t->tm_year, t->tm_wday);
  123. *buf++ = 0; /* first register addr */
  124. buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
  125. buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
  126. buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
  127. buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
  128. buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
  129. buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
  130. /* assume 20YY not 19YY */
  131. tmp = t->tm_year - 100;
  132. buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
  133. if (ds1307->type == ds_1337)
  134. buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
  135. else if (ds1307->type == ds_1340)
  136. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
  137. | DS1340_BIT_CENTURY;
  138. ds1307->msg[1].flags = 0;
  139. ds1307->msg[1].len = 8;
  140. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  141. "write", buf[0], buf[1], buf[2], buf[3],
  142. buf[4], buf[5], buf[6]);
  143. result = i2c_transfer(ds1307->client.adapter, &ds1307->msg[1], 1);
  144. if (result != 1) {
  145. dev_err(dev, "%s error %d\n", "write", tmp);
  146. return -EIO;
  147. }
  148. return 0;
  149. }
  150. static const struct rtc_class_ops ds13xx_rtc_ops = {
  151. .read_time = ds1307_get_time,
  152. .set_time = ds1307_set_time,
  153. };
  154. static struct i2c_driver ds1307_driver;
  155. static int __devinit
  156. ds1307_detect(struct i2c_adapter *adapter, int address, int kind)
  157. {
  158. struct ds1307 *ds1307;
  159. int err = -ENODEV;
  160. struct i2c_client *client;
  161. int tmp;
  162. if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) {
  163. err = -ENOMEM;
  164. goto exit;
  165. }
  166. client = &ds1307->client;
  167. client->addr = address;
  168. client->adapter = adapter;
  169. client->driver = &ds1307_driver;
  170. client->flags = 0;
  171. i2c_set_clientdata(client, ds1307);
  172. ds1307->msg[0].addr = client->addr;
  173. ds1307->msg[0].flags = 0;
  174. ds1307->msg[0].len = 1;
  175. ds1307->msg[0].buf = &ds1307->reg_addr;
  176. ds1307->msg[1].addr = client->addr;
  177. ds1307->msg[1].flags = I2C_M_RD;
  178. ds1307->msg[1].len = sizeof(ds1307->regs);
  179. ds1307->msg[1].buf = ds1307->regs;
  180. /* HACK: "force" implies "needs ds1337-style-oscillator setup" */
  181. if (kind >= 0) {
  182. ds1307->type = ds_1337;
  183. ds1307->reg_addr = DS1337_REG_CONTROL;
  184. ds1307->msg[1].len = 2;
  185. tmp = i2c_transfer(client->adapter, ds1307->msg, 2);
  186. if (tmp != 2) {
  187. pr_debug("read error %d\n", tmp);
  188. err = -EIO;
  189. goto exit_free;
  190. }
  191. ds1307->reg_addr = 0;
  192. ds1307->msg[1].len = sizeof(ds1307->regs);
  193. /* oscillator is off; need to turn it on */
  194. if ((ds1307->regs[0] & DS1337_BIT_nEOSC)
  195. || (ds1307->regs[1] & DS1337_BIT_OSF)) {
  196. printk(KERN_ERR "no ds1337 oscillator code\n");
  197. goto exit_free;
  198. }
  199. } else
  200. ds1307->type = ds_1307;
  201. read_rtc:
  202. /* read RTC registers */
  203. tmp = i2c_transfer(client->adapter, ds1307->msg, 2);
  204. if (tmp != 2) {
  205. pr_debug("read error %d\n", tmp);
  206. err = -EIO;
  207. goto exit_free;
  208. }
  209. /* minimal sanity checking; some chips (like DS1340) don't
  210. * specify the extra bits as must-be-zero, but there are
  211. * still a few values that are clearly out-of-range.
  212. */
  213. tmp = ds1307->regs[DS1307_REG_SECS];
  214. if (tmp & DS1307_BIT_CH) {
  215. if (ds1307->type && ds1307->type != ds_1307) {
  216. pr_debug("not a ds1307?\n");
  217. goto exit_free;
  218. }
  219. ds1307->type = ds_1307;
  220. /* this partial initialization should work for ds1307,
  221. * ds1338, ds1340, st m41t00, and more.
  222. */
  223. dev_warn(&client->dev, "oscillator started; SET TIME!\n");
  224. i2c_smbus_write_byte_data(client, 0, 0);
  225. goto read_rtc;
  226. }
  227. tmp = BCD2BIN(tmp & 0x7f);
  228. if (tmp > 60)
  229. goto exit_free;
  230. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  231. if (tmp > 60)
  232. goto exit_free;
  233. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  234. if (tmp == 0 || tmp > 31)
  235. goto exit_free;
  236. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
  237. if (tmp == 0 || tmp > 12)
  238. goto exit_free;
  239. /* force into in 24 hour mode (most chips) or
  240. * disable century bit (ds1340)
  241. */
  242. tmp = ds1307->regs[DS1307_REG_HOUR];
  243. if (tmp & (1 << 6)) {
  244. if (tmp & (1 << 5))
  245. tmp = BCD2BIN(tmp & 0x1f) + 12;
  246. else
  247. tmp = BCD2BIN(tmp);
  248. i2c_smbus_write_byte_data(client,
  249. DS1307_REG_HOUR,
  250. BIN2BCD(tmp));
  251. }
  252. /* FIXME chips like 1337 can generate alarm irqs too; those are
  253. * worth exposing through the API (especially when the irq is
  254. * wakeup-capable).
  255. */
  256. switch (ds1307->type) {
  257. case unknown:
  258. strlcpy(client->name, "unknown", I2C_NAME_SIZE);
  259. break;
  260. case ds_1307:
  261. strlcpy(client->name, "ds1307", I2C_NAME_SIZE);
  262. break;
  263. case ds_1337:
  264. strlcpy(client->name, "ds1337", I2C_NAME_SIZE);
  265. break;
  266. case ds_1340:
  267. strlcpy(client->name, "ds1340", I2C_NAME_SIZE);
  268. break;
  269. }
  270. /* Tell the I2C layer a new client has arrived */
  271. if ((err = i2c_attach_client(client)))
  272. goto exit_free;
  273. ds1307->rtc = rtc_device_register(client->name, &client->dev,
  274. &ds13xx_rtc_ops, THIS_MODULE);
  275. if (IS_ERR(ds1307->rtc)) {
  276. err = PTR_ERR(ds1307->rtc);
  277. dev_err(&client->dev,
  278. "unable to register the class device\n");
  279. goto exit_detach;
  280. }
  281. return 0;
  282. exit_detach:
  283. i2c_detach_client(client);
  284. exit_free:
  285. kfree(ds1307);
  286. exit:
  287. return err;
  288. }
  289. static int __devinit
  290. ds1307_attach_adapter(struct i2c_adapter *adapter)
  291. {
  292. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  293. return 0;
  294. return i2c_probe(adapter, &addr_data, ds1307_detect);
  295. }
  296. static int __devexit ds1307_detach_client(struct i2c_client *client)
  297. {
  298. int err;
  299. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  300. rtc_device_unregister(ds1307->rtc);
  301. if ((err = i2c_detach_client(client)))
  302. return err;
  303. kfree(ds1307);
  304. return 0;
  305. }
  306. static struct i2c_driver ds1307_driver = {
  307. .driver = {
  308. .name = "ds1307",
  309. .owner = THIS_MODULE,
  310. },
  311. .attach_adapter = ds1307_attach_adapter,
  312. .detach_client = __devexit_p(ds1307_detach_client),
  313. };
  314. static int __init ds1307_init(void)
  315. {
  316. return i2c_add_driver(&ds1307_driver);
  317. }
  318. module_init(ds1307_init);
  319. static void __exit ds1307_exit(void)
  320. {
  321. i2c_del_driver(&ds1307_driver);
  322. }
  323. module_exit(ds1307_exit);
  324. MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
  325. MODULE_LICENSE("GPL");