rtc-ds1553.c 11 KB

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