Staging: hv: hv_mouse: use an anonymous union for struct mousevsc_prt_msg
[wandboard.git] / drivers / rtc / rtc-mv.c
blob60627a7645143721459cd78eb97f5addc885856e
1 /*
2 * Driver for the RTC in Marvell SoCs.
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
16 #include <linux/gfp.h>
19 #define RTC_TIME_REG_OFFS 0
20 #define RTC_SECONDS_OFFS 0
21 #define RTC_MINUTES_OFFS 8
22 #define RTC_HOURS_OFFS 16
23 #define RTC_WDAY_OFFS 24
24 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
26 #define RTC_DATE_REG_OFFS 4
27 #define RTC_MDAY_OFFS 0
28 #define RTC_MONTH_OFFS 8
29 #define RTC_YEAR_OFFS 16
31 #define RTC_ALARM_TIME_REG_OFFS 8
32 #define RTC_ALARM_DATE_REG_OFFS 0xc
33 #define RTC_ALARM_VALID (1 << 7)
35 #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
36 #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
38 struct rtc_plat_data {
39 struct rtc_device *rtc;
40 void __iomem *ioaddr;
41 int irq;
44 static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
46 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
47 void __iomem *ioaddr = pdata->ioaddr;
48 u32 rtc_reg;
50 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
51 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
52 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
53 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
54 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
56 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
57 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
58 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
59 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
61 return 0;
64 static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
66 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
67 void __iomem *ioaddr = pdata->ioaddr;
68 u32 rtc_time, rtc_date;
69 unsigned int year, month, day, hour, minute, second, wday;
71 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
72 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
74 second = rtc_time & 0x7f;
75 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
76 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
77 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
79 day = rtc_date & 0x3f;
80 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
81 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
83 tm->tm_sec = bcd2bin(second);
84 tm->tm_min = bcd2bin(minute);
85 tm->tm_hour = bcd2bin(hour);
86 tm->tm_mday = bcd2bin(day);
87 tm->tm_wday = bcd2bin(wday);
88 tm->tm_mon = bcd2bin(month) - 1;
89 /* hw counts from year 2000, but tm_year is relative to 1900 */
90 tm->tm_year = bcd2bin(year) + 100;
92 return rtc_valid_tm(tm);
95 static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
97 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
98 void __iomem *ioaddr = pdata->ioaddr;
99 u32 rtc_time, rtc_date;
100 unsigned int year, month, day, hour, minute, second, wday;
102 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
103 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
105 second = rtc_time & 0x7f;
106 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
107 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
108 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
110 day = rtc_date & 0x3f;
111 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
112 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
114 alm->time.tm_sec = bcd2bin(second);
115 alm->time.tm_min = bcd2bin(minute);
116 alm->time.tm_hour = bcd2bin(hour);
117 alm->time.tm_mday = bcd2bin(day);
118 alm->time.tm_wday = bcd2bin(wday);
119 alm->time.tm_mon = bcd2bin(month) - 1;
120 /* hw counts from year 2000, but tm_year is relative to 1900 */
121 alm->time.tm_year = bcd2bin(year) + 100;
123 if (rtc_valid_tm(&alm->time) < 0) {
124 dev_err(dev, "retrieved alarm date/time is not valid.\n");
125 rtc_time_to_tm(0, &alm->time);
128 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
129 return 0;
132 static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
134 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
135 void __iomem *ioaddr = pdata->ioaddr;
136 u32 rtc_reg = 0;
138 if (alm->time.tm_sec >= 0)
139 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
140 << RTC_SECONDS_OFFS;
141 if (alm->time.tm_min >= 0)
142 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
143 << RTC_MINUTES_OFFS;
144 if (alm->time.tm_hour >= 0)
145 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
146 << RTC_HOURS_OFFS;
148 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
150 if (alm->time.tm_mday >= 0)
151 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
152 << RTC_MDAY_OFFS;
153 else
154 rtc_reg = 0;
156 if (alm->time.tm_mon >= 0)
157 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
158 << RTC_MONTH_OFFS;
160 if (alm->time.tm_year >= 0)
161 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
162 << RTC_YEAR_OFFS;
164 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
165 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
166 writel(alm->enabled ? 1 : 0,
167 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
169 return 0;
172 static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
174 struct platform_device *pdev = to_platform_device(dev);
175 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
176 void __iomem *ioaddr = pdata->ioaddr;
178 if (pdata->irq < 0)
179 return -EINVAL; /* fall back into rtc-dev's emulation */
181 if (enabled)
182 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
183 else
184 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
185 return 0;
188 static irqreturn_t mv_rtc_interrupt(int irq, void *data)
190 struct rtc_plat_data *pdata = data;
191 void __iomem *ioaddr = pdata->ioaddr;
193 /* alarm irq? */
194 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
195 return IRQ_NONE;
197 /* clear interrupt */
198 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
199 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
200 return IRQ_HANDLED;
203 static const struct rtc_class_ops mv_rtc_ops = {
204 .read_time = mv_rtc_read_time,
205 .set_time = mv_rtc_set_time,
208 static const struct rtc_class_ops mv_rtc_alarm_ops = {
209 .read_time = mv_rtc_read_time,
210 .set_time = mv_rtc_set_time,
211 .read_alarm = mv_rtc_read_alarm,
212 .set_alarm = mv_rtc_set_alarm,
213 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
216 static int __devinit mv_rtc_probe(struct platform_device *pdev)
218 struct resource *res;
219 struct rtc_plat_data *pdata;
220 resource_size_t size;
221 u32 rtc_time;
223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
224 if (!res)
225 return -ENODEV;
227 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
228 if (!pdata)
229 return -ENOMEM;
231 size = resource_size(res);
232 if (!devm_request_mem_region(&pdev->dev, res->start, size,
233 pdev->name))
234 return -EBUSY;
236 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
237 if (!pdata->ioaddr)
238 return -ENOMEM;
240 /* make sure the 24 hours mode is enabled */
241 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
242 if (rtc_time & RTC_HOURS_12H_MODE) {
243 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
244 return -EINVAL;
247 /* make sure it is actually functional */
248 if (rtc_time == 0x01000000) {
249 ssleep(1);
250 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
251 if (rtc_time == 0x01000000) {
252 dev_err(&pdev->dev, "internal RTC not ticking\n");
253 return -ENODEV;
257 pdata->irq = platform_get_irq(pdev, 0);
259 platform_set_drvdata(pdev, pdata);
261 if (pdata->irq >= 0) {
262 device_init_wakeup(&pdev->dev, 1);
263 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
264 &mv_rtc_alarm_ops,
265 THIS_MODULE);
266 } else
267 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
268 &mv_rtc_ops, THIS_MODULE);
269 if (IS_ERR(pdata->rtc))
270 return PTR_ERR(pdata->rtc);
272 if (pdata->irq >= 0) {
273 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
274 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
275 IRQF_DISABLED | IRQF_SHARED,
276 pdev->name, pdata) < 0) {
277 dev_warn(&pdev->dev, "interrupt not available.\n");
278 pdata->irq = -1;
282 return 0;
285 static int __exit mv_rtc_remove(struct platform_device *pdev)
287 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
289 if (pdata->irq >= 0)
290 device_init_wakeup(&pdev->dev, 0);
292 rtc_device_unregister(pdata->rtc);
293 return 0;
296 static struct platform_driver mv_rtc_driver = {
297 .remove = __exit_p(mv_rtc_remove),
298 .driver = {
299 .name = "rtc-mv",
300 .owner = THIS_MODULE,
304 static __init int mv_init(void)
306 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
309 static __exit void mv_exit(void)
311 platform_driver_unregister(&mv_rtc_driver);
314 module_init(mv_init);
315 module_exit(mv_exit);
317 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
318 MODULE_DESCRIPTION("Marvell RTC driver");
319 MODULE_LICENSE("GPL");
320 MODULE_ALIAS("platform:rtc-mv");