rtc-ds3232.c 13 KB

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