rtc-ds1553.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. return 0;
  114. }
  115. static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
  116. {
  117. void __iomem *ioaddr = pdata->ioaddr;
  118. unsigned long flags;
  119. spin_lock_irqsave(&pdata->lock, flags);
  120. writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  121. 0x80 : bin2bcd(pdata->alrm_mday),
  122. ioaddr + RTC_DATE_ALARM);
  123. writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  124. 0x80 : bin2bcd(pdata->alrm_hour),
  125. ioaddr + RTC_HOURS_ALARM);
  126. writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  127. 0x80 : bin2bcd(pdata->alrm_min),
  128. ioaddr + RTC_MINUTES_ALARM);
  129. writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  130. 0x80 : bin2bcd(pdata->alrm_sec),
  131. ioaddr + RTC_SECONDS_ALARM);
  132. writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
  133. readb(ioaddr + RTC_FLAGS); /* clear interrupts */
  134. spin_unlock_irqrestore(&pdata->lock, flags);
  135. }
  136. static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  137. {
  138. struct platform_device *pdev = to_platform_device(dev);
  139. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  140. if (pdata->irq <= 0)
  141. return -EINVAL;
  142. pdata->alrm_mday = alrm->time.tm_mday;
  143. pdata->alrm_hour = alrm->time.tm_hour;
  144. pdata->alrm_min = alrm->time.tm_min;
  145. pdata->alrm_sec = alrm->time.tm_sec;
  146. if (alrm->enabled)
  147. pdata->irqen |= RTC_AF;
  148. ds1553_rtc_update_alarm(pdata);
  149. return 0;
  150. }
  151. static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  152. {
  153. struct platform_device *pdev = to_platform_device(dev);
  154. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  155. if (pdata->irq <= 0)
  156. return -EINVAL;
  157. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  158. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  159. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  160. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  161. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  162. return 0;
  163. }
  164. static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
  165. {
  166. struct platform_device *pdev = dev_id;
  167. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  168. void __iomem *ioaddr = pdata->ioaddr;
  169. unsigned long events = 0;
  170. spin_lock(&pdata->lock);
  171. /* read and clear interrupt */
  172. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
  173. events = RTC_IRQF;
  174. if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
  175. events |= RTC_UF;
  176. else
  177. events |= RTC_AF;
  178. rtc_update_irq(pdata->rtc, 1, events);
  179. }
  180. spin_unlock(&pdata->lock);
  181. return events ? IRQ_HANDLED : IRQ_NONE;
  182. }
  183. static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  184. {
  185. struct platform_device *pdev = to_platform_device(dev);
  186. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  187. if (pdata->irq <= 0)
  188. return -EINVAL;
  189. if (enabled)
  190. pdata->irqen |= RTC_AF;
  191. else
  192. pdata->irqen &= ~RTC_AF;
  193. ds1553_rtc_update_alarm(pdata);
  194. return 0;
  195. }
  196. static const struct rtc_class_ops ds1553_rtc_ops = {
  197. .read_time = ds1553_rtc_read_time,
  198. .set_time = ds1553_rtc_set_time,
  199. .read_alarm = ds1553_rtc_read_alarm,
  200. .set_alarm = ds1553_rtc_set_alarm,
  201. .alarm_irq_enable = ds1553_rtc_alarm_irq_enable,
  202. };
  203. static int ds1553_nvram_read(void *priv, unsigned int pos, void *val,
  204. size_t bytes)
  205. {
  206. struct platform_device *pdev = priv;
  207. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  208. void __iomem *ioaddr = pdata->ioaddr;
  209. u8 *buf = val;
  210. for (; bytes; bytes--)
  211. *buf++ = readb(ioaddr + pos++);
  212. return 0;
  213. }
  214. static int ds1553_nvram_write(void *priv, unsigned int pos, void *val,
  215. size_t bytes)
  216. {
  217. struct platform_device *pdev = priv;
  218. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  219. void __iomem *ioaddr = pdata->ioaddr;
  220. u8 *buf = val;
  221. for (; bytes; bytes--)
  222. writeb(*buf++, ioaddr + pos++);
  223. return 0;
  224. }
  225. static int ds1553_rtc_probe(struct platform_device *pdev)
  226. {
  227. struct resource *res;
  228. unsigned int cen, sec;
  229. struct rtc_plat_data *pdata;
  230. void __iomem *ioaddr;
  231. int ret = 0;
  232. struct nvmem_config nvmem_cfg = {
  233. .name = "ds1553_nvram",
  234. .word_size = 1,
  235. .stride = 1,
  236. .size = RTC_OFFSET,
  237. .reg_read = ds1553_nvram_read,
  238. .reg_write = ds1553_nvram_write,
  239. .priv = pdev,
  240. };
  241. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  242. if (!pdata)
  243. return -ENOMEM;
  244. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. ioaddr = devm_ioremap_resource(&pdev->dev, res);
  246. if (IS_ERR(ioaddr))
  247. return PTR_ERR(ioaddr);
  248. pdata->ioaddr = ioaddr;
  249. pdata->irq = platform_get_irq(pdev, 0);
  250. /* turn RTC on if it was not on */
  251. sec = readb(ioaddr + RTC_SECONDS);
  252. if (sec & RTC_STOP) {
  253. sec &= RTC_SECONDS_MASK;
  254. cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
  255. writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
  256. writeb(sec, ioaddr + RTC_SECONDS);
  257. writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
  258. }
  259. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
  260. dev_warn(&pdev->dev, "voltage-low detected.\n");
  261. spin_lock_init(&pdata->lock);
  262. pdata->last_jiffies = jiffies;
  263. platform_set_drvdata(pdev, pdata);
  264. pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
  265. if (IS_ERR(pdata->rtc))
  266. return PTR_ERR(pdata->rtc);
  267. pdata->rtc->ops = &ds1553_rtc_ops;
  268. pdata->rtc->nvram_old_abi = true;
  269. ret = rtc_register_device(pdata->rtc);
  270. if (ret)
  271. return ret;
  272. if (pdata->irq > 0) {
  273. writeb(0, ioaddr + RTC_INTERRUPTS);
  274. if (devm_request_irq(&pdev->dev, pdata->irq,
  275. ds1553_rtc_interrupt,
  276. 0, pdev->name, pdev) < 0) {
  277. dev_warn(&pdev->dev, "interrupt not available.\n");
  278. pdata->irq = 0;
  279. }
  280. }
  281. if (rtc_nvmem_register(pdata->rtc, &nvmem_cfg))
  282. dev_err(&pdev->dev, "unable to register nvmem\n");
  283. return 0;
  284. }
  285. /* work with hotplug and coldplug */
  286. MODULE_ALIAS("platform:rtc-ds1553");
  287. static struct platform_driver ds1553_rtc_driver = {
  288. .probe = ds1553_rtc_probe,
  289. .driver = {
  290. .name = "rtc-ds1553",
  291. },
  292. };
  293. module_platform_driver(ds1553_rtc_driver);
  294. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  295. MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
  296. MODULE_LICENSE("GPL");