rtc-ds1286.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * DS1286 Real Time Clock interface for Linux
  3. *
  4. * Copyright (C) 1998, 1999, 2000 Ralf Baechle
  5. * Copyright (C) 2008 Thomas Bogendoerfer
  6. *
  7. * Based on code written by Paul Gortmaker.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/rtc.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/bcd.h>
  18. #include <linux/ds1286.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #define DRV_VERSION "1.0"
  22. struct ds1286_priv {
  23. struct rtc_device *rtc;
  24. u32 __iomem *rtcregs;
  25. size_t size;
  26. unsigned long baseaddr;
  27. spinlock_t lock;
  28. };
  29. static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
  30. {
  31. return __raw_readl(&priv->rtcregs[reg]) & 0xff;
  32. }
  33. static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
  34. {
  35. __raw_writel(data, &priv->rtcregs[reg]);
  36. }
  37. static int ds1286_alarm_irq_enable(struct device *dev, unsigned int enabled)
  38. {
  39. struct ds1286_priv *priv = dev_get_drvdata(dev);
  40. unsigned long flags;
  41. unsigned char val;
  42. /* Allow or mask alarm interrupts */
  43. spin_lock_irqsave(&priv->lock, flags);
  44. val = ds1286_rtc_read(priv, RTC_CMD);
  45. if (enabled)
  46. val &= ~RTC_TDM;
  47. else
  48. val |= RTC_TDM;
  49. ds1286_rtc_write(priv, val, RTC_CMD);
  50. spin_unlock_irqrestore(&priv->lock, flags);
  51. return 0;
  52. }
  53. #ifdef CONFIG_RTC_INTF_DEV
  54. static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  55. {
  56. struct ds1286_priv *priv = dev_get_drvdata(dev);
  57. unsigned long flags;
  58. unsigned char val;
  59. switch (cmd) {
  60. case RTC_WIE_OFF:
  61. /* Mask watchdog int. enab. bit */
  62. spin_lock_irqsave(&priv->lock, flags);
  63. val = ds1286_rtc_read(priv, RTC_CMD);
  64. val |= RTC_WAM;
  65. ds1286_rtc_write(priv, val, RTC_CMD);
  66. spin_unlock_irqrestore(&priv->lock, flags);
  67. break;
  68. case RTC_WIE_ON:
  69. /* Allow watchdog interrupts. */
  70. spin_lock_irqsave(&priv->lock, flags);
  71. val = ds1286_rtc_read(priv, RTC_CMD);
  72. val &= ~RTC_WAM;
  73. ds1286_rtc_write(priv, val, RTC_CMD);
  74. spin_unlock_irqrestore(&priv->lock, flags);
  75. break;
  76. default:
  77. return -ENOIOCTLCMD;
  78. }
  79. return 0;
  80. }
  81. #else
  82. #define ds1286_ioctl NULL
  83. #endif
  84. #ifdef CONFIG_PROC_FS
  85. static int ds1286_proc(struct device *dev, struct seq_file *seq)
  86. {
  87. struct ds1286_priv *priv = dev_get_drvdata(dev);
  88. unsigned char month, cmd, amode;
  89. const char *s;
  90. month = ds1286_rtc_read(priv, RTC_MONTH);
  91. seq_printf(seq,
  92. "oscillator\t: %s\n"
  93. "square_wave\t: %s\n",
  94. (month & RTC_EOSC) ? "disabled" : "enabled",
  95. (month & RTC_ESQW) ? "disabled" : "enabled");
  96. amode = ((ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x80) >> 5) |
  97. ((ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x80) >> 6) |
  98. ((ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x80) >> 7);
  99. switch (amode) {
  100. case 7:
  101. s = "each minute";
  102. break;
  103. case 3:
  104. s = "minutes match";
  105. break;
  106. case 1:
  107. s = "hours and minutes match";
  108. break;
  109. case 0:
  110. s = "days, hours and minutes match";
  111. break;
  112. default:
  113. s = "invalid";
  114. break;
  115. }
  116. seq_printf(seq, "alarm_mode\t: %s\n", s);
  117. cmd = ds1286_rtc_read(priv, RTC_CMD);
  118. seq_printf(seq,
  119. "alarm_enable\t: %s\n"
  120. "wdog_alarm\t: %s\n"
  121. "alarm_mask\t: %s\n"
  122. "wdog_alarm_mask\t: %s\n"
  123. "interrupt_mode\t: %s\n"
  124. "INTB_mode\t: %s_active\n"
  125. "interrupt_pins\t: %s\n",
  126. (cmd & RTC_TDF) ? "yes" : "no",
  127. (cmd & RTC_WAF) ? "yes" : "no",
  128. (cmd & RTC_TDM) ? "disabled" : "enabled",
  129. (cmd & RTC_WAM) ? "disabled" : "enabled",
  130. (cmd & RTC_PU_LVL) ? "pulse" : "level",
  131. (cmd & RTC_IBH_LO) ? "low" : "high",
  132. (cmd & RTC_IPSW) ? "unswapped" : "swapped");
  133. return 0;
  134. }
  135. #else
  136. #define ds1286_proc NULL
  137. #endif
  138. static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
  139. {
  140. struct ds1286_priv *priv = dev_get_drvdata(dev);
  141. unsigned char save_control;
  142. unsigned long flags;
  143. unsigned long uip_watchdog = jiffies;
  144. /*
  145. * read RTC once any update in progress is done. The update
  146. * can take just over 2ms. We wait 10 to 20ms. There is no need to
  147. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  148. * If you need to know *exactly* when a second has started, enable
  149. * periodic update complete interrupts, (via ioctl) and then
  150. * immediately read /dev/rtc which will block until you get the IRQ.
  151. * Once the read clears, read the RTC time (again via ioctl). Easy.
  152. */
  153. if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
  154. while (time_before(jiffies, uip_watchdog + 2*HZ/100))
  155. barrier();
  156. /*
  157. * Only the values that we read from the RTC are set. We leave
  158. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  159. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  160. * by the RTC when initially set to a non-zero value.
  161. */
  162. spin_lock_irqsave(&priv->lock, flags);
  163. save_control = ds1286_rtc_read(priv, RTC_CMD);
  164. ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
  165. tm->tm_sec = ds1286_rtc_read(priv, RTC_SECONDS);
  166. tm->tm_min = ds1286_rtc_read(priv, RTC_MINUTES);
  167. tm->tm_hour = ds1286_rtc_read(priv, RTC_HOURS) & 0x3f;
  168. tm->tm_mday = ds1286_rtc_read(priv, RTC_DATE);
  169. tm->tm_mon = ds1286_rtc_read(priv, RTC_MONTH) & 0x1f;
  170. tm->tm_year = ds1286_rtc_read(priv, RTC_YEAR);
  171. ds1286_rtc_write(priv, save_control, RTC_CMD);
  172. spin_unlock_irqrestore(&priv->lock, flags);
  173. tm->tm_sec = bcd2bin(tm->tm_sec);
  174. tm->tm_min = bcd2bin(tm->tm_min);
  175. tm->tm_hour = bcd2bin(tm->tm_hour);
  176. tm->tm_mday = bcd2bin(tm->tm_mday);
  177. tm->tm_mon = bcd2bin(tm->tm_mon);
  178. tm->tm_year = bcd2bin(tm->tm_year);
  179. /*
  180. * Account for differences between how the RTC uses the values
  181. * and how they are defined in a struct rtc_time;
  182. */
  183. if (tm->tm_year < 45)
  184. tm->tm_year += 30;
  185. tm->tm_year += 40;
  186. if (tm->tm_year < 70)
  187. tm->tm_year += 100;
  188. tm->tm_mon--;
  189. return rtc_valid_tm(tm);
  190. }
  191. static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
  192. {
  193. struct ds1286_priv *priv = dev_get_drvdata(dev);
  194. unsigned char mon, day, hrs, min, sec;
  195. unsigned char save_control;
  196. unsigned int yrs;
  197. unsigned long flags;
  198. yrs = tm->tm_year + 1900;
  199. mon = tm->tm_mon + 1; /* tm_mon starts at zero */
  200. day = tm->tm_mday;
  201. hrs = tm->tm_hour;
  202. min = tm->tm_min;
  203. sec = tm->tm_sec;
  204. if (yrs < 1970)
  205. return -EINVAL;
  206. yrs -= 1940;
  207. if (yrs > 255) /* They are unsigned */
  208. return -EINVAL;
  209. if (yrs >= 100)
  210. yrs -= 100;
  211. sec = bin2bcd(sec);
  212. min = bin2bcd(min);
  213. hrs = bin2bcd(hrs);
  214. day = bin2bcd(day);
  215. mon = bin2bcd(mon);
  216. yrs = bin2bcd(yrs);
  217. spin_lock_irqsave(&priv->lock, flags);
  218. save_control = ds1286_rtc_read(priv, RTC_CMD);
  219. ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
  220. ds1286_rtc_write(priv, yrs, RTC_YEAR);
  221. ds1286_rtc_write(priv, mon, RTC_MONTH);
  222. ds1286_rtc_write(priv, day, RTC_DATE);
  223. ds1286_rtc_write(priv, hrs, RTC_HOURS);
  224. ds1286_rtc_write(priv, min, RTC_MINUTES);
  225. ds1286_rtc_write(priv, sec, RTC_SECONDS);
  226. ds1286_rtc_write(priv, 0, RTC_HUNDREDTH_SECOND);
  227. ds1286_rtc_write(priv, save_control, RTC_CMD);
  228. spin_unlock_irqrestore(&priv->lock, flags);
  229. return 0;
  230. }
  231. static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  232. {
  233. struct ds1286_priv *priv = dev_get_drvdata(dev);
  234. unsigned char cmd;
  235. unsigned long flags;
  236. /*
  237. * Only the values that we read from the RTC are set. That
  238. * means only tm_wday, tm_hour, tm_min.
  239. */
  240. spin_lock_irqsave(&priv->lock, flags);
  241. alm->time.tm_min = ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x7f;
  242. alm->time.tm_hour = ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x1f;
  243. alm->time.tm_wday = ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x07;
  244. cmd = ds1286_rtc_read(priv, RTC_CMD);
  245. spin_unlock_irqrestore(&priv->lock, flags);
  246. alm->time.tm_min = bcd2bin(alm->time.tm_min);
  247. alm->time.tm_hour = bcd2bin(alm->time.tm_hour);
  248. alm->time.tm_sec = 0;
  249. return 0;
  250. }
  251. static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  252. {
  253. struct ds1286_priv *priv = dev_get_drvdata(dev);
  254. unsigned char hrs, min, sec;
  255. hrs = alm->time.tm_hour;
  256. min = alm->time.tm_min;
  257. sec = alm->time.tm_sec;
  258. if (hrs >= 24)
  259. hrs = 0xff;
  260. if (min >= 60)
  261. min = 0xff;
  262. if (sec != 0)
  263. return -EINVAL;
  264. min = bin2bcd(min);
  265. hrs = bin2bcd(hrs);
  266. spin_lock(&priv->lock);
  267. ds1286_rtc_write(priv, hrs, RTC_HOURS_ALARM);
  268. ds1286_rtc_write(priv, min, RTC_MINUTES_ALARM);
  269. spin_unlock(&priv->lock);
  270. return 0;
  271. }
  272. static const struct rtc_class_ops ds1286_ops = {
  273. .ioctl = ds1286_ioctl,
  274. .proc = ds1286_proc,
  275. .read_time = ds1286_read_time,
  276. .set_time = ds1286_set_time,
  277. .read_alarm = ds1286_read_alarm,
  278. .set_alarm = ds1286_set_alarm,
  279. .alarm_irq_enable = ds1286_alarm_irq_enable,
  280. };
  281. static int __devinit ds1286_probe(struct platform_device *pdev)
  282. {
  283. struct rtc_device *rtc;
  284. struct resource *res;
  285. struct ds1286_priv *priv;
  286. int ret = 0;
  287. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  288. if (!res)
  289. return -ENODEV;
  290. priv = kzalloc(sizeof(struct ds1286_priv), GFP_KERNEL);
  291. if (!priv)
  292. return -ENOMEM;
  293. priv->size = resource_size(res);
  294. if (!request_mem_region(res->start, priv->size, pdev->name)) {
  295. ret = -EBUSY;
  296. goto out;
  297. }
  298. priv->baseaddr = res->start;
  299. priv->rtcregs = ioremap(priv->baseaddr, priv->size);
  300. if (!priv->rtcregs) {
  301. ret = -ENOMEM;
  302. goto out;
  303. }
  304. spin_lock_init(&priv->lock);
  305. platform_set_drvdata(pdev, priv);
  306. rtc = rtc_device_register("ds1286", &pdev->dev,
  307. &ds1286_ops, THIS_MODULE);
  308. if (IS_ERR(rtc)) {
  309. ret = PTR_ERR(rtc);
  310. goto out;
  311. }
  312. priv->rtc = rtc;
  313. return 0;
  314. out:
  315. if (priv->rtc)
  316. rtc_device_unregister(priv->rtc);
  317. if (priv->rtcregs)
  318. iounmap(priv->rtcregs);
  319. if (priv->baseaddr)
  320. release_mem_region(priv->baseaddr, priv->size);
  321. kfree(priv);
  322. return ret;
  323. }
  324. static int __devexit ds1286_remove(struct platform_device *pdev)
  325. {
  326. struct ds1286_priv *priv = platform_get_drvdata(pdev);
  327. rtc_device_unregister(priv->rtc);
  328. iounmap(priv->rtcregs);
  329. release_mem_region(priv->baseaddr, priv->size);
  330. kfree(priv);
  331. return 0;
  332. }
  333. static struct platform_driver ds1286_platform_driver = {
  334. .driver = {
  335. .name = "rtc-ds1286",
  336. .owner = THIS_MODULE,
  337. },
  338. .probe = ds1286_probe,
  339. .remove = __devexit_p(ds1286_remove),
  340. };
  341. module_platform_driver(ds1286_platform_driver);
  342. MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
  343. MODULE_DESCRIPTION("DS1286 RTC driver");
  344. MODULE_LICENSE("GPL");
  345. MODULE_VERSION(DRV_VERSION);
  346. MODULE_ALIAS("platform:rtc-ds1286");