rtc-ds1374.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
  3. *
  4. * Based on code by Randy Vinson <rvinson@mvista.com>,
  5. * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
  6. *
  7. * Copyright (C) 2006-2007 Freescale Semiconductor
  8. *
  9. * 2005 (c) MontaVista Software, Inc. This file is licensed under
  10. * the terms of the GNU General Public License version 2. This program
  11. * is licensed "as is" without any warranty of any kind, whether express
  12. * or implied.
  13. */
  14. /*
  15. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  16. * recommened in .../Documentation/i2c/writing-clients section
  17. * "Sending and receiving", using SMBus level communication is preferred.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/i2c.h>
  23. #include <linux/rtc.h>
  24. #include <linux/bcd.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/pm.h>
  28. #define DS1374_REG_TOD0 0x00 /* Time of Day */
  29. #define DS1374_REG_TOD1 0x01
  30. #define DS1374_REG_TOD2 0x02
  31. #define DS1374_REG_TOD3 0x03
  32. #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
  33. #define DS1374_REG_WDALM1 0x05
  34. #define DS1374_REG_WDALM2 0x06
  35. #define DS1374_REG_CR 0x07 /* Control */
  36. #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
  37. #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
  38. #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
  39. #define DS1374_REG_SR 0x08 /* Status */
  40. #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
  41. #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
  42. #define DS1374_REG_TCR 0x09 /* Trickle Charge */
  43. static const struct i2c_device_id ds1374_id[] = {
  44. { "ds1374", 0 },
  45. { }
  46. };
  47. MODULE_DEVICE_TABLE(i2c, ds1374_id);
  48. struct ds1374 {
  49. struct i2c_client *client;
  50. struct rtc_device *rtc;
  51. struct work_struct work;
  52. /* The mutex protects alarm operations, and prevents a race
  53. * between the enable_irq() in the workqueue and the free_irq()
  54. * in the remove function.
  55. */
  56. struct mutex mutex;
  57. int exiting;
  58. };
  59. static struct i2c_driver ds1374_driver;
  60. static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
  61. int reg, int nbytes)
  62. {
  63. u8 buf[4];
  64. int ret;
  65. int i;
  66. if (nbytes > 4) {
  67. WARN_ON(1);
  68. return -EINVAL;
  69. }
  70. ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
  71. if (ret < 0)
  72. return ret;
  73. if (ret < nbytes)
  74. return -EIO;
  75. for (i = nbytes - 1, *time = 0; i >= 0; i--)
  76. *time = (*time << 8) | buf[i];
  77. return 0;
  78. }
  79. static int ds1374_write_rtc(struct i2c_client *client, u32 time,
  80. int reg, int nbytes)
  81. {
  82. u8 buf[4];
  83. int i;
  84. if (nbytes > 4) {
  85. WARN_ON(1);
  86. return -EINVAL;
  87. }
  88. for (i = 0; i < nbytes; i++) {
  89. buf[i] = time & 0xff;
  90. time >>= 8;
  91. }
  92. return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
  93. }
  94. static int ds1374_check_rtc_status(struct i2c_client *client)
  95. {
  96. int ret = 0;
  97. int control, stat;
  98. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  99. if (stat < 0)
  100. return stat;
  101. if (stat & DS1374_REG_SR_OSF)
  102. dev_warn(&client->dev,
  103. "oscillator discontinuity flagged, "
  104. "time unreliable\n");
  105. stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
  106. ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  107. if (ret < 0)
  108. return ret;
  109. /* If the alarm is pending, clear it before requesting
  110. * the interrupt, so an interrupt event isn't reported
  111. * before everything is initialized.
  112. */
  113. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  114. if (control < 0)
  115. return control;
  116. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  117. return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  118. }
  119. static int ds1374_read_time(struct device *dev, struct rtc_time *time)
  120. {
  121. struct i2c_client *client = to_i2c_client(dev);
  122. u32 itime;
  123. int ret;
  124. ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
  125. if (!ret)
  126. rtc_time_to_tm(itime, time);
  127. return ret;
  128. }
  129. static int ds1374_set_time(struct device *dev, struct rtc_time *time)
  130. {
  131. struct i2c_client *client = to_i2c_client(dev);
  132. unsigned long itime;
  133. rtc_tm_to_time(time, &itime);
  134. return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
  135. }
  136. /* The ds1374 has a decrementer for an alarm, rather than a comparator.
  137. * If the time of day is changed, then the alarm will need to be
  138. * reset.
  139. */
  140. static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  141. {
  142. struct i2c_client *client = to_i2c_client(dev);
  143. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  144. u32 now, cur_alarm;
  145. int cr, sr;
  146. int ret = 0;
  147. if (client->irq <= 0)
  148. return -EINVAL;
  149. mutex_lock(&ds1374->mutex);
  150. cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  151. if (ret < 0)
  152. goto out;
  153. sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  154. if (ret < 0)
  155. goto out;
  156. ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
  157. if (ret)
  158. goto out;
  159. ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
  160. if (ret)
  161. goto out;
  162. rtc_time_to_tm(now + cur_alarm, &alarm->time);
  163. alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
  164. alarm->pending = !!(sr & DS1374_REG_SR_AF);
  165. out:
  166. mutex_unlock(&ds1374->mutex);
  167. return ret;
  168. }
  169. static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  170. {
  171. struct i2c_client *client = to_i2c_client(dev);
  172. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  173. struct rtc_time now;
  174. unsigned long new_alarm, itime;
  175. int cr;
  176. int ret = 0;
  177. if (client->irq <= 0)
  178. return -EINVAL;
  179. ret = ds1374_read_time(dev, &now);
  180. if (ret < 0)
  181. return ret;
  182. rtc_tm_to_time(&alarm->time, &new_alarm);
  183. rtc_tm_to_time(&now, &itime);
  184. /* This can happen due to races, in addition to dates that are
  185. * truly in the past. To avoid requiring the caller to check for
  186. * races, dates in the past are assumed to be in the recent past
  187. * (i.e. not something that we'd rather the caller know about via
  188. * an error), and the alarm is set to go off as soon as possible.
  189. */
  190. if (time_before_eq(new_alarm, itime))
  191. new_alarm = 1;
  192. else
  193. new_alarm -= itime;
  194. mutex_lock(&ds1374->mutex);
  195. ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  196. if (ret < 0)
  197. goto out;
  198. /* Disable any existing alarm before setting the new one
  199. * (or lack thereof). */
  200. cr &= ~DS1374_REG_CR_WACE;
  201. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  202. if (ret < 0)
  203. goto out;
  204. ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
  205. if (ret)
  206. goto out;
  207. if (alarm->enabled) {
  208. cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  209. cr &= ~DS1374_REG_CR_WDALM;
  210. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  211. }
  212. out:
  213. mutex_unlock(&ds1374->mutex);
  214. return ret;
  215. }
  216. static irqreturn_t ds1374_irq(int irq, void *dev_id)
  217. {
  218. struct i2c_client *client = dev_id;
  219. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  220. disable_irq_nosync(irq);
  221. schedule_work(&ds1374->work);
  222. return IRQ_HANDLED;
  223. }
  224. static void ds1374_work(struct work_struct *work)
  225. {
  226. struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
  227. struct i2c_client *client = ds1374->client;
  228. int stat, control;
  229. mutex_lock(&ds1374->mutex);
  230. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  231. if (stat < 0)
  232. goto unlock;
  233. if (stat & DS1374_REG_SR_AF) {
  234. stat &= ~DS1374_REG_SR_AF;
  235. i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  236. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  237. if (control < 0)
  238. goto out;
  239. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  240. i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  241. rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
  242. }
  243. out:
  244. if (!ds1374->exiting)
  245. enable_irq(client->irq);
  246. unlock:
  247. mutex_unlock(&ds1374->mutex);
  248. }
  249. static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
  250. {
  251. struct i2c_client *client = to_i2c_client(dev);
  252. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  253. int ret;
  254. mutex_lock(&ds1374->mutex);
  255. ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  256. if (ret < 0)
  257. goto out;
  258. if (enabled) {
  259. ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  260. ret &= ~DS1374_REG_CR_WDALM;
  261. } else {
  262. ret &= ~DS1374_REG_CR_WACE;
  263. }
  264. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
  265. out:
  266. mutex_unlock(&ds1374->mutex);
  267. return ret;
  268. }
  269. static const struct rtc_class_ops ds1374_rtc_ops = {
  270. .read_time = ds1374_read_time,
  271. .set_time = ds1374_set_time,
  272. .read_alarm = ds1374_read_alarm,
  273. .set_alarm = ds1374_set_alarm,
  274. .alarm_irq_enable = ds1374_alarm_irq_enable,
  275. };
  276. static int ds1374_probe(struct i2c_client *client,
  277. const struct i2c_device_id *id)
  278. {
  279. struct ds1374 *ds1374;
  280. int ret;
  281. ds1374 = kzalloc(sizeof(struct ds1374), GFP_KERNEL);
  282. if (!ds1374)
  283. return -ENOMEM;
  284. ds1374->client = client;
  285. i2c_set_clientdata(client, ds1374);
  286. INIT_WORK(&ds1374->work, ds1374_work);
  287. mutex_init(&ds1374->mutex);
  288. ret = ds1374_check_rtc_status(client);
  289. if (ret)
  290. goto out_free;
  291. if (client->irq > 0) {
  292. ret = request_irq(client->irq, ds1374_irq, 0,
  293. "ds1374", client);
  294. if (ret) {
  295. dev_err(&client->dev, "unable to request IRQ\n");
  296. goto out_free;
  297. }
  298. device_set_wakeup_capable(&client->dev, 1);
  299. }
  300. ds1374->rtc = rtc_device_register(client->name, &client->dev,
  301. &ds1374_rtc_ops, THIS_MODULE);
  302. if (IS_ERR(ds1374->rtc)) {
  303. ret = PTR_ERR(ds1374->rtc);
  304. dev_err(&client->dev, "unable to register the class device\n");
  305. goto out_irq;
  306. }
  307. return 0;
  308. out_irq:
  309. if (client->irq > 0)
  310. free_irq(client->irq, client);
  311. out_free:
  312. kfree(ds1374);
  313. return ret;
  314. }
  315. static int __devexit ds1374_remove(struct i2c_client *client)
  316. {
  317. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  318. if (client->irq > 0) {
  319. mutex_lock(&ds1374->mutex);
  320. ds1374->exiting = 1;
  321. mutex_unlock(&ds1374->mutex);
  322. free_irq(client->irq, client);
  323. cancel_work_sync(&ds1374->work);
  324. }
  325. rtc_device_unregister(ds1374->rtc);
  326. kfree(ds1374);
  327. return 0;
  328. }
  329. #ifdef CONFIG_PM
  330. static int ds1374_suspend(struct device *dev)
  331. {
  332. struct i2c_client *client = to_i2c_client(dev);
  333. if (client->irq >= 0 && device_may_wakeup(&client->dev))
  334. enable_irq_wake(client->irq);
  335. return 0;
  336. }
  337. static int ds1374_resume(struct device *dev)
  338. {
  339. struct i2c_client *client = to_i2c_client(dev);
  340. if (client->irq >= 0 && device_may_wakeup(&client->dev))
  341. disable_irq_wake(client->irq);
  342. return 0;
  343. }
  344. static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
  345. #define DS1374_PM (&ds1374_pm)
  346. #else
  347. #define DS1374_PM NULL
  348. #endif
  349. static struct i2c_driver ds1374_driver = {
  350. .driver = {
  351. .name = "rtc-ds1374",
  352. .owner = THIS_MODULE,
  353. .pm = DS1374_PM,
  354. },
  355. .probe = ds1374_probe,
  356. .remove = __devexit_p(ds1374_remove),
  357. .id_table = ds1374_id,
  358. };
  359. module_i2c_driver(ds1374_driver);
  360. MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
  361. MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
  362. MODULE_LICENSE("GPL");