rtc-ds1553.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * An rtc driver for the Dallas DS1553
  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. #include <linux/bcd.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gfp.h>
  14. #include <linux/delay.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/rtc.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #define RTC_REG_SIZE 0x2000
  22. #define RTC_OFFSET 0x1ff0
  23. #define RTC_FLAGS (RTC_OFFSET + 0)
  24. #define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
  25. #define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
  26. #define RTC_HOURS_ALARM (RTC_OFFSET + 4)
  27. #define RTC_DATE_ALARM (RTC_OFFSET + 5)
  28. #define RTC_INTERRUPTS (RTC_OFFSET + 6)
  29. #define RTC_WATCHDOG (RTC_OFFSET + 7)
  30. #define RTC_CONTROL (RTC_OFFSET + 8)
  31. #define RTC_CENTURY (RTC_OFFSET + 8)
  32. #define RTC_SECONDS (RTC_OFFSET + 9)
  33. #define RTC_MINUTES (RTC_OFFSET + 10)
  34. #define RTC_HOURS (RTC_OFFSET + 11)
  35. #define RTC_DAY (RTC_OFFSET + 12)
  36. #define RTC_DATE (RTC_OFFSET + 13)
  37. #define RTC_MONTH (RTC_OFFSET + 14)
  38. #define RTC_YEAR (RTC_OFFSET + 15)
  39. #define RTC_CENTURY_MASK 0x3f
  40. #define RTC_SECONDS_MASK 0x7f
  41. #define RTC_DAY_MASK 0x07
  42. /* Bits in the Control/Century register */
  43. #define RTC_WRITE 0x80
  44. #define RTC_READ 0x40
  45. /* Bits in the Seconds register */
  46. #define RTC_STOP 0x80
  47. /* Bits in the Flags register */
  48. #define RTC_FLAGS_AF 0x40
  49. #define RTC_FLAGS_BLF 0x10
  50. /* Bits in the Interrupts register */
  51. #define RTC_INTS_AE 0x80
  52. struct rtc_plat_data {
  53. struct rtc_device *rtc;
  54. void __iomem *ioaddr;
  55. unsigned long last_jiffies;
  56. int irq;
  57. unsigned int irqen;
  58. int alrm_sec;
  59. int alrm_min;
  60. int alrm_hour;
  61. int alrm_mday;
  62. spinlock_t lock;
  63. };
  64. static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
  65. {
  66. struct platform_device *pdev = to_platform_device(dev);
  67. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  68. void __iomem *ioaddr = pdata->ioaddr;
  69. u8 century;
  70. century = bin2bcd((tm->tm_year + 1900) / 100);
  71. writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
  72. writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
  73. writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
  74. writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
  75. writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
  76. writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
  77. writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
  78. writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
  79. /* RTC_CENTURY and RTC_CONTROL share same register */
  80. writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
  81. writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  82. return 0;
  83. }
  84. static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
  85. {
  86. struct platform_device *pdev = to_platform_device(dev);
  87. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  88. void __iomem *ioaddr = pdata->ioaddr;
  89. unsigned int year, month, day, hour, minute, second, week;
  90. unsigned int century;
  91. /* give enough time to update RTC in case of continuous read */
  92. if (pdata->last_jiffies == jiffies)
  93. msleep(1);
  94. pdata->last_jiffies = jiffies;
  95. writeb(RTC_READ, ioaddr + RTC_CONTROL);
  96. second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
  97. minute = readb(ioaddr + RTC_MINUTES);
  98. hour = readb(ioaddr + RTC_HOURS);
  99. day = readb(ioaddr + RTC_DATE);
  100. week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
  101. month = readb(ioaddr + RTC_MONTH);
  102. year = readb(ioaddr + RTC_YEAR);
  103. century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  104. writeb(0, ioaddr + RTC_CONTROL);
  105. tm->tm_sec = bcd2bin(second);
  106. tm->tm_min = bcd2bin(minute);
  107. tm->tm_hour = bcd2bin(hour);
  108. tm->tm_mday = bcd2bin(day);
  109. tm->tm_wday = bcd2bin(week);
  110. tm->tm_mon = bcd2bin(month) - 1;
  111. /* year is 1900 + tm->tm_year */
  112. tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
  113. if (rtc_valid_tm(tm) < 0) {
  114. dev_err(dev, "retrieved date/time is not valid.\n");
  115. rtc_time_to_tm(0, tm);
  116. }
  117. return 0;
  118. }
  119. static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
  120. {
  121. void __iomem *ioaddr = pdata->ioaddr;
  122. unsigned long flags;
  123. spin_lock_irqsave(&pdata->lock, flags);
  124. writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  125. 0x80 : bin2bcd(pdata->alrm_mday),
  126. ioaddr + RTC_DATE_ALARM);
  127. writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  128. 0x80 : bin2bcd(pdata->alrm_hour),
  129. ioaddr + RTC_HOURS_ALARM);
  130. writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  131. 0x80 : bin2bcd(pdata->alrm_min),
  132. ioaddr + RTC_MINUTES_ALARM);
  133. writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  134. 0x80 : bin2bcd(pdata->alrm_sec),
  135. ioaddr + RTC_SECONDS_ALARM);
  136. writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
  137. readb(ioaddr + RTC_FLAGS); /* clear interrupts */
  138. spin_unlock_irqrestore(&pdata->lock, flags);
  139. }
  140. static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  141. {
  142. struct platform_device *pdev = to_platform_device(dev);
  143. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  144. if (pdata->irq <= 0)
  145. return -EINVAL;
  146. pdata->alrm_mday = alrm->time.tm_mday;
  147. pdata->alrm_hour = alrm->time.tm_hour;
  148. pdata->alrm_min = alrm->time.tm_min;
  149. pdata->alrm_sec = alrm->time.tm_sec;
  150. if (alrm->enabled)
  151. pdata->irqen |= RTC_AF;
  152. ds1553_rtc_update_alarm(pdata);
  153. return 0;
  154. }
  155. static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  156. {
  157. struct platform_device *pdev = to_platform_device(dev);
  158. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  159. if (pdata->irq <= 0)
  160. return -EINVAL;
  161. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  162. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  163. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  164. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  165. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  166. return 0;
  167. }
  168. static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
  169. {
  170. struct platform_device *pdev = dev_id;
  171. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  172. void __iomem *ioaddr = pdata->ioaddr;
  173. unsigned long events = 0;
  174. spin_lock(&pdata->lock);
  175. /* read and clear interrupt */
  176. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
  177. events = RTC_IRQF;
  178. if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
  179. events |= RTC_UF;
  180. else
  181. events |= RTC_AF;
  182. rtc_update_irq(pdata->rtc, 1, events);
  183. }
  184. spin_unlock(&pdata->lock);
  185. return events ? IRQ_HANDLED : IRQ_NONE;
  186. }
  187. static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  188. {
  189. struct platform_device *pdev = to_platform_device(dev);
  190. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  191. if (pdata->irq <= 0)
  192. return -EINVAL;
  193. if (enabled)
  194. pdata->irqen |= RTC_AF;
  195. else
  196. pdata->irqen &= ~RTC_AF;
  197. ds1553_rtc_update_alarm(pdata);
  198. return 0;
  199. }
  200. static const struct rtc_class_ops ds1553_rtc_ops = {
  201. .read_time = ds1553_rtc_read_time,
  202. .set_time = ds1553_rtc_set_time,
  203. .read_alarm = ds1553_rtc_read_alarm,
  204. .set_alarm = ds1553_rtc_set_alarm,
  205. .alarm_irq_enable = ds1553_rtc_alarm_irq_enable,
  206. };
  207. static ssize_t ds1553_nvram_read(struct file *filp, struct kobject *kobj,
  208. struct bin_attribute *bin_attr,
  209. char *buf, loff_t pos, size_t size)
  210. {
  211. struct device *dev = container_of(kobj, struct device, kobj);
  212. struct platform_device *pdev = to_platform_device(dev);
  213. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  214. void __iomem *ioaddr = pdata->ioaddr;
  215. ssize_t count;
  216. for (count = 0; count < size; count++)
  217. *buf++ = readb(ioaddr + pos++);
  218. return count;
  219. }
  220. static ssize_t ds1553_nvram_write(struct file *filp, struct kobject *kobj,
  221. struct bin_attribute *bin_attr,
  222. char *buf, loff_t pos, size_t size)
  223. {
  224. struct device *dev = container_of(kobj, struct device, kobj);
  225. struct platform_device *pdev = to_platform_device(dev);
  226. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  227. void __iomem *ioaddr = pdata->ioaddr;
  228. ssize_t count;
  229. for (count = 0; count < size; count++)
  230. writeb(*buf++, ioaddr + pos++);
  231. return count;
  232. }
  233. static struct bin_attribute ds1553_nvram_attr = {
  234. .attr = {
  235. .name = "nvram",
  236. .mode = S_IRUGO | S_IWUSR,
  237. },
  238. .size = RTC_OFFSET,
  239. .read = ds1553_nvram_read,
  240. .write = ds1553_nvram_write,
  241. };
  242. static int ds1553_rtc_probe(struct platform_device *pdev)
  243. {
  244. struct resource *res;
  245. unsigned int cen, sec;
  246. struct rtc_plat_data *pdata;
  247. void __iomem *ioaddr;
  248. int ret = 0;
  249. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  250. if (!pdata)
  251. return -ENOMEM;
  252. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  253. ioaddr = devm_ioremap_resource(&pdev->dev, res);
  254. if (IS_ERR(ioaddr))
  255. return PTR_ERR(ioaddr);
  256. pdata->ioaddr = ioaddr;
  257. pdata->irq = platform_get_irq(pdev, 0);
  258. /* turn RTC on if it was not on */
  259. sec = readb(ioaddr + RTC_SECONDS);
  260. if (sec & RTC_STOP) {
  261. sec &= RTC_SECONDS_MASK;
  262. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  263. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  264. writeb(sec, ioaddr + RTC_SECONDS);
  265. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  266. }
  267. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
  268. dev_warn(&pdev->dev, "voltage-low detected.\n");
  269. spin_lock_init(&pdata->lock);
  270. pdata->last_jiffies = jiffies;
  271. platform_set_drvdata(pdev, pdata);
  272. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  273. &ds1553_rtc_ops, THIS_MODULE);
  274. if (IS_ERR(pdata->rtc))
  275. return PTR_ERR(pdata->rtc);
  276. if (pdata->irq > 0) {
  277. writeb(0, ioaddr + RTC_INTERRUPTS);
  278. if (devm_request_irq(&pdev->dev, pdata->irq,
  279. ds1553_rtc_interrupt,
  280. 0, pdev->name, pdev) < 0) {
  281. dev_warn(&pdev->dev, "interrupt not available.\n");
  282. pdata->irq = 0;
  283. }
  284. }
  285. ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
  286. if (ret)
  287. dev_err(&pdev->dev, "unable to create sysfs file: %s\n",
  288. ds1553_nvram_attr.attr.name);
  289. return 0;
  290. }
  291. static int ds1553_rtc_remove(struct platform_device *pdev)
  292. {
  293. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  294. sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
  295. if (pdata->irq > 0)
  296. writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
  297. return 0;
  298. }
  299. /* work with hotplug and coldplug */
  300. MODULE_ALIAS("platform:rtc-ds1553");
  301. static struct platform_driver ds1553_rtc_driver = {
  302. .probe = ds1553_rtc_probe,
  303. .remove = ds1553_rtc_remove,
  304. .driver = {
  305. .name = "rtc-ds1553",
  306. },
  307. };
  308. module_platform_driver(ds1553_rtc_driver);
  309. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  310. MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
  311. MODULE_LICENSE("GPL");