rtc-ds1307.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. enum ds_type {
  24. ds_1307,
  25. ds_1337,
  26. ds_1338,
  27. ds_1339,
  28. ds_1340,
  29. m41t00,
  30. // rs5c372 too? different address...
  31. };
  32. /* RTC registers don't differ much, except for the century flag */
  33. #define DS1307_REG_SECS 0x00 /* 00-59 */
  34. # define DS1307_BIT_CH 0x80
  35. # define DS1340_BIT_nEOSC 0x80
  36. #define DS1307_REG_MIN 0x01 /* 00-59 */
  37. #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
  38. # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
  39. # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
  40. # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
  41. # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
  42. #define DS1307_REG_WDAY 0x03 /* 01-07 */
  43. #define DS1307_REG_MDAY 0x04 /* 01-31 */
  44. #define DS1307_REG_MONTH 0x05 /* 01-12 */
  45. # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
  46. #define DS1307_REG_YEAR 0x06 /* 00-99 */
  47. /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  48. * start at 7, and they differ a LOT. Only control and status matter for
  49. * basic RTC date and time functionality; be careful using them.
  50. */
  51. #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
  52. # define DS1307_BIT_OUT 0x80
  53. # define DS1338_BIT_OSF 0x20
  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 DS1339_BIT_BBSQI 0x20
  60. # define DS1337_BIT_RS2 0x10
  61. # define DS1337_BIT_RS1 0x08
  62. # define DS1337_BIT_INTCN 0x04
  63. # define DS1337_BIT_A2IE 0x02
  64. # define DS1337_BIT_A1IE 0x01
  65. #define DS1340_REG_CONTROL 0x07
  66. # define DS1340_BIT_OUT 0x80
  67. # define DS1340_BIT_FT 0x40
  68. # define DS1340_BIT_CALIB_SIGN 0x20
  69. # define DS1340_M_CALIBRATION 0x1f
  70. #define DS1340_REG_FLAG 0x09
  71. # define DS1340_BIT_OSF 0x80
  72. #define DS1337_REG_STATUS 0x0f
  73. # define DS1337_BIT_OSF 0x80
  74. # define DS1337_BIT_A2I 0x02
  75. # define DS1337_BIT_A1I 0x01
  76. #define DS1339_REG_ALARM1_SECS 0x07
  77. #define DS1339_REG_TRICKLE 0x10
  78. struct ds1307 {
  79. u8 regs[11];
  80. enum ds_type type;
  81. unsigned long flags;
  82. #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
  83. #define HAS_ALARM 1 /* bit 1 == irq claimed */
  84. struct i2c_client *client;
  85. struct rtc_device *rtc;
  86. struct work_struct work;
  87. };
  88. struct chip_desc {
  89. unsigned nvram56:1;
  90. unsigned alarm:1;
  91. };
  92. static const struct chip_desc chips[] = {
  93. [ds_1307] = {
  94. .nvram56 = 1,
  95. },
  96. [ds_1337] = {
  97. .alarm = 1,
  98. },
  99. [ds_1338] = {
  100. .nvram56 = 1,
  101. },
  102. [ds_1339] = {
  103. .alarm = 1,
  104. },
  105. [ds_1340] = {
  106. },
  107. [m41t00] = {
  108. }, };
  109. static const struct i2c_device_id ds1307_id[] = {
  110. { "ds1307", ds_1307 },
  111. { "ds1337", ds_1337 },
  112. { "ds1338", ds_1338 },
  113. { "ds1339", ds_1339 },
  114. { "ds1340", ds_1340 },
  115. { "m41t00", m41t00 },
  116. { }
  117. };
  118. MODULE_DEVICE_TABLE(i2c, ds1307_id);
  119. /*----------------------------------------------------------------------*/
  120. /*
  121. * The IRQ logic includes a "real" handler running in IRQ context just
  122. * long enough to schedule this workqueue entry. We need a task context
  123. * to talk to the RTC, since I2C I/O calls require that; and disable the
  124. * IRQ until we clear its status on the chip, so that this handler can
  125. * work with any type of triggering (not just falling edge).
  126. *
  127. * The ds1337 and ds1339 both have two alarms, but we only use the first
  128. * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
  129. * signal; ds1339 chips have only one alarm signal.
  130. */
  131. static void ds1307_work(struct work_struct *work)
  132. {
  133. struct ds1307 *ds1307;
  134. struct i2c_client *client;
  135. struct mutex *lock;
  136. int stat, control;
  137. ds1307 = container_of(work, struct ds1307, work);
  138. client = ds1307->client;
  139. lock = &ds1307->rtc->ops_lock;
  140. mutex_lock(lock);
  141. stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
  142. if (stat < 0)
  143. goto out;
  144. if (stat & DS1337_BIT_A1I) {
  145. stat &= ~DS1337_BIT_A1I;
  146. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
  147. control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  148. if (control < 0)
  149. goto out;
  150. control &= ~DS1337_BIT_A1IE;
  151. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
  152. /* rtc_update_irq() assumes that it is called
  153. * from IRQ-disabled context.
  154. */
  155. local_irq_disable();
  156. rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
  157. local_irq_enable();
  158. }
  159. out:
  160. if (test_bit(HAS_ALARM, &ds1307->flags))
  161. enable_irq(client->irq);
  162. mutex_unlock(lock);
  163. }
  164. static irqreturn_t ds1307_irq(int irq, void *dev_id)
  165. {
  166. struct i2c_client *client = dev_id;
  167. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  168. disable_irq_nosync(irq);
  169. schedule_work(&ds1307->work);
  170. return IRQ_HANDLED;
  171. }
  172. /*----------------------------------------------------------------------*/
  173. static int ds1307_get_time(struct device *dev, struct rtc_time *t)
  174. {
  175. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  176. int tmp;
  177. /* read the RTC date and time registers all at once */
  178. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  179. DS1307_REG_SECS, 7, ds1307->regs);
  180. if (tmp != 7) {
  181. dev_err(dev, "%s error %d\n", "read", tmp);
  182. return -EIO;
  183. }
  184. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  185. "read",
  186. ds1307->regs[0], ds1307->regs[1],
  187. ds1307->regs[2], ds1307->regs[3],
  188. ds1307->regs[4], ds1307->regs[5],
  189. ds1307->regs[6]);
  190. t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
  191. t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  192. tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
  193. t->tm_hour = bcd2bin(tmp);
  194. t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
  195. t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  196. tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
  197. t->tm_mon = bcd2bin(tmp) - 1;
  198. /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
  199. t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
  200. dev_dbg(dev, "%s secs=%d, mins=%d, "
  201. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  202. "read", t->tm_sec, t->tm_min,
  203. t->tm_hour, t->tm_mday,
  204. t->tm_mon, t->tm_year, t->tm_wday);
  205. /* initial clock setting can be undefined */
  206. return rtc_valid_tm(t);
  207. }
  208. static int ds1307_set_time(struct device *dev, struct rtc_time *t)
  209. {
  210. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  211. int result;
  212. int tmp;
  213. u8 *buf = ds1307->regs;
  214. dev_dbg(dev, "%s secs=%d, mins=%d, "
  215. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  216. "write", t->tm_sec, t->tm_min,
  217. t->tm_hour, t->tm_mday,
  218. t->tm_mon, t->tm_year, t->tm_wday);
  219. buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
  220. buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
  221. buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
  222. buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
  223. buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
  224. buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
  225. /* assume 20YY not 19YY */
  226. tmp = t->tm_year - 100;
  227. buf[DS1307_REG_YEAR] = bin2bcd(tmp);
  228. switch (ds1307->type) {
  229. case ds_1337:
  230. case ds_1339:
  231. buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
  232. break;
  233. case ds_1340:
  234. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
  235. | DS1340_BIT_CENTURY;
  236. break;
  237. default:
  238. break;
  239. }
  240. dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
  241. "write", buf[0], buf[1], buf[2], buf[3],
  242. buf[4], buf[5], buf[6]);
  243. result = i2c_smbus_write_i2c_block_data(ds1307->client, 0, 7, buf);
  244. if (result < 0) {
  245. dev_err(dev, "%s error %d\n", "write", result);
  246. return result;
  247. }
  248. return 0;
  249. }
  250. static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  251. {
  252. struct i2c_client *client = to_i2c_client(dev);
  253. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  254. int ret;
  255. if (!test_bit(HAS_ALARM, &ds1307->flags))
  256. return -EINVAL;
  257. /* read all ALARM1, ALARM2, and status registers at once */
  258. ret = i2c_smbus_read_i2c_block_data(client,
  259. DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
  260. if (ret != 9) {
  261. dev_err(dev, "%s error %d\n", "alarm read", ret);
  262. return -EIO;
  263. }
  264. dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
  265. "alarm read",
  266. ds1307->regs[0], ds1307->regs[1],
  267. ds1307->regs[2], ds1307->regs[3],
  268. ds1307->regs[4], ds1307->regs[5],
  269. ds1307->regs[6], ds1307->regs[7],
  270. ds1307->regs[8]);
  271. /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
  272. * and that all four fields are checked matches
  273. */
  274. t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
  275. t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
  276. t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
  277. t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
  278. t->time.tm_mon = -1;
  279. t->time.tm_year = -1;
  280. t->time.tm_wday = -1;
  281. t->time.tm_yday = -1;
  282. t->time.tm_isdst = -1;
  283. /* ... and status */
  284. t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
  285. t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
  286. dev_dbg(dev, "%s secs=%d, mins=%d, "
  287. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  288. "alarm read", t->time.tm_sec, t->time.tm_min,
  289. t->time.tm_hour, t->time.tm_mday,
  290. t->enabled, t->pending);
  291. return 0;
  292. }
  293. static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  294. {
  295. struct i2c_client *client = to_i2c_client(dev);
  296. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  297. unsigned char *buf = ds1307->regs;
  298. u8 control, status;
  299. int ret;
  300. if (!test_bit(HAS_ALARM, &ds1307->flags))
  301. return -EINVAL;
  302. dev_dbg(dev, "%s secs=%d, mins=%d, "
  303. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  304. "alarm set", t->time.tm_sec, t->time.tm_min,
  305. t->time.tm_hour, t->time.tm_mday,
  306. t->enabled, t->pending);
  307. /* read current status of both alarms and the chip */
  308. ret = i2c_smbus_read_i2c_block_data(client,
  309. DS1339_REG_ALARM1_SECS, 9, buf);
  310. if (ret != 9) {
  311. dev_err(dev, "%s error %d\n", "alarm write", ret);
  312. return -EIO;
  313. }
  314. control = ds1307->regs[7];
  315. status = ds1307->regs[8];
  316. dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
  317. "alarm set (old status)",
  318. ds1307->regs[0], ds1307->regs[1],
  319. ds1307->regs[2], ds1307->regs[3],
  320. ds1307->regs[4], ds1307->regs[5],
  321. ds1307->regs[6], control, status);
  322. /* set ALARM1, using 24 hour and day-of-month modes */
  323. buf[0] = bin2bcd(t->time.tm_sec);
  324. buf[1] = bin2bcd(t->time.tm_min);
  325. buf[2] = bin2bcd(t->time.tm_hour);
  326. buf[3] = bin2bcd(t->time.tm_mday);
  327. /* set ALARM2 to non-garbage */
  328. buf[4] = 0;
  329. buf[5] = 0;
  330. buf[6] = 0;
  331. /* optionally enable ALARM1 */
  332. buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
  333. if (t->enabled) {
  334. dev_dbg(dev, "alarm IRQ armed\n");
  335. buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
  336. }
  337. buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
  338. ret = i2c_smbus_write_i2c_block_data(client,
  339. DS1339_REG_ALARM1_SECS, 9, buf);
  340. if (ret < 0) {
  341. dev_err(dev, "can't set alarm time\n");
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  347. {
  348. struct i2c_client *client = to_i2c_client(dev);
  349. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  350. int ret;
  351. switch (cmd) {
  352. case RTC_AIE_OFF:
  353. if (!test_bit(HAS_ALARM, &ds1307->flags))
  354. return -ENOTTY;
  355. ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  356. if (ret < 0)
  357. return ret;
  358. ret &= ~DS1337_BIT_A1IE;
  359. ret = i2c_smbus_write_byte_data(client,
  360. DS1337_REG_CONTROL, ret);
  361. if (ret < 0)
  362. return ret;
  363. break;
  364. case RTC_AIE_ON:
  365. if (!test_bit(HAS_ALARM, &ds1307->flags))
  366. return -ENOTTY;
  367. ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  368. if (ret < 0)
  369. return ret;
  370. ret |= DS1337_BIT_A1IE;
  371. ret = i2c_smbus_write_byte_data(client,
  372. DS1337_REG_CONTROL, ret);
  373. if (ret < 0)
  374. return ret;
  375. break;
  376. default:
  377. return -ENOIOCTLCMD;
  378. }
  379. return 0;
  380. }
  381. static const struct rtc_class_ops ds13xx_rtc_ops = {
  382. .read_time = ds1307_get_time,
  383. .set_time = ds1307_set_time,
  384. .read_alarm = ds1337_read_alarm,
  385. .set_alarm = ds1337_set_alarm,
  386. .ioctl = ds1307_ioctl,
  387. };
  388. /*----------------------------------------------------------------------*/
  389. #define NVRAM_SIZE 56
  390. static ssize_t
  391. ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
  392. char *buf, loff_t off, size_t count)
  393. {
  394. struct i2c_client *client;
  395. struct ds1307 *ds1307;
  396. int result;
  397. client = kobj_to_i2c_client(kobj);
  398. ds1307 = i2c_get_clientdata(client);
  399. if (unlikely(off >= NVRAM_SIZE))
  400. return 0;
  401. if ((off + count) > NVRAM_SIZE)
  402. count = NVRAM_SIZE - off;
  403. if (unlikely(!count))
  404. return count;
  405. result = i2c_smbus_read_i2c_block_data(client, 8 + off, count, buf);
  406. if (result < 0)
  407. dev_err(&client->dev, "%s error %d\n", "nvram read", result);
  408. return result;
  409. }
  410. static ssize_t
  411. ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
  412. char *buf, loff_t off, size_t count)
  413. {
  414. struct i2c_client *client;
  415. int result;
  416. client = kobj_to_i2c_client(kobj);
  417. if (unlikely(off >= NVRAM_SIZE))
  418. return -EFBIG;
  419. if ((off + count) > NVRAM_SIZE)
  420. count = NVRAM_SIZE - off;
  421. if (unlikely(!count))
  422. return count;
  423. result = i2c_smbus_write_i2c_block_data(client, 8 + off, count, buf);
  424. if (result < 0) {
  425. dev_err(&client->dev, "%s error %d\n", "nvram write", result);
  426. return result;
  427. }
  428. return count;
  429. }
  430. static struct bin_attribute nvram = {
  431. .attr = {
  432. .name = "nvram",
  433. .mode = S_IRUGO | S_IWUSR,
  434. },
  435. .read = ds1307_nvram_read,
  436. .write = ds1307_nvram_write,
  437. .size = NVRAM_SIZE,
  438. };
  439. /*----------------------------------------------------------------------*/
  440. static struct i2c_driver ds1307_driver;
  441. static int __devinit ds1307_probe(struct i2c_client *client,
  442. const struct i2c_device_id *id)
  443. {
  444. struct ds1307 *ds1307;
  445. int err = -ENODEV;
  446. int tmp;
  447. const struct chip_desc *chip = &chips[id->driver_data];
  448. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  449. int want_irq = false;
  450. unsigned char *buf;
  451. if (!i2c_check_functionality(adapter,
  452. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  453. I2C_FUNC_SMBUS_I2C_BLOCK))
  454. return -EIO;
  455. if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
  456. return -ENOMEM;
  457. ds1307->client = client;
  458. i2c_set_clientdata(client, ds1307);
  459. ds1307->type = id->driver_data;
  460. buf = ds1307->regs;
  461. switch (ds1307->type) {
  462. case ds_1337:
  463. case ds_1339:
  464. /* has IRQ? */
  465. if (ds1307->client->irq > 0 && chip->alarm) {
  466. INIT_WORK(&ds1307->work, ds1307_work);
  467. want_irq = true;
  468. }
  469. /* get registers that the "rtc" read below won't read... */
  470. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  471. DS1337_REG_CONTROL, 2, buf);
  472. if (tmp != 2) {
  473. pr_debug("read error %d\n", tmp);
  474. err = -EIO;
  475. goto exit_free;
  476. }
  477. /* oscillator off? turn it on, so clock can tick. */
  478. if (ds1307->regs[0] & DS1337_BIT_nEOSC)
  479. ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
  480. /* Using IRQ? Disable the square wave and both alarms.
  481. * For ds1339, be sure alarms can trigger when we're
  482. * running on Vbackup (BBSQI); we assume ds1337 will
  483. * ignore that bit
  484. */
  485. if (want_irq) {
  486. ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI;
  487. ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
  488. }
  489. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
  490. ds1307->regs[0]);
  491. /* oscillator fault? clear flag, and warn */
  492. if (ds1307->regs[1] & DS1337_BIT_OSF) {
  493. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
  494. ds1307->regs[1] & ~DS1337_BIT_OSF);
  495. dev_warn(&client->dev, "SET TIME!\n");
  496. }
  497. break;
  498. default:
  499. break;
  500. }
  501. read_rtc:
  502. /* read RTC registers */
  503. tmp = i2c_smbus_read_i2c_block_data(ds1307->client, 0, 8, buf);
  504. if (tmp != 8) {
  505. pr_debug("read error %d\n", tmp);
  506. err = -EIO;
  507. goto exit_free;
  508. }
  509. /* minimal sanity checking; some chips (like DS1340) don't
  510. * specify the extra bits as must-be-zero, but there are
  511. * still a few values that are clearly out-of-range.
  512. */
  513. tmp = ds1307->regs[DS1307_REG_SECS];
  514. switch (ds1307->type) {
  515. case ds_1307:
  516. case m41t00:
  517. /* clock halted? turn it on, so clock can tick. */
  518. if (tmp & DS1307_BIT_CH) {
  519. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  520. dev_warn(&client->dev, "SET TIME!\n");
  521. goto read_rtc;
  522. }
  523. break;
  524. case ds_1338:
  525. /* clock halted? turn it on, so clock can tick. */
  526. if (tmp & DS1307_BIT_CH)
  527. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  528. /* oscillator fault? clear flag, and warn */
  529. if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
  530. i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
  531. ds1307->regs[DS1307_REG_CONTROL]
  532. & ~DS1338_BIT_OSF);
  533. dev_warn(&client->dev, "SET TIME!\n");
  534. goto read_rtc;
  535. }
  536. break;
  537. case ds_1340:
  538. /* clock halted? turn it on, so clock can tick. */
  539. if (tmp & DS1340_BIT_nEOSC)
  540. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  541. tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
  542. if (tmp < 0) {
  543. pr_debug("read error %d\n", tmp);
  544. err = -EIO;
  545. goto exit_free;
  546. }
  547. /* oscillator fault? clear flag, and warn */
  548. if (tmp & DS1340_BIT_OSF) {
  549. i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
  550. dev_warn(&client->dev, "SET TIME!\n");
  551. }
  552. break;
  553. case ds_1337:
  554. case ds_1339:
  555. break;
  556. }
  557. tmp = ds1307->regs[DS1307_REG_HOUR];
  558. switch (ds1307->type) {
  559. case ds_1340:
  560. case m41t00:
  561. /* NOTE: ignores century bits; fix before deploying
  562. * systems that will run through year 2100.
  563. */
  564. break;
  565. default:
  566. if (!(tmp & DS1307_BIT_12HR))
  567. break;
  568. /* Be sure we're in 24 hour mode. Multi-master systems
  569. * take note...
  570. */
  571. tmp = bcd2bin(tmp & 0x1f);
  572. if (tmp == 12)
  573. tmp = 0;
  574. if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
  575. tmp += 12;
  576. i2c_smbus_write_byte_data(client,
  577. DS1307_REG_HOUR,
  578. bin2bcd(tmp));
  579. }
  580. ds1307->rtc = rtc_device_register(client->name, &client->dev,
  581. &ds13xx_rtc_ops, THIS_MODULE);
  582. if (IS_ERR(ds1307->rtc)) {
  583. err = PTR_ERR(ds1307->rtc);
  584. dev_err(&client->dev,
  585. "unable to register the class device\n");
  586. goto exit_free;
  587. }
  588. if (want_irq) {
  589. err = request_irq(client->irq, ds1307_irq, 0,
  590. ds1307->rtc->name, client);
  591. if (err) {
  592. dev_err(&client->dev,
  593. "unable to request IRQ!\n");
  594. goto exit_irq;
  595. }
  596. set_bit(HAS_ALARM, &ds1307->flags);
  597. dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
  598. }
  599. if (chip->nvram56) {
  600. err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
  601. if (err == 0) {
  602. set_bit(HAS_NVRAM, &ds1307->flags);
  603. dev_info(&client->dev, "56 bytes nvram\n");
  604. }
  605. }
  606. return 0;
  607. exit_irq:
  608. if (ds1307->rtc)
  609. rtc_device_unregister(ds1307->rtc);
  610. exit_free:
  611. kfree(ds1307);
  612. return err;
  613. }
  614. static int __devexit ds1307_remove(struct i2c_client *client)
  615. {
  616. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  617. if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
  618. free_irq(client->irq, client);
  619. cancel_work_sync(&ds1307->work);
  620. }
  621. if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
  622. sysfs_remove_bin_file(&client->dev.kobj, &nvram);
  623. rtc_device_unregister(ds1307->rtc);
  624. kfree(ds1307);
  625. return 0;
  626. }
  627. static struct i2c_driver ds1307_driver = {
  628. .driver = {
  629. .name = "rtc-ds1307",
  630. .owner = THIS_MODULE,
  631. },
  632. .probe = ds1307_probe,
  633. .remove = __devexit_p(ds1307_remove),
  634. .id_table = ds1307_id,
  635. };
  636. static int __init ds1307_init(void)
  637. {
  638. return i2c_add_driver(&ds1307_driver);
  639. }
  640. module_init(ds1307_init);
  641. static void __exit ds1307_exit(void)
  642. {
  643. i2c_del_driver(&ds1307_driver);
  644. }
  645. module_exit(ds1307_exit);
  646. MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
  647. MODULE_LICENSE("GPL");