1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/rtc/rtc-vt8500.c
5 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
10 #include <linux/module.h>
11 #include <linux/rtc.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/bcd.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
21 * Register definitions
23 #define VT8500_RTC_TS 0x00 /* Time set */
24 #define VT8500_RTC_DS 0x04 /* Date set */
25 #define VT8500_RTC_AS 0x08 /* Alarm set */
26 #define VT8500_RTC_CR 0x0c /* Control */
27 #define VT8500_RTC_TR 0x10 /* Time read */
28 #define VT8500_RTC_DR 0x14 /* Date read */
29 #define VT8500_RTC_WS 0x18 /* Write status */
30 #define VT8500_RTC_CL 0x20 /* Calibration */
31 #define VT8500_RTC_IS 0x24 /* Interrupt status */
32 #define VT8500_RTC_ST 0x28 /* Status */
34 #define INVALID_TIME_BIT (1 << 31)
36 #define DATE_CENTURY_S 19
37 #define DATE_YEAR_S 11
38 #define DATE_YEAR_MASK (0xff << DATE_YEAR_S)
39 #define DATE_MONTH_S 6
40 #define DATE_MONTH_MASK (0x1f << DATE_MONTH_S)
41 #define DATE_DAY_MASK 0x3f
44 #define TIME_DOW_MASK (0x07 << TIME_DOW_S)
45 #define TIME_HOUR_S 14
46 #define TIME_HOUR_MASK (0x3f << TIME_HOUR_S)
48 #define TIME_MIN_MASK (0x7f << TIME_MIN_S)
49 #define TIME_SEC_MASK 0x7f
51 #define ALARM_DAY_S 20
52 #define ALARM_DAY_MASK (0x3f << ALARM_DAY_S)
54 #define ALARM_DAY_BIT (1 << 29)
55 #define ALARM_HOUR_BIT (1 << 28)
56 #define ALARM_MIN_BIT (1 << 27)
57 #define ALARM_SEC_BIT (1 << 26)
59 #define ALARM_ENABLE_MASK (ALARM_DAY_BIT \
64 #define VT8500_RTC_CR_ENABLE (1 << 0) /* Enable RTC */
65 #define VT8500_RTC_CR_12H (1 << 1) /* 12h time format */
66 #define VT8500_RTC_CR_SM_ENABLE (1 << 2) /* Enable periodic irqs */
67 #define VT8500_RTC_CR_SM_SEC (1 << 3) /* 0: 1Hz/60, 1: 1Hz */
68 #define VT8500_RTC_CR_CALIB (1 << 4) /* Enable calibration */
70 #define VT8500_RTC_IS_ALARM (1 << 0) /* Alarm interrupt status */
73 void __iomem
*regbase
;
75 struct rtc_device
*rtc
;
76 spinlock_t lock
; /* Protects this structure */
79 static irqreturn_t
vt8500_rtc_irq(int irq
, void *dev_id
)
81 struct vt8500_rtc
*vt8500_rtc
= dev_id
;
83 unsigned long events
= 0;
85 spin_lock(&vt8500_rtc
->lock
);
87 /* clear interrupt sources */
88 isr
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
89 writel(isr
, vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
91 spin_unlock(&vt8500_rtc
->lock
);
93 if (isr
& VT8500_RTC_IS_ALARM
)
94 events
|= RTC_AF
| RTC_IRQF
;
96 rtc_update_irq(vt8500_rtc
->rtc
, 1, events
);
101 static int vt8500_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
103 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
106 date
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_DR
);
107 time
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_TR
);
109 tm
->tm_sec
= bcd2bin(time
& TIME_SEC_MASK
);
110 tm
->tm_min
= bcd2bin((time
& TIME_MIN_MASK
) >> TIME_MIN_S
);
111 tm
->tm_hour
= bcd2bin((time
& TIME_HOUR_MASK
) >> TIME_HOUR_S
);
112 tm
->tm_mday
= bcd2bin(date
& DATE_DAY_MASK
);
113 tm
->tm_mon
= bcd2bin((date
& DATE_MONTH_MASK
) >> DATE_MONTH_S
) - 1;
114 tm
->tm_year
= bcd2bin((date
& DATE_YEAR_MASK
) >> DATE_YEAR_S
)
115 + ((date
>> DATE_CENTURY_S
) & 1 ? 200 : 100);
116 tm
->tm_wday
= (time
& TIME_DOW_MASK
) >> TIME_DOW_S
;
121 static int vt8500_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
123 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
125 writel((bin2bcd(tm
->tm_year
% 100) << DATE_YEAR_S
)
126 | (bin2bcd(tm
->tm_mon
+ 1) << DATE_MONTH_S
)
127 | (bin2bcd(tm
->tm_mday
))
128 | ((tm
->tm_year
>= 200) << DATE_CENTURY_S
),
129 vt8500_rtc
->regbase
+ VT8500_RTC_DS
);
130 writel((bin2bcd(tm
->tm_wday
) << TIME_DOW_S
)
131 | (bin2bcd(tm
->tm_hour
) << TIME_HOUR_S
)
132 | (bin2bcd(tm
->tm_min
) << TIME_MIN_S
)
133 | (bin2bcd(tm
->tm_sec
)),
134 vt8500_rtc
->regbase
+ VT8500_RTC_TS
);
139 static int vt8500_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
141 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
144 alarm
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
145 isr
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
147 alrm
->time
.tm_mday
= bcd2bin((alarm
& ALARM_DAY_MASK
) >> ALARM_DAY_S
);
148 alrm
->time
.tm_hour
= bcd2bin((alarm
& TIME_HOUR_MASK
) >> TIME_HOUR_S
);
149 alrm
->time
.tm_min
= bcd2bin((alarm
& TIME_MIN_MASK
) >> TIME_MIN_S
);
150 alrm
->time
.tm_sec
= bcd2bin((alarm
& TIME_SEC_MASK
));
152 alrm
->enabled
= (alarm
& ALARM_ENABLE_MASK
) ? 1 : 0;
153 alrm
->pending
= (isr
& VT8500_RTC_IS_ALARM
) ? 1 : 0;
155 return rtc_valid_tm(&alrm
->time
);
158 static int vt8500_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
160 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
162 writel((alrm
->enabled
? ALARM_ENABLE_MASK
: 0)
163 | (bin2bcd(alrm
->time
.tm_mday
) << ALARM_DAY_S
)
164 | (bin2bcd(alrm
->time
.tm_hour
) << TIME_HOUR_S
)
165 | (bin2bcd(alrm
->time
.tm_min
) << TIME_MIN_S
)
166 | (bin2bcd(alrm
->time
.tm_sec
)),
167 vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
172 static int vt8500_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
174 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
175 unsigned long tmp
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
178 tmp
|= ALARM_ENABLE_MASK
;
180 tmp
&= ~ALARM_ENABLE_MASK
;
182 writel(tmp
, vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
186 static const struct rtc_class_ops vt8500_rtc_ops
= {
187 .read_time
= vt8500_rtc_read_time
,
188 .set_time
= vt8500_rtc_set_time
,
189 .read_alarm
= vt8500_rtc_read_alarm
,
190 .set_alarm
= vt8500_rtc_set_alarm
,
191 .alarm_irq_enable
= vt8500_alarm_irq_enable
,
194 static int vt8500_rtc_probe(struct platform_device
*pdev
)
196 struct vt8500_rtc
*vt8500_rtc
;
199 vt8500_rtc
= devm_kzalloc(&pdev
->dev
,
200 sizeof(struct vt8500_rtc
), GFP_KERNEL
);
204 spin_lock_init(&vt8500_rtc
->lock
);
205 platform_set_drvdata(pdev
, vt8500_rtc
);
207 vt8500_rtc
->irq_alarm
= platform_get_irq(pdev
, 0);
208 if (vt8500_rtc
->irq_alarm
< 0)
209 return vt8500_rtc
->irq_alarm
;
211 vt8500_rtc
->regbase
= devm_platform_ioremap_resource(pdev
, 0);
212 if (IS_ERR(vt8500_rtc
->regbase
))
213 return PTR_ERR(vt8500_rtc
->regbase
);
215 /* Enable RTC and set it to 24-hour mode */
216 writel(VT8500_RTC_CR_ENABLE
,
217 vt8500_rtc
->regbase
+ VT8500_RTC_CR
);
219 vt8500_rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
220 if (IS_ERR(vt8500_rtc
->rtc
))
221 return PTR_ERR(vt8500_rtc
->rtc
);
223 vt8500_rtc
->rtc
->ops
= &vt8500_rtc_ops
;
224 vt8500_rtc
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
225 vt8500_rtc
->rtc
->range_max
= RTC_TIMESTAMP_END_2199
;
227 ret
= devm_request_irq(&pdev
->dev
, vt8500_rtc
->irq_alarm
,
228 vt8500_rtc_irq
, 0, "rtc alarm", vt8500_rtc
);
230 dev_err(&pdev
->dev
, "can't get irq %i, err %d\n",
231 vt8500_rtc
->irq_alarm
, ret
);
235 return devm_rtc_register_device(vt8500_rtc
->rtc
);
238 static int vt8500_rtc_remove(struct platform_device
*pdev
)
240 struct vt8500_rtc
*vt8500_rtc
= platform_get_drvdata(pdev
);
242 /* Disable alarm matching */
243 writel(0, vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
248 static const struct of_device_id wmt_dt_ids
[] = {
249 { .compatible
= "via,vt8500-rtc", },
252 MODULE_DEVICE_TABLE(of
, wmt_dt_ids
);
254 static struct platform_driver vt8500_rtc_driver
= {
255 .probe
= vt8500_rtc_probe
,
256 .remove
= vt8500_rtc_remove
,
258 .name
= "vt8500-rtc",
259 .of_match_table
= wmt_dt_ids
,
263 module_platform_driver(vt8500_rtc_driver
);
265 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
266 MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
267 MODULE_LICENSE("GPL v2");
268 MODULE_ALIAS("platform:vt8500-rtc");