rtc-ds1307.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. * This is currently a simple no-alarms driver. If your board has the
  24. * alarm irq wired up on a ds1337 or ds1339, and you want to use that,
  25. * then look at the rtc-rs5c372 driver for code to steal...
  26. *
  27. * If the I2C "force" mechanism is used, we assume the chip is a ds1337.
  28. * (Much better would be board-specific tables of I2C devices, along with
  29. * the platform_data drivers would use to sort such issues out.)
  30. */
  31. enum ds_type {
  32. unknown = 0,
  33. ds_1307,
  34. ds_1337,
  35. ds_1338,
  36. ds_1339,
  37. ds_1340,
  38. m41t00,
  39. // rs5c372 too? different address...
  40. };
  41. static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
  42. I2C_CLIENT_INSMOD;
  43. /* RTC registers don't differ much, except for the century flag */
  44. #define DS1307_REG_SECS 0x00 /* 00-59 */
  45. # define DS1307_BIT_CH 0x80
  46. # define DS1340_BIT_nEOSC 0x80
  47. #define DS1307_REG_MIN 0x01 /* 00-59 */
  48. #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
  49. # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
  50. # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
  51. #define DS1307_REG_WDAY 0x03 /* 01-07 */
  52. #define DS1307_REG_MDAY 0x04 /* 01-31 */
  53. #define DS1307_REG_MONTH 0x05 /* 01-12 */
  54. # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
  55. #define DS1307_REG_YEAR 0x06 /* 00-99 */
  56. /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  57. * start at 7, and they differ a LOT. Only control and status matter for
  58. * basic RTC date and time functionality; be careful using them.
  59. */
  60. #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
  61. # define DS1307_BIT_OUT 0x80
  62. # define DS1338_BIT_OSF 0x20
  63. # define DS1307_BIT_SQWE 0x10
  64. # define DS1307_BIT_RS1 0x02
  65. # define DS1307_BIT_RS0 0x01
  66. #define DS1337_REG_CONTROL 0x0e
  67. # define DS1337_BIT_nEOSC 0x80
  68. # define DS1337_BIT_RS2 0x10
  69. # define DS1337_BIT_RS1 0x08
  70. # define DS1337_BIT_INTCN 0x04
  71. # define DS1337_BIT_A2IE 0x02
  72. # define DS1337_BIT_A1IE 0x01
  73. #define DS1340_REG_CONTROL 0x07
  74. # define DS1340_BIT_OUT 0x80
  75. # define DS1340_BIT_FT 0x40
  76. # define DS1340_BIT_CALIB_SIGN 0x20
  77. # define DS1340_M_CALIBRATION 0x1f
  78. #define DS1340_REG_FLAG 0x09
  79. # define DS1340_BIT_OSF 0x80
  80. #define DS1337_REG_STATUS 0x0f
  81. # define DS1337_BIT_OSF 0x80
  82. # define DS1337_BIT_A2I 0x02
  83. # define DS1337_BIT_A1I 0x01
  84. #define DS1339_REG_TRICKLE 0x10
  85. struct ds1307 {
  86. u8 reg_addr;
  87. u8 regs[8];
  88. enum ds_type type;
  89. struct i2c_msg msg[2];
  90. struct i2c_client *client;
  91. struct i2c_client dev;
  92. struct rtc_device *rtc;
  93. };
  94. struct chip_desc {
  95. char name[9];
  96. unsigned nvram56:1;
  97. unsigned alarm:1;
  98. enum ds_type type;
  99. };
  100. static const struct chip_desc chips[] = { {
  101. .name = "ds1307",
  102. .type = ds_1307,
  103. .nvram56 = 1,
  104. }, {
  105. .name = "ds1337",
  106. .type = ds_1337,
  107. .alarm = 1,
  108. }, {
  109. .name = "ds1338",
  110. .type = ds_1338,
  111. .nvram56 = 1,
  112. }, {
  113. .name = "ds1339",
  114. .type = ds_1339,
  115. .alarm = 1,
  116. }, {
  117. .name = "ds1340",
  118. .type = ds_1340,
  119. }, {
  120. .name = "m41t00",
  121. .type = m41t00,
  122. }, };
  123. static inline const struct chip_desc *find_chip(const char *s)
  124. {
  125. unsigned i;
  126. for (i = 0; i < ARRAY_SIZE(chips); i++)
  127. if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0)
  128. return &chips[i];
  129. return NULL;
  130. }
  131. static int ds1307_get_time(struct device *dev, struct rtc_time *t)
  132. {
  133. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  134. int tmp;
  135. /* read the RTC date and time registers all at once */
  136. ds1307->msg[1].flags = I2C_M_RD;
  137. ds1307->msg[1].len = 7;
  138. tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
  139. ds1307->msg, 2);
  140. if (tmp != 2) {
  141. dev_err(dev, "%s error %d\n", "read", tmp);
  142. return -EIO;
  143. }
  144. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  145. "read",
  146. ds1307->regs[0], ds1307->regs[1],
  147. ds1307->regs[2], ds1307->regs[3],
  148. ds1307->regs[4], ds1307->regs[5],
  149. ds1307->regs[6]);
  150. t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
  151. t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  152. tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
  153. t->tm_hour = BCD2BIN(tmp);
  154. t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
  155. t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  156. tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
  157. t->tm_mon = BCD2BIN(tmp) - 1;
  158. /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
  159. t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
  160. dev_dbg(dev, "%s secs=%d, mins=%d, "
  161. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  162. "read", t->tm_sec, t->tm_min,
  163. t->tm_hour, t->tm_mday,
  164. t->tm_mon, t->tm_year, t->tm_wday);
  165. /* initial clock setting can be undefined */
  166. return rtc_valid_tm(t);
  167. }
  168. static int ds1307_set_time(struct device *dev, struct rtc_time *t)
  169. {
  170. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  171. int result;
  172. int tmp;
  173. u8 *buf = ds1307->regs;
  174. dev_dbg(dev, "%s secs=%d, mins=%d, "
  175. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  176. "write", t->tm_sec, t->tm_min,
  177. t->tm_hour, t->tm_mday,
  178. t->tm_mon, t->tm_year, t->tm_wday);
  179. *buf++ = 0; /* first register addr */
  180. buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
  181. buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
  182. buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
  183. buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
  184. buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
  185. buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
  186. /* assume 20YY not 19YY */
  187. tmp = t->tm_year - 100;
  188. buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
  189. switch (ds1307->type) {
  190. case ds_1337:
  191. case ds_1339:
  192. buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
  193. break;
  194. case ds_1340:
  195. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
  196. | DS1340_BIT_CENTURY;
  197. break;
  198. default:
  199. break;
  200. }
  201. ds1307->msg[1].flags = 0;
  202. ds1307->msg[1].len = 8;
  203. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  204. "write", buf[0], buf[1], buf[2], buf[3],
  205. buf[4], buf[5], buf[6]);
  206. result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
  207. &ds1307->msg[1], 1);
  208. if (result != 1) {
  209. dev_err(dev, "%s error %d\n", "write", tmp);
  210. return -EIO;
  211. }
  212. return 0;
  213. }
  214. static const struct rtc_class_ops ds13xx_rtc_ops = {
  215. .read_time = ds1307_get_time,
  216. .set_time = ds1307_set_time,
  217. };
  218. static struct i2c_driver ds1307_driver;
  219. static int __devinit
  220. ds1307_detect(struct i2c_adapter *adapter, int address, int kind)
  221. {
  222. struct ds1307 *ds1307;
  223. int err = -ENODEV;
  224. struct i2c_client *client;
  225. int tmp;
  226. const struct chip_desc *chip;
  227. if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) {
  228. err = -ENOMEM;
  229. goto exit;
  230. }
  231. /* REVISIT: pending driver model conversion, set up "client"
  232. * ourselves, and use a hack to determine the RTC type (instead
  233. * of reading the client->name we're given)
  234. */
  235. client = &ds1307->dev;
  236. client->addr = address;
  237. client->adapter = adapter;
  238. client->driver = &ds1307_driver;
  239. /* HACK: "force" implies "needs ds1337-style-oscillator setup", and
  240. * that's the only kind of chip setup we'll know about. Until the
  241. * driver model conversion, here's where to add any board-specific
  242. * code to say what kind of chip is present...
  243. */
  244. if (kind >= 0)
  245. chip = find_chip("ds1337");
  246. else
  247. chip = find_chip("ds1307");
  248. strlcpy(client->name, chip->name, I2C_NAME_SIZE);
  249. ds1307->client = client;
  250. i2c_set_clientdata(client, ds1307);
  251. ds1307->msg[0].addr = client->addr;
  252. ds1307->msg[0].flags = 0;
  253. ds1307->msg[0].len = 1;
  254. ds1307->msg[0].buf = &ds1307->reg_addr;
  255. ds1307->msg[1].addr = client->addr;
  256. ds1307->msg[1].flags = I2C_M_RD;
  257. ds1307->msg[1].len = sizeof(ds1307->regs);
  258. ds1307->msg[1].buf = ds1307->regs;
  259. ds1307->type = chip->type;
  260. switch (ds1307->type) {
  261. case ds_1337:
  262. case ds_1339:
  263. ds1307->reg_addr = DS1337_REG_CONTROL;
  264. ds1307->msg[1].len = 2;
  265. /* get registers that the "rtc" read below won't read... */
  266. tmp = i2c_transfer(adapter, ds1307->msg, 2);
  267. if (tmp != 2) {
  268. pr_debug("read error %d\n", tmp);
  269. err = -EIO;
  270. goto exit_free;
  271. }
  272. ds1307->reg_addr = 0;
  273. ds1307->msg[1].len = sizeof(ds1307->regs);
  274. /* oscillator off? turn it on, so clock can tick. */
  275. if (ds1307->regs[0] & DS1337_BIT_nEOSC)
  276. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
  277. ds1307->regs[0] & ~DS1337_BIT_nEOSC);
  278. /* oscillator fault? clear flag, and warn */
  279. if (ds1307->regs[1] & DS1337_BIT_OSF) {
  280. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
  281. ds1307->regs[1] & ~DS1337_BIT_OSF);
  282. dev_warn(&client->dev, "SET TIME!\n");
  283. }
  284. break;
  285. default:
  286. break;
  287. }
  288. read_rtc:
  289. /* read RTC registers */
  290. tmp = i2c_transfer(adapter, ds1307->msg, 2);
  291. if (tmp != 2) {
  292. pr_debug("read error %d\n", tmp);
  293. err = -EIO;
  294. goto exit_free;
  295. }
  296. /* minimal sanity checking; some chips (like DS1340) don't
  297. * specify the extra bits as must-be-zero, but there are
  298. * still a few values that are clearly out-of-range.
  299. */
  300. tmp = ds1307->regs[DS1307_REG_SECS];
  301. switch (ds1307->type) {
  302. case ds_1340:
  303. /* FIXME read register with DS1340_BIT_OSF, use that to
  304. * trigger the "set time" warning (*after* restarting the
  305. * oscillator!) instead of this weaker ds1307/m41t00 test.
  306. */
  307. case ds_1307:
  308. case m41t00:
  309. /* clock halted? turn it on, so clock can tick. */
  310. if (tmp & DS1307_BIT_CH) {
  311. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  312. dev_warn(&client->dev, "SET TIME!\n");
  313. goto read_rtc;
  314. }
  315. break;
  316. case ds_1338:
  317. /* clock halted? turn it on, so clock can tick. */
  318. if (tmp & DS1307_BIT_CH)
  319. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  320. /* oscillator fault? clear flag, and warn */
  321. if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
  322. i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
  323. ds1307->regs[DS1337_REG_CONTROL]
  324. & ~DS1338_BIT_OSF);
  325. dev_warn(&client->dev, "SET TIME!\n");
  326. goto read_rtc;
  327. }
  328. break;
  329. default:
  330. break;
  331. }
  332. tmp = ds1307->regs[DS1307_REG_SECS];
  333. tmp = BCD2BIN(tmp & 0x7f);
  334. if (tmp > 60)
  335. goto exit_free;
  336. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  337. if (tmp > 60)
  338. goto exit_free;
  339. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  340. if (tmp == 0 || tmp > 31)
  341. goto exit_free;
  342. tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
  343. if (tmp == 0 || tmp > 12)
  344. goto exit_free;
  345. /* force into in 24 hour mode (most chips) or
  346. * disable century bit (ds1340)
  347. *
  348. * REVISIT forcing 24 hour mode can prevent multi-master
  349. * configs from sharing this RTC ... don't do this.
  350. * The clock needs to be reset after changing it, too...
  351. */
  352. tmp = ds1307->regs[DS1307_REG_HOUR];
  353. if (tmp & (1 << 6)) {
  354. if (tmp & (1 << 5))
  355. tmp = BCD2BIN(tmp & 0x1f) + 12;
  356. else
  357. tmp = BCD2BIN(tmp);
  358. i2c_smbus_write_byte_data(client,
  359. DS1307_REG_HOUR,
  360. BIN2BCD(tmp));
  361. }
  362. /* Tell the I2C layer a new client has arrived */
  363. if ((err = i2c_attach_client(client)))
  364. goto exit_free;
  365. ds1307->rtc = rtc_device_register(client->name, &client->dev,
  366. &ds13xx_rtc_ops, THIS_MODULE);
  367. if (IS_ERR(ds1307->rtc)) {
  368. err = PTR_ERR(ds1307->rtc);
  369. dev_err(&client->dev,
  370. "unable to register the class device\n");
  371. goto exit_detach;
  372. }
  373. return 0;
  374. exit_detach:
  375. i2c_detach_client(client);
  376. exit_free:
  377. kfree(ds1307);
  378. exit:
  379. return err;
  380. }
  381. static int __devinit
  382. ds1307_attach_adapter(struct i2c_adapter *adapter)
  383. {
  384. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  385. return 0;
  386. return i2c_probe(adapter, &addr_data, ds1307_detect);
  387. }
  388. static int __devexit ds1307_detach_client(struct i2c_client *client)
  389. {
  390. int err;
  391. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  392. rtc_device_unregister(ds1307->rtc);
  393. if ((err = i2c_detach_client(client)))
  394. return err;
  395. kfree(ds1307);
  396. return 0;
  397. }
  398. static struct i2c_driver ds1307_driver = {
  399. .driver = {
  400. .name = "ds1307",
  401. .owner = THIS_MODULE,
  402. },
  403. .attach_adapter = ds1307_attach_adapter,
  404. .detach_client = __devexit_p(ds1307_detach_client),
  405. };
  406. static int __init ds1307_init(void)
  407. {
  408. return i2c_add_driver(&ds1307_driver);
  409. }
  410. module_init(ds1307_init);
  411. static void __exit ds1307_exit(void)
  412. {
  413. i2c_del_driver(&ds1307_driver);
  414. }
  415. module_exit(ds1307_exit);
  416. MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
  417. MODULE_LICENSE("GPL");