rtc-ds1286.c 9.4 KB

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