rtc-dm355evm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <linux/module.h>
  17. /*
  18. * The MSP430 firmware on the DM355 EVM uses a watch crystal to feed
  19. * a 1 Hz counter. When a backup battery is supplied, that makes a
  20. * reasonable RTC for applications where alarms and non-NTP drift
  21. * compensation aren't important.
  22. *
  23. * The only real glitch is the inability to read or write all four
  24. * counter bytes atomically: the count may increment in the middle
  25. * of an operation, causing trouble when the LSB rolls over.
  26. *
  27. * This driver was tested with firmware revision A4.
  28. */
  29. union evm_time {
  30. u8 bytes[4];
  31. u32 value;
  32. };
  33. static int dm355evm_rtc_read_time(struct device *dev, struct rtc_time *tm)
  34. {
  35. union evm_time time;
  36. int status;
  37. int tries = 0;
  38. do {
  39. /*
  40. * Read LSB(0) to MSB(3) bytes. Defend against the counter
  41. * rolling over by re-reading until the value is stable,
  42. * and assuming the four reads take at most a few seconds.
  43. */
  44. status = dm355evm_msp_read(DM355EVM_MSP_RTC_0);
  45. if (status < 0)
  46. return status;
  47. if (tries && time.bytes[0] == status)
  48. break;
  49. time.bytes[0] = status;
  50. status = dm355evm_msp_read(DM355EVM_MSP_RTC_1);
  51. if (status < 0)
  52. return status;
  53. if (tries && time.bytes[1] == status)
  54. break;
  55. time.bytes[1] = status;
  56. status = dm355evm_msp_read(DM355EVM_MSP_RTC_2);
  57. if (status < 0)
  58. return status;
  59. if (tries && time.bytes[2] == status)
  60. break;
  61. time.bytes[2] = status;
  62. status = dm355evm_msp_read(DM355EVM_MSP_RTC_3);
  63. if (status < 0)
  64. return status;
  65. if (tries && time.bytes[3] == status)
  66. break;
  67. time.bytes[3] = status;
  68. } while (++tries < 5);
  69. dev_dbg(dev, "read timestamp %08x\n", time.value);
  70. rtc_time_to_tm(le32_to_cpu(time.value), tm);
  71. return 0;
  72. }
  73. static int dm355evm_rtc_set_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. union evm_time time;
  76. unsigned long value;
  77. int status;
  78. rtc_tm_to_time(tm, &value);
  79. time.value = cpu_to_le32(value);
  80. dev_dbg(dev, "write timestamp %08x\n", time.value);
  81. /*
  82. * REVISIT handle non-atomic writes ... maybe just retry until
  83. * byte[1] sticks (no rollover)?
  84. */
  85. status = dm355evm_msp_write(time.bytes[0], DM355EVM_MSP_RTC_0);
  86. if (status < 0)
  87. return status;
  88. status = dm355evm_msp_write(time.bytes[1], DM355EVM_MSP_RTC_1);
  89. if (status < 0)
  90. return status;
  91. status = dm355evm_msp_write(time.bytes[2], DM355EVM_MSP_RTC_2);
  92. if (status < 0)
  93. return status;
  94. status = dm355evm_msp_write(time.bytes[3], DM355EVM_MSP_RTC_3);
  95. if (status < 0)
  96. return status;
  97. return 0;
  98. }
  99. static struct rtc_class_ops dm355evm_rtc_ops = {
  100. .read_time = dm355evm_rtc_read_time,
  101. .set_time = dm355evm_rtc_set_time,
  102. };
  103. /*----------------------------------------------------------------------*/
  104. static int dm355evm_rtc_probe(struct platform_device *pdev)
  105. {
  106. struct rtc_device *rtc;
  107. rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  108. &dm355evm_rtc_ops, THIS_MODULE);
  109. if (IS_ERR(rtc)) {
  110. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  111. PTR_ERR(rtc));
  112. return PTR_ERR(rtc);
  113. }
  114. platform_set_drvdata(pdev, rtc);
  115. return 0;
  116. }
  117. /*
  118. * I2C is used to talk to the MSP430, but this platform device is
  119. * exposed by an MFD driver that manages I2C communications.
  120. */
  121. static struct platform_driver rtc_dm355evm_driver = {
  122. .probe = dm355evm_rtc_probe,
  123. .driver = {
  124. .name = "rtc-dm355evm",
  125. },
  126. };
  127. module_platform_driver(rtc_dm355evm_driver);
  128. MODULE_LICENSE("GPL");