xenbus_client.c: correct exit path for xenbus_map_ring_valloc_hvm
[linux/fpc-iii.git] / drivers / rtc / rtc-vt8500.c
blobd89efee6d29e051d89761c33205434aa39a1e90e
1 /*
2 * drivers/rtc/rtc-vt8500.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6 * Based on rtc-pxa.c
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/bcd.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/of.h>
29 * Register definitions
31 #define VT8500_RTC_TS 0x00 /* Time set */
32 #define VT8500_RTC_DS 0x04 /* Date set */
33 #define VT8500_RTC_AS 0x08 /* Alarm set */
34 #define VT8500_RTC_CR 0x0c /* Control */
35 #define VT8500_RTC_TR 0x10 /* Time read */
36 #define VT8500_RTC_DR 0x14 /* Date read */
37 #define VT8500_RTC_WS 0x18 /* Write status */
38 #define VT8500_RTC_CL 0x20 /* Calibration */
39 #define VT8500_RTC_IS 0x24 /* Interrupt status */
40 #define VT8500_RTC_ST 0x28 /* Status */
42 #define INVALID_TIME_BIT (1 << 31)
44 #define DATE_CENTURY_S 19
45 #define DATE_YEAR_S 11
46 #define DATE_YEAR_MASK (0xff << DATE_YEAR_S)
47 #define DATE_MONTH_S 6
48 #define DATE_MONTH_MASK (0x1f << DATE_MONTH_S)
49 #define DATE_DAY_MASK 0x3f
51 #define TIME_DOW_S 20
52 #define TIME_DOW_MASK (0x07 << TIME_DOW_S)
53 #define TIME_HOUR_S 14
54 #define TIME_HOUR_MASK (0x3f << TIME_HOUR_S)
55 #define TIME_MIN_S 7
56 #define TIME_MIN_MASK (0x7f << TIME_MIN_S)
57 #define TIME_SEC_MASK 0x7f
59 #define ALARM_DAY_S 20
60 #define ALARM_DAY_MASK (0x3f << ALARM_DAY_S)
62 #define ALARM_DAY_BIT (1 << 29)
63 #define ALARM_HOUR_BIT (1 << 28)
64 #define ALARM_MIN_BIT (1 << 27)
65 #define ALARM_SEC_BIT (1 << 26)
67 #define ALARM_ENABLE_MASK (ALARM_DAY_BIT \
68 | ALARM_HOUR_BIT \
69 | ALARM_MIN_BIT \
70 | ALARM_SEC_BIT)
72 #define VT8500_RTC_CR_ENABLE (1 << 0) /* Enable RTC */
73 #define VT8500_RTC_CR_12H (1 << 1) /* 12h time format */
74 #define VT8500_RTC_CR_SM_ENABLE (1 << 2) /* Enable periodic irqs */
75 #define VT8500_RTC_CR_SM_SEC (1 << 3) /* 0: 1Hz/60, 1: 1Hz */
76 #define VT8500_RTC_CR_CALIB (1 << 4) /* Enable calibration */
78 #define VT8500_RTC_IS_ALARM (1 << 0) /* Alarm interrupt status */
80 struct vt8500_rtc {
81 void __iomem *regbase;
82 struct resource *res;
83 int irq_alarm;
84 struct rtc_device *rtc;
85 spinlock_t lock; /* Protects this structure */
88 static irqreturn_t vt8500_rtc_irq(int irq, void *dev_id)
90 struct vt8500_rtc *vt8500_rtc = dev_id;
91 u32 isr;
92 unsigned long events = 0;
94 spin_lock(&vt8500_rtc->lock);
96 /* clear interrupt sources */
97 isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
98 writel(isr, vt8500_rtc->regbase + VT8500_RTC_IS);
100 spin_unlock(&vt8500_rtc->lock);
102 if (isr & VT8500_RTC_IS_ALARM)
103 events |= RTC_AF | RTC_IRQF;
105 rtc_update_irq(vt8500_rtc->rtc, 1, events);
107 return IRQ_HANDLED;
110 static int vt8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
112 struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
113 u32 date, time;
115 date = readl(vt8500_rtc->regbase + VT8500_RTC_DR);
116 time = readl(vt8500_rtc->regbase + VT8500_RTC_TR);
118 tm->tm_sec = bcd2bin(time & TIME_SEC_MASK);
119 tm->tm_min = bcd2bin((time & TIME_MIN_MASK) >> TIME_MIN_S);
120 tm->tm_hour = bcd2bin((time & TIME_HOUR_MASK) >> TIME_HOUR_S);
121 tm->tm_mday = bcd2bin(date & DATE_DAY_MASK);
122 tm->tm_mon = bcd2bin((date & DATE_MONTH_MASK) >> DATE_MONTH_S) - 1;
123 tm->tm_year = bcd2bin((date & DATE_YEAR_MASK) >> DATE_YEAR_S)
124 + ((date >> DATE_CENTURY_S) & 1 ? 200 : 100);
125 tm->tm_wday = (time & TIME_DOW_MASK) >> TIME_DOW_S;
127 return 0;
130 static int vt8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
132 struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
134 if (tm->tm_year < 100) {
135 dev_warn(dev, "Only years 2000-2199 are supported by the "
136 "hardware!\n");
137 return -EINVAL;
140 writel((bin2bcd(tm->tm_year % 100) << DATE_YEAR_S)
141 | (bin2bcd(tm->tm_mon + 1) << DATE_MONTH_S)
142 | (bin2bcd(tm->tm_mday))
143 | ((tm->tm_year >= 200) << DATE_CENTURY_S),
144 vt8500_rtc->regbase + VT8500_RTC_DS);
145 writel((bin2bcd(tm->tm_wday) << TIME_DOW_S)
146 | (bin2bcd(tm->tm_hour) << TIME_HOUR_S)
147 | (bin2bcd(tm->tm_min) << TIME_MIN_S)
148 | (bin2bcd(tm->tm_sec)),
149 vt8500_rtc->regbase + VT8500_RTC_TS);
151 return 0;
154 static int vt8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
156 struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
157 u32 isr, alarm;
159 alarm = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
160 isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
162 alrm->time.tm_mday = bcd2bin((alarm & ALARM_DAY_MASK) >> ALARM_DAY_S);
163 alrm->time.tm_hour = bcd2bin((alarm & TIME_HOUR_MASK) >> TIME_HOUR_S);
164 alrm->time.tm_min = bcd2bin((alarm & TIME_MIN_MASK) >> TIME_MIN_S);
165 alrm->time.tm_sec = bcd2bin((alarm & TIME_SEC_MASK));
167 alrm->enabled = (alarm & ALARM_ENABLE_MASK) ? 1 : 0;
168 alrm->pending = (isr & VT8500_RTC_IS_ALARM) ? 1 : 0;
170 return rtc_valid_tm(&alrm->time);
173 static int vt8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
175 struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
177 writel((alrm->enabled ? ALARM_ENABLE_MASK : 0)
178 | (bin2bcd(alrm->time.tm_mday) << ALARM_DAY_S)
179 | (bin2bcd(alrm->time.tm_hour) << TIME_HOUR_S)
180 | (bin2bcd(alrm->time.tm_min) << TIME_MIN_S)
181 | (bin2bcd(alrm->time.tm_sec)),
182 vt8500_rtc->regbase + VT8500_RTC_AS);
184 return 0;
187 static int vt8500_alarm_irq_enable(struct device *dev, unsigned int enabled)
189 struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
190 unsigned long tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
192 if (enabled)
193 tmp |= ALARM_ENABLE_MASK;
194 else
195 tmp &= ~ALARM_ENABLE_MASK;
197 writel(tmp, vt8500_rtc->regbase + VT8500_RTC_AS);
198 return 0;
201 static const struct rtc_class_ops vt8500_rtc_ops = {
202 .read_time = vt8500_rtc_read_time,
203 .set_time = vt8500_rtc_set_time,
204 .read_alarm = vt8500_rtc_read_alarm,
205 .set_alarm = vt8500_rtc_set_alarm,
206 .alarm_irq_enable = vt8500_alarm_irq_enable,
209 static int vt8500_rtc_probe(struct platform_device *pdev)
211 struct vt8500_rtc *vt8500_rtc;
212 int ret;
214 vt8500_rtc = devm_kzalloc(&pdev->dev,
215 sizeof(struct vt8500_rtc), GFP_KERNEL);
216 if (!vt8500_rtc)
217 return -ENOMEM;
219 spin_lock_init(&vt8500_rtc->lock);
220 platform_set_drvdata(pdev, vt8500_rtc);
222 vt8500_rtc->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
223 if (!vt8500_rtc->res) {
224 dev_err(&pdev->dev, "No I/O memory resource defined\n");
225 return -ENXIO;
228 vt8500_rtc->irq_alarm = platform_get_irq(pdev, 0);
229 if (vt8500_rtc->irq_alarm < 0) {
230 dev_err(&pdev->dev, "No alarm IRQ resource defined\n");
231 return -ENXIO;
234 vt8500_rtc->res = devm_request_mem_region(&pdev->dev,
235 vt8500_rtc->res->start,
236 resource_size(vt8500_rtc->res),
237 "vt8500-rtc");
238 if (vt8500_rtc->res == NULL) {
239 dev_err(&pdev->dev, "failed to request I/O memory\n");
240 return -EBUSY;
243 vt8500_rtc->regbase = devm_ioremap(&pdev->dev, vt8500_rtc->res->start,
244 resource_size(vt8500_rtc->res));
245 if (!vt8500_rtc->regbase) {
246 dev_err(&pdev->dev, "Unable to map RTC I/O memory\n");
247 ret = -EBUSY;
248 goto err_return;
251 /* Enable RTC and set it to 24-hour mode */
252 writel(VT8500_RTC_CR_ENABLE,
253 vt8500_rtc->regbase + VT8500_RTC_CR);
255 vt8500_rtc->rtc = devm_rtc_device_register(&pdev->dev, "vt8500-rtc",
256 &vt8500_rtc_ops, THIS_MODULE);
257 if (IS_ERR(vt8500_rtc->rtc)) {
258 ret = PTR_ERR(vt8500_rtc->rtc);
259 dev_err(&pdev->dev,
260 "Failed to register RTC device -> %d\n", ret);
261 goto err_return;
264 ret = devm_request_irq(&pdev->dev, vt8500_rtc->irq_alarm,
265 vt8500_rtc_irq, 0, "rtc alarm", vt8500_rtc);
266 if (ret < 0) {
267 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
268 vt8500_rtc->irq_alarm, ret);
269 goto err_return;
272 return 0;
274 err_return:
275 return ret;
278 static int vt8500_rtc_remove(struct platform_device *pdev)
280 struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev);
282 /* Disable alarm matching */
283 writel(0, vt8500_rtc->regbase + VT8500_RTC_IS);
285 platform_set_drvdata(pdev, NULL);
287 return 0;
290 static const struct of_device_id wmt_dt_ids[] = {
291 { .compatible = "via,vt8500-rtc", },
295 static struct platform_driver vt8500_rtc_driver = {
296 .probe = vt8500_rtc_probe,
297 .remove = vt8500_rtc_remove,
298 .driver = {
299 .name = "vt8500-rtc",
300 .owner = THIS_MODULE,
301 .of_match_table = of_match_ptr(wmt_dt_ids),
305 module_platform_driver(vt8500_rtc_driver);
307 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
308 MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
309 MODULE_LICENSE("GPL v2");
310 MODULE_ALIAS("platform:vt8500-rtc");