rtc-ds1742.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/rtc.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #define DRV_VERSION "0.4"
  23. #define RTC_SIZE 8
  24. #define RTC_CONTROL 0
  25. #define RTC_CENTURY 0
  26. #define RTC_SECONDS 1
  27. #define RTC_MINUTES 2
  28. #define RTC_HOURS 3
  29. #define RTC_DAY 4
  30. #define RTC_DATE 5
  31. #define RTC_MONTH 6
  32. #define RTC_YEAR 7
  33. #define RTC_CENTURY_MASK 0x3f
  34. #define RTC_SECONDS_MASK 0x7f
  35. #define RTC_DAY_MASK 0x07
  36. /* Bits in the Control/Century register */
  37. #define RTC_WRITE 0x80
  38. #define RTC_READ 0x40
  39. /* Bits in the Seconds register */
  40. #define RTC_STOP 0x80
  41. /* Bits in the Day register */
  42. #define RTC_BATT_FLAG 0x80
  43. struct rtc_plat_data {
  44. struct rtc_device *rtc;
  45. void __iomem *ioaddr_nvram;
  46. void __iomem *ioaddr_rtc;
  47. size_t size_nvram;
  48. size_t size;
  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. if (rtc_valid_tm(tm) < 0) {
  102. dev_err(dev, "retrieved date/time is not valid.\n");
  103. rtc_time_to_tm(0, tm);
  104. }
  105. return 0;
  106. }
  107. static const struct rtc_class_ops ds1742_rtc_ops = {
  108. .read_time = ds1742_rtc_read_time,
  109. .set_time = ds1742_rtc_set_time,
  110. };
  111. static ssize_t ds1742_nvram_read(struct kobject *kobj,
  112. struct bin_attribute *bin_attr,
  113. char *buf, loff_t pos, size_t size)
  114. {
  115. struct device *dev = container_of(kobj, struct device, kobj);
  116. struct platform_device *pdev = to_platform_device(dev);
  117. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  118. void __iomem *ioaddr = pdata->ioaddr_nvram;
  119. ssize_t count;
  120. for (count = 0; size > 0 && pos < pdata->size_nvram; count++, size--)
  121. *buf++ = readb(ioaddr + pos++);
  122. return count;
  123. }
  124. static ssize_t ds1742_nvram_write(struct kobject *kobj,
  125. struct bin_attribute *bin_attr,
  126. char *buf, loff_t pos, size_t size)
  127. {
  128. struct device *dev = container_of(kobj, struct device, kobj);
  129. struct platform_device *pdev = to_platform_device(dev);
  130. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  131. void __iomem *ioaddr = pdata->ioaddr_nvram;
  132. ssize_t count;
  133. for (count = 0; size > 0 && pos < pdata->size_nvram; count++, size--)
  134. writeb(*buf++, ioaddr + pos++);
  135. return count;
  136. }
  137. static int __devinit ds1742_rtc_probe(struct platform_device *pdev)
  138. {
  139. struct rtc_device *rtc;
  140. struct resource *res;
  141. unsigned int cen, sec;
  142. struct rtc_plat_data *pdata;
  143. void __iomem *ioaddr;
  144. int ret = 0;
  145. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  146. if (!res)
  147. return -ENODEV;
  148. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  149. if (!pdata)
  150. return -ENOMEM;
  151. pdata->size = res->end - res->start + 1;
  152. if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size,
  153. pdev->name))
  154. return -EBUSY;
  155. ioaddr = devm_ioremap(&pdev->dev, res->start, pdata->size);
  156. if (!ioaddr)
  157. return -ENOMEM;
  158. pdata->ioaddr_nvram = ioaddr;
  159. pdata->size_nvram = pdata->size - RTC_SIZE;
  160. pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
  161. pdata->nvram_attr.attr.name = "nvram";
  162. pdata->nvram_attr.attr.mode = S_IRUGO | S_IWUSR;
  163. pdata->nvram_attr.read = ds1742_nvram_read;
  164. pdata->nvram_attr.write = ds1742_nvram_write;
  165. pdata->nvram_attr.size = pdata->size_nvram;
  166. /* turn RTC on if it was not on */
  167. ioaddr = pdata->ioaddr_rtc;
  168. sec = readb(ioaddr + RTC_SECONDS);
  169. if (sec & RTC_STOP) {
  170. sec &= RTC_SECONDS_MASK;
  171. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  172. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  173. writeb(sec, ioaddr + RTC_SECONDS);
  174. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  175. }
  176. if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
  177. dev_warn(&pdev->dev, "voltage-low detected.\n");
  178. pdata->last_jiffies = jiffies;
  179. platform_set_drvdata(pdev, pdata);
  180. rtc = rtc_device_register(pdev->name, &pdev->dev,
  181. &ds1742_rtc_ops, THIS_MODULE);
  182. if (IS_ERR(rtc))
  183. return PTR_ERR(rtc);
  184. pdata->rtc = rtc;
  185. ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
  186. if (ret) {
  187. dev_err(&pdev->dev, "creating nvram file in sysfs failed\n");
  188. rtc_device_unregister(rtc);
  189. }
  190. return ret;
  191. }
  192. static int __devexit ds1742_rtc_remove(struct platform_device *pdev)
  193. {
  194. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  195. sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
  196. rtc_device_unregister(pdata->rtc);
  197. return 0;
  198. }
  199. static struct platform_driver ds1742_rtc_driver = {
  200. .probe = ds1742_rtc_probe,
  201. .remove = __devexit_p(ds1742_rtc_remove),
  202. .driver = {
  203. .name = "rtc-ds1742",
  204. .owner = THIS_MODULE,
  205. },
  206. };
  207. static __init int ds1742_init(void)
  208. {
  209. return platform_driver_register(&ds1742_rtc_driver);
  210. }
  211. static __exit void ds1742_exit(void)
  212. {
  213. platform_driver_unregister(&ds1742_rtc_driver);
  214. }
  215. module_init(ds1742_init);
  216. module_exit(ds1742_exit);
  217. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  218. MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
  219. MODULE_LICENSE("GPL");
  220. MODULE_VERSION(DRV_VERSION);
  221. MODULE_ALIAS("platform:rtc-ds1742");