rtc-ds3232.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
  3. *
  4. * Copyright (C) 2009-2011 Freescale Semiconductor.
  5. * Author: Jack Lan <jack.lan@freescale.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. /*
  13. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  14. * recommened in .../Documentation/i2c/writing-clients section
  15. * "Sending and receiving", using SMBus level communication is preferred.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/i2c.h>
  21. #include <linux/rtc.h>
  22. #include <linux/bcd.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/slab.h>
  25. #define DS3232_REG_SECONDS 0x00
  26. #define DS3232_REG_MINUTES 0x01
  27. #define DS3232_REG_HOURS 0x02
  28. #define DS3232_REG_AMPM 0x02
  29. #define DS3232_REG_DAY 0x03
  30. #define DS3232_REG_DATE 0x04
  31. #define DS3232_REG_MONTH 0x05
  32. #define DS3232_REG_CENTURY 0x05
  33. #define DS3232_REG_YEAR 0x06
  34. #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
  35. #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
  36. #define DS3232_REG_CR 0x0E /* Control register */
  37. # define DS3232_REG_CR_nEOSC 0x80
  38. # define DS3232_REG_CR_INTCN 0x04
  39. # define DS3232_REG_CR_A2IE 0x02
  40. # define DS3232_REG_CR_A1IE 0x01
  41. #define DS3232_REG_SR 0x0F /* control/status register */
  42. # define DS3232_REG_SR_OSF 0x80
  43. # define DS3232_REG_SR_BSY 0x04
  44. # define DS3232_REG_SR_A2F 0x02
  45. # define DS3232_REG_SR_A1F 0x01
  46. struct ds3232 {
  47. struct i2c_client *client;
  48. struct rtc_device *rtc;
  49. struct work_struct work;
  50. /* The mutex protects alarm operations, and prevents a race
  51. * between the enable_irq() in the workqueue and the free_irq()
  52. * in the remove function.
  53. */
  54. struct mutex mutex;
  55. bool suspended;
  56. int exiting;
  57. };
  58. static struct i2c_driver ds3232_driver;
  59. static int ds3232_check_rtc_status(struct i2c_client *client)
  60. {
  61. int ret = 0;
  62. int control, stat;
  63. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  64. if (stat < 0)
  65. return stat;
  66. if (stat & DS3232_REG_SR_OSF)
  67. dev_warn(&client->dev,
  68. "oscillator discontinuity flagged, "
  69. "time unreliable\n");
  70. stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  71. ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  72. if (ret < 0)
  73. return ret;
  74. /* If the alarm is pending, clear it before requesting
  75. * the interrupt, so an interrupt event isn't reported
  76. * before everything is initialized.
  77. */
  78. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  79. if (control < 0)
  80. return control;
  81. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  82. control |= DS3232_REG_CR_INTCN;
  83. return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  84. }
  85. static int ds3232_read_time(struct device *dev, struct rtc_time *time)
  86. {
  87. struct i2c_client *client = to_i2c_client(dev);
  88. int ret;
  89. u8 buf[7];
  90. unsigned int year, month, day, hour, minute, second;
  91. unsigned int week, twelve_hr, am_pm;
  92. unsigned int century, add_century = 0;
  93. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf);
  94. if (ret < 0)
  95. return ret;
  96. if (ret < 7)
  97. return -EIO;
  98. second = buf[0];
  99. minute = buf[1];
  100. hour = buf[2];
  101. week = buf[3];
  102. day = buf[4];
  103. month = buf[5];
  104. year = buf[6];
  105. /* Extract additional information for AM/PM and century */
  106. twelve_hr = hour & 0x40;
  107. am_pm = hour & 0x20;
  108. century = month & 0x80;
  109. /* Write to rtc_time structure */
  110. time->tm_sec = bcd2bin(second);
  111. time->tm_min = bcd2bin(minute);
  112. if (twelve_hr) {
  113. /* Convert to 24 hr */
  114. if (am_pm)
  115. time->tm_hour = bcd2bin(hour & 0x1F) + 12;
  116. else
  117. time->tm_hour = bcd2bin(hour & 0x1F);
  118. } else {
  119. time->tm_hour = bcd2bin(hour);
  120. }
  121. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  122. time->tm_wday = bcd2bin(week) - 1;
  123. time->tm_mday = bcd2bin(day);
  124. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  125. time->tm_mon = bcd2bin(month & 0x7F) - 1;
  126. if (century)
  127. add_century = 100;
  128. time->tm_year = bcd2bin(year) + add_century;
  129. return rtc_valid_tm(time);
  130. }
  131. static int ds3232_set_time(struct device *dev, struct rtc_time *time)
  132. {
  133. struct i2c_client *client = to_i2c_client(dev);
  134. u8 buf[7];
  135. /* Extract time from rtc_time and load into ds3232*/
  136. buf[0] = bin2bcd(time->tm_sec);
  137. buf[1] = bin2bcd(time->tm_min);
  138. buf[2] = bin2bcd(time->tm_hour);
  139. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  140. buf[3] = bin2bcd(time->tm_wday + 1);
  141. buf[4] = bin2bcd(time->tm_mday); /* Date */
  142. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  143. buf[5] = bin2bcd(time->tm_mon + 1);
  144. if (time->tm_year >= 100) {
  145. buf[5] |= 0x80;
  146. buf[6] = bin2bcd(time->tm_year - 100);
  147. } else {
  148. buf[6] = bin2bcd(time->tm_year);
  149. }
  150. return i2c_smbus_write_i2c_block_data(client,
  151. DS3232_REG_SECONDS, 7, buf);
  152. }
  153. /*
  154. * DS3232 has two alarm, we only use alarm1
  155. * According to linux specification, only support one-shot alarm
  156. * no periodic alarm mode
  157. */
  158. static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  159. {
  160. struct i2c_client *client = to_i2c_client(dev);
  161. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  162. int control, stat;
  163. int ret;
  164. u8 buf[4];
  165. mutex_lock(&ds3232->mutex);
  166. ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  167. if (ret < 0)
  168. goto out;
  169. stat = ret;
  170. ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  171. if (ret < 0)
  172. goto out;
  173. control = ret;
  174. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  175. if (ret < 0)
  176. goto out;
  177. alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  178. alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
  179. alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
  180. alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F);
  181. alarm->time.tm_mon = -1;
  182. alarm->time.tm_year = -1;
  183. alarm->time.tm_wday = -1;
  184. alarm->time.tm_yday = -1;
  185. alarm->time.tm_isdst = -1;
  186. alarm->enabled = !!(control & DS3232_REG_CR_A1IE);
  187. alarm->pending = !!(stat & DS3232_REG_SR_A1F);
  188. ret = 0;
  189. out:
  190. mutex_unlock(&ds3232->mutex);
  191. return ret;
  192. }
  193. /*
  194. * linux rtc-module does not support wday alarm
  195. * and only 24h time mode supported indeed
  196. */
  197. static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  198. {
  199. struct i2c_client *client = to_i2c_client(dev);
  200. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  201. int control, stat;
  202. int ret;
  203. u8 buf[4];
  204. if (client->irq <= 0)
  205. return -EINVAL;
  206. mutex_lock(&ds3232->mutex);
  207. buf[0] = bin2bcd(alarm->time.tm_sec);
  208. buf[1] = bin2bcd(alarm->time.tm_min);
  209. buf[2] = bin2bcd(alarm->time.tm_hour);
  210. buf[3] = bin2bcd(alarm->time.tm_mday);
  211. /* clear alarm interrupt enable bit */
  212. ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  213. if (ret < 0)
  214. goto out;
  215. control = ret;
  216. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  217. ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  218. if (ret < 0)
  219. goto out;
  220. /* clear any pending alarm flag */
  221. ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  222. if (ret < 0)
  223. goto out;
  224. stat = ret;
  225. stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  226. ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  227. if (ret < 0)
  228. goto out;
  229. ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  230. if (alarm->enabled) {
  231. control |= DS3232_REG_CR_A1IE;
  232. ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  233. }
  234. out:
  235. mutex_unlock(&ds3232->mutex);
  236. return ret;
  237. }
  238. static void ds3232_update_alarm(struct i2c_client *client)
  239. {
  240. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  241. int control;
  242. int ret;
  243. u8 buf[4];
  244. mutex_lock(&ds3232->mutex);
  245. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  246. if (ret < 0)
  247. goto unlock;
  248. buf[0] = bcd2bin(buf[0]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  249. 0x80 : buf[0];
  250. buf[1] = bcd2bin(buf[1]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  251. 0x80 : buf[1];
  252. buf[2] = bcd2bin(buf[2]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  253. 0x80 : buf[2];
  254. buf[3] = bcd2bin(buf[3]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  255. 0x80 : buf[3];
  256. ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  257. if (ret < 0)
  258. goto unlock;
  259. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  260. if (control < 0)
  261. goto unlock;
  262. if (ds3232->rtc->irq_data & (RTC_AF | RTC_UF))
  263. /* enable alarm1 interrupt */
  264. control |= DS3232_REG_CR_A1IE;
  265. else
  266. /* disable alarm1 interrupt */
  267. control &= ~(DS3232_REG_CR_A1IE);
  268. i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  269. unlock:
  270. mutex_unlock(&ds3232->mutex);
  271. }
  272. static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)
  273. {
  274. struct i2c_client *client = to_i2c_client(dev);
  275. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  276. if (client->irq <= 0)
  277. return -EINVAL;
  278. if (enabled)
  279. ds3232->rtc->irq_data |= RTC_AF;
  280. else
  281. ds3232->rtc->irq_data &= ~RTC_AF;
  282. ds3232_update_alarm(client);
  283. return 0;
  284. }
  285. static irqreturn_t ds3232_irq(int irq, void *dev_id)
  286. {
  287. struct i2c_client *client = dev_id;
  288. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  289. disable_irq_nosync(irq);
  290. /*
  291. * If rtc as a wakeup source, can't schedule the work
  292. * at system resume flow, because at this time the i2c bus
  293. * has not been resumed.
  294. */
  295. if (!ds3232->suspended)
  296. schedule_work(&ds3232->work);
  297. return IRQ_HANDLED;
  298. }
  299. static void ds3232_work(struct work_struct *work)
  300. {
  301. struct ds3232 *ds3232 = container_of(work, struct ds3232, work);
  302. struct i2c_client *client = ds3232->client;
  303. int stat, control;
  304. mutex_lock(&ds3232->mutex);
  305. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  306. if (stat < 0)
  307. goto unlock;
  308. if (stat & DS3232_REG_SR_A1F) {
  309. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  310. if (control < 0) {
  311. pr_warn("Read DS3232 Control Register error."
  312. "Disable IRQ%d.\n", client->irq);
  313. } else {
  314. /* disable alarm1 interrupt */
  315. control &= ~(DS3232_REG_CR_A1IE);
  316. i2c_smbus_write_byte_data(client, DS3232_REG_CR,
  317. control);
  318. /* clear the alarm pend flag */
  319. stat &= ~DS3232_REG_SR_A1F;
  320. i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  321. rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
  322. if (!ds3232->exiting)
  323. enable_irq(client->irq);
  324. }
  325. }
  326. unlock:
  327. mutex_unlock(&ds3232->mutex);
  328. }
  329. static const struct rtc_class_ops ds3232_rtc_ops = {
  330. .read_time = ds3232_read_time,
  331. .set_time = ds3232_set_time,
  332. .read_alarm = ds3232_read_alarm,
  333. .set_alarm = ds3232_set_alarm,
  334. .alarm_irq_enable = ds3232_alarm_irq_enable,
  335. };
  336. static int ds3232_probe(struct i2c_client *client,
  337. const struct i2c_device_id *id)
  338. {
  339. struct ds3232 *ds3232;
  340. int ret;
  341. ds3232 = devm_kzalloc(&client->dev, sizeof(struct ds3232), GFP_KERNEL);
  342. if (!ds3232)
  343. return -ENOMEM;
  344. ds3232->client = client;
  345. i2c_set_clientdata(client, ds3232);
  346. INIT_WORK(&ds3232->work, ds3232_work);
  347. mutex_init(&ds3232->mutex);
  348. ret = ds3232_check_rtc_status(client);
  349. if (ret)
  350. return ret;
  351. if (client->irq > 0) {
  352. ret = devm_request_irq(&client->dev, client->irq, ds3232_irq,
  353. IRQF_SHARED, "ds3232", client);
  354. if (ret) {
  355. dev_err(&client->dev, "unable to request IRQ\n");
  356. }
  357. device_init_wakeup(&client->dev, 1);
  358. }
  359. ds3232->rtc = devm_rtc_device_register(&client->dev, client->name,
  360. &ds3232_rtc_ops, THIS_MODULE);
  361. return PTR_ERR_OR_ZERO(ds3232->rtc);
  362. }
  363. static int ds3232_remove(struct i2c_client *client)
  364. {
  365. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  366. if (client->irq >= 0) {
  367. mutex_lock(&ds3232->mutex);
  368. ds3232->exiting = 1;
  369. mutex_unlock(&ds3232->mutex);
  370. devm_free_irq(&client->dev, client->irq, client);
  371. cancel_work_sync(&ds3232->work);
  372. }
  373. return 0;
  374. }
  375. #ifdef CONFIG_PM_SLEEP
  376. static int ds3232_suspend(struct device *dev)
  377. {
  378. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  379. struct i2c_client *client = to_i2c_client(dev);
  380. if (device_can_wakeup(dev)) {
  381. ds3232->suspended = true;
  382. irq_set_irq_wake(client->irq, 1);
  383. }
  384. return 0;
  385. }
  386. static int ds3232_resume(struct device *dev)
  387. {
  388. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  389. struct i2c_client *client = to_i2c_client(dev);
  390. if (ds3232->suspended) {
  391. ds3232->suspended = false;
  392. /* Clear the hardware alarm pend flag */
  393. schedule_work(&ds3232->work);
  394. irq_set_irq_wake(client->irq, 0);
  395. }
  396. return 0;
  397. }
  398. #endif
  399. static const struct dev_pm_ops ds3232_pm_ops = {
  400. SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend, ds3232_resume)
  401. };
  402. static const struct i2c_device_id ds3232_id[] = {
  403. { "ds3232", 0 },
  404. { }
  405. };
  406. MODULE_DEVICE_TABLE(i2c, ds3232_id);
  407. static struct i2c_driver ds3232_driver = {
  408. .driver = {
  409. .name = "rtc-ds3232",
  410. .owner = THIS_MODULE,
  411. .pm = &ds3232_pm_ops,
  412. },
  413. .probe = ds3232_probe,
  414. .remove = ds3232_remove,
  415. .id_table = ds3232_id,
  416. };
  417. module_i2c_driver(ds3232_driver);
  418. MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
  419. MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
  420. MODULE_LICENSE("GPL");