Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * A RTC driver for the Simtek STK17TA8
4 *
5 * By Thomas Hommel <thomas.hommel@ge.com>
6 *
7 * Based on the DS1553 driver from
8 * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
9 */
10
11#include <linux/bcd.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/gfp.h>
15#include <linux/delay.h>
16#include <linux/jiffies.h>
17#include <linux/interrupt.h>
18#include <linux/rtc.h>
19#include <linux/platform_device.h>
20#include <linux/io.h>
21#include <linux/module.h>
22
23#define RTC_REG_SIZE 0x20000
24#define RTC_OFFSET 0x1fff0
25
26#define RTC_FLAGS (RTC_OFFSET + 0)
27#define RTC_CENTURY (RTC_OFFSET + 1)
28#define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
29#define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
30#define RTC_HOURS_ALARM (RTC_OFFSET + 4)
31#define RTC_DATE_ALARM (RTC_OFFSET + 5)
32#define RTC_INTERRUPTS (RTC_OFFSET + 6)
33#define RTC_WATCHDOG (RTC_OFFSET + 7)
34#define RTC_CALIBRATION (RTC_OFFSET + 8)
35#define RTC_SECONDS (RTC_OFFSET + 9)
36#define RTC_MINUTES (RTC_OFFSET + 10)
37#define RTC_HOURS (RTC_OFFSET + 11)
38#define RTC_DAY (RTC_OFFSET + 12)
39#define RTC_DATE (RTC_OFFSET + 13)
40#define RTC_MONTH (RTC_OFFSET + 14)
41#define RTC_YEAR (RTC_OFFSET + 15)
42
43#define RTC_SECONDS_MASK 0x7f
44#define RTC_DAY_MASK 0x07
45#define RTC_CAL_MASK 0x3f
46
47/* Bits in the Calibration register */
48#define RTC_STOP 0x80
49
50/* Bits in the Flags register */
51#define RTC_FLAGS_AF 0x40
52#define RTC_FLAGS_PF 0x20
53#define RTC_WRITE 0x02
54#define RTC_READ 0x01
55
56/* Bits in the Interrupts register */
57#define RTC_INTS_AIE 0x40
58
59struct rtc_plat_data {
60 struct rtc_device *rtc;
61 void __iomem *ioaddr;
62 unsigned long last_jiffies;
63 int irq;
64 unsigned int irqen;
65 int alrm_sec;
66 int alrm_min;
67 int alrm_hour;
68 int alrm_mday;
69 spinlock_t lock;
70};
71
72static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
73{
74 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
75 void __iomem *ioaddr = pdata->ioaddr;
76 u8 flags;
77
78 flags = readb(pdata->ioaddr + RTC_FLAGS);
79 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
80
81 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
82 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
83 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
84 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
85 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
86 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
87 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
88 writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
89
90 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
91 return 0;
92}
93
94static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
95{
96 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
97 void __iomem *ioaddr = pdata->ioaddr;
98 unsigned int year, month, day, hour, minute, second, week;
99 unsigned int century;
100 u8 flags;
101
102 /* give enough time to update RTC in case of continuous read */
103 if (pdata->last_jiffies == jiffies)
104 msleep(1);
105 pdata->last_jiffies = jiffies;
106
107 flags = readb(pdata->ioaddr + RTC_FLAGS);
108 writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
109 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
110 minute = readb(ioaddr + RTC_MINUTES);
111 hour = readb(ioaddr + RTC_HOURS);
112 day = readb(ioaddr + RTC_DATE);
113 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
114 month = readb(ioaddr + RTC_MONTH);
115 year = readb(ioaddr + RTC_YEAR);
116 century = readb(ioaddr + RTC_CENTURY);
117 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
118 tm->tm_sec = bcd2bin(second);
119 tm->tm_min = bcd2bin(minute);
120 tm->tm_hour = bcd2bin(hour);
121 tm->tm_mday = bcd2bin(day);
122 tm->tm_wday = bcd2bin(week);
123 tm->tm_mon = bcd2bin(month) - 1;
124 /* year is 1900 + tm->tm_year */
125 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
126
127 return 0;
128}
129
130static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
131{
132 void __iomem *ioaddr = pdata->ioaddr;
133 unsigned long irqflags;
134 u8 flags;
135
136 spin_lock_irqsave(&pdata->lock, irqflags);
137
138 flags = readb(ioaddr + RTC_FLAGS);
139 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
140
141 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
142 0x80 : bin2bcd(pdata->alrm_mday),
143 ioaddr + RTC_DATE_ALARM);
144 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
145 0x80 : bin2bcd(pdata->alrm_hour),
146 ioaddr + RTC_HOURS_ALARM);
147 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
148 0x80 : bin2bcd(pdata->alrm_min),
149 ioaddr + RTC_MINUTES_ALARM);
150 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
151 0x80 : bin2bcd(pdata->alrm_sec),
152 ioaddr + RTC_SECONDS_ALARM);
153 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
154 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
155 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
156 spin_unlock_irqrestore(&pdata->lock, irqflags);
157}
158
159static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
160{
161 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
162
163 if (pdata->irq <= 0)
164 return -EINVAL;
165 pdata->alrm_mday = alrm->time.tm_mday;
166 pdata->alrm_hour = alrm->time.tm_hour;
167 pdata->alrm_min = alrm->time.tm_min;
168 pdata->alrm_sec = alrm->time.tm_sec;
169 if (alrm->enabled)
170 pdata->irqen |= RTC_AF;
171 stk17ta8_rtc_update_alarm(pdata);
172 return 0;
173}
174
175static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
176{
177 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
178
179 if (pdata->irq <= 0)
180 return -EINVAL;
181 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
182 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
183 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
184 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
185 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
186 return 0;
187}
188
189static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
190{
191 struct platform_device *pdev = dev_id;
192 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
193 void __iomem *ioaddr = pdata->ioaddr;
194 unsigned long events = 0;
195
196 spin_lock(&pdata->lock);
197 /* read and clear interrupt */
198 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
199 events = RTC_IRQF;
200 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
201 events |= RTC_UF;
202 else
203 events |= RTC_AF;
204 rtc_update_irq(pdata->rtc, 1, events);
205 }
206 spin_unlock(&pdata->lock);
207 return events ? IRQ_HANDLED : IRQ_NONE;
208}
209
210static int stk17ta8_rtc_alarm_irq_enable(struct device *dev,
211 unsigned int enabled)
212{
213 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
214
215 if (pdata->irq <= 0)
216 return -EINVAL;
217 if (enabled)
218 pdata->irqen |= RTC_AF;
219 else
220 pdata->irqen &= ~RTC_AF;
221 stk17ta8_rtc_update_alarm(pdata);
222 return 0;
223}
224
225static const struct rtc_class_ops stk17ta8_rtc_ops = {
226 .read_time = stk17ta8_rtc_read_time,
227 .set_time = stk17ta8_rtc_set_time,
228 .read_alarm = stk17ta8_rtc_read_alarm,
229 .set_alarm = stk17ta8_rtc_set_alarm,
230 .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable,
231};
232
233static int stk17ta8_nvram_read(void *priv, unsigned int pos, void *val,
234 size_t bytes)
235{
236 struct rtc_plat_data *pdata = priv;
237 void __iomem *ioaddr = pdata->ioaddr;
238 u8 *buf = val;
239
240 for (; bytes; bytes--)
241 *buf++ = readb(ioaddr + pos++);
242 return 0;
243}
244
245static int stk17ta8_nvram_write(void *priv, unsigned int pos, void *val,
246 size_t bytes)
247{
248 struct rtc_plat_data *pdata = priv;
249 void __iomem *ioaddr = pdata->ioaddr;
250 u8 *buf = val;
251
252 for (; bytes; bytes--)
253 writeb(*buf++, ioaddr + pos++);
254 return 0;
255}
256
257static int stk17ta8_rtc_probe(struct platform_device *pdev)
258{
259 struct resource *res;
260 unsigned int cal;
261 unsigned int flags;
262 struct rtc_plat_data *pdata;
263 void __iomem *ioaddr;
264 int ret = 0;
265 struct nvmem_config nvmem_cfg = {
266 .name = "stk17ta8_nvram",
267 .word_size = 1,
268 .stride = 1,
269 .size = RTC_OFFSET,
270 .reg_read = stk17ta8_nvram_read,
271 .reg_write = stk17ta8_nvram_write,
272 };
273
274 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
275 if (!pdata)
276 return -ENOMEM;
277
278 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
279 ioaddr = devm_ioremap_resource(&pdev->dev, res);
280 if (IS_ERR(ioaddr))
281 return PTR_ERR(ioaddr);
282 pdata->ioaddr = ioaddr;
283 pdata->irq = platform_get_irq(pdev, 0);
284
285 /* turn RTC on if it was not on */
286 cal = readb(ioaddr + RTC_CALIBRATION);
287 if (cal & RTC_STOP) {
288 cal &= RTC_CAL_MASK;
289 flags = readb(ioaddr + RTC_FLAGS);
290 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
291 writeb(cal, ioaddr + RTC_CALIBRATION);
292 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
293 }
294 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
295 dev_warn(&pdev->dev, "voltage-low detected.\n");
296
297 spin_lock_init(&pdata->lock);
298 pdata->last_jiffies = jiffies;
299 platform_set_drvdata(pdev, pdata);
300 if (pdata->irq > 0) {
301 writeb(0, ioaddr + RTC_INTERRUPTS);
302 if (devm_request_irq(&pdev->dev, pdata->irq,
303 stk17ta8_rtc_interrupt,
304 IRQF_SHARED,
305 pdev->name, pdev) < 0) {
306 dev_warn(&pdev->dev, "interrupt not available.\n");
307 pdata->irq = 0;
308 }
309 }
310
311 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
312 if (IS_ERR(pdata->rtc))
313 return PTR_ERR(pdata->rtc);
314
315 pdata->rtc->ops = &stk17ta8_rtc_ops;
316 pdata->rtc->nvram_old_abi = true;
317
318 nvmem_cfg.priv = pdata;
319 ret = rtc_nvmem_register(pdata->rtc, &nvmem_cfg);
320 if (ret)
321 return ret;
322
323 return rtc_register_device(pdata->rtc);
324}
325
326/* work with hotplug and coldplug */
327MODULE_ALIAS("platform:stk17ta8");
328
329static struct platform_driver stk17ta8_rtc_driver = {
330 .probe = stk17ta8_rtc_probe,
331 .driver = {
332 .name = "stk17ta8",
333 },
334};
335
336module_platform_driver(stk17ta8_rtc_driver);
337
338MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>");
339MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
340MODULE_LICENSE("GPL");
1/*
2 * A RTC driver for the Simtek STK17TA8
3 *
4 * By Thomas Hommel <thomas.hommel@ge.com>
5 *
6 * Based on the DS1553 driver from
7 * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/bcd.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/gfp.h>
18#include <linux/delay.h>
19#include <linux/jiffies.h>
20#include <linux/interrupt.h>
21#include <linux/rtc.h>
22#include <linux/platform_device.h>
23#include <linux/io.h>
24#include <linux/module.h>
25
26#define RTC_REG_SIZE 0x20000
27#define RTC_OFFSET 0x1fff0
28
29#define RTC_FLAGS (RTC_OFFSET + 0)
30#define RTC_CENTURY (RTC_OFFSET + 1)
31#define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
32#define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
33#define RTC_HOURS_ALARM (RTC_OFFSET + 4)
34#define RTC_DATE_ALARM (RTC_OFFSET + 5)
35#define RTC_INTERRUPTS (RTC_OFFSET + 6)
36#define RTC_WATCHDOG (RTC_OFFSET + 7)
37#define RTC_CALIBRATION (RTC_OFFSET + 8)
38#define RTC_SECONDS (RTC_OFFSET + 9)
39#define RTC_MINUTES (RTC_OFFSET + 10)
40#define RTC_HOURS (RTC_OFFSET + 11)
41#define RTC_DAY (RTC_OFFSET + 12)
42#define RTC_DATE (RTC_OFFSET + 13)
43#define RTC_MONTH (RTC_OFFSET + 14)
44#define RTC_YEAR (RTC_OFFSET + 15)
45
46#define RTC_SECONDS_MASK 0x7f
47#define RTC_DAY_MASK 0x07
48#define RTC_CAL_MASK 0x3f
49
50/* Bits in the Calibration register */
51#define RTC_STOP 0x80
52
53/* Bits in the Flags register */
54#define RTC_FLAGS_AF 0x40
55#define RTC_FLAGS_PF 0x20
56#define RTC_WRITE 0x02
57#define RTC_READ 0x01
58
59/* Bits in the Interrupts register */
60#define RTC_INTS_AIE 0x40
61
62struct rtc_plat_data {
63 struct rtc_device *rtc;
64 void __iomem *ioaddr;
65 unsigned long last_jiffies;
66 int irq;
67 unsigned int irqen;
68 int alrm_sec;
69 int alrm_min;
70 int alrm_hour;
71 int alrm_mday;
72 spinlock_t lock;
73};
74
75static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
76{
77 struct platform_device *pdev = to_platform_device(dev);
78 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
79 void __iomem *ioaddr = pdata->ioaddr;
80 u8 flags;
81
82 flags = readb(pdata->ioaddr + RTC_FLAGS);
83 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
84
85 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
86 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
87 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
88 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
89 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
90 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
91 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
92 writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
93
94 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
95 return 0;
96}
97
98static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
99{
100 struct platform_device *pdev = to_platform_device(dev);
101 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
102 void __iomem *ioaddr = pdata->ioaddr;
103 unsigned int year, month, day, hour, minute, second, week;
104 unsigned int century;
105 u8 flags;
106
107 /* give enough time to update RTC in case of continuous read */
108 if (pdata->last_jiffies == jiffies)
109 msleep(1);
110 pdata->last_jiffies = jiffies;
111
112 flags = readb(pdata->ioaddr + RTC_FLAGS);
113 writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
114 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
115 minute = readb(ioaddr + RTC_MINUTES);
116 hour = readb(ioaddr + RTC_HOURS);
117 day = readb(ioaddr + RTC_DATE);
118 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
119 month = readb(ioaddr + RTC_MONTH);
120 year = readb(ioaddr + RTC_YEAR);
121 century = readb(ioaddr + RTC_CENTURY);
122 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
123 tm->tm_sec = bcd2bin(second);
124 tm->tm_min = bcd2bin(minute);
125 tm->tm_hour = bcd2bin(hour);
126 tm->tm_mday = bcd2bin(day);
127 tm->tm_wday = bcd2bin(week);
128 tm->tm_mon = bcd2bin(month) - 1;
129 /* year is 1900 + tm->tm_year */
130 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
131
132 if (rtc_valid_tm(tm) < 0) {
133 dev_err(dev, "retrieved date/time is not valid.\n");
134 rtc_time_to_tm(0, tm);
135 }
136 return 0;
137}
138
139static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
140{
141 void __iomem *ioaddr = pdata->ioaddr;
142 unsigned long irqflags;
143 u8 flags;
144
145 spin_lock_irqsave(&pdata->lock, irqflags);
146
147 flags = readb(ioaddr + RTC_FLAGS);
148 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
149
150 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
151 0x80 : bin2bcd(pdata->alrm_mday),
152 ioaddr + RTC_DATE_ALARM);
153 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
154 0x80 : bin2bcd(pdata->alrm_hour),
155 ioaddr + RTC_HOURS_ALARM);
156 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
157 0x80 : bin2bcd(pdata->alrm_min),
158 ioaddr + RTC_MINUTES_ALARM);
159 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
160 0x80 : bin2bcd(pdata->alrm_sec),
161 ioaddr + RTC_SECONDS_ALARM);
162 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
163 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
164 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
165 spin_unlock_irqrestore(&pdata->lock, irqflags);
166}
167
168static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
169{
170 struct platform_device *pdev = to_platform_device(dev);
171 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
172
173 if (pdata->irq <= 0)
174 return -EINVAL;
175 pdata->alrm_mday = alrm->time.tm_mday;
176 pdata->alrm_hour = alrm->time.tm_hour;
177 pdata->alrm_min = alrm->time.tm_min;
178 pdata->alrm_sec = alrm->time.tm_sec;
179 if (alrm->enabled)
180 pdata->irqen |= RTC_AF;
181 stk17ta8_rtc_update_alarm(pdata);
182 return 0;
183}
184
185static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
186{
187 struct platform_device *pdev = to_platform_device(dev);
188 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
189
190 if (pdata->irq <= 0)
191 return -EINVAL;
192 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
193 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
194 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
195 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
196 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
197 return 0;
198}
199
200static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
201{
202 struct platform_device *pdev = dev_id;
203 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
204 void __iomem *ioaddr = pdata->ioaddr;
205 unsigned long events = 0;
206
207 spin_lock(&pdata->lock);
208 /* read and clear interrupt */
209 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
210 events = RTC_IRQF;
211 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
212 events |= RTC_UF;
213 else
214 events |= RTC_AF;
215 rtc_update_irq(pdata->rtc, 1, events);
216 }
217 spin_unlock(&pdata->lock);
218 return events ? IRQ_HANDLED : IRQ_NONE;
219}
220
221static int stk17ta8_rtc_alarm_irq_enable(struct device *dev,
222 unsigned int enabled)
223{
224 struct platform_device *pdev = to_platform_device(dev);
225 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
226
227 if (pdata->irq <= 0)
228 return -EINVAL;
229 if (enabled)
230 pdata->irqen |= RTC_AF;
231 else
232 pdata->irqen &= ~RTC_AF;
233 stk17ta8_rtc_update_alarm(pdata);
234 return 0;
235}
236
237static const struct rtc_class_ops stk17ta8_rtc_ops = {
238 .read_time = stk17ta8_rtc_read_time,
239 .set_time = stk17ta8_rtc_set_time,
240 .read_alarm = stk17ta8_rtc_read_alarm,
241 .set_alarm = stk17ta8_rtc_set_alarm,
242 .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable,
243};
244
245static ssize_t stk17ta8_nvram_read(struct file *filp, struct kobject *kobj,
246 struct bin_attribute *attr, char *buf,
247 loff_t pos, size_t size)
248{
249 struct device *dev = container_of(kobj, struct device, kobj);
250 struct platform_device *pdev = to_platform_device(dev);
251 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
252 void __iomem *ioaddr = pdata->ioaddr;
253 ssize_t count;
254
255 for (count = 0; count < size; count++)
256 *buf++ = readb(ioaddr + pos++);
257 return count;
258}
259
260static ssize_t stk17ta8_nvram_write(struct file *filp, struct kobject *kobj,
261 struct bin_attribute *attr, char *buf,
262 loff_t pos, size_t size)
263{
264 struct device *dev = container_of(kobj, struct device, kobj);
265 struct platform_device *pdev = to_platform_device(dev);
266 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
267 void __iomem *ioaddr = pdata->ioaddr;
268 ssize_t count;
269
270 for (count = 0; count < size; count++)
271 writeb(*buf++, ioaddr + pos++);
272 return count;
273}
274
275static struct bin_attribute stk17ta8_nvram_attr = {
276 .attr = {
277 .name = "nvram",
278 .mode = S_IRUGO | S_IWUSR,
279 },
280 .size = RTC_OFFSET,
281 .read = stk17ta8_nvram_read,
282 .write = stk17ta8_nvram_write,
283};
284
285static int stk17ta8_rtc_probe(struct platform_device *pdev)
286{
287 struct resource *res;
288 unsigned int cal;
289 unsigned int flags;
290 struct rtc_plat_data *pdata;
291 void __iomem *ioaddr;
292 int ret = 0;
293
294 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
295 if (!pdata)
296 return -ENOMEM;
297
298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
299 ioaddr = devm_ioremap_resource(&pdev->dev, res);
300 if (IS_ERR(ioaddr))
301 return PTR_ERR(ioaddr);
302 pdata->ioaddr = ioaddr;
303 pdata->irq = platform_get_irq(pdev, 0);
304
305 /* turn RTC on if it was not on */
306 cal = readb(ioaddr + RTC_CALIBRATION);
307 if (cal & RTC_STOP) {
308 cal &= RTC_CAL_MASK;
309 flags = readb(ioaddr + RTC_FLAGS);
310 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
311 writeb(cal, ioaddr + RTC_CALIBRATION);
312 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
313 }
314 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
315 dev_warn(&pdev->dev, "voltage-low detected.\n");
316
317 spin_lock_init(&pdata->lock);
318 pdata->last_jiffies = jiffies;
319 platform_set_drvdata(pdev, pdata);
320 if (pdata->irq > 0) {
321 writeb(0, ioaddr + RTC_INTERRUPTS);
322 if (devm_request_irq(&pdev->dev, pdata->irq,
323 stk17ta8_rtc_interrupt,
324 IRQF_SHARED,
325 pdev->name, pdev) < 0) {
326 dev_warn(&pdev->dev, "interrupt not available.\n");
327 pdata->irq = 0;
328 }
329 }
330
331 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
332 &stk17ta8_rtc_ops, THIS_MODULE);
333 if (IS_ERR(pdata->rtc))
334 return PTR_ERR(pdata->rtc);
335
336 ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
337
338 return ret;
339}
340
341static int stk17ta8_rtc_remove(struct platform_device *pdev)
342{
343 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
344
345 sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
346 if (pdata->irq > 0)
347 writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
348 return 0;
349}
350
351/* work with hotplug and coldplug */
352MODULE_ALIAS("platform:stk17ta8");
353
354static struct platform_driver stk17ta8_rtc_driver = {
355 .probe = stk17ta8_rtc_probe,
356 .remove = stk17ta8_rtc_remove,
357 .driver = {
358 .name = "stk17ta8",
359 },
360};
361
362module_platform_driver(stk17ta8_rtc_driver);
363
364MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>");
365MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
366MODULE_LICENSE("GPL");