rtc-ds1742.c 7.0 KB

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