rtc-ds1286.c 9.3 KB

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