blob: a63872c4c76d07182a39aeac4212174505a189e6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrew Sharp8f267952008-02-06 01:38:46 -08002/*
3 * An rtc driver for the Dallas DS1511
4 *
5 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
Andrew Sharp5b73a412009-09-23 16:03:20 -07006 * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com>
Andrew Sharp8f267952008-02-06 01:38:46 -08007 *
Andrew Sharp8f267952008-02-06 01:38:46 -08008 * Real time clock driver for the Dallas 1511 chip, which also
9 * contains a watchdog timer. There is a tiny amount of code that
10 * platform code could use to mess with the watchdog device a little
11 * bit, but not a full watchdog driver.
12 */
13
14#include <linux/bcd.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Andrew Sharp8f267952008-02-06 01:38:46 -080018#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/rtc.h>
21#include <linux/platform_device.h>
22#include <linux/io.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040023#include <linux/module.h>
Andrew Sharp8f267952008-02-06 01:38:46 -080024
Andrew Sharp8f267952008-02-06 01:38:46 -080025enum ds1511reg {
26 DS1511_SEC = 0x0,
27 DS1511_MIN = 0x1,
28 DS1511_HOUR = 0x2,
29 DS1511_DOW = 0x3,
30 DS1511_DOM = 0x4,
31 DS1511_MONTH = 0x5,
32 DS1511_YEAR = 0x6,
33 DS1511_CENTURY = 0x7,
34 DS1511_AM1_SEC = 0x8,
35 DS1511_AM2_MIN = 0x9,
36 DS1511_AM3_HOUR = 0xa,
37 DS1511_AM4_DATE = 0xb,
38 DS1511_WD_MSEC = 0xc,
39 DS1511_WD_SEC = 0xd,
40 DS1511_CONTROL_A = 0xe,
41 DS1511_CONTROL_B = 0xf,
42 DS1511_RAMADDR_LSB = 0x10,
43 DS1511_RAMDATA = 0x13
44};
45
46#define DS1511_BLF1 0x80
47#define DS1511_BLF2 0x40
48#define DS1511_PRS 0x20
49#define DS1511_PAB 0x10
50#define DS1511_TDF 0x08
51#define DS1511_KSF 0x04
52#define DS1511_WDF 0x02
53#define DS1511_IRQF 0x01
54#define DS1511_TE 0x80
55#define DS1511_CS 0x40
56#define DS1511_BME 0x20
57#define DS1511_TPE 0x10
58#define DS1511_TIE 0x08
59#define DS1511_KIE 0x04
60#define DS1511_WDE 0x02
61#define DS1511_WDS 0x01
Vladimir Zapolskiy8ccba142015-08-05 21:12:58 +030062#define DS1511_RAM_MAX 0x100
Andrew Sharp8f267952008-02-06 01:38:46 -080063
64#define RTC_CMD DS1511_CONTROL_B
65#define RTC_CMD1 DS1511_CONTROL_A
66
67#define RTC_ALARM_SEC DS1511_AM1_SEC
68#define RTC_ALARM_MIN DS1511_AM2_MIN
69#define RTC_ALARM_HOUR DS1511_AM3_HOUR
70#define RTC_ALARM_DATE DS1511_AM4_DATE
71
72#define RTC_SEC DS1511_SEC
73#define RTC_MIN DS1511_MIN
74#define RTC_HOUR DS1511_HOUR
75#define RTC_DOW DS1511_DOW
76#define RTC_DOM DS1511_DOM
77#define RTC_MON DS1511_MONTH
78#define RTC_YEAR DS1511_YEAR
79#define RTC_CENTURY DS1511_CENTURY
80
81#define RTC_TIE DS1511_TIE
82#define RTC_TE DS1511_TE
83
84struct rtc_plat_data {
85 struct rtc_device *rtc;
86 void __iomem *ioaddr; /* virtual base address */
Andrew Sharp8f267952008-02-06 01:38:46 -080087 int irq;
88 unsigned int irqen;
89 int alrm_sec;
90 int alrm_min;
91 int alrm_hour;
92 int alrm_mday;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -080093 spinlock_t lock;
Andrew Sharp8f267952008-02-06 01:38:46 -080094};
95
96static DEFINE_SPINLOCK(ds1511_lock);
97
98static __iomem char *ds1511_base;
99static u32 reg_spacing = 1;
100
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700101static noinline void
Andrew Sharp8f267952008-02-06 01:38:46 -0800102rtc_write(uint8_t val, uint32_t reg)
103{
104 writeb(val, ds1511_base + (reg * reg_spacing));
105}
106
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700107static inline void
Andrew Sharp8f267952008-02-06 01:38:46 -0800108rtc_write_alarm(uint8_t val, enum ds1511reg reg)
109{
110 rtc_write((val | 0x80), reg);
111}
112
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700113static noinline uint8_t
Andrew Sharp8f267952008-02-06 01:38:46 -0800114rtc_read(enum ds1511reg reg)
115{
116 return readb(ds1511_base + (reg * reg_spacing));
117}
118
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700119static inline void
Andrew Sharp8f267952008-02-06 01:38:46 -0800120rtc_disable_update(void)
121{
122 rtc_write((rtc_read(RTC_CMD) & ~RTC_TE), RTC_CMD);
123}
124
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700125static void
Andrew Sharp8f267952008-02-06 01:38:46 -0800126rtc_enable_update(void)
127{
128 rtc_write((rtc_read(RTC_CMD) | RTC_TE), RTC_CMD);
129}
130
131/*
132 * #define DS1511_WDOG_RESET_SUPPORT
133 *
134 * Uncomment this if you want to use these routines in
135 * some platform code.
136 */
137#ifdef DS1511_WDOG_RESET_SUPPORT
138/*
139 * just enough code to set the watchdog timer so that it
140 * will reboot the system
141 */
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700142void
Andrew Sharp8f267952008-02-06 01:38:46 -0800143ds1511_wdog_set(unsigned long deciseconds)
144{
145 /*
146 * the wdog timer can take 99.99 seconds
147 */
148 deciseconds %= 10000;
149 /*
150 * set the wdog values in the wdog registers
151 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700152 rtc_write(bin2bcd(deciseconds % 100), DS1511_WD_MSEC);
153 rtc_write(bin2bcd(deciseconds / 100), DS1511_WD_SEC);
Andrew Sharp8f267952008-02-06 01:38:46 -0800154 /*
155 * set wdog enable and wdog 'steering' bit to issue a reset
156 */
Vladimir Zapolskiy8ccba142015-08-05 21:12:58 +0300157 rtc_write(rtc_read(RTC_CMD) | DS1511_WDE | DS1511_WDS, RTC_CMD);
Andrew Sharp8f267952008-02-06 01:38:46 -0800158}
159
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700160void
Andrew Sharp8f267952008-02-06 01:38:46 -0800161ds1511_wdog_disable(void)
162{
163 /*
164 * clear wdog enable and wdog 'steering' bits
165 */
166 rtc_write(rtc_read(RTC_CMD) & ~(DS1511_WDE | DS1511_WDS), RTC_CMD);
167 /*
168 * clear the wdog counter
169 */
170 rtc_write(0, DS1511_WD_MSEC);
171 rtc_write(0, DS1511_WD_SEC);
172}
173#endif
174
175/*
176 * set the rtc chip's idea of the time.
177 * stupidly, some callers call with year unmolested;
178 * and some call with year = year - 1900. thanks.
179 */
Adrian Bunka3ed1072008-04-28 02:11:55 -0700180static int ds1511_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
Andrew Sharp8f267952008-02-06 01:38:46 -0800181{
182 u8 mon, day, dow, hrs, min, sec, yrs, cen;
Steven Rostedt9a0f4ae2008-05-06 20:42:30 -0700183 unsigned long flags;
Andrew Sharp8f267952008-02-06 01:38:46 -0800184
185 /*
186 * won't have to change this for a while
187 */
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700188 if (rtc_tm->tm_year < 1900)
Andrew Sharp8f267952008-02-06 01:38:46 -0800189 rtc_tm->tm_year += 1900;
Andrew Sharp8f267952008-02-06 01:38:46 -0800190
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700191 if (rtc_tm->tm_year < 1970)
Andrew Sharp8f267952008-02-06 01:38:46 -0800192 return -EINVAL;
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700193
Andrew Sharp8f267952008-02-06 01:38:46 -0800194 yrs = rtc_tm->tm_year % 100;
195 cen = rtc_tm->tm_year / 100;
196 mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
197 day = rtc_tm->tm_mday;
198 dow = rtc_tm->tm_wday & 0x7; /* automatic BCD */
199 hrs = rtc_tm->tm_hour;
200 min = rtc_tm->tm_min;
201 sec = rtc_tm->tm_sec;
202
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700203 if ((mon > 12) || (day == 0))
Andrew Sharp8f267952008-02-06 01:38:46 -0800204 return -EINVAL;
Andrew Sharp8f267952008-02-06 01:38:46 -0800205
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700206 if (day > rtc_month_days(rtc_tm->tm_mon, rtc_tm->tm_year))
Andrew Sharp8f267952008-02-06 01:38:46 -0800207 return -EINVAL;
Andrew Sharp8f267952008-02-06 01:38:46 -0800208
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700209 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
Andrew Sharp8f267952008-02-06 01:38:46 -0800210 return -EINVAL;
Andrew Sharp8f267952008-02-06 01:38:46 -0800211
212 /*
213 * each register is a different number of valid bits
214 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700215 sec = bin2bcd(sec) & 0x7f;
216 min = bin2bcd(min) & 0x7f;
217 hrs = bin2bcd(hrs) & 0x3f;
218 day = bin2bcd(day) & 0x3f;
219 mon = bin2bcd(mon) & 0x1f;
220 yrs = bin2bcd(yrs) & 0xff;
221 cen = bin2bcd(cen) & 0xff;
Andrew Sharp8f267952008-02-06 01:38:46 -0800222
223 spin_lock_irqsave(&ds1511_lock, flags);
224 rtc_disable_update();
225 rtc_write(cen, RTC_CENTURY);
226 rtc_write(yrs, RTC_YEAR);
227 rtc_write((rtc_read(RTC_MON) & 0xe0) | mon, RTC_MON);
228 rtc_write(day, RTC_DOM);
229 rtc_write(hrs, RTC_HOUR);
230 rtc_write(min, RTC_MIN);
231 rtc_write(sec, RTC_SEC);
232 rtc_write(dow, RTC_DOW);
233 rtc_enable_update();
234 spin_unlock_irqrestore(&ds1511_lock, flags);
235
236 return 0;
237}
238
Adrian Bunka3ed1072008-04-28 02:11:55 -0700239static int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
Andrew Sharp8f267952008-02-06 01:38:46 -0800240{
241 unsigned int century;
Steven Rostedt9a0f4ae2008-05-06 20:42:30 -0700242 unsigned long flags;
Andrew Sharp8f267952008-02-06 01:38:46 -0800243
244 spin_lock_irqsave(&ds1511_lock, flags);
245 rtc_disable_update();
246
247 rtc_tm->tm_sec = rtc_read(RTC_SEC) & 0x7f;
248 rtc_tm->tm_min = rtc_read(RTC_MIN) & 0x7f;
249 rtc_tm->tm_hour = rtc_read(RTC_HOUR) & 0x3f;
250 rtc_tm->tm_mday = rtc_read(RTC_DOM) & 0x3f;
251 rtc_tm->tm_wday = rtc_read(RTC_DOW) & 0x7;
252 rtc_tm->tm_mon = rtc_read(RTC_MON) & 0x1f;
253 rtc_tm->tm_year = rtc_read(RTC_YEAR) & 0x7f;
254 century = rtc_read(RTC_CENTURY);
255
256 rtc_enable_update();
257 spin_unlock_irqrestore(&ds1511_lock, flags);
258
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700259 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
260 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
261 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
262 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
263 rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
264 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
265 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
266 century = bcd2bin(century) * 100;
Andrew Sharp8f267952008-02-06 01:38:46 -0800267
268 /*
269 * Account for differences between how the RTC uses the values
270 * and how they are defined in a struct rtc_time;
271 */
272 century += rtc_tm->tm_year;
273 rtc_tm->tm_year = century - 1900;
274
275 rtc_tm->tm_mon--;
276
Andrew Sharp8f267952008-02-06 01:38:46 -0800277 return 0;
278}
279
280/*
281 * write the alarm register settings
282 *
283 * we only have the use to interrupt every second, otherwise
284 * known as the update interrupt, or the interrupt if the whole
285 * date/hours/mins/secs matches. the ds1511 has many more
286 * permutations, but the kernel doesn't.
287 */
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700288static void
Andrew Sharp8f267952008-02-06 01:38:46 -0800289ds1511_rtc_update_alarm(struct rtc_plat_data *pdata)
290{
291 unsigned long flags;
292
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800293 spin_lock_irqsave(&pdata->lock, flags);
Andrew Sharp8f267952008-02-06 01:38:46 -0800294 rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700295 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800296 RTC_ALARM_DATE);
297 rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700298 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800299 RTC_ALARM_HOUR);
300 rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700301 0x80 : bin2bcd(pdata->alrm_min) & 0x7f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800302 RTC_ALARM_MIN);
303 rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700304 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800305 RTC_ALARM_SEC);
306 rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD);
307 rtc_read(RTC_CMD1); /* clear interrupts */
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800308 spin_unlock_irqrestore(&pdata->lock, flags);
Andrew Sharp8f267952008-02-06 01:38:46 -0800309}
310
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700311static int
Andrew Sharp8f267952008-02-06 01:38:46 -0800312ds1511_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
313{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200314 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Andrew Sharp8f267952008-02-06 01:38:46 -0800315
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800316 if (pdata->irq <= 0)
Andrew Sharp8f267952008-02-06 01:38:46 -0800317 return -EINVAL;
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800318
Andrew Sharp8f267952008-02-06 01:38:46 -0800319 pdata->alrm_mday = alrm->time.tm_mday;
320 pdata->alrm_hour = alrm->time.tm_hour;
321 pdata->alrm_min = alrm->time.tm_min;
322 pdata->alrm_sec = alrm->time.tm_sec;
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700323 if (alrm->enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800324 pdata->irqen |= RTC_AF;
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700325
Andrew Sharp8f267952008-02-06 01:38:46 -0800326 ds1511_rtc_update_alarm(pdata);
327 return 0;
328}
329
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700330static int
Andrew Sharp8f267952008-02-06 01:38:46 -0800331ds1511_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
332{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200333 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Andrew Sharp8f267952008-02-06 01:38:46 -0800334
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800335 if (pdata->irq <= 0)
Andrew Sharp8f267952008-02-06 01:38:46 -0800336 return -EINVAL;
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800337
Andrew Sharp8f267952008-02-06 01:38:46 -0800338 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
339 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
340 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
341 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
342 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
343 return 0;
344}
345
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700346static irqreturn_t
Andrew Sharp8f267952008-02-06 01:38:46 -0800347ds1511_interrupt(int irq, void *dev_id)
348{
349 struct platform_device *pdev = dev_id;
350 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800351 unsigned long events = 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800352
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800353 spin_lock(&pdata->lock);
Andrew Sharp8f267952008-02-06 01:38:46 -0800354 /*
355 * read and clear interrupt
356 */
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800357 if (rtc_read(RTC_CMD1) & DS1511_IRQF) {
358 events = RTC_IRQF;
359 if (rtc_read(RTC_ALARM_SEC) & 0x80)
360 events |= RTC_UF;
361 else
362 events |= RTC_AF;
Alexander Shiyan0d719152014-04-03 14:50:20 -0700363 rtc_update_irq(pdata->rtc, 1, events);
Andrew Sharp8f267952008-02-06 01:38:46 -0800364 }
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800365 spin_unlock(&pdata->lock);
366 return events ? IRQ_HANDLED : IRQ_NONE;
Andrew Sharp8f267952008-02-06 01:38:46 -0800367}
368
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800369static int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800370{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200371 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Andrew Sharp8f267952008-02-06 01:38:46 -0800372
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800373 if (pdata->irq <= 0)
374 return -EINVAL;
375 if (enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800376 pdata->irqen |= RTC_AF;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800377 else
378 pdata->irqen &= ~RTC_AF;
379 ds1511_rtc_update_alarm(pdata);
380 return 0;
381}
382
Andrew Sharp8f267952008-02-06 01:38:46 -0800383static const struct rtc_class_ops ds1511_rtc_ops = {
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800384 .read_time = ds1511_rtc_read_time,
385 .set_time = ds1511_rtc_set_time,
386 .read_alarm = ds1511_rtc_read_alarm,
387 .set_alarm = ds1511_rtc_set_alarm,
388 .alarm_irq_enable = ds1511_rtc_alarm_irq_enable,
Andrew Sharp8f267952008-02-06 01:38:46 -0800389};
390
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200391static int ds1511_nvram_read(void *priv, unsigned int pos, void *buf,
392 size_t size)
Andrew Sharp8f267952008-02-06 01:38:46 -0800393{
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200394 int i;
Andrew Sharp8f267952008-02-06 01:38:46 -0800395
Andrew Sharp8f267952008-02-06 01:38:46 -0800396 rtc_write(pos, DS1511_RAMADDR_LSB);
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200397 for (i = 0; i < size; i++)
398 *(char *)buf++ = rtc_read(DS1511_RAMDATA);
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700399
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200400 return 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800401}
402
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200403static int ds1511_nvram_write(void *priv, unsigned int pos, void *buf,
404 size_t size)
Andrew Sharp8f267952008-02-06 01:38:46 -0800405{
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200406 int i;
Andrew Sharp8f267952008-02-06 01:38:46 -0800407
Andrew Sharp8f267952008-02-06 01:38:46 -0800408 rtc_write(pos, DS1511_RAMADDR_LSB);
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200409 for (i = 0; i < size; i++)
410 rtc_write(*(char *)buf++, DS1511_RAMDATA);
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700411
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200412 return 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800413}
414
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800415static int ds1511_rtc_probe(struct platform_device *pdev)
Andrew Sharp8f267952008-02-06 01:38:46 -0800416{
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800417 struct rtc_plat_data *pdata;
Andrew Sharp8f267952008-02-06 01:38:46 -0800418 int ret = 0;
Alexandre Belloni71e19c52018-02-12 23:47:25 +0100419 struct nvmem_config ds1511_nvmem_cfg = {
420 .name = "ds1511_nvram",
421 .word_size = 1,
422 .stride = 1,
423 .size = DS1511_RAM_MAX,
424 .reg_read = ds1511_nvram_read,
425 .reg_write = ds1511_nvram_write,
426 .priv = &pdev->dev,
427 };
Andrew Sharp8f267952008-02-06 01:38:46 -0800428
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800429 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
430 if (!pdata)
Andrew Sharp8f267952008-02-06 01:38:46 -0800431 return -ENOMEM;
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700432
YueHaibing09ef18b2019-10-06 18:29:20 +0800433 ds1511_base = devm_platform_ioremap_resource(pdev, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700434 if (IS_ERR(ds1511_base))
435 return PTR_ERR(ds1511_base);
Andrew Sharp8f267952008-02-06 01:38:46 -0800436 pdata->ioaddr = ds1511_base;
437 pdata->irq = platform_get_irq(pdev, 0);
438
439 /*
440 * turn on the clock and the crystal, etc.
441 */
Vladimir Zapolskiy8ccba142015-08-05 21:12:58 +0300442 rtc_write(DS1511_BME, RTC_CMD);
Andrew Sharp8f267952008-02-06 01:38:46 -0800443 rtc_write(0, RTC_CMD1);
444 /*
445 * clear the wdog counter
446 */
447 rtc_write(0, DS1511_WD_MSEC);
448 rtc_write(0, DS1511_WD_SEC);
449 /*
450 * start the clock
451 */
452 rtc_enable_update();
453
454 /*
455 * check for a dying bat-tree
456 */
Sachin Kamat7b2f0052013-07-03 15:05:49 -0700457 if (rtc_read(RTC_CMD1) & DS1511_BLF1)
Andrew Sharp8f267952008-02-06 01:38:46 -0800458 dev_warn(&pdev->dev, "voltage-low detected.\n");
Andrew Sharp8f267952008-02-06 01:38:46 -0800459
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800460 spin_lock_init(&pdata->lock);
461 platform_set_drvdata(pdev, pdata);
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700462
Alexandre Belloni30322692017-10-13 00:05:27 +0200463 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700464 if (IS_ERR(pdata->rtc))
465 return PTR_ERR(pdata->rtc);
466
Alexandre Belloni30322692017-10-13 00:05:27 +0200467 pdata->rtc->ops = &ds1511_rtc_ops;
468
Alexandre Belloni9d7ed212017-10-13 00:05:29 +0200469 pdata->rtc->nvram_old_abi = true;
470
Alexandre Belloni30322692017-10-13 00:05:27 +0200471 ret = rtc_register_device(pdata->rtc);
472 if (ret)
473 return ret;
474
Alexandre Belloni16fef392018-02-12 23:47:24 +0100475 rtc_nvmem_register(pdata->rtc, &ds1511_nvmem_cfg);
476
Andrew Sharp8f267952008-02-06 01:38:46 -0800477 /*
478 * if the platform has an interrupt in mind for this device,
479 * then by all means, set it
480 */
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800481 if (pdata->irq > 0) {
Andrew Sharp8f267952008-02-06 01:38:46 -0800482 rtc_read(RTC_CMD1);
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800483 if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700484 IRQF_SHARED, pdev->name, pdev) < 0) {
Andrew Sharp8f267952008-02-06 01:38:46 -0800485
486 dev_warn(&pdev->dev, "interrupt not available.\n");
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800487 pdata->irq = 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800488 }
489 }
490
Andrew Sharp8f267952008-02-06 01:38:46 -0800491 return 0;
492}
493
Kay Sieversad28a072008-04-10 21:29:25 -0700494/* work with hotplug and coldplug */
495MODULE_ALIAS("platform:ds1511");
496
Andrew Sharp8f267952008-02-06 01:38:46 -0800497static struct platform_driver ds1511_rtc_driver = {
498 .probe = ds1511_rtc_probe,
Andrew Sharp8f267952008-02-06 01:38:46 -0800499 .driver = {
500 .name = "ds1511",
Andrew Sharp8f267952008-02-06 01:38:46 -0800501 },
502};
503
Axel Lin0c4eae62012-01-10 15:10:48 -0800504module_platform_driver(ds1511_rtc_driver);
Andrew Sharp8f267952008-02-06 01:38:46 -0800505
Andrew Sharp5b73a412009-09-23 16:03:20 -0700506MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
Andrew Sharp8f267952008-02-06 01:38:46 -0800507MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
508MODULE_LICENSE("GPL");