rtc-ds3232.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. int exiting;
  56. };
  57. static struct i2c_driver ds3232_driver;
  58. static int ds3232_check_rtc_status(struct i2c_client *client)
  59. {
  60. int ret = 0;
  61. int control, stat;
  62. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  63. if (stat < 0)
  64. return stat;
  65. if (stat & DS3232_REG_SR_OSF)
  66. dev_warn(&client->dev,
  67. "oscillator discontinuity flagged, "
  68. "time unreliable\n");
  69. stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  70. ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  71. if (ret < 0)
  72. return ret;
  73. /* If the alarm is pending, clear it before requesting
  74. * the interrupt, so an interrupt event isn't reported
  75. * before everything is initialized.
  76. */
  77. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  78. if (control < 0)
  79. return control;
  80. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  81. control |= DS3232_REG_CR_INTCN;
  82. return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  83. }
  84. static int ds3232_read_time(struct device *dev, struct rtc_time *time)
  85. {
  86. struct i2c_client *client = to_i2c_client(dev);
  87. int ret;
  88. u8 buf[7];
  89. unsigned int year, month, day, hour, minute, second;
  90. unsigned int week, twelve_hr, am_pm;
  91. unsigned int century, add_century = 0;
  92. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf);
  93. if (ret < 0)
  94. return ret;
  95. if (ret < 7)
  96. return -EIO;
  97. second = buf[0];
  98. minute = buf[1];
  99. hour = buf[2];
  100. week = buf[3];
  101. day = buf[4];
  102. month = buf[5];
  103. year = buf[6];
  104. /* Extract additional information for AM/PM and century */
  105. twelve_hr = hour & 0x40;
  106. am_pm = hour & 0x20;
  107. century = month & 0x80;
  108. /* Write to rtc_time structure */
  109. time->tm_sec = bcd2bin(second);
  110. time->tm_min = bcd2bin(minute);
  111. if (twelve_hr) {
  112. /* Convert to 24 hr */
  113. if (am_pm)
  114. time->tm_hour = bcd2bin(hour & 0x1F) + 12;
  115. else
  116. time->tm_hour = bcd2bin(hour & 0x1F);
  117. } else {
  118. time->tm_hour = bcd2bin(hour);
  119. }
  120. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  121. time->tm_wday = bcd2bin(week) - 1;
  122. time->tm_mday = bcd2bin(day);
  123. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  124. time->tm_mon = bcd2bin(month & 0x7F) - 1;
  125. if (century)
  126. add_century = 100;
  127. time->tm_year = bcd2bin(year) + add_century;
  128. return rtc_valid_tm(time);
  129. }
  130. static int ds3232_set_time(struct device *dev, struct rtc_time *time)
  131. {
  132. struct i2c_client *client = to_i2c_client(dev);
  133. u8 buf[7];
  134. /* Extract time from rtc_time and load into ds3232*/
  135. buf[0] = bin2bcd(time->tm_sec);
  136. buf[1] = bin2bcd(time->tm_min);
  137. buf[2] = bin2bcd(time->tm_hour);
  138. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  139. buf[3] = bin2bcd(time->tm_wday + 1);
  140. buf[4] = bin2bcd(time->tm_mday); /* Date */
  141. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  142. buf[5] = bin2bcd(time->tm_mon + 1);
  143. if (time->tm_year >= 100) {
  144. buf[5] |= 0x80;
  145. buf[6] = bin2bcd(time->tm_year - 100);
  146. } else {
  147. buf[6] = bin2bcd(time->tm_year);
  148. }
  149. return i2c_smbus_write_i2c_block_data(client,
  150. DS3232_REG_SECONDS, 7, buf);
  151. }
  152. /*
  153. * DS3232 has two alarm, we only use alarm1
  154. * According to linux specification, only support one-shot alarm
  155. * no periodic alarm mode
  156. */
  157. static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  158. {
  159. struct i2c_client *client = to_i2c_client(dev);
  160. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  161. int control, stat;
  162. int ret;
  163. u8 buf[4];
  164. mutex_lock(&ds3232->mutex);
  165. ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  166. if (ret < 0)
  167. goto out;
  168. stat = ret;
  169. ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  170. if (ret < 0)
  171. goto out;
  172. control = ret;
  173. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  174. if (ret < 0)
  175. goto out;
  176. alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  177. alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
  178. alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
  179. alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F);
  180. alarm->time.tm_mon = -1;
  181. alarm->time.tm_year = -1;
  182. alarm->time.tm_wday = -1;
  183. alarm->time.tm_yday = -1;
  184. alarm->time.tm_isdst = -1;
  185. alarm->enabled = !!(control & DS3232_REG_CR_A1IE);
  186. alarm->pending = !!(stat & DS3232_REG_SR_A1F);
  187. ret = 0;
  188. out:
  189. mutex_unlock(&ds3232->mutex);
  190. return ret;
  191. }
  192. /*
  193. * linux rtc-module does not support wday alarm
  194. * and only 24h time mode supported indeed
  195. */
  196. static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  197. {
  198. struct i2c_client *client = to_i2c_client(dev);
  199. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  200. int control, stat;
  201. int ret;
  202. u8 buf[4];
  203. if (client->irq <= 0)
  204. return -EINVAL;
  205. mutex_lock(&ds3232->mutex);
  206. buf[0] = bin2bcd(alarm->time.tm_sec);
  207. buf[1] = bin2bcd(alarm->time.tm_min);
  208. buf[2] = bin2bcd(alarm->time.tm_hour);
  209. buf[3] = bin2bcd(alarm->time.tm_mday);
  210. /* clear alarm interrupt enable bit */
  211. ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  212. if (ret < 0)
  213. goto out;
  214. control = ret;
  215. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  216. ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  217. if (ret < 0)
  218. goto out;
  219. /* clear any pending alarm flag */
  220. ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  221. if (ret < 0)
  222. goto out;
  223. stat = ret;
  224. stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  225. ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  226. if (ret < 0)
  227. goto out;
  228. ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  229. if (alarm->enabled) {
  230. control |= DS3232_REG_CR_A1IE;
  231. ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  232. }
  233. out:
  234. mutex_unlock(&ds3232->mutex);
  235. return ret;
  236. }
  237. static void ds3232_update_alarm(struct i2c_client *client)
  238. {
  239. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  240. int control;
  241. int ret;
  242. u8 buf[4];
  243. mutex_lock(&ds3232->mutex);
  244. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  245. if (ret < 0)
  246. goto unlock;
  247. buf[0] = bcd2bin(buf[0]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  248. 0x80 : buf[0];
  249. buf[1] = bcd2bin(buf[1]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  250. 0x80 : buf[1];
  251. buf[2] = bcd2bin(buf[2]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  252. 0x80 : buf[2];
  253. buf[3] = bcd2bin(buf[3]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
  254. 0x80 : buf[3];
  255. ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
  256. if (ret < 0)
  257. goto unlock;
  258. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  259. if (control < 0)
  260. goto unlock;
  261. if (ds3232->rtc->irq_data & (RTC_AF | RTC_UF))
  262. /* enable alarm1 interrupt */
  263. control |= DS3232_REG_CR_A1IE;
  264. else
  265. /* disable alarm1 interrupt */
  266. control &= ~(DS3232_REG_CR_A1IE);
  267. i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  268. unlock:
  269. mutex_unlock(&ds3232->mutex);
  270. }
  271. static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)
  272. {
  273. struct i2c_client *client = to_i2c_client(dev);
  274. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  275. if (client->irq <= 0)
  276. return -EINVAL;
  277. if (enabled)
  278. ds3232->rtc->irq_data |= RTC_AF;
  279. else
  280. ds3232->rtc->irq_data &= ~RTC_AF;
  281. ds3232_update_alarm(client);
  282. return 0;
  283. }
  284. static irqreturn_t ds3232_irq(int irq, void *dev_id)
  285. {
  286. struct i2c_client *client = dev_id;
  287. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  288. disable_irq_nosync(irq);
  289. schedule_work(&ds3232->work);
  290. return IRQ_HANDLED;
  291. }
  292. static void ds3232_work(struct work_struct *work)
  293. {
  294. struct ds3232 *ds3232 = container_of(work, struct ds3232, work);
  295. struct i2c_client *client = ds3232->client;
  296. int stat, control;
  297. mutex_lock(&ds3232->mutex);
  298. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  299. if (stat < 0)
  300. goto unlock;
  301. if (stat & DS3232_REG_SR_A1F) {
  302. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  303. if (control < 0)
  304. goto out;
  305. /* disable alarm1 interrupt */
  306. control &= ~(DS3232_REG_CR_A1IE);
  307. i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  308. /* clear the alarm pend flag */
  309. stat &= ~DS3232_REG_SR_A1F;
  310. i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  311. rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
  312. }
  313. out:
  314. if (!ds3232->exiting)
  315. enable_irq(client->irq);
  316. unlock:
  317. mutex_unlock(&ds3232->mutex);
  318. }
  319. static const struct rtc_class_ops ds3232_rtc_ops = {
  320. .read_time = ds3232_read_time,
  321. .set_time = ds3232_set_time,
  322. .read_alarm = ds3232_read_alarm,
  323. .set_alarm = ds3232_set_alarm,
  324. .alarm_irq_enable = ds3232_alarm_irq_enable,
  325. };
  326. static int __devinit ds3232_probe(struct i2c_client *client,
  327. const struct i2c_device_id *id)
  328. {
  329. struct ds3232 *ds3232;
  330. int ret;
  331. ds3232 = kzalloc(sizeof(struct ds3232), GFP_KERNEL);
  332. if (!ds3232)
  333. return -ENOMEM;
  334. ds3232->client = client;
  335. i2c_set_clientdata(client, ds3232);
  336. INIT_WORK(&ds3232->work, ds3232_work);
  337. mutex_init(&ds3232->mutex);
  338. ret = ds3232_check_rtc_status(client);
  339. if (ret)
  340. goto out_free;
  341. ds3232->rtc = rtc_device_register(client->name, &client->dev,
  342. &ds3232_rtc_ops, THIS_MODULE);
  343. if (IS_ERR(ds3232->rtc)) {
  344. ret = PTR_ERR(ds3232->rtc);
  345. dev_err(&client->dev, "unable to register the class device\n");
  346. goto out_irq;
  347. }
  348. if (client->irq >= 0) {
  349. ret = request_irq(client->irq, ds3232_irq, 0,
  350. "ds3232", client);
  351. if (ret) {
  352. dev_err(&client->dev, "unable to request IRQ\n");
  353. goto out_free;
  354. }
  355. }
  356. return 0;
  357. out_irq:
  358. if (client->irq >= 0)
  359. free_irq(client->irq, client);
  360. out_free:
  361. kfree(ds3232);
  362. return ret;
  363. }
  364. static int __devexit 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. free_irq(client->irq, client);
  372. cancel_work_sync(&ds3232->work);
  373. }
  374. rtc_device_unregister(ds3232->rtc);
  375. kfree(ds3232);
  376. return 0;
  377. }
  378. static const struct i2c_device_id ds3232_id[] = {
  379. { "ds3232", 0 },
  380. { }
  381. };
  382. MODULE_DEVICE_TABLE(i2c, ds3232_id);
  383. static struct i2c_driver ds3232_driver = {
  384. .driver = {
  385. .name = "rtc-ds3232",
  386. .owner = THIS_MODULE,
  387. },
  388. .probe = ds3232_probe,
  389. .remove = __devexit_p(ds3232_remove),
  390. .id_table = ds3232_id,
  391. };
  392. module_i2c_driver(ds3232_driver);
  393. MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
  394. MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
  395. MODULE_LICENSE("GPL");