rtc-ds1742.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * An rtc driver for the Dallas DS1742
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <[email protected]>
  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 <[email protected]>
  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. size_t size_nvram;
  49. unsigned long last_jiffies;
  50. struct bin_attribute nvram_attr;
  51. };
  52. static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
  53. {
  54. struct platform_device *pdev = to_platform_device(dev);
  55. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  56. void __iomem *ioaddr = pdata->ioaddr_rtc;
  57. u8 century;
  58. century = bin2bcd((tm->tm_year + 1900) / 100);
  59. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  60. writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
  61. writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
  62. writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
  63. writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
  64. writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
  65. writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
  66. writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
  67. /* RTC_CENTURY and RTC_CONTROL share same register */
  68. writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
  69. writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  70. return 0;
  71. }
  72. static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. struct platform_device *pdev = to_platform_device(dev);
  75. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  76. void __iomem *ioaddr = pdata->ioaddr_rtc;
  77. unsigned int year, month, day, hour, minute, second, week;
  78. unsigned int century;
  79. /* give enough time to update RTC in case of continuous read */
  80. if (pdata->last_jiffies == jiffies)
  81. msleep(1);
  82. pdata->last_jiffies = jiffies;
  83. writeb(RTC_READ, ioaddr + RTC_CONTROL);
  84. second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
  85. minute = readb(ioaddr + RTC_MINUTES);
  86. hour = readb(ioaddr + RTC_HOURS);
  87. day = readb(ioaddr + RTC_DATE);
  88. week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
  89. month = readb(ioaddr + RTC_MONTH);
  90. year = readb(ioaddr + RTC_YEAR);
  91. century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  92. writeb(0, ioaddr + RTC_CONTROL);
  93. tm->tm_sec = bcd2bin(second);
  94. tm->tm_min = bcd2bin(minute);
  95. tm->tm_hour = bcd2bin(hour);
  96. tm->tm_mday = bcd2bin(day);
  97. tm->tm_wday = bcd2bin(week);
  98. tm->tm_mon = bcd2bin(month) - 1;
  99. /* year is 1900 + tm->tm_year */
  100. tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
  101. return rtc_valid_tm(tm);
  102. }
  103. static const struct rtc_class_ops ds1742_rtc_ops = {
  104. .read_time = ds1742_rtc_read_time,
  105. .set_time = ds1742_rtc_set_time,
  106. };
  107. static ssize_t ds1742_nvram_read(struct file *filp, struct kobject *kobj,
  108. struct bin_attribute *bin_attr,
  109. char *buf, loff_t pos, size_t size)
  110. {
  111. struct device *dev = container_of(kobj, struct device, kobj);
  112. struct platform_device *pdev = to_platform_device(dev);
  113. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  114. void __iomem *ioaddr = pdata->ioaddr_nvram;
  115. ssize_t count;
  116. for (count = 0; count < size; count++)
  117. *buf++ = readb(ioaddr + pos++);
  118. return count;
  119. }
  120. static ssize_t ds1742_nvram_write(struct file *filp, struct kobject *kobj,
  121. struct bin_attribute *bin_attr,
  122. char *buf, loff_t pos, size_t size)
  123. {
  124. struct device *dev = container_of(kobj, struct device, kobj);
  125. struct platform_device *pdev = to_platform_device(dev);
  126. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  127. void __iomem *ioaddr = pdata->ioaddr_nvram;
  128. ssize_t count;
  129. for (count = 0; count < size; count++)
  130. writeb(*buf++, ioaddr + pos++);
  131. return count;
  132. }
  133. static int ds1742_rtc_probe(struct platform_device *pdev)
  134. {
  135. struct rtc_device *rtc;
  136. struct resource *res;
  137. unsigned int cen, sec;
  138. struct rtc_plat_data *pdata;
  139. void __iomem *ioaddr;
  140. int ret = 0;
  141. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  142. if (!pdata)
  143. return -ENOMEM;
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. ioaddr = devm_ioremap_resource(&pdev->dev, res);
  146. if (IS_ERR(ioaddr))
  147. return PTR_ERR(ioaddr);
  148. pdata->ioaddr_nvram = ioaddr;
  149. pdata->size_nvram = resource_size(res) - RTC_SIZE;
  150. pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
  151. sysfs_bin_attr_init(&pdata->nvram_attr);
  152. pdata->nvram_attr.attr.name = "nvram";
  153. pdata->nvram_attr.attr.mode = S_IRUGO | S_IWUSR;
  154. pdata->nvram_attr.read = ds1742_nvram_read;
  155. pdata->nvram_attr.write = ds1742_nvram_write;
  156. pdata->nvram_attr.size = pdata->size_nvram;
  157. /* turn RTC on if it was not on */
  158. ioaddr = pdata->ioaddr_rtc;
  159. sec = readb(ioaddr + RTC_SECONDS);
  160. if (sec & RTC_STOP) {
  161. sec &= RTC_SECONDS_MASK;
  162. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  163. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  164. writeb(sec, ioaddr + RTC_SECONDS);
  165. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  166. }
  167. if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
  168. dev_warn(&pdev->dev, "voltage-low detected.\n");
  169. pdata->last_jiffies = jiffies;
  170. platform_set_drvdata(pdev, pdata);
  171. rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  172. &ds1742_rtc_ops, THIS_MODULE);
  173. if (IS_ERR(rtc))
  174. return PTR_ERR(rtc);
  175. ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
  176. if (ret)
  177. dev_err(&pdev->dev, "Unable to create sysfs entry: %s\n",
  178. pdata->nvram_attr.attr.name);
  179. return 0;
  180. }
  181. static int ds1742_rtc_remove(struct platform_device *pdev)
  182. {
  183. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  184. sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
  185. return 0;
  186. }
  187. static const struct of_device_id __maybe_unused ds1742_rtc_of_match[] = {
  188. { .compatible = "maxim,ds1742", },
  189. { }
  190. };
  191. MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match);
  192. static struct platform_driver ds1742_rtc_driver = {
  193. .probe = ds1742_rtc_probe,
  194. .remove = ds1742_rtc_remove,
  195. .driver = {
  196. .name = "rtc-ds1742",
  197. .of_match_table = of_match_ptr(ds1742_rtc_of_match),
  198. },
  199. };
  200. module_platform_driver(ds1742_rtc_driver);
  201. MODULE_AUTHOR("Atsushi Nemoto <[email protected]>");
  202. MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
  203. MODULE_LICENSE("GPL");
  204. MODULE_ALIAS("platform:rtc-ds1742");