mm: account for MAP_SHARED mappings using VM_MAYSHARE and not VM_SHARED in hugetlbfs
[linux-2.6/mini2440.git] / drivers / rtc / rtc-wm8350.c
blob5c5e3aa91385bd7efa94f6ae831c03858e02a4f5
1 /*
2 * Real Time Clock driver for Wolfson Microelectronics WM8350
4 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood
7 * linux@wolfsonmicro.com
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/time.h>
19 #include <linux/rtc.h>
20 #include <linux/bcd.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioctl.h>
23 #include <linux/completion.h>
24 #include <linux/mfd/wm8350/rtc.h>
25 #include <linux/mfd/wm8350/core.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #define WM8350_SET_ALM_RETRIES 5
30 #define WM8350_SET_TIME_RETRIES 5
31 #define WM8350_GET_TIME_RETRIES 5
33 #define to_wm8350_from_rtc_dev(d) container_of(d, struct wm8350, rtc.pdev.dev)
36 * Read current time and date in RTC
38 static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm)
40 struct wm8350 *wm8350 = dev_get_drvdata(dev);
41 u16 time1[4], time2[4];
42 int retries = WM8350_GET_TIME_RETRIES, ret;
45 * Read the time twice and compare.
46 * If time1 == time2, then time is valid else retry.
48 do {
49 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
50 4, time1);
51 if (ret < 0)
52 return ret;
53 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
54 4, time2);
55 if (ret < 0)
56 return ret;
58 if (memcmp(time1, time2, sizeof(time1)) == 0) {
59 tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK;
61 tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK)
62 >> WM8350_RTC_MINS_SHIFT;
64 tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK;
66 tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT)
67 & 0x7) - 1;
69 tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK)
70 >> WM8350_RTC_MTH_SHIFT) - 1;
72 tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK);
74 tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK)
75 >> WM8350_RTC_YHUNDREDS_SHIFT) * 100;
76 tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK;
78 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
79 tm->tm_year);
80 tm->tm_year -= 1900;
82 dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n",
83 retries,
84 time1[0], time1[1], time1[2], time1[3]);
86 return 0;
88 } while (retries--);
90 dev_err(dev, "timed out reading RTC time\n");
91 return -EIO;
95 * Set current time and date in RTC
97 static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm)
99 struct wm8350 *wm8350 = dev_get_drvdata(dev);
100 u16 time[4];
101 u16 rtc_ctrl;
102 int ret, retries = WM8350_SET_TIME_RETRIES;
104 time[0] = tm->tm_sec;
105 time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT;
106 time[1] = tm->tm_hour;
107 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT;
108 time[2] = tm->tm_mday;
109 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT;
110 time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT;
111 time[3] |= (tm->tm_year + 1900) % 100;
113 dev_dbg(dev, "Setting: %04x %04x %04x %04x\n",
114 time[0], time[1], time[2], time[3]);
116 /* Set RTC_SET to stop the clock */
117 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET);
118 if (ret < 0)
119 return ret;
121 /* Wait until confirmation of stopping */
122 do {
123 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
124 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
125 } while (retries-- && !(rtc_ctrl & WM8350_RTC_STS));
127 if (!retries) {
128 dev_err(dev, "timed out on set confirmation\n");
129 return -EIO;
132 /* Write time to RTC */
133 ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time);
134 if (ret < 0)
135 return ret;
137 /* Clear RTC_SET to start the clock */
138 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
139 WM8350_RTC_SET);
140 return ret;
144 * Read alarm time and date in RTC
146 static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
148 struct wm8350 *wm8350 = dev_get_drvdata(dev);
149 struct rtc_time *tm = &alrm->time;
150 u16 time[4];
151 int ret;
153 ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time);
154 if (ret < 0)
155 return ret;
157 tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK;
158 if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK)
159 tm->tm_sec = -1;
161 tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK;
162 if (tm->tm_min == WM8350_RTC_ALMMINS_MASK)
163 tm->tm_min = -1;
164 else
165 tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT;
167 tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK;
168 if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK)
169 tm->tm_hour = -1;
171 tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1;
172 if (tm->tm_wday > 7)
173 tm->tm_wday = -1;
175 tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK;
176 if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK)
177 tm->tm_mon = -1;
178 else
179 tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1;
181 tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK);
182 if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK)
183 tm->tm_mday = -1;
185 tm->tm_year = -1;
187 alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS);
189 return 0;
192 static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350)
194 int retries = WM8350_SET_ALM_RETRIES;
195 u16 rtc_ctrl;
196 int ret;
198 /* Set RTC_SET to stop the clock */
199 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
200 WM8350_RTC_ALMSET);
201 if (ret < 0)
202 return ret;
204 /* Wait until confirmation of stopping */
205 do {
206 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
207 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
208 } while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS));
210 if (!(rtc_ctrl & WM8350_RTC_ALMSTS))
211 return -ETIMEDOUT;
213 return 0;
216 static int wm8350_rtc_start_alarm(struct wm8350 *wm8350)
218 int ret;
219 int retries = WM8350_SET_ALM_RETRIES;
220 u16 rtc_ctrl;
222 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
223 WM8350_RTC_ALMSET);
224 if (ret < 0)
225 return ret;
227 /* Wait until confirmation */
228 do {
229 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
230 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
231 } while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS);
233 if (rtc_ctrl & WM8350_RTC_ALMSTS)
234 return -ETIMEDOUT;
236 return 0;
239 static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
241 struct wm8350 *wm8350 = dev_get_drvdata(dev);
242 struct rtc_time *tm = &alrm->time;
243 u16 time[3];
244 int ret;
246 memset(time, 0, sizeof(time));
248 if (tm->tm_sec != -1)
249 time[0] |= tm->tm_sec;
250 else
251 time[0] |= WM8350_RTC_ALMSECS_MASK;
253 if (tm->tm_min != -1)
254 time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT;
255 else
256 time[0] |= WM8350_RTC_ALMMINS_MASK;
258 if (tm->tm_hour != -1)
259 time[1] |= tm->tm_hour;
260 else
261 time[1] |= WM8350_RTC_ALMHRS_MASK;
263 if (tm->tm_wday != -1)
264 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT;
265 else
266 time[1] |= WM8350_RTC_ALMDAY_MASK;
268 if (tm->tm_mday != -1)
269 time[2] |= tm->tm_mday;
270 else
271 time[2] |= WM8350_RTC_ALMDATE_MASK;
273 if (tm->tm_mon != -1)
274 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT;
275 else
276 time[2] |= WM8350_RTC_ALMMTH_MASK;
278 ret = wm8350_rtc_stop_alarm(wm8350);
279 if (ret < 0)
280 return ret;
282 /* Write time to RTC */
283 ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES,
284 3, time);
285 if (ret < 0)
286 return ret;
288 if (alrm->enabled)
289 ret = wm8350_rtc_start_alarm(wm8350);
291 return ret;
295 * Handle commands from user-space
297 static int wm8350_rtc_ioctl(struct device *dev, unsigned int cmd,
298 unsigned long arg)
300 struct wm8350 *wm8350 = dev_get_drvdata(dev);
302 switch (cmd) {
303 case RTC_AIE_OFF:
304 return wm8350_rtc_stop_alarm(wm8350);
305 case RTC_AIE_ON:
306 return wm8350_rtc_start_alarm(wm8350);
308 case RTC_UIE_OFF:
309 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
310 break;
311 case RTC_UIE_ON:
312 wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_SEC);
313 break;
315 default:
316 return -ENOIOCTLCMD;
319 return 0;
322 static void wm8350_rtc_alarm_handler(struct wm8350 *wm8350, int irq,
323 void *data)
325 struct rtc_device *rtc = wm8350->rtc.rtc;
326 int ret;
328 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
330 /* Make it one shot */
331 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
332 WM8350_RTC_ALMSET);
333 if (ret != 0) {
334 dev_err(&(wm8350->rtc.pdev->dev),
335 "Failed to disable alarm: %d\n", ret);
339 static void wm8350_rtc_update_handler(struct wm8350 *wm8350, int irq,
340 void *data)
342 struct rtc_device *rtc = wm8350->rtc.rtc;
344 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF);
347 static const struct rtc_class_ops wm8350_rtc_ops = {
348 .ioctl = wm8350_rtc_ioctl,
349 .read_time = wm8350_rtc_readtime,
350 .set_time = wm8350_rtc_settime,
351 .read_alarm = wm8350_rtc_readalarm,
352 .set_alarm = wm8350_rtc_setalarm,
355 #ifdef CONFIG_PM
356 static int wm8350_rtc_suspend(struct platform_device *pdev, pm_message_t state)
358 struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
359 int ret = 0;
360 u16 reg;
362 reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
364 if (device_may_wakeup(&wm8350->rtc.pdev->dev) &&
365 reg & WM8350_RTC_ALMSTS) {
366 ret = wm8350_rtc_stop_alarm(wm8350);
367 if (ret != 0)
368 dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n",
369 ret);
372 return ret;
375 static int wm8350_rtc_resume(struct platform_device *pdev)
377 struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
378 int ret;
380 if (wm8350->rtc.alarm_enabled) {
381 ret = wm8350_rtc_start_alarm(wm8350);
382 if (ret != 0)
383 dev_err(&pdev->dev,
384 "Failed to restart RTC alarm: %d\n", ret);
387 return 0;
390 #else
391 #define wm8350_rtc_suspend NULL
392 #define wm8350_rtc_resume NULL
393 #endif
395 static int wm8350_rtc_probe(struct platform_device *pdev)
397 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
398 struct wm8350_rtc *wm_rtc = &wm8350->rtc;
399 int ret = 0;
400 u16 timectl, power5;
402 timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
403 if (timectl & WM8350_RTC_BCD) {
404 dev_err(&pdev->dev, "RTC BCD mode not supported\n");
405 return -EINVAL;
407 if (timectl & WM8350_RTC_12HR) {
408 dev_err(&pdev->dev, "RTC 12 hour mode not supported\n");
409 return -EINVAL;
412 /* enable the RTC if it's not already enabled */
413 power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
414 if (!(power5 & WM8350_RTC_TICK_ENA)) {
415 dev_info(wm8350->dev, "Starting RTC\n");
417 wm8350_reg_unlock(wm8350);
419 ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5,
420 WM8350_RTC_TICK_ENA);
421 if (ret < 0) {
422 dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret);
423 return ret;
426 wm8350_reg_lock(wm8350);
429 if (timectl & WM8350_RTC_STS) {
430 int retries;
432 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
433 WM8350_RTC_SET);
434 if (ret < 0) {
435 dev_err(&pdev->dev, "failed to start: %d\n", ret);
436 return ret;
439 retries = WM8350_SET_TIME_RETRIES;
440 do {
441 timectl = wm8350_reg_read(wm8350,
442 WM8350_RTC_TIME_CONTROL);
443 } while (timectl & WM8350_RTC_STS && retries--);
445 if (retries == 0) {
446 dev_err(&pdev->dev, "failed to start: timeout\n");
447 return -ENODEV;
451 device_init_wakeup(&pdev->dev, 1);
453 wm_rtc->rtc = rtc_device_register("wm8350", &pdev->dev,
454 &wm8350_rtc_ops, THIS_MODULE);
455 if (IS_ERR(wm_rtc->rtc)) {
456 ret = PTR_ERR(wm_rtc->rtc);
457 dev_err(&pdev->dev, "failed to register RTC: %d\n", ret);
458 return ret;
461 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
462 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_PER);
464 wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC,
465 wm8350_rtc_update_handler, NULL);
467 wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM,
468 wm8350_rtc_alarm_handler, NULL);
469 wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_ALM);
471 return 0;
474 static int __devexit wm8350_rtc_remove(struct platform_device *pdev)
476 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
477 struct wm8350_rtc *wm_rtc = &wm8350->rtc;
479 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
481 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC);
482 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM);
484 rtc_device_unregister(wm_rtc->rtc);
486 return 0;
489 static struct platform_driver wm8350_rtc_driver = {
490 .probe = wm8350_rtc_probe,
491 .remove = __devexit_p(wm8350_rtc_remove),
492 .suspend = wm8350_rtc_suspend,
493 .resume = wm8350_rtc_resume,
494 .driver = {
495 .name = "wm8350-rtc",
499 static int __init wm8350_rtc_init(void)
501 return platform_driver_register(&wm8350_rtc_driver);
503 module_init(wm8350_rtc_init);
505 static void __exit wm8350_rtc_exit(void)
507 platform_driver_unregister(&wm8350_rtc_driver);
509 module_exit(wm8350_rtc_exit);
511 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
512 MODULE_DESCRIPTION("RTC driver for the WM8350");
513 MODULE_LICENSE("GPL");
514 MODULE_ALIAS("platform:wm8350-rtc");