rtc-m48t86.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/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. #define DRV_VERSION "0.1"
  39. static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
  40. {
  41. unsigned char reg;
  42. struct platform_device *pdev = to_platform_device(dev);
  43. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  44. reg = ops->readbyte(M48T86_REG_B);
  45. if (reg & M48T86_REG_B_DM) {
  46. /* data (binary) mode */
  47. tm->tm_sec = ops->readbyte(M48T86_REG_SEC);
  48. tm->tm_min = ops->readbyte(M48T86_REG_MIN);
  49. tm->tm_hour = ops->readbyte(M48T86_REG_HOUR) & 0x3F;
  50. tm->tm_mday = ops->readbyte(M48T86_REG_DOM);
  51. /* tm_mon is 0-11 */
  52. tm->tm_mon = ops->readbyte(M48T86_REG_MONTH) - 1;
  53. tm->tm_year = ops->readbyte(M48T86_REG_YEAR) + 100;
  54. tm->tm_wday = ops->readbyte(M48T86_REG_DOW);
  55. } else {
  56. /* bcd mode */
  57. tm->tm_sec = bcd2bin(ops->readbyte(M48T86_REG_SEC));
  58. tm->tm_min = bcd2bin(ops->readbyte(M48T86_REG_MIN));
  59. tm->tm_hour = bcd2bin(ops->readbyte(M48T86_REG_HOUR) & 0x3F);
  60. tm->tm_mday = bcd2bin(ops->readbyte(M48T86_REG_DOM));
  61. /* tm_mon is 0-11 */
  62. tm->tm_mon = bcd2bin(ops->readbyte(M48T86_REG_MONTH)) - 1;
  63. tm->tm_year = bcd2bin(ops->readbyte(M48T86_REG_YEAR)) + 100;
  64. tm->tm_wday = bcd2bin(ops->readbyte(M48T86_REG_DOW));
  65. }
  66. /* correct the hour if the clock is in 12h mode */
  67. if (!(reg & M48T86_REG_B_H24))
  68. if (ops->readbyte(M48T86_REG_HOUR) & 0x80)
  69. tm->tm_hour += 12;
  70. return rtc_valid_tm(tm);
  71. }
  72. static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. unsigned char reg;
  75. struct platform_device *pdev = to_platform_device(dev);
  76. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  77. reg = ops->readbyte(M48T86_REG_B);
  78. /* update flag and 24h mode */
  79. reg |= M48T86_REG_B_SET | M48T86_REG_B_H24;
  80. ops->writebyte(reg, M48T86_REG_B);
  81. if (reg & M48T86_REG_B_DM) {
  82. /* data (binary) mode */
  83. ops->writebyte(tm->tm_sec, M48T86_REG_SEC);
  84. ops->writebyte(tm->tm_min, M48T86_REG_MIN);
  85. ops->writebyte(tm->tm_hour, M48T86_REG_HOUR);
  86. ops->writebyte(tm->tm_mday, M48T86_REG_DOM);
  87. ops->writebyte(tm->tm_mon + 1, M48T86_REG_MONTH);
  88. ops->writebyte(tm->tm_year % 100, M48T86_REG_YEAR);
  89. ops->writebyte(tm->tm_wday, M48T86_REG_DOW);
  90. } else {
  91. /* bcd mode */
  92. ops->writebyte(bin2bcd(tm->tm_sec), M48T86_REG_SEC);
  93. ops->writebyte(bin2bcd(tm->tm_min), M48T86_REG_MIN);
  94. ops->writebyte(bin2bcd(tm->tm_hour), M48T86_REG_HOUR);
  95. ops->writebyte(bin2bcd(tm->tm_mday), M48T86_REG_DOM);
  96. ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH);
  97. ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR);
  98. ops->writebyte(bin2bcd(tm->tm_wday), M48T86_REG_DOW);
  99. }
  100. /* update ended */
  101. reg &= ~M48T86_REG_B_SET;
  102. ops->writebyte(reg, M48T86_REG_B);
  103. return 0;
  104. }
  105. static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
  106. {
  107. unsigned char reg;
  108. struct platform_device *pdev = to_platform_device(dev);
  109. struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
  110. reg = ops->readbyte(M48T86_REG_B);
  111. seq_printf(seq, "mode\t\t: %s\n",
  112. (reg & M48T86_REG_B_DM) ? "binary" : "bcd");
  113. reg = ops->readbyte(M48T86_REG_D);
  114. seq_printf(seq, "battery\t\t: %s\n",
  115. (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
  116. return 0;
  117. }
  118. static const struct rtc_class_ops m48t86_rtc_ops = {
  119. .read_time = m48t86_rtc_read_time,
  120. .set_time = m48t86_rtc_set_time,
  121. .proc = m48t86_rtc_proc,
  122. };
  123. static int m48t86_rtc_probe(struct platform_device *dev)
  124. {
  125. unsigned char reg;
  126. struct m48t86_ops *ops = dev_get_platdata(&dev->dev);
  127. struct rtc_device *rtc;
  128. rtc = devm_rtc_device_register(&dev->dev, "m48t86",
  129. &m48t86_rtc_ops, THIS_MODULE);
  130. if (IS_ERR(rtc))
  131. return PTR_ERR(rtc);
  132. platform_set_drvdata(dev, rtc);
  133. /* read battery status */
  134. reg = ops->readbyte(M48T86_REG_D);
  135. dev_info(&dev->dev, "battery %s\n",
  136. (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
  137. return 0;
  138. }
  139. static struct platform_driver m48t86_rtc_platform_driver = {
  140. .driver = {
  141. .name = "rtc-m48t86",
  142. },
  143. .probe = m48t86_rtc_probe,
  144. };
  145. module_platform_driver(m48t86_rtc_platform_driver);
  146. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  147. MODULE_DESCRIPTION("M48T86 RTC driver");
  148. MODULE_LICENSE("GPL");
  149. MODULE_VERSION(DRV_VERSION);
  150. MODULE_ALIAS("platform:rtc-m48t86");