1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DS1286 Real Time Clock interface for Linux
5 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
6 * Copyright (C) 2008 Thomas Bogendoerfer
8 * Based on code written by Paul Gortmaker.
11 #include <linux/module.h>
12 #include <linux/rtc.h>
13 #include <linux/platform_device.h>
14 #include <linux/bcd.h>
15 #include <linux/rtc/ds1286.h>
17 #include <linux/slab.h>
20 struct rtc_device *rtc;
25 static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
27 return __raw_readl(&priv->rtcregs[reg]) & 0xff;
30 static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
32 __raw_writel(data, &priv->rtcregs[reg]);
36 static int ds1286_alarm_irq_enable(struct device *dev, unsigned int enabled)
38 struct ds1286_priv *priv = dev_get_drvdata(dev);
42 /* Allow or mask alarm interrupts */
43 spin_lock_irqsave(&priv->lock, flags);
44 val = ds1286_rtc_read(priv, RTC_CMD);
49 ds1286_rtc_write(priv, val, RTC_CMD);
50 spin_unlock_irqrestore(&priv->lock, flags);
55 #ifdef CONFIG_RTC_INTF_DEV
57 static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
59 struct ds1286_priv *priv = dev_get_drvdata(dev);
65 /* Mask watchdog int. enab. bit */
66 spin_lock_irqsave(&priv->lock, flags);
67 val = ds1286_rtc_read(priv, RTC_CMD);
69 ds1286_rtc_write(priv, val, RTC_CMD);
70 spin_unlock_irqrestore(&priv->lock, flags);
73 /* Allow watchdog interrupts. */
74 spin_lock_irqsave(&priv->lock, flags);
75 val = ds1286_rtc_read(priv, RTC_CMD);
77 ds1286_rtc_write(priv, val, RTC_CMD);
78 spin_unlock_irqrestore(&priv->lock, flags);
87 #define ds1286_ioctl NULL
92 static int ds1286_proc(struct device *dev, struct seq_file *seq)
94 struct ds1286_priv *priv = dev_get_drvdata(dev);
95 unsigned char month, cmd, amode;
98 month = ds1286_rtc_read(priv, RTC_MONTH);
101 "square_wave\t: %s\n",
102 (month & RTC_EOSC) ? "disabled" : "enabled",
103 (month & RTC_ESQW) ? "disabled" : "enabled");
105 amode = ((ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x80) >> 5) |
106 ((ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x80) >> 6) |
107 ((ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x80) >> 7);
116 s = "hours and minutes match";
119 s = "days, hours and minutes match";
125 seq_printf(seq, "alarm_mode\t: %s\n", s);
127 cmd = ds1286_rtc_read(priv, RTC_CMD);
129 "alarm_enable\t: %s\n"
132 "wdog_alarm_mask\t: %s\n"
133 "interrupt_mode\t: %s\n"
134 "INTB_mode\t: %s_active\n"
135 "interrupt_pins\t: %s\n",
136 (cmd & RTC_TDF) ? "yes" : "no",
137 (cmd & RTC_WAF) ? "yes" : "no",
138 (cmd & RTC_TDM) ? "disabled" : "enabled",
139 (cmd & RTC_WAM) ? "disabled" : "enabled",
140 (cmd & RTC_PU_LVL) ? "pulse" : "level",
141 (cmd & RTC_IBH_LO) ? "low" : "high",
142 (cmd & RTC_IPSW) ? "unswapped" : "swapped");
147 #define ds1286_proc NULL
150 static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
152 struct ds1286_priv *priv = dev_get_drvdata(dev);
153 unsigned char save_control;
155 unsigned long uip_watchdog = jiffies;
158 * read RTC once any update in progress is done. The update
159 * can take just over 2ms. We wait 10 to 20ms. There is no need to
160 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
161 * If you need to know *exactly* when a second has started, enable
162 * periodic update complete interrupts, (via ioctl) and then
163 * immediately read /dev/rtc which will block until you get the IRQ.
164 * Once the read clears, read the RTC time (again via ioctl). Easy.
167 if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
168 while (time_before(jiffies, uip_watchdog + 2*HZ/100))
172 * Only the values that we read from the RTC are set. We leave
173 * tm_wday, tm_yday and tm_isdst untouched. Even though the
174 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
175 * by the RTC when initially set to a non-zero value.
177 spin_lock_irqsave(&priv->lock, flags);
178 save_control = ds1286_rtc_read(priv, RTC_CMD);
179 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
181 tm->tm_sec = ds1286_rtc_read(priv, RTC_SECONDS);
182 tm->tm_min = ds1286_rtc_read(priv, RTC_MINUTES);
183 tm->tm_hour = ds1286_rtc_read(priv, RTC_HOURS) & 0x3f;
184 tm->tm_mday = ds1286_rtc_read(priv, RTC_DATE);
185 tm->tm_mon = ds1286_rtc_read(priv, RTC_MONTH) & 0x1f;
186 tm->tm_year = ds1286_rtc_read(priv, RTC_YEAR);
188 ds1286_rtc_write(priv, save_control, RTC_CMD);
189 spin_unlock_irqrestore(&priv->lock, flags);
191 tm->tm_sec = bcd2bin(tm->tm_sec);
192 tm->tm_min = bcd2bin(tm->tm_min);
193 tm->tm_hour = bcd2bin(tm->tm_hour);
194 tm->tm_mday = bcd2bin(tm->tm_mday);
195 tm->tm_mon = bcd2bin(tm->tm_mon);
196 tm->tm_year = bcd2bin(tm->tm_year);
199 * Account for differences between how the RTC uses the values
200 * and how they are defined in a struct rtc_time;
202 if (tm->tm_year < 45)
205 if (tm->tm_year < 70)
213 static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
215 struct ds1286_priv *priv = dev_get_drvdata(dev);
216 unsigned char mon, day, hrs, min, sec;
217 unsigned char save_control;
221 yrs = tm->tm_year + 1900;
222 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
232 if (yrs > 255) /* They are unsigned */
245 spin_lock_irqsave(&priv->lock, flags);
246 save_control = ds1286_rtc_read(priv, RTC_CMD);
247 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
249 ds1286_rtc_write(priv, yrs, RTC_YEAR);
250 ds1286_rtc_write(priv, mon, RTC_MONTH);
251 ds1286_rtc_write(priv, day, RTC_DATE);
252 ds1286_rtc_write(priv, hrs, RTC_HOURS);
253 ds1286_rtc_write(priv, min, RTC_MINUTES);
254 ds1286_rtc_write(priv, sec, RTC_SECONDS);
255 ds1286_rtc_write(priv, 0, RTC_HUNDREDTH_SECOND);
257 ds1286_rtc_write(priv, save_control, RTC_CMD);
258 spin_unlock_irqrestore(&priv->lock, flags);
262 static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
264 struct ds1286_priv *priv = dev_get_drvdata(dev);
268 * Only the values that we read from the RTC are set. That
269 * means only tm_wday, tm_hour, tm_min.
271 spin_lock_irqsave(&priv->lock, flags);
272 alm->time.tm_min = ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x7f;
273 alm->time.tm_hour = ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x1f;
274 alm->time.tm_wday = ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x07;
275 ds1286_rtc_read(priv, RTC_CMD);
276 spin_unlock_irqrestore(&priv->lock, flags);
278 alm->time.tm_min = bcd2bin(alm->time.tm_min);
279 alm->time.tm_hour = bcd2bin(alm->time.tm_hour);
280 alm->time.tm_sec = 0;
284 static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
286 struct ds1286_priv *priv = dev_get_drvdata(dev);
287 unsigned char hrs, min, sec;
289 hrs = alm->time.tm_hour;
290 min = alm->time.tm_min;
291 sec = alm->time.tm_sec;
305 spin_lock(&priv->lock);
306 ds1286_rtc_write(priv, hrs, RTC_HOURS_ALARM);
307 ds1286_rtc_write(priv, min, RTC_MINUTES_ALARM);
308 spin_unlock(&priv->lock);
313 static const struct rtc_class_ops ds1286_ops = {
314 .ioctl = ds1286_ioctl,
316 .read_time = ds1286_read_time,
317 .set_time = ds1286_set_time,
318 .read_alarm = ds1286_read_alarm,
319 .set_alarm = ds1286_set_alarm,
320 .alarm_irq_enable = ds1286_alarm_irq_enable,
323 static int ds1286_probe(struct platform_device *pdev)
325 struct rtc_device *rtc;
326 struct ds1286_priv *priv;
328 priv = devm_kzalloc(&pdev->dev, sizeof(struct ds1286_priv), GFP_KERNEL);
332 priv->rtcregs = devm_platform_ioremap_resource(pdev, 0);
333 if (IS_ERR(priv->rtcregs))
334 return PTR_ERR(priv->rtcregs);
336 spin_lock_init(&priv->lock);
337 platform_set_drvdata(pdev, priv);
338 rtc = devm_rtc_device_register(&pdev->dev, "ds1286", &ds1286_ops,
346 static struct platform_driver ds1286_platform_driver = {
348 .name = "rtc-ds1286",
350 .probe = ds1286_probe,
353 module_platform_driver(ds1286_platform_driver);
355 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
356 MODULE_DESCRIPTION("DS1286 RTC driver");
357 MODULE_LICENSE("GPL");
358 MODULE_ALIAS("platform:rtc-ds1286");