rtc-m48t86.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ST M48T86 / Dallas DS12887 RTC driver
  3. * Copyright (c) 2006 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This drivers only supports the clock running in BCD and 24H mode.
  12. * If it will be ever adapted to binary and 12H mode, care must be taken
  13. * to not introduce bugs.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/rtc.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/platform_data/rtc-m48t86.h>
  19. #include <linux/bcd.h>
  20. #define M48T86_REG_SEC 0x00
  21. #define M48T86_REG_SECALRM 0x01
  22. #define M48T86_REG_MIN 0x02
  23. #define M48T86_REG_MINALRM 0x03
  24. #define M48T86_REG_HOUR 0x04
  25. #define M48T86_REG_HOURALRM 0x05
  26. #define M48T86_REG_DOW 0x06 /* 1 = sunday */
  27. #define M48T86_REG_DOM 0x07
  28. #define M48T86_REG_MONTH 0x08 /* 1 - 12 */
  29. #define M48T86_REG_YEAR 0x09 /* 0 - 99 */
  30. #define M48T86_REG_A 0x0A
  31. #define M48T86_REG_B 0x0B
  32. #define M48T86_REG_C 0x0C
  33. #define M48T86_REG_D 0x0D
  34. #define M48T86_REG_B_H24 (1 << 1)
  35. #define M48T86_REG_B_DM (1 << 2)
  36. #define M48T86_REG_B_SET (1 << 7)
  37. #define M48T86_REG_D_VRT (1 << 7)
  38. static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
  39. {
  40. unsigned char reg;
  41. struct platform_device *pdev = to_platform_device(dev);
  42. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  43. reg = ops->readbyte(M48T86_REG_B);
  44. if (reg & M48T86_REG_B_DM) {
  45. /* data (binary) mode */
  46. tm->tm_sec = ops->readbyte(M48T86_REG_SEC);
  47. tm->tm_min = ops->readbyte(M48T86_REG_MIN);
  48. tm->tm_hour = ops->readbyte(M48T86_REG_HOUR) & 0x3F;
  49. tm->tm_mday = ops->readbyte(M48T86_REG_DOM);
  50. /* tm_mon is 0-11 */
  51. tm->tm_mon = ops->readbyte(M48T86_REG_MONTH) - 1;
  52. tm->tm_year = ops->readbyte(M48T86_REG_YEAR) + 100;
  53. tm->tm_wday = ops->readbyte(M48T86_REG_DOW);
  54. } else {
  55. /* bcd mode */
  56. tm->tm_sec = bcd2bin(ops->readbyte(M48T86_REG_SEC));
  57. tm->tm_min = bcd2bin(ops->readbyte(M48T86_REG_MIN));
  58. tm->tm_hour = bcd2bin(ops->readbyte(M48T86_REG_HOUR) & 0x3F);
  59. tm->tm_mday = bcd2bin(ops->readbyte(M48T86_REG_DOM));
  60. /* tm_mon is 0-11 */
  61. tm->tm_mon = bcd2bin(ops->readbyte(M48T86_REG_MONTH)) - 1;
  62. tm->tm_year = bcd2bin(ops->readbyte(M48T86_REG_YEAR)) + 100;
  63. tm->tm_wday = bcd2bin(ops->readbyte(M48T86_REG_DOW));
  64. }
  65. /* correct the hour if the clock is in 12h mode */
  66. if (!(reg & M48T86_REG_B_H24))
  67. if (ops->readbyte(M48T86_REG_HOUR) & 0x80)
  68. tm->tm_hour += 12;
  69. return rtc_valid_tm(tm);
  70. }
  71. static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
  72. {
  73. unsigned char reg;
  74. struct platform_device *pdev = to_platform_device(dev);
  75. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  76. reg = ops->readbyte(M48T86_REG_B);
  77. /* update flag and 24h mode */
  78. reg |= M48T86_REG_B_SET | M48T86_REG_B_H24;
  79. ops->writebyte(reg, M48T86_REG_B);
  80. if (reg & M48T86_REG_B_DM) {
  81. /* data (binary) mode */
  82. ops->writebyte(tm->tm_sec, M48T86_REG_SEC);
  83. ops->writebyte(tm->tm_min, M48T86_REG_MIN);
  84. ops->writebyte(tm->tm_hour, M48T86_REG_HOUR);
  85. ops->writebyte(tm->tm_mday, M48T86_REG_DOM);
  86. ops->writebyte(tm->tm_mon + 1, M48T86_REG_MONTH);
  87. ops->writebyte(tm->tm_year % 100, M48T86_REG_YEAR);
  88. ops->writebyte(tm->tm_wday, M48T86_REG_DOW);
  89. } else {
  90. /* bcd mode */
  91. ops->writebyte(bin2bcd(tm->tm_sec), M48T86_REG_SEC);
  92. ops->writebyte(bin2bcd(tm->tm_min), M48T86_REG_MIN);
  93. ops->writebyte(bin2bcd(tm->tm_hour), M48T86_REG_HOUR);
  94. ops->writebyte(bin2bcd(tm->tm_mday), M48T86_REG_DOM);
  95. ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH);
  96. ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR);
  97. ops->writebyte(bin2bcd(tm->tm_wday), M48T86_REG_DOW);
  98. }
  99. /* update ended */
  100. reg &= ~M48T86_REG_B_SET;
  101. ops->writebyte(reg, M48T86_REG_B);
  102. return 0;
  103. }
  104. static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
  105. {
  106. unsigned char reg;
  107. struct platform_device *pdev = to_platform_device(dev);
  108. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  109. reg = ops->readbyte(M48T86_REG_B);
  110. seq_printf(seq, "mode\t\t: %s\n",
  111. (reg & M48T86_REG_B_DM) ? "binary" : "bcd");
  112. reg = ops->readbyte(M48T86_REG_D);
  113. seq_printf(seq, "battery\t\t: %s\n",
  114. (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
  115. return 0;
  116. }
  117. static const struct rtc_class_ops m48t86_rtc_ops = {
  118. .read_time = m48t86_rtc_read_time,
  119. .set_time = m48t86_rtc_set_time,
  120. .proc = m48t86_rtc_proc,
  121. };
  122. static int m48t86_rtc_probe(struct platform_device *dev)
  123. {
  124. unsigned char reg;
  125. struct m48t86_ops *ops = dev_get_platdata(&dev->dev);
  126. struct rtc_device *rtc;
  127. rtc = devm_rtc_device_register(&dev->dev, "m48t86",
  128. &m48t86_rtc_ops, THIS_MODULE);
  129. if (IS_ERR(rtc))
  130. return PTR_ERR(rtc);
  131. platform_set_drvdata(dev, rtc);
  132. /* read battery status */
  133. reg = ops->readbyte(M48T86_REG_D);
  134. dev_info(&dev->dev, "battery %s\n",
  135. (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
  136. return 0;
  137. }
  138. static struct platform_driver m48t86_rtc_platform_driver = {
  139. .driver = {
  140. .name = "rtc-m48t86",
  141. },
  142. .probe = m48t86_rtc_probe,
  143. };
  144. module_platform_driver(m48t86_rtc_platform_driver);
  145. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  146. MODULE_DESCRIPTION("M48T86 RTC driver");
  147. MODULE_LICENSE("GPL");
  148. MODULE_ALIAS("platform:rtc-m48t86");