2 * Real-time clock driver for MPC5121
4 * Copyright 2007, Domen Puncer <domen.puncer@telargo.com>
5 * Copyright 2008, Freescale Semiconductor, Inc. All rights reserved.
6 * Copyright 2011, Dmitry Eremin-Solenikov
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/init.h>
14 #include <linux/module.h>
15 #include <linux/rtc.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
22 #include <linux/slab.h>
24 struct mpc5121_rtc_regs
{
25 u8 set_time
; /* RTC + 0x00 */
26 u8 hour_set
; /* RTC + 0x01 */
27 u8 minute_set
; /* RTC + 0x02 */
28 u8 second_set
; /* RTC + 0x03 */
30 u8 set_date
; /* RTC + 0x04 */
31 u8 month_set
; /* RTC + 0x05 */
32 u8 weekday_set
; /* RTC + 0x06 */
33 u8 date_set
; /* RTC + 0x07 */
35 u8 write_sw
; /* RTC + 0x08 */
36 u8 sw_set
; /* RTC + 0x09 */
37 u16 year_set
; /* RTC + 0x0a */
39 u8 alm_enable
; /* RTC + 0x0c */
40 u8 alm_hour_set
; /* RTC + 0x0d */
41 u8 alm_min_set
; /* RTC + 0x0e */
42 u8 int_enable
; /* RTC + 0x0f */
45 u8 hour
; /* RTC + 0x11 */
46 u8 minute
; /* RTC + 0x12 */
47 u8 second
; /* RTC + 0x13 */
49 u8 month
; /* RTC + 0x14 */
50 u8 wday_mday
; /* RTC + 0x15 */
51 u16 year
; /* RTC + 0x16 */
53 u8 int_alm
; /* RTC + 0x18 */
54 u8 int_sw
; /* RTC + 0x19 */
55 u8 alm_status
; /* RTC + 0x1a */
56 u8 sw_minute
; /* RTC + 0x1b */
58 u8 bus_error_1
; /* RTC + 0x1c */
59 u8 int_day
; /* RTC + 0x1d */
60 u8 int_min
; /* RTC + 0x1e */
61 u8 int_sec
; /* RTC + 0x1f */
65 * intended to be used for hibernation but hibernation
66 * does not work on silicon rev 1.5 so use it for non-volatile
67 * storage of offset between the actual_time register and linux
70 u32 target_time
; /* RTC + 0x20 */
73 * readonly time since VBAT_RTC was last connected
75 u32 actual_time
; /* RTC + 0x24 */
76 u32 keep_alive
; /* RTC + 0x28 */
79 struct mpc5121_rtc_data
{
81 unsigned irq_periodic
;
82 struct mpc5121_rtc_regs __iomem
*regs
;
83 struct rtc_device
*rtc
;
84 struct rtc_wkalrm wkalarm
;
88 * Update second/minute/hour registers.
90 * This is just so alarm will work.
92 static void mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem
*regs
,
95 out_8(®s
->second_set
, tm
->tm_sec
);
96 out_8(®s
->minute_set
, tm
->tm_min
);
97 out_8(®s
->hour_set
, tm
->tm_hour
);
99 /* set time sequence */
100 out_8(®s
->set_time
, 0x1);
101 out_8(®s
->set_time
, 0x3);
102 out_8(®s
->set_time
, 0x1);
103 out_8(®s
->set_time
, 0x0);
106 static int mpc5121_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
108 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
109 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
113 * linux time is actual_time plus the offset saved in target_time
115 now
= in_be32(®s
->actual_time
) + in_be32(®s
->target_time
);
117 rtc_time_to_tm(now
, tm
);
120 * update second minute hour registers
121 * so alarms will work
123 mpc5121_rtc_update_smh(regs
, tm
);
125 return rtc_valid_tm(tm
);
128 static int mpc5121_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
130 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
131 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
136 * The actual_time register is read only so we write the offset
137 * between it and linux time to the target_time register.
139 ret
= rtc_tm_to_time(tm
, &now
);
141 out_be32(®s
->target_time
, now
- in_be32(®s
->actual_time
));
144 * update second minute hour registers
145 * so alarms will work
147 mpc5121_rtc_update_smh(regs
, tm
);
152 static int mpc5200_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
154 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
155 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
158 tm
->tm_sec
= in_8(®s
->second
);
159 tm
->tm_min
= in_8(®s
->minute
);
161 /* 12 hour format? */
162 if (in_8(®s
->hour
) & 0x20)
163 tm
->tm_hour
= (in_8(®s
->hour
) >> 1) +
164 (in_8(®s
->hour
) & 1 ? 12 : 0);
166 tm
->tm_hour
= in_8(®s
->hour
);
168 tmp
= in_8(®s
->wday_mday
);
169 tm
->tm_mday
= tmp
& 0x1f;
170 tm
->tm_mon
= in_8(®s
->month
) - 1;
171 tm
->tm_year
= in_be16(®s
->year
) - 1900;
172 tm
->tm_wday
= (tmp
>> 5) % 7;
173 tm
->tm_yday
= rtc_year_days(tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
);
179 static int mpc5200_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
181 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
182 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
184 mpc5121_rtc_update_smh(regs
, tm
);
187 out_8(®s
->month_set
, tm
->tm_mon
+ 1);
188 out_8(®s
->weekday_set
, tm
->tm_wday
? tm
->tm_wday
: 7);
189 out_8(®s
->date_set
, tm
->tm_mday
);
190 out_be16(®s
->year_set
, tm
->tm_year
+ 1900);
192 /* set date sequence */
193 out_8(®s
->set_date
, 0x1);
194 out_8(®s
->set_date
, 0x3);
195 out_8(®s
->set_date
, 0x1);
196 out_8(®s
->set_date
, 0x0);
201 static int mpc5121_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
203 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
204 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
206 *alarm
= rtc
->wkalarm
;
208 alarm
->pending
= in_8(®s
->alm_status
);
213 static int mpc5121_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
215 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
216 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
219 * the alarm has no seconds so deal with it
221 if (alarm
->time
.tm_sec
) {
222 alarm
->time
.tm_sec
= 0;
223 alarm
->time
.tm_min
++;
224 if (alarm
->time
.tm_min
>= 60) {
225 alarm
->time
.tm_min
= 0;
226 alarm
->time
.tm_hour
++;
227 if (alarm
->time
.tm_hour
>= 24)
228 alarm
->time
.tm_hour
= 0;
232 alarm
->time
.tm_mday
= -1;
233 alarm
->time
.tm_mon
= -1;
234 alarm
->time
.tm_year
= -1;
236 out_8(®s
->alm_min_set
, alarm
->time
.tm_min
);
237 out_8(®s
->alm_hour_set
, alarm
->time
.tm_hour
);
239 out_8(®s
->alm_enable
, alarm
->enabled
);
241 rtc
->wkalarm
= *alarm
;
245 static irqreturn_t
mpc5121_rtc_handler(int irq
, void *dev
)
247 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata((struct device
*)dev
);
248 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
250 if (in_8(®s
->int_alm
)) {
251 /* acknowledge and clear status */
252 out_8(®s
->int_alm
, 1);
253 out_8(®s
->alm_status
, 1);
255 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
262 static irqreturn_t
mpc5121_rtc_handler_upd(int irq
, void *dev
)
264 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata((struct device
*)dev
);
265 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
267 if (in_8(®s
->int_sec
) && (in_8(®s
->int_enable
) & 0x1)) {
269 out_8(®s
->int_sec
, 1);
271 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_UF
);
278 static int mpc5121_rtc_alarm_irq_enable(struct device
*dev
,
279 unsigned int enabled
)
281 struct mpc5121_rtc_data
*rtc
= dev_get_drvdata(dev
);
282 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
290 out_8(®s
->alm_enable
, val
);
291 rtc
->wkalarm
.enabled
= val
;
296 static const struct rtc_class_ops mpc5121_rtc_ops
= {
297 .read_time
= mpc5121_rtc_read_time
,
298 .set_time
= mpc5121_rtc_set_time
,
299 .read_alarm
= mpc5121_rtc_read_alarm
,
300 .set_alarm
= mpc5121_rtc_set_alarm
,
301 .alarm_irq_enable
= mpc5121_rtc_alarm_irq_enable
,
304 static const struct rtc_class_ops mpc5200_rtc_ops
= {
305 .read_time
= mpc5200_rtc_read_time
,
306 .set_time
= mpc5200_rtc_set_time
,
307 .read_alarm
= mpc5121_rtc_read_alarm
,
308 .set_alarm
= mpc5121_rtc_set_alarm
,
309 .alarm_irq_enable
= mpc5121_rtc_alarm_irq_enable
,
312 static int mpc5121_rtc_probe(struct platform_device
*op
)
314 struct mpc5121_rtc_data
*rtc
;
317 rtc
= devm_kzalloc(&op
->dev
, sizeof(*rtc
), GFP_KERNEL
);
321 rtc
->regs
= of_iomap(op
->dev
.of_node
, 0);
323 dev_err(&op
->dev
, "%s: couldn't map io space\n", __func__
);
327 device_init_wakeup(&op
->dev
, 1);
329 platform_set_drvdata(op
, rtc
);
331 rtc
->irq
= irq_of_parse_and_map(op
->dev
.of_node
, 1);
332 err
= request_irq(rtc
->irq
, mpc5121_rtc_handler
, 0,
333 "mpc5121-rtc", &op
->dev
);
335 dev_err(&op
->dev
, "%s: could not request irq: %i\n",
340 rtc
->irq_periodic
= irq_of_parse_and_map(op
->dev
.of_node
, 0);
341 err
= request_irq(rtc
->irq_periodic
, mpc5121_rtc_handler_upd
,
342 0, "mpc5121-rtc_upd", &op
->dev
);
344 dev_err(&op
->dev
, "%s: could not request irq: %i\n",
345 __func__
, rtc
->irq_periodic
);
349 if (of_device_is_compatible(op
->dev
.of_node
, "fsl,mpc5121-rtc")) {
351 ka
= in_be32(&rtc
->regs
->keep_alive
);
354 "mpc5121-rtc: Battery or oscillator failure!\n");
355 out_be32(&rtc
->regs
->keep_alive
, ka
);
358 rtc
->rtc
= devm_rtc_device_register(&op
->dev
, "mpc5121-rtc",
359 &mpc5121_rtc_ops
, THIS_MODULE
);
361 rtc
->rtc
= devm_rtc_device_register(&op
->dev
, "mpc5200-rtc",
362 &mpc5200_rtc_ops
, THIS_MODULE
);
365 if (IS_ERR(rtc
->rtc
)) {
366 err
= PTR_ERR(rtc
->rtc
);
369 rtc
->rtc
->uie_unsupported
= 1;
374 free_irq(rtc
->irq_periodic
, &op
->dev
);
376 irq_dispose_mapping(rtc
->irq_periodic
);
377 free_irq(rtc
->irq
, &op
->dev
);
379 irq_dispose_mapping(rtc
->irq
);
385 static int mpc5121_rtc_remove(struct platform_device
*op
)
387 struct mpc5121_rtc_data
*rtc
= platform_get_drvdata(op
);
388 struct mpc5121_rtc_regs __iomem
*regs
= rtc
->regs
;
390 /* disable interrupt, so there are no nasty surprises */
391 out_8(®s
->alm_enable
, 0);
392 out_8(®s
->int_enable
, in_8(®s
->int_enable
) & ~0x1);
395 free_irq(rtc
->irq
, &op
->dev
);
396 free_irq(rtc
->irq_periodic
, &op
->dev
);
397 irq_dispose_mapping(rtc
->irq
);
398 irq_dispose_mapping(rtc
->irq_periodic
);
404 static struct of_device_id mpc5121_rtc_match
[] = {
405 { .compatible
= "fsl,mpc5121-rtc", },
406 { .compatible
= "fsl,mpc5200-rtc", },
411 static struct platform_driver mpc5121_rtc_driver
= {
413 .name
= "mpc5121-rtc",
414 .owner
= THIS_MODULE
,
415 .of_match_table
= of_match_ptr(mpc5121_rtc_match
),
417 .probe
= mpc5121_rtc_probe
,
418 .remove
= mpc5121_rtc_remove
,
421 module_platform_driver(mpc5121_rtc_driver
);
423 MODULE_LICENSE("GPL");
424 MODULE_AUTHOR("John Rigby <jcrigby@gmail.com>");