rtc-m48t86.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = pdev->dev.platform_data;
  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 0;
  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 = pdev->dev.platform_data;
  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 = pdev->dev.platform_data;
  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 __devinit m48t86_rtc_probe(struct platform_device *dev)
  124. {
  125. unsigned char reg;
  126. struct m48t86_ops *ops = dev->dev.platform_data;
  127. struct rtc_device *rtc = rtc_device_register("m48t86",
  128. &dev->dev, &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 int __devexit m48t86_rtc_remove(struct platform_device *dev)
  139. {
  140. struct rtc_device *rtc = platform_get_drvdata(dev);
  141. if (rtc)
  142. rtc_device_unregister(rtc);
  143. platform_set_drvdata(dev, NULL);
  144. return 0;
  145. }
  146. static struct platform_driver m48t86_rtc_platform_driver = {
  147. .driver = {
  148. .name = "rtc-m48t86",
  149. .owner = THIS_MODULE,
  150. },
  151. .probe = m48t86_rtc_probe,
  152. .remove = __devexit_p(m48t86_rtc_remove),
  153. };
  154. static int __init m48t86_rtc_init(void)
  155. {
  156. return platform_driver_register(&m48t86_rtc_platform_driver);
  157. }
  158. static void __exit m48t86_rtc_exit(void)
  159. {
  160. platform_driver_unregister(&m48t86_rtc_platform_driver);
  161. }
  162. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  163. MODULE_DESCRIPTION("M48T86 RTC driver");
  164. MODULE_LICENSE("GPL");
  165. MODULE_VERSION(DRV_VERSION);
  166. module_init(m48t86_rtc_init);
  167. module_exit(m48t86_rtc_exit);