rtc-ds3232.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
  3. *
  4. * Copyright (C) 2009-2010 Freescale Semiconductor.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. /*
  12. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  13. * recommened in .../Documentation/i2c/writing-clients section
  14. * "Sending and receiving", using SMBus level communication is preferred.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/i2c.h>
  20. #include <linux/rtc.h>
  21. #include <linux/bcd.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #define DS3232_REG_SECONDS 0x00
  25. #define DS3232_REG_MINUTES 0x01
  26. #define DS3232_REG_HOURS 0x02
  27. #define DS3232_REG_AMPM 0x02
  28. #define DS3232_REG_DAY 0x03
  29. #define DS3232_REG_DATE 0x04
  30. #define DS3232_REG_MONTH 0x05
  31. #define DS3232_REG_CENTURY 0x05
  32. #define DS3232_REG_YEAR 0x06
  33. #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
  34. #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
  35. #define DS3232_REG_CR 0x0E /* Control register */
  36. # define DS3232_REG_CR_nEOSC 0x80
  37. # define DS3232_REG_CR_INTCN 0x04
  38. # define DS3232_REG_CR_A2IE 0x02
  39. # define DS3232_REG_CR_A1IE 0x01
  40. #define DS3232_REG_SR 0x0F /* control/status register */
  41. # define DS3232_REG_SR_OSF 0x80
  42. # define DS3232_REG_SR_BSY 0x04
  43. # define DS3232_REG_SR_A2F 0x02
  44. # define DS3232_REG_SR_A1F 0x01
  45. struct ds3232 {
  46. struct i2c_client *client;
  47. struct rtc_device *rtc;
  48. struct work_struct work;
  49. /* The mutex protects alarm operations, and prevents a race
  50. * between the enable_irq() in the workqueue and the free_irq()
  51. * in the remove function.
  52. */
  53. struct mutex mutex;
  54. int exiting;
  55. };
  56. static struct i2c_driver ds3232_driver;
  57. static int ds3232_check_rtc_status(struct i2c_client *client)
  58. {
  59. int ret = 0;
  60. int control, stat;
  61. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  62. if (stat < 0)
  63. return stat;
  64. if (stat & DS3232_REG_SR_OSF)
  65. dev_warn(&client->dev,
  66. "oscillator discontinuity flagged, "
  67. "time unreliable\n");
  68. stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  69. ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  70. if (ret < 0)
  71. return ret;
  72. /* If the alarm is pending, clear it before requesting
  73. * the interrupt, so an interrupt event isn't reported
  74. * before everything is initialized.
  75. */
  76. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  77. if (control < 0)
  78. return control;
  79. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  80. control |= DS3232_REG_CR_INTCN;
  81. return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  82. }
  83. static int ds3232_read_time(struct device *dev, struct rtc_time *time)
  84. {
  85. struct i2c_client *client = to_i2c_client(dev);
  86. int ret;
  87. u8 buf[7];
  88. unsigned int year, month, day, hour, minute, second;
  89. unsigned int week, twelve_hr, am_pm;
  90. unsigned int century, add_century = 0;
  91. ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf);
  92. if (ret < 0)
  93. return ret;
  94. if (ret < 7)
  95. return -EIO;
  96. second = buf[0];
  97. minute = buf[1];
  98. hour = buf[2];
  99. week = buf[3];
  100. day = buf[4];
  101. month = buf[5];
  102. year = buf[6];
  103. /* Extract additional information for AM/PM and century */
  104. twelve_hr = hour & 0x40;
  105. am_pm = hour & 0x20;
  106. century = month & 0x80;
  107. /* Write to rtc_time structure */
  108. time->tm_sec = bcd2bin(second);
  109. time->tm_min = bcd2bin(minute);
  110. if (twelve_hr) {
  111. /* Convert to 24 hr */
  112. if (am_pm)
  113. time->tm_hour = bcd2bin(hour & 0x1F) + 12;
  114. else
  115. time->tm_hour = bcd2bin(hour & 0x1F);
  116. } else {
  117. time->tm_hour = bcd2bin(hour);
  118. }
  119. time->tm_wday = bcd2bin(week);
  120. time->tm_mday = bcd2bin(day);
  121. time->tm_mon = bcd2bin(month & 0x7F);
  122. if (century)
  123. add_century = 100;
  124. time->tm_year = bcd2bin(year) + add_century;
  125. return rtc_valid_tm(time);
  126. }
  127. static int ds3232_set_time(struct device *dev, struct rtc_time *time)
  128. {
  129. struct i2c_client *client = to_i2c_client(dev);
  130. u8 buf[7];
  131. /* Extract time from rtc_time and load into ds3232*/
  132. buf[0] = bin2bcd(time->tm_sec);
  133. buf[1] = bin2bcd(time->tm_min);
  134. buf[2] = bin2bcd(time->tm_hour);
  135. buf[3] = bin2bcd(time->tm_wday); /* Day of the week */
  136. buf[4] = bin2bcd(time->tm_mday); /* Date */
  137. buf[5] = bin2bcd(time->tm_mon);
  138. if (time->tm_year >= 100) {
  139. buf[5] |= 0x80;
  140. buf[6] = bin2bcd(time->tm_year - 100);
  141. } else {
  142. buf[6] = bin2bcd(time->tm_year);
  143. }
  144. return i2c_smbus_write_i2c_block_data(client,
  145. DS3232_REG_SECONDS, 7, buf);
  146. }
  147. static irqreturn_t ds3232_irq(int irq, void *dev_id)
  148. {
  149. struct i2c_client *client = dev_id;
  150. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  151. disable_irq_nosync(irq);
  152. schedule_work(&ds3232->work);
  153. return IRQ_HANDLED;
  154. }
  155. static void ds3232_work(struct work_struct *work)
  156. {
  157. struct ds3232 *ds3232 = container_of(work, struct ds3232, work);
  158. struct i2c_client *client = ds3232->client;
  159. int stat, control;
  160. mutex_lock(&ds3232->mutex);
  161. stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
  162. if (stat < 0)
  163. goto unlock;
  164. if (stat & DS3232_REG_SR_A1F) {
  165. control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
  166. if (control < 0)
  167. goto out;
  168. /* disable alarm1 interrupt */
  169. control &= ~(DS3232_REG_CR_A1IE);
  170. i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
  171. /* clear the alarm pend flag */
  172. stat &= ~DS3232_REG_SR_A1F;
  173. i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
  174. rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
  175. }
  176. out:
  177. if (!ds3232->exiting)
  178. enable_irq(client->irq);
  179. unlock:
  180. mutex_unlock(&ds3232->mutex);
  181. }
  182. static const struct rtc_class_ops ds3232_rtc_ops = {
  183. .read_time = ds3232_read_time,
  184. .set_time = ds3232_set_time,
  185. };
  186. static int __devinit ds3232_probe(struct i2c_client *client,
  187. const struct i2c_device_id *id)
  188. {
  189. struct ds3232 *ds3232;
  190. int ret;
  191. ds3232 = kzalloc(sizeof(struct ds3232), GFP_KERNEL);
  192. if (!ds3232)
  193. return -ENOMEM;
  194. ds3232->client = client;
  195. i2c_set_clientdata(client, ds3232);
  196. INIT_WORK(&ds3232->work, ds3232_work);
  197. mutex_init(&ds3232->mutex);
  198. ret = ds3232_check_rtc_status(client);
  199. if (ret)
  200. goto out_free;
  201. ds3232->rtc = rtc_device_register(client->name, &client->dev,
  202. &ds3232_rtc_ops, THIS_MODULE);
  203. if (IS_ERR(ds3232->rtc)) {
  204. ret = PTR_ERR(ds3232->rtc);
  205. dev_err(&client->dev, "unable to register the class device\n");
  206. goto out_irq;
  207. }
  208. if (client->irq >= 0) {
  209. ret = request_irq(client->irq, ds3232_irq, 0,
  210. "ds3232", client);
  211. if (ret) {
  212. dev_err(&client->dev, "unable to request IRQ\n");
  213. goto out_free;
  214. }
  215. }
  216. return 0;
  217. out_irq:
  218. if (client->irq >= 0)
  219. free_irq(client->irq, client);
  220. out_free:
  221. kfree(ds3232);
  222. return ret;
  223. }
  224. static int __devexit ds3232_remove(struct i2c_client *client)
  225. {
  226. struct ds3232 *ds3232 = i2c_get_clientdata(client);
  227. if (client->irq >= 0) {
  228. mutex_lock(&ds3232->mutex);
  229. ds3232->exiting = 1;
  230. mutex_unlock(&ds3232->mutex);
  231. free_irq(client->irq, client);
  232. flush_scheduled_work();
  233. }
  234. rtc_device_unregister(ds3232->rtc);
  235. kfree(ds3232);
  236. return 0;
  237. }
  238. static const struct i2c_device_id ds3232_id[] = {
  239. { "ds3232", 0 },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(i2c, ds3232_id);
  243. static struct i2c_driver ds3232_driver = {
  244. .driver = {
  245. .name = "rtc-ds3232",
  246. .owner = THIS_MODULE,
  247. },
  248. .probe = ds3232_probe,
  249. .remove = __devexit_p(ds3232_remove),
  250. .id_table = ds3232_id,
  251. };
  252. static int __init ds3232_init(void)
  253. {
  254. return i2c_add_driver(&ds3232_driver);
  255. }
  256. static void __exit ds3232_exit(void)
  257. {
  258. i2c_del_driver(&ds3232_driver);
  259. }
  260. module_init(ds3232_init);
  261. module_exit(ds3232_exit);
  262. MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
  263. MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
  264. MODULE_LICENSE("GPL");