rtc-dm355evm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * rtc-dm355evm.c - access battery-backed counter in MSP430 firmware
  3. *
  4. * Copyright (c) 2008 by David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/rtc.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/i2c/dm355evm_msp.h>
  16. /*
  17. * The MSP430 firmware on the DM355 EVM uses a watch crystal to feed
  18. * a 1 Hz counter. When a backup battery is supplied, that makes a
  19. * reasonable RTC for applications where alarms and non-NTP drift
  20. * compensation aren't important.
  21. *
  22. * The only real glitch is the inability to read or write all four
  23. * counter bytes atomically: the count may increment in the middle
  24. * of an operation, causing trouble when the LSB rolls over.
  25. *
  26. * This driver was tested with firmware revision A4.
  27. */
  28. union evm_time {
  29. u8 bytes[4];
  30. u32 value;
  31. };
  32. static int dm355evm_rtc_read_time(struct device *dev, struct rtc_time *tm)
  33. {
  34. union evm_time time;
  35. int status;
  36. int tries = 0;
  37. do {
  38. /*
  39. * Read LSB(0) to MSB(3) bytes. Defend against the counter
  40. * rolling over by re-reading until the value is stable,
  41. * and assuming the four reads take at most a few seconds.
  42. */
  43. status = dm355evm_msp_read(DM355EVM_MSP_RTC_0);
  44. if (status < 0)
  45. return status;
  46. if (tries && time.bytes[0] == status)
  47. break;
  48. time.bytes[0] = status;
  49. status = dm355evm_msp_read(DM355EVM_MSP_RTC_1);
  50. if (status < 0)
  51. return status;
  52. if (tries && time.bytes[1] == status)
  53. break;
  54. time.bytes[1] = status;
  55. status = dm355evm_msp_read(DM355EVM_MSP_RTC_2);
  56. if (status < 0)
  57. return status;
  58. if (tries && time.bytes[2] == status)
  59. break;
  60. time.bytes[2] = status;
  61. status = dm355evm_msp_read(DM355EVM_MSP_RTC_3);
  62. if (status < 0)
  63. return status;
  64. if (tries && time.bytes[3] == status)
  65. break;
  66. time.bytes[3] = status;
  67. } while (++tries < 5);
  68. dev_dbg(dev, "read timestamp %08x\n", time.value);
  69. rtc_time_to_tm(le32_to_cpu(time.value), tm);
  70. return 0;
  71. }
  72. static int dm355evm_rtc_set_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. union evm_time time;
  75. unsigned long value;
  76. int status;
  77. rtc_tm_to_time(tm, &value);
  78. time.value = cpu_to_le32(value);
  79. dev_dbg(dev, "write timestamp %08x\n", time.value);
  80. /*
  81. * REVISIT handle non-atomic writes ... maybe just retry until
  82. * byte[1] sticks (no rollover)?
  83. */
  84. status = dm355evm_msp_write(time.bytes[0], DM355EVM_MSP_RTC_0);
  85. if (status < 0)
  86. return status;
  87. status = dm355evm_msp_write(time.bytes[1], DM355EVM_MSP_RTC_1);
  88. if (status < 0)
  89. return status;
  90. status = dm355evm_msp_write(time.bytes[2], DM355EVM_MSP_RTC_2);
  91. if (status < 0)
  92. return status;
  93. status = dm355evm_msp_write(time.bytes[3], DM355EVM_MSP_RTC_3);
  94. if (status < 0)
  95. return status;
  96. return 0;
  97. }
  98. static struct rtc_class_ops dm355evm_rtc_ops = {
  99. .read_time = dm355evm_rtc_read_time,
  100. .set_time = dm355evm_rtc_set_time,
  101. };
  102. /*----------------------------------------------------------------------*/
  103. static int __devinit dm355evm_rtc_probe(struct platform_device *pdev)
  104. {
  105. struct rtc_device *rtc;
  106. rtc = rtc_device_register(pdev->name,
  107. &pdev->dev, &dm355evm_rtc_ops, THIS_MODULE);
  108. if (IS_ERR(rtc)) {
  109. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  110. PTR_ERR(rtc));
  111. return PTR_ERR(rtc);
  112. }
  113. platform_set_drvdata(pdev, rtc);
  114. return 0;
  115. }
  116. static int __devexit dm355evm_rtc_remove(struct platform_device *pdev)
  117. {
  118. struct rtc_device *rtc = platform_get_drvdata(pdev);
  119. rtc_device_unregister(rtc);
  120. platform_set_drvdata(pdev, NULL);
  121. return 0;
  122. }
  123. /*
  124. * I2C is used to talk to the MSP430, but this platform device is
  125. * exposed by an MFD driver that manages I2C communications.
  126. */
  127. static struct platform_driver rtc_dm355evm_driver = {
  128. .probe = dm355evm_rtc_probe,
  129. .remove = __devexit_p(dm355evm_rtc_remove),
  130. .driver = {
  131. .owner = THIS_MODULE,
  132. .name = "rtc-dm355evm",
  133. },
  134. };
  135. static int __init dm355evm_rtc_init(void)
  136. {
  137. return platform_driver_register(&rtc_dm355evm_driver);
  138. }
  139. module_init(dm355evm_rtc_init);
  140. static void __exit dm355evm_rtc_exit(void)
  141. {
  142. platform_driver_unregister(&rtc_dm355evm_driver);
  143. }
  144. module_exit(dm355evm_rtc_exit);
  145. MODULE_LICENSE("GPL");