rtc-ds1553.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #define DRV_VERSION "0.3"
  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. if (likely(pdata->rtc))
  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; size > 0 && pos < RTC_OFFSET; count++, size--)
  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; size > 0 && pos < RTC_OFFSET; count++, size--)
  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 __devinit ds1553_rtc_probe(struct platform_device *pdev)
  244. {
  245. struct rtc_device *rtc;
  246. struct resource *res;
  247. unsigned int cen, sec;
  248. struct rtc_plat_data *pdata;
  249. void __iomem *ioaddr;
  250. int ret = 0;
  251. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  252. if (!res)
  253. return -ENODEV;
  254. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  255. if (!pdata)
  256. return -ENOMEM;
  257. if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE,
  258. pdev->name))
  259. return -EBUSY;
  260. ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE);
  261. if (!ioaddr)
  262. return -ENOMEM;
  263. pdata->ioaddr = ioaddr;
  264. pdata->irq = platform_get_irq(pdev, 0);
  265. /* turn RTC on if it was not on */
  266. sec = readb(ioaddr + RTC_SECONDS);
  267. if (sec & RTC_STOP) {
  268. sec &= RTC_SECONDS_MASK;
  269. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  270. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  271. writeb(sec, ioaddr + RTC_SECONDS);
  272. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  273. }
  274. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
  275. dev_warn(&pdev->dev, "voltage-low detected.\n");
  276. spin_lock_init(&pdata->lock);
  277. pdata->last_jiffies = jiffies;
  278. platform_set_drvdata(pdev, pdata);
  279. if (pdata->irq > 0) {
  280. writeb(0, ioaddr + RTC_INTERRUPTS);
  281. if (devm_request_irq(&pdev->dev, pdata->irq,
  282. ds1553_rtc_interrupt,
  283. IRQF_DISABLED, pdev->name, pdev) < 0) {
  284. dev_warn(&pdev->dev, "interrupt not available.\n");
  285. pdata->irq = 0;
  286. }
  287. }
  288. rtc = rtc_device_register(pdev->name, &pdev->dev,
  289. &ds1553_rtc_ops, THIS_MODULE);
  290. if (IS_ERR(rtc))
  291. return PTR_ERR(rtc);
  292. pdata->rtc = rtc;
  293. ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
  294. if (ret)
  295. rtc_device_unregister(rtc);
  296. return ret;
  297. }
  298. static int __devexit ds1553_rtc_remove(struct platform_device *pdev)
  299. {
  300. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  301. sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
  302. rtc_device_unregister(pdata->rtc);
  303. if (pdata->irq > 0)
  304. writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
  305. return 0;
  306. }
  307. /* work with hotplug and coldplug */
  308. MODULE_ALIAS("platform:rtc-ds1553");
  309. static struct platform_driver ds1553_rtc_driver = {
  310. .probe = ds1553_rtc_probe,
  311. .remove = __devexit_p(ds1553_rtc_remove),
  312. .driver = {
  313. .name = "rtc-ds1553",
  314. .owner = THIS_MODULE,
  315. },
  316. };
  317. static __init int ds1553_init(void)
  318. {
  319. return platform_driver_register(&ds1553_rtc_driver);
  320. }
  321. static __exit void ds1553_exit(void)
  322. {
  323. platform_driver_unregister(&ds1553_rtc_driver);
  324. }
  325. module_init(ds1553_init);
  326. module_exit(ds1553_exit);
  327. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  328. MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
  329. MODULE_LICENSE("GPL");
  330. MODULE_VERSION(DRV_VERSION);