4 * Copyright (c) 2007 Wind River Systems, Inc.
6 * Author: Mark Zhan <rongkai.zhan@windriver.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/rtc.h>
20 #include <linux/rtc/m48t59.h>
21 #include <linux/bcd.h>
22 #include <linux/slab.h>
28 #define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
29 #define M48T59_WRITE(val, reg) \
30 (pdata->write_byte(dev, pdata->offset + reg, val))
32 #define M48T59_SET_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
34 #define M48T59_CLEAR_BITS(mask, reg) \
35 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
37 struct m48t59_private
{
40 struct rtc_device
*rtc
;
41 spinlock_t lock
; /* serialize the NVRAM and RTC access */
45 * This is the generic access method when the chip is memory-mapped
48 m48t59_mem_writeb(struct device
*dev
, u32 ofs
, u8 val
)
50 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
52 writeb(val
, m48t59
->ioaddr
+ofs
);
56 m48t59_mem_readb(struct device
*dev
, u32 ofs
)
58 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
60 return readb(m48t59
->ioaddr
+ofs
);
64 * NOTE: M48T59 only uses BCD mode
66 static int m48t59_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
68 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
69 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
73 spin_lock_irqsave(&m48t59
->lock
, flags
);
74 /* Issue the READ command */
75 M48T59_SET_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
77 tm
->tm_year
= bcd2bin(M48T59_READ(M48T59_YEAR
));
79 tm
->tm_mon
= bcd2bin(M48T59_READ(M48T59_MONTH
)) - 1;
80 tm
->tm_mday
= bcd2bin(M48T59_READ(M48T59_MDAY
));
82 val
= M48T59_READ(M48T59_WDAY
);
83 if ((pdata
->type
== M48T59RTC_TYPE_M48T59
) &&
84 (val
& M48T59_WDAY_CEB
) && (val
& M48T59_WDAY_CB
)) {
85 dev_dbg(dev
, "Century bit is enabled\n");
86 tm
->tm_year
+= 100; /* one century */
89 /* Sun SPARC machines count years since 1968 */
93 tm
->tm_wday
= bcd2bin(val
& 0x07);
94 tm
->tm_hour
= bcd2bin(M48T59_READ(M48T59_HOUR
) & 0x3F);
95 tm
->tm_min
= bcd2bin(M48T59_READ(M48T59_MIN
) & 0x7F);
96 tm
->tm_sec
= bcd2bin(M48T59_READ(M48T59_SEC
) & 0x7F);
98 /* Clear the READ bit */
99 M48T59_CLEAR_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
100 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
102 dev_dbg(dev
, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
103 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
104 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
108 static int m48t59_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
110 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
111 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
114 int year
= tm
->tm_year
;
117 /* Sun SPARC machines count years since 1968 */
121 dev_dbg(dev
, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
122 year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
123 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
128 spin_lock_irqsave(&m48t59
->lock
, flags
);
129 /* Issue the WRITE command */
130 M48T59_SET_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
132 M48T59_WRITE((bin2bcd(tm
->tm_sec
) & 0x7F), M48T59_SEC
);
133 M48T59_WRITE((bin2bcd(tm
->tm_min
) & 0x7F), M48T59_MIN
);
134 M48T59_WRITE((bin2bcd(tm
->tm_hour
) & 0x3F), M48T59_HOUR
);
135 M48T59_WRITE((bin2bcd(tm
->tm_mday
) & 0x3F), M48T59_MDAY
);
137 M48T59_WRITE((bin2bcd(tm
->tm_mon
+ 1) & 0x1F), M48T59_MONTH
);
138 M48T59_WRITE(bin2bcd(year
% 100), M48T59_YEAR
);
140 if (pdata
->type
== M48T59RTC_TYPE_M48T59
&& (year
/ 100))
141 val
= (M48T59_WDAY_CEB
| M48T59_WDAY_CB
);
142 val
|= (bin2bcd(tm
->tm_wday
) & 0x07);
143 M48T59_WRITE(val
, M48T59_WDAY
);
145 /* Clear the WRITE bit */
146 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
147 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
152 * Read alarm time and date in RTC
154 static int m48t59_rtc_readalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
156 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
157 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
158 struct rtc_time
*tm
= &alrm
->time
;
162 /* If no irq, we don't support ALARM */
163 if (m48t59
->irq
== NO_IRQ
)
166 spin_lock_irqsave(&m48t59
->lock
, flags
);
167 /* Issue the READ command */
168 M48T59_SET_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
170 tm
->tm_year
= bcd2bin(M48T59_READ(M48T59_YEAR
));
172 /* Sun SPARC machines count years since 1968 */
176 tm
->tm_mon
= bcd2bin(M48T59_READ(M48T59_MONTH
)) - 1;
178 val
= M48T59_READ(M48T59_WDAY
);
179 if ((val
& M48T59_WDAY_CEB
) && (val
& M48T59_WDAY_CB
))
180 tm
->tm_year
+= 100; /* one century */
182 tm
->tm_mday
= bcd2bin(M48T59_READ(M48T59_ALARM_DATE
));
183 tm
->tm_hour
= bcd2bin(M48T59_READ(M48T59_ALARM_HOUR
));
184 tm
->tm_min
= bcd2bin(M48T59_READ(M48T59_ALARM_MIN
));
185 tm
->tm_sec
= bcd2bin(M48T59_READ(M48T59_ALARM_SEC
));
187 /* Clear the READ bit */
188 M48T59_CLEAR_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
189 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
191 dev_dbg(dev
, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
192 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
193 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
194 return rtc_valid_tm(tm
);
198 * Set alarm time and date in RTC
200 static int m48t59_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
202 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
203 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
204 struct rtc_time
*tm
= &alrm
->time
;
205 u8 mday
, hour
, min
, sec
;
207 int year
= tm
->tm_year
;
210 /* Sun SPARC machines count years since 1968 */
214 /* If no irq, we don't support ALARM */
215 if (m48t59
->irq
== NO_IRQ
)
222 * 0xff means "always match"
225 mday
= (mday
>= 1 && mday
<= 31) ? bin2bcd(mday
) : 0xff;
227 mday
= M48T59_READ(M48T59_MDAY
);
230 hour
= (hour
< 24) ? bin2bcd(hour
) : 0x00;
233 min
= (min
< 60) ? bin2bcd(min
) : 0x00;
236 sec
= (sec
< 60) ? bin2bcd(sec
) : 0x00;
238 spin_lock_irqsave(&m48t59
->lock
, flags
);
239 /* Issue the WRITE command */
240 M48T59_SET_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
242 M48T59_WRITE(mday
, M48T59_ALARM_DATE
);
243 M48T59_WRITE(hour
, M48T59_ALARM_HOUR
);
244 M48T59_WRITE(min
, M48T59_ALARM_MIN
);
245 M48T59_WRITE(sec
, M48T59_ALARM_SEC
);
247 /* Clear the WRITE bit */
248 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
249 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
251 dev_dbg(dev
, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
252 year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
253 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
258 * Handle commands from user-space
260 static int m48t59_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
262 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
263 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
266 spin_lock_irqsave(&m48t59
->lock
, flags
);
268 M48T59_WRITE(M48T59_INTR_AFE
, M48T59_INTR
);
270 M48T59_WRITE(0x00, M48T59_INTR
);
271 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
276 static int m48t59_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
278 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
279 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
283 spin_lock_irqsave(&m48t59
->lock
, flags
);
284 val
= M48T59_READ(M48T59_FLAGS
);
285 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
287 seq_printf(seq
, "battery\t\t: %s\n",
288 (val
& M48T59_FLAGS_BF
) ? "low" : "normal");
293 * IRQ handler for the RTC
295 static irqreturn_t
m48t59_rtc_interrupt(int irq
, void *dev_id
)
297 struct device
*dev
= (struct device
*)dev_id
;
298 struct m48t59_plat_data
*pdata
= dev_get_platdata(dev
);
299 struct m48t59_private
*m48t59
= dev_get_drvdata(dev
);
302 spin_lock(&m48t59
->lock
);
303 event
= M48T59_READ(M48T59_FLAGS
);
304 spin_unlock(&m48t59
->lock
);
306 if (event
& M48T59_FLAGS_AF
) {
307 rtc_update_irq(m48t59
->rtc
, 1, (RTC_AF
| RTC_IRQF
));
314 static const struct rtc_class_ops m48t59_rtc_ops
= {
315 .read_time
= m48t59_rtc_read_time
,
316 .set_time
= m48t59_rtc_set_time
,
317 .read_alarm
= m48t59_rtc_readalarm
,
318 .set_alarm
= m48t59_rtc_setalarm
,
319 .proc
= m48t59_rtc_proc
,
320 .alarm_irq_enable
= m48t59_rtc_alarm_irq_enable
,
323 static const struct rtc_class_ops m48t02_rtc_ops
= {
324 .read_time
= m48t59_rtc_read_time
,
325 .set_time
= m48t59_rtc_set_time
,
328 static int m48t59_nvram_read(void *priv
, unsigned int offset
, void *val
,
331 struct platform_device
*pdev
= priv
;
332 struct device
*dev
= &pdev
->dev
;
333 struct m48t59_plat_data
*pdata
= dev_get_platdata(&pdev
->dev
);
334 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
339 spin_lock_irqsave(&m48t59
->lock
, flags
);
341 for (; cnt
< size
; cnt
++)
342 *buf
++ = M48T59_READ(cnt
);
344 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
349 static int m48t59_nvram_write(void *priv
, unsigned int offset
, void *val
,
352 struct platform_device
*pdev
= priv
;
353 struct device
*dev
= &pdev
->dev
;
354 struct m48t59_plat_data
*pdata
= dev_get_platdata(&pdev
->dev
);
355 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
360 spin_lock_irqsave(&m48t59
->lock
, flags
);
362 for (; cnt
< size
; cnt
++)
363 M48T59_WRITE(*buf
++, cnt
);
365 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
370 static int m48t59_rtc_probe(struct platform_device
*pdev
)
372 struct m48t59_plat_data
*pdata
= dev_get_platdata(&pdev
->dev
);
373 struct m48t59_private
*m48t59
= NULL
;
374 struct resource
*res
;
377 const struct rtc_class_ops
*ops
;
378 struct nvmem_config nvmem_cfg
= {
382 .reg_read
= m48t59_nvram_read
,
383 .reg_write
= m48t59_nvram_write
,
387 /* This chip could be memory-mapped or I/O-mapped */
388 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
390 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
395 if (res
->flags
& IORESOURCE_IO
) {
396 /* If we are I/O-mapped, the platform should provide
397 * the operations accessing chip registers.
399 if (!pdata
|| !pdata
->write_byte
|| !pdata
->read_byte
)
401 } else if (res
->flags
& IORESOURCE_MEM
) {
402 /* we are memory-mapped */
404 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
),
408 /* Ensure we only kmalloc platform data once */
409 pdev
->dev
.platform_data
= pdata
;
412 pdata
->type
= M48T59RTC_TYPE_M48T59
;
414 /* Try to use the generic memory read/write ops */
415 if (!pdata
->write_byte
)
416 pdata
->write_byte
= m48t59_mem_writeb
;
417 if (!pdata
->read_byte
)
418 pdata
->read_byte
= m48t59_mem_readb
;
421 m48t59
= devm_kzalloc(&pdev
->dev
, sizeof(*m48t59
), GFP_KERNEL
);
425 m48t59
->ioaddr
= pdata
->ioaddr
;
427 if (!m48t59
->ioaddr
) {
428 /* ioaddr not mapped externally */
429 m48t59
->ioaddr
= devm_ioremap(&pdev
->dev
, res
->start
,
435 /* Try to get irq number. We also can work in
436 * the mode without IRQ.
438 m48t59
->irq
= platform_get_irq(pdev
, 0);
439 if (m48t59
->irq
<= 0)
440 m48t59
->irq
= NO_IRQ
;
442 if (m48t59
->irq
!= NO_IRQ
) {
443 ret
= devm_request_irq(&pdev
->dev
, m48t59
->irq
,
444 m48t59_rtc_interrupt
, IRQF_SHARED
,
445 "rtc-m48t59", &pdev
->dev
);
449 switch (pdata
->type
) {
450 case M48T59RTC_TYPE_M48T59
:
452 ops
= &m48t59_rtc_ops
;
453 pdata
->offset
= 0x1ff0;
455 case M48T59RTC_TYPE_M48T02
:
457 ops
= &m48t02_rtc_ops
;
458 pdata
->offset
= 0x7f0;
460 case M48T59RTC_TYPE_M48T08
:
462 ops
= &m48t02_rtc_ops
;
463 pdata
->offset
= 0x1ff0;
466 dev_err(&pdev
->dev
, "Unknown RTC type\n");
470 spin_lock_init(&m48t59
->lock
);
471 platform_set_drvdata(pdev
, m48t59
);
473 m48t59
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
474 if (IS_ERR(m48t59
->rtc
))
475 return PTR_ERR(m48t59
->rtc
);
477 m48t59
->rtc
->nvram_old_abi
= true;
478 m48t59
->rtc
->ops
= ops
;
480 nvmem_cfg
.size
= pdata
->offset
;
481 ret
= rtc_nvmem_register(m48t59
->rtc
, &nvmem_cfg
);
485 ret
= rtc_register_device(m48t59
->rtc
);
492 /* work with hotplug and coldplug */
493 MODULE_ALIAS("platform:rtc-m48t59");
495 static struct platform_driver m48t59_rtc_driver
= {
497 .name
= "rtc-m48t59",
499 .probe
= m48t59_rtc_probe
,
502 module_platform_driver(m48t59_rtc_driver
);
504 MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
505 MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
506 MODULE_LICENSE("GPL");