rtc-ds1307.c 22 KB

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