Blame view
drivers/rtc/rtc-ds1553.c
9.17 KB
d2912cb15
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
9bf5b4f5f
|
2 3 4 5 |
/* * An rtc driver for the Dallas DS1553 * * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
9bf5b4f5f
|
6 7 8 9 10 |
*/ #include <linux/bcd.h> #include <linux/init.h> #include <linux/kernel.h> |
5a0e3ad6a
|
11 |
#include <linux/gfp.h> |
9bf5b4f5f
|
12 13 14 15 16 17 |
#include <linux/delay.h> #include <linux/jiffies.h> #include <linux/interrupt.h> #include <linux/rtc.h> #include <linux/platform_device.h> #include <linux/io.h> |
2113852b2
|
18 |
#include <linux/module.h> |
9bf5b4f5f
|
19 |
|
9bf5b4f5f
|
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#define RTC_REG_SIZE 0x2000 #define RTC_OFFSET 0x1ff0 #define RTC_FLAGS (RTC_OFFSET + 0) #define RTC_SECONDS_ALARM (RTC_OFFSET + 2) #define RTC_MINUTES_ALARM (RTC_OFFSET + 3) #define RTC_HOURS_ALARM (RTC_OFFSET + 4) #define RTC_DATE_ALARM (RTC_OFFSET + 5) #define RTC_INTERRUPTS (RTC_OFFSET + 6) #define RTC_WATCHDOG (RTC_OFFSET + 7) #define RTC_CONTROL (RTC_OFFSET + 8) #define RTC_CENTURY (RTC_OFFSET + 8) #define RTC_SECONDS (RTC_OFFSET + 9) #define RTC_MINUTES (RTC_OFFSET + 10) #define RTC_HOURS (RTC_OFFSET + 11) #define RTC_DAY (RTC_OFFSET + 12) #define RTC_DATE (RTC_OFFSET + 13) #define RTC_MONTH (RTC_OFFSET + 14) #define RTC_YEAR (RTC_OFFSET + 15) #define RTC_CENTURY_MASK 0x3f #define RTC_SECONDS_MASK 0x7f #define RTC_DAY_MASK 0x07 /* Bits in the Control/Century register */ #define RTC_WRITE 0x80 #define RTC_READ 0x40 /* Bits in the Seconds register */ #define RTC_STOP 0x80 /* Bits in the Flags register */ #define RTC_FLAGS_AF 0x40 #define RTC_FLAGS_BLF 0x10 /* Bits in the Interrupts register */ #define RTC_INTS_AE 0x80 struct rtc_plat_data { struct rtc_device *rtc; void __iomem *ioaddr; |
9bf5b4f5f
|
61 62 63 64 65 66 67 |
unsigned long last_jiffies; int irq; unsigned int irqen; int alrm_sec; int alrm_min; int alrm_hour; int alrm_mday; |
618161f71
|
68 |
spinlock_t lock; |
9bf5b4f5f
|
69 70 71 72 |
}; static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm) { |
85368bb9d
|
73 |
struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
9bf5b4f5f
|
74 75 |
void __iomem *ioaddr = pdata->ioaddr; u8 century; |
fe20ba70a
|
76 |
century = bin2bcd((tm->tm_year + 1900) / 100); |
9bf5b4f5f
|
77 78 |
writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL); |
fe20ba70a
|
79 80 81 82 83 84 85 |
writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR); writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH); writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE); writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS); writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES); writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); |
9bf5b4f5f
|
86 87 88 89 90 91 92 93 94 |
/* RTC_CENTURY and RTC_CONTROL share same register */ writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY); writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); return 0; } static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm) { |
85368bb9d
|
95 |
struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
9bf5b4f5f
|
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
void __iomem *ioaddr = pdata->ioaddr; unsigned int year, month, day, hour, minute, second, week; unsigned int century; /* give enough time to update RTC in case of continuous read */ if (pdata->last_jiffies == jiffies) msleep(1); pdata->last_jiffies = jiffies; writeb(RTC_READ, ioaddr + RTC_CONTROL); second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK; minute = readb(ioaddr + RTC_MINUTES); hour = readb(ioaddr + RTC_HOURS); day = readb(ioaddr + RTC_DATE); week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK; month = readb(ioaddr + RTC_MONTH); year = readb(ioaddr + RTC_YEAR); century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; writeb(0, ioaddr + RTC_CONTROL); |
fe20ba70a
|
114 115 116 117 118 119 |
tm->tm_sec = bcd2bin(second); tm->tm_min = bcd2bin(minute); tm->tm_hour = bcd2bin(hour); tm->tm_mday = bcd2bin(day); tm->tm_wday = bcd2bin(week); tm->tm_mon = bcd2bin(month) - 1; |
9bf5b4f5f
|
120 |
/* year is 1900 + tm->tm_year */ |
fe20ba70a
|
121 |
tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900; |
9bf5b4f5f
|
122 |
|
9bf5b4f5f
|
123 124 125 126 127 128 129 |
return 0; } static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata) { void __iomem *ioaddr = pdata->ioaddr; unsigned long flags; |
618161f71
|
130 |
spin_lock_irqsave(&pdata->lock, flags); |
9bf5b4f5f
|
131 |
writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? |
fe20ba70a
|
132 |
0x80 : bin2bcd(pdata->alrm_mday), |
9bf5b4f5f
|
133 134 |
ioaddr + RTC_DATE_ALARM); writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? |
fe20ba70a
|
135 |
0x80 : bin2bcd(pdata->alrm_hour), |
9bf5b4f5f
|
136 137 |
ioaddr + RTC_HOURS_ALARM); writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? |
fe20ba70a
|
138 |
0x80 : bin2bcd(pdata->alrm_min), |
9bf5b4f5f
|
139 140 |
ioaddr + RTC_MINUTES_ALARM); writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? |
fe20ba70a
|
141 |
0x80 : bin2bcd(pdata->alrm_sec), |
9bf5b4f5f
|
142 143 144 |
ioaddr + RTC_SECONDS_ALARM); writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS); readb(ioaddr + RTC_FLAGS); /* clear interrupts */ |
618161f71
|
145 |
spin_unlock_irqrestore(&pdata->lock, flags); |
9bf5b4f5f
|
146 147 148 149 |
} static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) { |
85368bb9d
|
150 |
struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
9bf5b4f5f
|
151 |
|
2fac6674d
|
152 |
if (pdata->irq <= 0) |
9bf5b4f5f
|
153 154 155 156 157 158 159 160 161 162 163 164 165 |
return -EINVAL; pdata->alrm_mday = alrm->time.tm_mday; pdata->alrm_hour = alrm->time.tm_hour; pdata->alrm_min = alrm->time.tm_min; pdata->alrm_sec = alrm->time.tm_sec; if (alrm->enabled) pdata->irqen |= RTC_AF; ds1553_rtc_update_alarm(pdata); return 0; } static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) { |
85368bb9d
|
166 |
struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
9bf5b4f5f
|
167 |
|
2fac6674d
|
168 |
if (pdata->irq <= 0) |
9bf5b4f5f
|
169 170 171 172 173 174 175 176 |
return -EINVAL; alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; return 0; } |
7d12e780e
|
177 |
static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id) |
9bf5b4f5f
|
178 179 180 181 |
{ struct platform_device *pdev = dev_id; struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; |
618161f71
|
182 |
unsigned long events = 0; |
9bf5b4f5f
|
183 |
|
618161f71
|
184 |
spin_lock(&pdata->lock); |
9bf5b4f5f
|
185 |
/* read and clear interrupt */ |
618161f71
|
186 187 188 189 190 191 |
if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { events = RTC_IRQF; if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) events |= RTC_UF; else events |= RTC_AF; |
0d71915d2
|
192 |
rtc_update_irq(pdata->rtc, 1, events); |
618161f71
|
193 194 195 |
} spin_unlock(&pdata->lock); return events ? IRQ_HANDLED : IRQ_NONE; |
9bf5b4f5f
|
196 |
} |
618161f71
|
197 |
static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
9bf5b4f5f
|
198 |
{ |
85368bb9d
|
199 |
struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
9bf5b4f5f
|
200 |
|
2fac6674d
|
201 |
if (pdata->irq <= 0) |
618161f71
|
202 203 |
return -EINVAL; if (enabled) |
9bf5b4f5f
|
204 |
pdata->irqen |= RTC_AF; |
618161f71
|
205 206 207 208 209 |
else pdata->irqen &= ~RTC_AF; ds1553_rtc_update_alarm(pdata); return 0; } |
ff8371ac9
|
210 |
static const struct rtc_class_ops ds1553_rtc_ops = { |
618161f71
|
211 212 213 214 215 |
.read_time = ds1553_rtc_read_time, .set_time = ds1553_rtc_set_time, .read_alarm = ds1553_rtc_read_alarm, .set_alarm = ds1553_rtc_set_alarm, .alarm_irq_enable = ds1553_rtc_alarm_irq_enable, |
9bf5b4f5f
|
216 |
}; |
3a9a06d44
|
217 218 |
static int ds1553_nvram_read(void *priv, unsigned int pos, void *val, size_t bytes) |
9bf5b4f5f
|
219 |
{ |
3a9a06d44
|
220 |
struct platform_device *pdev = priv; |
9bf5b4f5f
|
221 222 |
struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; |
3a9a06d44
|
223 |
u8 *buf = val; |
9bf5b4f5f
|
224 |
|
3a9a06d44
|
225 |
for (; bytes; bytes--) |
9bf5b4f5f
|
226 |
*buf++ = readb(ioaddr + pos++); |
3a9a06d44
|
227 |
return 0; |
9bf5b4f5f
|
228 |
} |
3a9a06d44
|
229 230 |
static int ds1553_nvram_write(void *priv, unsigned int pos, void *val, size_t bytes) |
9bf5b4f5f
|
231 |
{ |
3a9a06d44
|
232 |
struct platform_device *pdev = priv; |
9bf5b4f5f
|
233 234 |
struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; |
3a9a06d44
|
235 |
u8 *buf = val; |
9bf5b4f5f
|
236 |
|
3a9a06d44
|
237 |
for (; bytes; bytes--) |
9bf5b4f5f
|
238 |
writeb(*buf++, ioaddr + pos++); |
3a9a06d44
|
239 |
return 0; |
9bf5b4f5f
|
240 |
} |
5a167f454
|
241 |
static int ds1553_rtc_probe(struct platform_device *pdev) |
9bf5b4f5f
|
242 |
{ |
9bf5b4f5f
|
243 |
unsigned int cen, sec; |
618161f71
|
244 245 |
struct rtc_plat_data *pdata; void __iomem *ioaddr; |
9bf5b4f5f
|
246 |
int ret = 0; |
3a9a06d44
|
247 248 249 250 251 252 253 254 255 |
struct nvmem_config nvmem_cfg = { .name = "ds1553_nvram", .word_size = 1, .stride = 1, .size = RTC_OFFSET, .reg_read = ds1553_nvram_read, .reg_write = ds1553_nvram_write, .priv = pdev, }; |
9bf5b4f5f
|
256 |
|
618161f71
|
257 |
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
9bf5b4f5f
|
258 259 |
if (!pdata) return -ENOMEM; |
618161f71
|
260 |
|
09ef18bcd
|
261 |
ioaddr = devm_platform_ioremap_resource(pdev, 0); |
7c1d69ee1
|
262 263 |
if (IS_ERR(ioaddr)) return PTR_ERR(ioaddr); |
9bf5b4f5f
|
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
pdata->ioaddr = ioaddr; pdata->irq = platform_get_irq(pdev, 0); /* turn RTC on if it was not on */ sec = readb(ioaddr + RTC_SECONDS); if (sec & RTC_STOP) { sec &= RTC_SECONDS_MASK; cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; writeb(RTC_WRITE, ioaddr + RTC_CONTROL); writeb(sec, ioaddr + RTC_SECONDS); writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); } if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF) dev_warn(&pdev->dev, "voltage-low detected. "); |
618161f71
|
279 280 281 |
spin_lock_init(&pdata->lock); pdata->last_jiffies = jiffies; platform_set_drvdata(pdev, pdata); |
4071ea25c
|
282 |
|
18c88cc9b
|
283 |
pdata->rtc = devm_rtc_allocate_device(&pdev->dev); |
4071ea25c
|
284 285 |
if (IS_ERR(pdata->rtc)) return PTR_ERR(pdata->rtc); |
18c88cc9b
|
286 |
pdata->rtc->ops = &ds1553_rtc_ops; |
3a9a06d44
|
287 |
pdata->rtc->nvram_old_abi = true; |
18c88cc9b
|
288 289 290 291 |
ret = rtc_register_device(pdata->rtc); if (ret) return ret; |
2fac6674d
|
292 |
if (pdata->irq > 0) { |
9bf5b4f5f
|
293 |
writeb(0, ioaddr + RTC_INTERRUPTS); |
618161f71
|
294 295 |
if (devm_request_irq(&pdev->dev, pdata->irq, ds1553_rtc_interrupt, |
2f6e5f945
|
296 |
0, pdev->name, pdev) < 0) { |
9bf5b4f5f
|
297 298 |
dev_warn(&pdev->dev, "interrupt not available. "); |
2fac6674d
|
299 |
pdata->irq = 0; |
9bf5b4f5f
|
300 301 |
} } |
3a9a06d44
|
302 303 304 |
if (rtc_nvmem_register(pdata->rtc, &nvmem_cfg)) dev_err(&pdev->dev, "unable to register nvmem "); |
c1be915ee
|
305 |
|
4071ea25c
|
306 |
return 0; |
9bf5b4f5f
|
307 |
} |
ad28a07bc
|
308 309 |
/* work with hotplug and coldplug */ MODULE_ALIAS("platform:rtc-ds1553"); |
9bf5b4f5f
|
310 311 |
static struct platform_driver ds1553_rtc_driver = { .probe = ds1553_rtc_probe, |
9bf5b4f5f
|
312 |
.driver = { |
e7634c271
|
313 |
.name = "rtc-ds1553", |
9bf5b4f5f
|
314 315 |
}, }; |
0c4eae665
|
316 |
module_platform_driver(ds1553_rtc_driver); |
9bf5b4f5f
|
317 318 319 320 |
MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); MODULE_DESCRIPTION("Dallas DS1553 RTC driver"); MODULE_LICENSE("GPL"); |