rtc-ds1742.c 7.2 KB

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