blob: 34af7a802f4301fc3f12a6627b0bcf3ecfbb0704 [file] [log] [blame]
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -07001/*
2 * An rtc driver for the Dallas DS1553
3 *
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070015#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>
Paul Gortmaker21138522011-05-27 09:57:25 -040021#include <linux/module.h>
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070022
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070023#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
61struct rtc_plat_data {
62 struct rtc_device *rtc;
63 void __iomem *ioaddr;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070064 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;
Atsushi Nemoto618161f2009-12-15 16:46:01 -080071 spinlock_t lock;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070072};
73
74static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
75{
Wolfram Sang85368bb2018-04-19 16:06:14 +020076 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070077 void __iomem *ioaddr = pdata->ioaddr;
78 u8 century;
79
Adrian Bunkfe20ba72008-10-18 20:28:41 -070080 century = bin2bcd((tm->tm_year + 1900) / 100);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070081
82 writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
83
Adrian Bunkfe20ba72008-10-18 20:28:41 -070084 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
85 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
86 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
87 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
88 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
89 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
90 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070091
92 /* RTC_CENTURY and RTC_CONTROL share same register */
93 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
94 writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
95 return 0;
96}
97
98static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
99{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200100 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700101 void __iomem *ioaddr = pdata->ioaddr;
102 unsigned int year, month, day, hour, minute, second, week;
103 unsigned int century;
104
105 /* give enough time to update RTC in case of continuous read */
106 if (pdata->last_jiffies == jiffies)
107 msleep(1);
108 pdata->last_jiffies = jiffies;
109 writeb(RTC_READ, ioaddr + RTC_CONTROL);
110 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
111 minute = readb(ioaddr + RTC_MINUTES);
112 hour = readb(ioaddr + RTC_HOURS);
113 day = readb(ioaddr + RTC_DATE);
114 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
115 month = readb(ioaddr + RTC_MONTH);
116 year = readb(ioaddr + RTC_YEAR);
117 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
118 writeb(0, ioaddr + RTC_CONTROL);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700119 tm->tm_sec = bcd2bin(second);
120 tm->tm_min = bcd2bin(minute);
121 tm->tm_hour = bcd2bin(hour);
122 tm->tm_mday = bcd2bin(day);
123 tm->tm_wday = bcd2bin(week);
124 tm->tm_mon = bcd2bin(month) - 1;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700125 /* year is 1900 + tm->tm_year */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700126 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700127
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700128 return 0;
129}
130
131static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
132{
133 void __iomem *ioaddr = pdata->ioaddr;
134 unsigned long flags;
135
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800136 spin_lock_irqsave(&pdata->lock, flags);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700137 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700138 0x80 : bin2bcd(pdata->alrm_mday),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700139 ioaddr + RTC_DATE_ALARM);
140 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700141 0x80 : bin2bcd(pdata->alrm_hour),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700142 ioaddr + RTC_HOURS_ALARM);
143 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700144 0x80 : bin2bcd(pdata->alrm_min),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700145 ioaddr + RTC_MINUTES_ALARM);
146 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700147 0x80 : bin2bcd(pdata->alrm_sec),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700148 ioaddr + RTC_SECONDS_ALARM);
149 writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
150 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800151 spin_unlock_irqrestore(&pdata->lock, flags);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700152}
153
154static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
155{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200156 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700157
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800158 if (pdata->irq <= 0)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700159 return -EINVAL;
160 pdata->alrm_mday = alrm->time.tm_mday;
161 pdata->alrm_hour = alrm->time.tm_hour;
162 pdata->alrm_min = alrm->time.tm_min;
163 pdata->alrm_sec = alrm->time.tm_sec;
164 if (alrm->enabled)
165 pdata->irqen |= RTC_AF;
166 ds1553_rtc_update_alarm(pdata);
167 return 0;
168}
169
170static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
171{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200172 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700173
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800174 if (pdata->irq <= 0)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700175 return -EINVAL;
176 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
177 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
178 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
179 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
180 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
181 return 0;
182}
183
David Howells7d12e782006-10-05 14:55:46 +0100184static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700185{
186 struct platform_device *pdev = dev_id;
187 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
188 void __iomem *ioaddr = pdata->ioaddr;
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800189 unsigned long events = 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700190
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800191 spin_lock(&pdata->lock);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700192 /* read and clear interrupt */
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800193 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
194 events = RTC_IRQF;
195 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
196 events |= RTC_UF;
197 else
198 events |= RTC_AF;
Alexander Shiyan0d719152014-04-03 14:50:20 -0700199 rtc_update_irq(pdata->rtc, 1, events);
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800200 }
201 spin_unlock(&pdata->lock);
202 return events ? IRQ_HANDLED : IRQ_NONE;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700203}
204
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800205static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700206{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200207 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700208
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800209 if (pdata->irq <= 0)
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800210 return -EINVAL;
211 if (enabled)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700212 pdata->irqen |= RTC_AF;
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800213 else
214 pdata->irqen &= ~RTC_AF;
215 ds1553_rtc_update_alarm(pdata);
216 return 0;
217}
218
David Brownellff8371a2006-09-30 23:28:17 -0700219static const struct rtc_class_ops ds1553_rtc_ops = {
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800220 .read_time = ds1553_rtc_read_time,
221 .set_time = ds1553_rtc_set_time,
222 .read_alarm = ds1553_rtc_read_alarm,
223 .set_alarm = ds1553_rtc_set_alarm,
224 .alarm_irq_enable = ds1553_rtc_alarm_irq_enable,
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700225};
226
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100227static int ds1553_nvram_read(void *priv, unsigned int pos, void *val,
228 size_t bytes)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700229{
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100230 struct platform_device *pdev = priv;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700231 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
232 void __iomem *ioaddr = pdata->ioaddr;
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100233 u8 *buf = val;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700234
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100235 for (; bytes; bytes--)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700236 *buf++ = readb(ioaddr + pos++);
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100237 return 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700238}
239
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100240static int ds1553_nvram_write(void *priv, unsigned int pos, void *val,
241 size_t bytes)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700242{
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100243 struct platform_device *pdev = priv;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700244 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
245 void __iomem *ioaddr = pdata->ioaddr;
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100246 u8 *buf = val;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700247
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100248 for (; bytes; bytes--)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700249 writeb(*buf++, ioaddr + pos++);
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100250 return 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700251}
252
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800253static int ds1553_rtc_probe(struct platform_device *pdev)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700254{
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700255 struct resource *res;
256 unsigned int cen, sec;
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800257 struct rtc_plat_data *pdata;
258 void __iomem *ioaddr;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700259 int ret = 0;
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100260 struct nvmem_config nvmem_cfg = {
261 .name = "ds1553_nvram",
262 .word_size = 1,
263 .stride = 1,
264 .size = RTC_OFFSET,
265 .reg_read = ds1553_nvram_read,
266 .reg_write = ds1553_nvram_write,
267 .priv = pdev,
268 };
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700269
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800270 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700271 if (!pdata)
272 return -ENOMEM;
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800273
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700274 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
275 ioaddr = devm_ioremap_resource(&pdev->dev, res);
276 if (IS_ERR(ioaddr))
277 return PTR_ERR(ioaddr);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700278 pdata->ioaddr = ioaddr;
279 pdata->irq = platform_get_irq(pdev, 0);
280
281 /* turn RTC on if it was not on */
282 sec = readb(ioaddr + RTC_SECONDS);
283 if (sec & RTC_STOP) {
284 sec &= RTC_SECONDS_MASK;
285 cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
286 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
287 writeb(sec, ioaddr + RTC_SECONDS);
288 writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
289 }
290 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
291 dev_warn(&pdev->dev, "voltage-low detected.\n");
292
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800293 spin_lock_init(&pdata->lock);
294 pdata->last_jiffies = jiffies;
295 platform_set_drvdata(pdev, pdata);
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700296
Alexandre Belloni18c88cc2018-02-12 23:47:42 +0100297 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700298 if (IS_ERR(pdata->rtc))
299 return PTR_ERR(pdata->rtc);
300
Alexandre Belloni18c88cc2018-02-12 23:47:42 +0100301 pdata->rtc->ops = &ds1553_rtc_ops;
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100302 pdata->rtc->nvram_old_abi = true;
Alexandre Belloni18c88cc2018-02-12 23:47:42 +0100303
304 ret = rtc_register_device(pdata->rtc);
305 if (ret)
306 return ret;
307
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800308 if (pdata->irq > 0) {
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700309 writeb(0, ioaddr + RTC_INTERRUPTS);
Atsushi Nemoto618161f2009-12-15 16:46:01 -0800310 if (devm_request_irq(&pdev->dev, pdata->irq,
311 ds1553_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700312 0, pdev->name, pdev) < 0) {
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700313 dev_warn(&pdev->dev, "interrupt not available.\n");
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800314 pdata->irq = 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700315 }
316 }
317
Alexandre Belloni3a9a06d2018-02-12 23:47:43 +0100318 if (rtc_nvmem_register(pdata->rtc, &nvmem_cfg))
319 dev_err(&pdev->dev, "unable to register nvmem\n");
Jingoo Hanc1be9152013-04-29 16:19:00 -0700320
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700321 return 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700322}
323
Kay Sieversad28a072008-04-10 21:29:25 -0700324/* work with hotplug and coldplug */
325MODULE_ALIAS("platform:rtc-ds1553");
326
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700327static struct platform_driver ds1553_rtc_driver = {
328 .probe = ds1553_rtc_probe,
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700329 .driver = {
Atsushi Nemotoe7634c22007-10-16 01:28:17 -0700330 .name = "rtc-ds1553",
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700331 },
332};
333
Axel Lin0c4eae62012-01-10 15:10:48 -0800334module_platform_driver(ds1553_rtc_driver);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700335
336MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
337MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
338MODULE_LICENSE("GPL");