rtc-ds1742.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * An rtc driver for the Dallas DS1742
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Copyright (C) 2006 Torsten Ertbjerg Rasmussen <tr@newtec.dk>
  11. * - nvram size determined from resource
  12. * - this ds1742 driver now supports ds1743.
  13. */
  14. #include <linux/bcd.h>
  15. #include <linux/kernel.h>
  16. #include <linux/gfp.h>
  17. #include <linux/delay.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/rtc.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #define RTC_SIZE 8
  26. #define RTC_CONTROL 0
  27. #define RTC_CENTURY 0
  28. #define RTC_SECONDS 1
  29. #define RTC_MINUTES 2
  30. #define RTC_HOURS 3
  31. #define RTC_DAY 4
  32. #define RTC_DATE 5
  33. #define RTC_MONTH 6
  34. #define RTC_YEAR 7
  35. #define RTC_CENTURY_MASK 0x3f
  36. #define RTC_SECONDS_MASK 0x7f
  37. #define RTC_DAY_MASK 0x07
  38. /* Bits in the Control/Century register */
  39. #define RTC_WRITE 0x80
  40. #define RTC_READ 0x40
  41. /* Bits in the Seconds register */
  42. #define RTC_STOP 0x80
  43. /* Bits in the Day register */
  44. #define RTC_BATT_FLAG 0x80
  45. struct rtc_plat_data {
  46. void __iomem *ioaddr_nvram;
  47. void __iomem *ioaddr_rtc;
  48. unsigned long last_jiffies;
  49. };
  50. static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
  51. {
  52. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  53. void __iomem *ioaddr = pdata->ioaddr_rtc;
  54. u8 century;
  55. century = bin2bcd((tm->tm_year + 1900) / 100);
  56. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  57. writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
  58. writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
  59. writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
  60. writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
  61. writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
  62. writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
  63. writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
  64. /* RTC_CENTURY and RTC_CONTROL share same register */
  65. writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
  66. writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  67. return 0;
  68. }
  69. static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  72. void __iomem *ioaddr = pdata->ioaddr_rtc;
  73. unsigned int year, month, day, hour, minute, second, week;
  74. unsigned int century;
  75. /* give enough time to update RTC in case of continuous read */
  76. if (pdata->last_jiffies == jiffies)
  77. msleep(1);
  78. pdata->last_jiffies = jiffies;
  79. writeb(RTC_READ, ioaddr + RTC_CONTROL);
  80. second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
  81. minute = readb(ioaddr + RTC_MINUTES);
  82. hour = readb(ioaddr + RTC_HOURS);
  83. day = readb(ioaddr + RTC_DATE);
  84. week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
  85. month = readb(ioaddr + RTC_MONTH);
  86. year = readb(ioaddr + RTC_YEAR);
  87. century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  88. writeb(0, ioaddr + RTC_CONTROL);
  89. tm->tm_sec = bcd2bin(second);
  90. tm->tm_min = bcd2bin(minute);
  91. tm->tm_hour = bcd2bin(hour);
  92. tm->tm_mday = bcd2bin(day);
  93. tm->tm_wday = bcd2bin(week);
  94. tm->tm_mon = bcd2bin(month) - 1;
  95. /* year is 1900 + tm->tm_year */
  96. tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
  97. return 0;
  98. }
  99. static const struct rtc_class_ops ds1742_rtc_ops = {
  100. .read_time = ds1742_rtc_read_time,
  101. .set_time = ds1742_rtc_set_time,
  102. };
  103. static int ds1742_nvram_read(void *priv, unsigned int pos, void *val,
  104. size_t bytes)
  105. {
  106. struct rtc_plat_data *pdata = priv;
  107. void __iomem *ioaddr = pdata->ioaddr_nvram;
  108. u8 *buf = val;
  109. for (; bytes; bytes--)
  110. *buf++ = readb(ioaddr + pos++);
  111. return 0;
  112. }
  113. static int ds1742_nvram_write(void *priv, unsigned int pos, void *val,
  114. size_t bytes)
  115. {
  116. struct rtc_plat_data *pdata = priv;
  117. void __iomem *ioaddr = pdata->ioaddr_nvram;
  118. u8 *buf = val;
  119. for (; bytes; bytes--)
  120. writeb(*buf++, ioaddr + pos++);
  121. return 0;
  122. }
  123. static int ds1742_rtc_probe(struct platform_device *pdev)
  124. {
  125. struct rtc_device *rtc;
  126. struct resource *res;
  127. unsigned int cen, sec;
  128. struct rtc_plat_data *pdata;
  129. void __iomem *ioaddr;
  130. int ret = 0;
  131. struct nvmem_config nvmem_cfg = {
  132. .name = "ds1742_nvram",
  133. .reg_read = ds1742_nvram_read,
  134. .reg_write = ds1742_nvram_write,
  135. };
  136. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  137. if (!pdata)
  138. return -ENOMEM;
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. ioaddr = devm_ioremap_resource(&pdev->dev, res);
  141. if (IS_ERR(ioaddr))
  142. return PTR_ERR(ioaddr);
  143. pdata->ioaddr_nvram = ioaddr;
  144. pdata->ioaddr_rtc = ioaddr + resource_size(res) - RTC_SIZE;
  145. nvmem_cfg.size = resource_size(res) - RTC_SIZE;
  146. nvmem_cfg.priv = pdata;
  147. /* turn RTC on if it was not on */
  148. ioaddr = pdata->ioaddr_rtc;
  149. sec = readb(ioaddr + RTC_SECONDS);
  150. if (sec & RTC_STOP) {
  151. sec &= RTC_SECONDS_MASK;
  152. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  153. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  154. writeb(sec, ioaddr + RTC_SECONDS);
  155. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  156. }
  157. if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
  158. dev_warn(&pdev->dev, "voltage-low detected.\n");
  159. pdata->last_jiffies = jiffies;
  160. platform_set_drvdata(pdev, pdata);
  161. rtc = devm_rtc_allocate_device(&pdev->dev);
  162. if (IS_ERR(rtc))
  163. return PTR_ERR(rtc);
  164. rtc->ops = &ds1742_rtc_ops;
  165. rtc->nvram_old_abi = true;
  166. ret = rtc_register_device(rtc);
  167. if (ret)
  168. return ret;
  169. if (rtc_nvmem_register(rtc, &nvmem_cfg))
  170. dev_err(&pdev->dev, "Unable to register nvmem\n");
  171. return 0;
  172. }
  173. static const struct of_device_id __maybe_unused ds1742_rtc_of_match[] = {
  174. { .compatible = "maxim,ds1742", },
  175. { }
  176. };
  177. MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match);
  178. static struct platform_driver ds1742_rtc_driver = {
  179. .probe = ds1742_rtc_probe,
  180. .driver = {
  181. .name = "rtc-ds1742",
  182. .of_match_table = of_match_ptr(ds1742_rtc_of_match),
  183. },
  184. };
  185. module_platform_driver(ds1742_rtc_driver);
  186. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  187. MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
  188. MODULE_LICENSE("GPL");
  189. MODULE_ALIAS("platform:rtc-ds1742");