]>
Commit | Line | Data |
---|---|---|
9bf5b4f5 AN |
1 | /* |
2 | * An rtc driver for the Dallas DS1553 | |
3 | * | |
4 | * Copyright (C) 2006 Atsushi Nemoto <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/bcd.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/jiffies.h> | |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/rtc.h> | |
18 | #include <linux/platform_device.h> | |
19 | #include <linux/io.h> | |
20 | ||
618161f7 | 21 | #define DRV_VERSION "0.3" |
9bf5b4f5 AN |
22 | |
23 | #define RTC_REG_SIZE 0x2000 | |
24 | #define RTC_OFFSET 0x1ff0 | |
25 | ||
26 | #define RTC_FLAGS (RTC_OFFSET + 0) | |
27 | #define RTC_SECONDS_ALARM (RTC_OFFSET + 2) | |
28 | #define RTC_MINUTES_ALARM (RTC_OFFSET + 3) | |
29 | #define RTC_HOURS_ALARM (RTC_OFFSET + 4) | |
30 | #define RTC_DATE_ALARM (RTC_OFFSET + 5) | |
31 | #define RTC_INTERRUPTS (RTC_OFFSET + 6) | |
32 | #define RTC_WATCHDOG (RTC_OFFSET + 7) | |
33 | #define RTC_CONTROL (RTC_OFFSET + 8) | |
34 | #define RTC_CENTURY (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_CENTURY_MASK 0x3f | |
44 | #define RTC_SECONDS_MASK 0x7f | |
45 | #define RTC_DAY_MASK 0x07 | |
46 | ||
47 | /* Bits in the Control/Century register */ | |
48 | #define RTC_WRITE 0x80 | |
49 | #define RTC_READ 0x40 | |
50 | ||
51 | /* Bits in the Seconds register */ | |
52 | #define RTC_STOP 0x80 | |
53 | ||
54 | /* Bits in the Flags register */ | |
55 | #define RTC_FLAGS_AF 0x40 | |
56 | #define RTC_FLAGS_BLF 0x10 | |
57 | ||
58 | /* Bits in the Interrupts register */ | |
59 | #define RTC_INTS_AE 0x80 | |
60 | ||
61 | struct rtc_plat_data { | |
62 | struct rtc_device *rtc; | |
63 | void __iomem *ioaddr; | |
9bf5b4f5 AN |
64 | unsigned long last_jiffies; |
65 | int irq; | |
66 | unsigned int irqen; | |
67 | int alrm_sec; | |
68 | int alrm_min; | |
69 | int alrm_hour; | |
70 | int alrm_mday; | |
618161f7 | 71 | spinlock_t lock; |
9bf5b4f5 AN |
72 | }; |
73 | ||
74 | static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
75 | { | |
76 | struct platform_device *pdev = to_platform_device(dev); | |
77 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
78 | void __iomem *ioaddr = pdata->ioaddr; | |
79 | u8 century; | |
80 | ||
fe20ba70 | 81 | century = bin2bcd((tm->tm_year + 1900) / 100); |
9bf5b4f5 AN |
82 | |
83 | writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL); | |
84 | ||
fe20ba70 AB |
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); | |
9bf5b4f5 AN |
92 | |
93 | /* RTC_CENTURY and RTC_CONTROL share same register */ | |
94 | writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY); | |
95 | writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
100 | { | |
101 | struct platform_device *pdev = to_platform_device(dev); | |
102 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
103 | void __iomem *ioaddr = pdata->ioaddr; | |
104 | unsigned int year, month, day, hour, minute, second, week; | |
105 | unsigned int century; | |
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 | writeb(RTC_READ, ioaddr + RTC_CONTROL); | |
112 | second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK; | |
113 | minute = readb(ioaddr + RTC_MINUTES); | |
114 | hour = readb(ioaddr + RTC_HOURS); | |
115 | day = readb(ioaddr + RTC_DATE); | |
116 | week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK; | |
117 | month = readb(ioaddr + RTC_MONTH); | |
118 | year = readb(ioaddr + RTC_YEAR); | |
119 | century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; | |
120 | writeb(0, ioaddr + RTC_CONTROL); | |
fe20ba70 AB |
121 | tm->tm_sec = bcd2bin(second); |
122 | tm->tm_min = bcd2bin(minute); | |
123 | tm->tm_hour = bcd2bin(hour); | |
124 | tm->tm_mday = bcd2bin(day); | |
125 | tm->tm_wday = bcd2bin(week); | |
126 | tm->tm_mon = bcd2bin(month) - 1; | |
9bf5b4f5 | 127 | /* year is 1900 + tm->tm_year */ |
fe20ba70 | 128 | tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900; |
9bf5b4f5 AN |
129 | |
130 | if (rtc_valid_tm(tm) < 0) { | |
131 | dev_err(dev, "retrieved date/time is not valid.\n"); | |
132 | rtc_time_to_tm(0, tm); | |
133 | } | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata) | |
138 | { | |
139 | void __iomem *ioaddr = pdata->ioaddr; | |
140 | unsigned long flags; | |
141 | ||
618161f7 | 142 | spin_lock_irqsave(&pdata->lock, flags); |
9bf5b4f5 | 143 | writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? |
fe20ba70 | 144 | 0x80 : bin2bcd(pdata->alrm_mday), |
9bf5b4f5 AN |
145 | ioaddr + RTC_DATE_ALARM); |
146 | writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? | |
fe20ba70 | 147 | 0x80 : bin2bcd(pdata->alrm_hour), |
9bf5b4f5 AN |
148 | ioaddr + RTC_HOURS_ALARM); |
149 | writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? | |
fe20ba70 | 150 | 0x80 : bin2bcd(pdata->alrm_min), |
9bf5b4f5 AN |
151 | ioaddr + RTC_MINUTES_ALARM); |
152 | writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? | |
fe20ba70 | 153 | 0x80 : bin2bcd(pdata->alrm_sec), |
9bf5b4f5 AN |
154 | ioaddr + RTC_SECONDS_ALARM); |
155 | writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS); | |
156 | readb(ioaddr + RTC_FLAGS); /* clear interrupts */ | |
618161f7 | 157 | spin_unlock_irqrestore(&pdata->lock, flags); |
9bf5b4f5 AN |
158 | } |
159 | ||
160 | static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | |
161 | { | |
162 | struct platform_device *pdev = to_platform_device(dev); | |
163 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
164 | ||
2fac6674 | 165 | if (pdata->irq <= 0) |
9bf5b4f5 AN |
166 | return -EINVAL; |
167 | pdata->alrm_mday = alrm->time.tm_mday; | |
168 | pdata->alrm_hour = alrm->time.tm_hour; | |
169 | pdata->alrm_min = alrm->time.tm_min; | |
170 | pdata->alrm_sec = alrm->time.tm_sec; | |
171 | if (alrm->enabled) | |
172 | pdata->irqen |= RTC_AF; | |
173 | ds1553_rtc_update_alarm(pdata); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | |
178 | { | |
179 | struct platform_device *pdev = to_platform_device(dev); | |
180 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
181 | ||
2fac6674 | 182 | if (pdata->irq <= 0) |
9bf5b4f5 AN |
183 | return -EINVAL; |
184 | alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; | |
185 | alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; | |
186 | alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; | |
187 | alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; | |
188 | alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; | |
189 | return 0; | |
190 | } | |
191 | ||
7d12e780 | 192 | static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id) |
9bf5b4f5 AN |
193 | { |
194 | struct platform_device *pdev = dev_id; | |
195 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
196 | void __iomem *ioaddr = pdata->ioaddr; | |
618161f7 | 197 | unsigned long events = 0; |
9bf5b4f5 | 198 | |
618161f7 | 199 | spin_lock(&pdata->lock); |
9bf5b4f5 | 200 | /* read and clear interrupt */ |
618161f7 AN |
201 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { |
202 | events = RTC_IRQF; | |
203 | if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) | |
204 | events |= RTC_UF; | |
205 | else | |
206 | events |= RTC_AF; | |
207 | if (likely(pdata->rtc)) | |
208 | rtc_update_irq(pdata->rtc, 1, events); | |
209 | } | |
210 | spin_unlock(&pdata->lock); | |
211 | return events ? IRQ_HANDLED : IRQ_NONE; | |
9bf5b4f5 AN |
212 | } |
213 | ||
618161f7 | 214 | static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
9bf5b4f5 AN |
215 | { |
216 | struct platform_device *pdev = to_platform_device(dev); | |
217 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
218 | ||
2fac6674 | 219 | if (pdata->irq <= 0) |
618161f7 AN |
220 | return -EINVAL; |
221 | if (enabled) | |
9bf5b4f5 | 222 | pdata->irqen |= RTC_AF; |
618161f7 AN |
223 | else |
224 | pdata->irqen &= ~RTC_AF; | |
225 | ds1553_rtc_update_alarm(pdata); | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int ds1553_rtc_update_irq_enable(struct device *dev, | |
230 | unsigned int enabled) | |
231 | { | |
232 | struct platform_device *pdev = to_platform_device(dev); | |
233 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
234 | ||
235 | if (pdata->irq <= 0) | |
236 | return -EINVAL; | |
237 | if (enabled) | |
9bf5b4f5 | 238 | pdata->irqen |= RTC_UF; |
618161f7 AN |
239 | else |
240 | pdata->irqen &= ~RTC_UF; | |
241 | ds1553_rtc_update_alarm(pdata); | |
9bf5b4f5 AN |
242 | return 0; |
243 | } | |
244 | ||
ff8371ac | 245 | static const struct rtc_class_ops ds1553_rtc_ops = { |
618161f7 AN |
246 | .read_time = ds1553_rtc_read_time, |
247 | .set_time = ds1553_rtc_set_time, | |
248 | .read_alarm = ds1553_rtc_read_alarm, | |
249 | .set_alarm = ds1553_rtc_set_alarm, | |
250 | .alarm_irq_enable = ds1553_rtc_alarm_irq_enable, | |
251 | .update_irq_enable = ds1553_rtc_update_irq_enable, | |
9bf5b4f5 AN |
252 | }; |
253 | ||
91a69029 ZR |
254 | static ssize_t ds1553_nvram_read(struct kobject *kobj, |
255 | struct bin_attribute *bin_attr, | |
256 | char *buf, loff_t pos, size_t size) | |
9bf5b4f5 AN |
257 | { |
258 | struct platform_device *pdev = | |
259 | to_platform_device(container_of(kobj, struct device, kobj)); | |
260 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
261 | void __iomem *ioaddr = pdata->ioaddr; | |
262 | ssize_t count; | |
263 | ||
264 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) | |
265 | *buf++ = readb(ioaddr + pos++); | |
266 | return count; | |
267 | } | |
268 | ||
91a69029 ZR |
269 | static ssize_t ds1553_nvram_write(struct kobject *kobj, |
270 | struct bin_attribute *bin_attr, | |
271 | char *buf, loff_t pos, size_t size) | |
9bf5b4f5 AN |
272 | { |
273 | struct platform_device *pdev = | |
274 | to_platform_device(container_of(kobj, struct device, kobj)); | |
275 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
276 | void __iomem *ioaddr = pdata->ioaddr; | |
277 | ssize_t count; | |
278 | ||
279 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) | |
280 | writeb(*buf++, ioaddr + pos++); | |
281 | return count; | |
282 | } | |
283 | ||
284 | static struct bin_attribute ds1553_nvram_attr = { | |
285 | .attr = { | |
286 | .name = "nvram", | |
a4b1d50e | 287 | .mode = S_IRUGO | S_IWUSR, |
9bf5b4f5 AN |
288 | }, |
289 | .size = RTC_OFFSET, | |
290 | .read = ds1553_nvram_read, | |
291 | .write = ds1553_nvram_write, | |
292 | }; | |
293 | ||
c239122d | 294 | static int __devinit ds1553_rtc_probe(struct platform_device *pdev) |
9bf5b4f5 AN |
295 | { |
296 | struct rtc_device *rtc; | |
297 | struct resource *res; | |
298 | unsigned int cen, sec; | |
618161f7 AN |
299 | struct rtc_plat_data *pdata; |
300 | void __iomem *ioaddr; | |
9bf5b4f5 AN |
301 | int ret = 0; |
302 | ||
303 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
304 | if (!res) | |
305 | return -ENODEV; | |
618161f7 | 306 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
9bf5b4f5 AN |
307 | if (!pdata) |
308 | return -ENOMEM; | |
618161f7 AN |
309 | if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE, |
310 | pdev->name)) | |
311 | return -EBUSY; | |
312 | ||
313 | ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE); | |
314 | if (!ioaddr) | |
315 | return -ENOMEM; | |
9bf5b4f5 AN |
316 | pdata->ioaddr = ioaddr; |
317 | pdata->irq = platform_get_irq(pdev, 0); | |
318 | ||
319 | /* turn RTC on if it was not on */ | |
320 | sec = readb(ioaddr + RTC_SECONDS); | |
321 | if (sec & RTC_STOP) { | |
322 | sec &= RTC_SECONDS_MASK; | |
323 | cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; | |
324 | writeb(RTC_WRITE, ioaddr + RTC_CONTROL); | |
325 | writeb(sec, ioaddr + RTC_SECONDS); | |
326 | writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); | |
327 | } | |
328 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF) | |
329 | dev_warn(&pdev->dev, "voltage-low detected.\n"); | |
330 | ||
618161f7 AN |
331 | spin_lock_init(&pdata->lock); |
332 | pdata->last_jiffies = jiffies; | |
333 | platform_set_drvdata(pdev, pdata); | |
2fac6674 | 334 | if (pdata->irq > 0) { |
9bf5b4f5 | 335 | writeb(0, ioaddr + RTC_INTERRUPTS); |
618161f7 AN |
336 | if (devm_request_irq(&pdev->dev, pdata->irq, |
337 | ds1553_rtc_interrupt, | |
014b6e90 | 338 | IRQF_DISABLED, pdev->name, pdev) < 0) { |
9bf5b4f5 | 339 | dev_warn(&pdev->dev, "interrupt not available.\n"); |
2fac6674 | 340 | pdata->irq = 0; |
9bf5b4f5 AN |
341 | } |
342 | } | |
343 | ||
344 | rtc = rtc_device_register(pdev->name, &pdev->dev, | |
345 | &ds1553_rtc_ops, THIS_MODULE); | |
618161f7 AN |
346 | if (IS_ERR(rtc)) |
347 | return PTR_ERR(rtc); | |
9bf5b4f5 | 348 | pdata->rtc = rtc; |
618161f7 | 349 | |
391b1fe6 AN |
350 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); |
351 | if (ret) | |
618161f7 | 352 | rtc_device_unregister(rtc); |
9bf5b4f5 AN |
353 | return ret; |
354 | } | |
355 | ||
356 | static int __devexit ds1553_rtc_remove(struct platform_device *pdev) | |
357 | { | |
358 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
359 | ||
360 | sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); | |
361 | rtc_device_unregister(pdata->rtc); | |
618161f7 | 362 | if (pdata->irq > 0) |
9bf5b4f5 | 363 | writeb(0, pdata->ioaddr + RTC_INTERRUPTS); |
9bf5b4f5 AN |
364 | return 0; |
365 | } | |
366 | ||
ad28a07b KS |
367 | /* work with hotplug and coldplug */ |
368 | MODULE_ALIAS("platform:rtc-ds1553"); | |
369 | ||
9bf5b4f5 AN |
370 | static struct platform_driver ds1553_rtc_driver = { |
371 | .probe = ds1553_rtc_probe, | |
372 | .remove = __devexit_p(ds1553_rtc_remove), | |
373 | .driver = { | |
e7634c27 | 374 | .name = "rtc-ds1553", |
9bf5b4f5 AN |
375 | .owner = THIS_MODULE, |
376 | }, | |
377 | }; | |
378 | ||
379 | static __init int ds1553_init(void) | |
380 | { | |
381 | return platform_driver_register(&ds1553_rtc_driver); | |
382 | } | |
383 | ||
384 | static __exit void ds1553_exit(void) | |
385 | { | |
ef154ec6 | 386 | platform_driver_unregister(&ds1553_rtc_driver); |
9bf5b4f5 AN |
387 | } |
388 | ||
389 | module_init(ds1553_init); | |
390 | module_exit(ds1553_exit); | |
391 | ||
392 | MODULE_AUTHOR("Atsushi Nemoto <[email protected]>"); | |
393 | MODULE_DESCRIPTION("Dallas DS1553 RTC driver"); | |
394 | MODULE_LICENSE("GPL"); | |
395 | MODULE_VERSION(DRV_VERSION); |