rtc-ds1553.c 10 KB

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