Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * RTC subsystem, base class |
| 3 | * |
| 4 | * Copyright (C) 2005 Tower Technologies |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 6 | * |
| 7 | * class skeleton from drivers/hwmon/hwmon.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Jingoo Han | c100a5e | 2013-02-21 16:45:23 -0800 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/kdev_t.h> |
| 19 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 21 | #include <linux/workqueue.h> |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 22 | |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 23 | #include "rtc-core.h" |
| 24 | |
| 25 | |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 26 | static DEFINE_IDA(rtc_ida); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 27 | struct class *rtc_class; |
| 28 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 29 | static void rtc_device_release(struct device *dev) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 30 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 31 | struct rtc_device *rtc = to_rtc_device(dev); |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 32 | ida_simple_remove(&rtc_ida, rtc->id); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 33 | kfree(rtc); |
| 34 | } |
| 35 | |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
| 37 | /* Result of the last RTC to system clock attempt. */ |
| 38 | int rtc_hctosys_ret = -ENODEV; |
| 39 | #endif |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 40 | |
Shuah Khan | 92e7f04 | 2013-07-03 15:07:59 -0700 | [diff] [blame] | 41 | #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 42 | /* |
| 43 | * On suspend(), measure the delta between one RTC and the |
| 44 | * system's wall clock; restore it on resume(). |
| 45 | */ |
| 46 | |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 47 | static struct timespec old_rtc, old_system, old_delta; |
| 48 | |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 49 | |
Shuah Khan | 92e7f04 | 2013-07-03 15:07:59 -0700 | [diff] [blame] | 50 | static int rtc_suspend(struct device *dev) |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 51 | { |
| 52 | struct rtc_device *rtc = to_rtc_device(dev); |
| 53 | struct rtc_time tm; |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 54 | struct timespec delta, delta_delta; |
Feng Tang | 9ecf37e | 2013-01-16 00:09:48 +0800 | [diff] [blame] | 55 | |
| 56 | if (has_persistent_clock()) |
| 57 | return 0; |
| 58 | |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 59 | if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 62 | /* snapshot the current RTC and system time at suspend*/ |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 63 | rtc_read_time(rtc, &tm); |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 64 | getnstimeofday(&old_system); |
| 65 | rtc_tm_to_time(&tm, &old_rtc.tv_sec); |
| 66 | |
| 67 | |
| 68 | /* |
| 69 | * To avoid drift caused by repeated suspend/resumes, |
| 70 | * which each can add ~1 second drift error, |
| 71 | * try to compensate so the difference in system time |
| 72 | * and rtc time stays close to constant. |
| 73 | */ |
| 74 | delta = timespec_sub(old_system, old_rtc); |
| 75 | delta_delta = timespec_sub(delta, old_delta); |
Arve Hjønnevåg | 6a8943d | 2011-11-22 18:24:51 -0800 | [diff] [blame] | 76 | if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 77 | /* |
| 78 | * if delta_delta is too large, assume time correction |
| 79 | * has occured and set old_delta to the current delta. |
| 80 | */ |
| 81 | old_delta = delta; |
| 82 | } else { |
| 83 | /* Otherwise try to adjust old_system to compensate */ |
| 84 | old_system = timespec_sub(old_system, delta_delta); |
| 85 | } |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 86 | |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int rtc_resume(struct device *dev) |
| 91 | { |
| 92 | struct rtc_device *rtc = to_rtc_device(dev); |
| 93 | struct rtc_time tm; |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 94 | struct timespec new_system, new_rtc; |
| 95 | struct timespec sleep_time; |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 96 | |
Feng Tang | 9ecf37e | 2013-01-16 00:09:48 +0800 | [diff] [blame] | 97 | if (has_persistent_clock()) |
| 98 | return 0; |
| 99 | |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 100 | rtc_hctosys_ret = -ENODEV; |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 101 | if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 104 | /* snapshot the current rtc and system time at resume */ |
| 105 | getnstimeofday(&new_system); |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 106 | rtc_read_time(rtc, &tm); |
| 107 | if (rtc_valid_tm(&tm) != 0) { |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 108 | pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev)); |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 111 | rtc_tm_to_time(&tm, &new_rtc.tv_sec); |
| 112 | new_rtc.tv_nsec = 0; |
| 113 | |
Arve Hjønnevåg | 6a8943d | 2011-11-22 18:24:51 -0800 | [diff] [blame] | 114 | if (new_rtc.tv_sec < old_rtc.tv_sec) { |
| 115 | pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 119 | /* calculate the RTC time delta (sleep time)*/ |
| 120 | sleep_time = timespec_sub(new_rtc, old_rtc); |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 121 | |
John Stultz | 3dcad5f | 2011-05-27 11:33:18 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Since these RTC suspend/resume handlers are not called |
| 124 | * at the very end of suspend or the start of resume, |
| 125 | * some run-time may pass on either sides of the sleep time |
| 126 | * so subtract kernel run-time between rtc_suspend to rtc_resume |
| 127 | * to keep things accurate. |
| 128 | */ |
| 129 | sleep_time = timespec_sub(sleep_time, |
| 130 | timespec_sub(new_system, old_system)); |
| 131 | |
Arve Hjønnevåg | 6a8943d | 2011-11-22 18:24:51 -0800 | [diff] [blame] | 132 | if (sleep_time.tv_sec >= 0) |
| 133 | timekeeping_inject_sleeptime(&sleep_time); |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 134 | rtc_hctosys_ret = 0; |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Shuah Khan | 92e7f04 | 2013-07-03 15:07:59 -0700 | [diff] [blame] | 138 | static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume); |
| 139 | #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops) |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 140 | #else |
Shuah Khan | 92e7f04 | 2013-07-03 15:07:59 -0700 | [diff] [blame] | 141 | #define RTC_CLASS_DEV_PM_OPS NULL |
David Brownell | 7ca1d48 | 2007-05-08 00:33:42 -0700 | [diff] [blame] | 142 | #endif |
| 143 | |
| 144 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 145 | /** |
| 146 | * rtc_device_register - register w/ RTC class |
| 147 | * @dev: the device to register |
| 148 | * |
| 149 | * rtc_device_unregister() must be called when the class device is no |
| 150 | * longer needed. |
| 151 | * |
| 152 | * Returns the pointer to the new struct class device. |
| 153 | */ |
| 154 | struct rtc_device *rtc_device_register(const char *name, struct device *dev, |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 155 | const struct rtc_class_ops *ops, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 156 | struct module *owner) |
| 157 | { |
| 158 | struct rtc_device *rtc; |
John Stultz | f44f7f9 | 2011-02-21 22:58:51 -0800 | [diff] [blame] | 159 | struct rtc_wkalrm alrm; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 160 | int id, err; |
| 161 | |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 162 | id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL); |
| 163 | if (id < 0) { |
| 164 | err = id; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 165 | goto exit; |
| 166 | } |
| 167 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 168 | rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL); |
| 169 | if (rtc == NULL) { |
| 170 | err = -ENOMEM; |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 171 | goto exit_ida; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | rtc->id = id; |
| 175 | rtc->ops = ops; |
| 176 | rtc->owner = owner; |
Marcelo Roberto Jimenez | 83a06bf | 2011-02-02 16:04:02 -0200 | [diff] [blame] | 177 | rtc->irq_freq = 1; |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 178 | rtc->max_user_freq = 64; |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 179 | rtc->dev.parent = dev; |
| 180 | rtc->dev.class = rtc_class; |
| 181 | rtc->dev.release = rtc_device_release; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 182 | |
| 183 | mutex_init(&rtc->ops_lock); |
| 184 | spin_lock_init(&rtc->irq_lock); |
| 185 | spin_lock_init(&rtc->irq_task_lock); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 186 | init_waitqueue_head(&rtc->irq_queue); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 187 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 188 | /* Init timerqueue */ |
| 189 | timerqueue_init_head(&rtc->timerqueue); |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame] | 190 | INIT_WORK(&rtc->irqwork, rtc_timer_do_work); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 191 | /* Init aie timer */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame] | 192 | rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 193 | /* Init uie timer */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame] | 194 | rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 195 | /* Init pie timer */ |
| 196 | hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 197 | rtc->pie_timer.function = rtc_pie_update_irq; |
| 198 | rtc->pie_enabled = 0; |
| 199 | |
John Stultz | f44f7f9 | 2011-02-21 22:58:51 -0800 | [diff] [blame] | 200 | /* Check to see if there is an ALARM already set in hw */ |
| 201 | err = __rtc_read_alarm(rtc, &alrm); |
| 202 | |
| 203 | if (!err && !rtc_valid_tm(&alrm.time)) |
John Stultz | f6d5b33 | 2011-03-29 18:00:27 -0700 | [diff] [blame] | 204 | rtc_initialize_alarm(rtc, &alrm); |
John Stultz | f44f7f9 | 2011-02-21 22:58:51 -0800 | [diff] [blame] | 205 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 206 | strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE); |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 207 | dev_set_name(&rtc->dev, "rtc%d", id); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 208 | |
David Brownell | cb3a58d | 2007-05-08 00:33:46 -0700 | [diff] [blame] | 209 | rtc_dev_prepare(rtc); |
| 210 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 211 | err = device_register(&rtc->dev); |
Vasiliy Kulikov | 59cca86 | 2010-10-27 15:33:04 -0700 | [diff] [blame] | 212 | if (err) { |
| 213 | put_device(&rtc->dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 214 | goto exit_kfree; |
Vasiliy Kulikov | 59cca86 | 2010-10-27 15:33:04 -0700 | [diff] [blame] | 215 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 216 | |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 217 | rtc_dev_add_device(rtc); |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 218 | rtc_sysfs_add_device(rtc); |
David Brownell | 7d9f99e | 2007-05-08 00:33:38 -0700 | [diff] [blame] | 219 | rtc_proc_add_device(rtc); |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 220 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 221 | dev_info(dev, "rtc core: registered %s as %s\n", |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 222 | rtc->name, dev_name(&rtc->dev)); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 223 | |
| 224 | return rtc; |
| 225 | |
| 226 | exit_kfree: |
| 227 | kfree(rtc); |
| 228 | |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 229 | exit_ida: |
| 230 | ida_simple_remove(&rtc_ida, id); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 231 | |
| 232 | exit: |
Alessandro Zummo | d1d65b7 | 2006-04-10 22:54:45 -0700 | [diff] [blame] | 233 | dev_err(dev, "rtc core: unable to register %s, err = %d\n", |
| 234 | name, err); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 235 | return ERR_PTR(err); |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(rtc_device_register); |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * rtc_device_unregister - removes the previously registered RTC class device |
| 242 | * |
| 243 | * @rtc: the RTC class device to destroy |
| 244 | */ |
| 245 | void rtc_device_unregister(struct rtc_device *rtc) |
| 246 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 247 | if (get_device(&rtc->dev) != NULL) { |
David Brownell | e109ebd | 2007-02-28 20:12:40 -0800 | [diff] [blame] | 248 | mutex_lock(&rtc->ops_lock); |
| 249 | /* remove innards of this RTC, then disable it, before |
| 250 | * letting any rtc_class_open() users access it again |
| 251 | */ |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 252 | rtc_sysfs_del_device(rtc); |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 253 | rtc_dev_del_device(rtc); |
David Brownell | 7d9f99e | 2007-05-08 00:33:38 -0700 | [diff] [blame] | 254 | rtc_proc_del_device(rtc); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 255 | device_unregister(&rtc->dev); |
David Brownell | e109ebd | 2007-02-28 20:12:40 -0800 | [diff] [blame] | 256 | rtc->ops = NULL; |
| 257 | mutex_unlock(&rtc->ops_lock); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 258 | put_device(&rtc->dev); |
David Brownell | e109ebd | 2007-02-28 20:12:40 -0800 | [diff] [blame] | 259 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 260 | } |
| 261 | EXPORT_SYMBOL_GPL(rtc_device_unregister); |
| 262 | |
Jingoo Han | 3e217b6 | 2013-04-29 16:18:27 -0700 | [diff] [blame] | 263 | static void devm_rtc_device_release(struct device *dev, void *res) |
| 264 | { |
| 265 | struct rtc_device *rtc = *(struct rtc_device **)res; |
| 266 | |
| 267 | rtc_device_unregister(rtc); |
| 268 | } |
| 269 | |
| 270 | static int devm_rtc_device_match(struct device *dev, void *res, void *data) |
| 271 | { |
| 272 | struct rtc **r = res; |
| 273 | |
| 274 | return *r == data; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * devm_rtc_device_register - resource managed rtc_device_register() |
Jingoo Han | 3e217b6 | 2013-04-29 16:18:27 -0700 | [diff] [blame] | 279 | * @dev: the device to register |
Jingoo Han | 6636a99 | 2013-04-29 16:18:33 -0700 | [diff] [blame] | 280 | * @name: the name of the device |
Jingoo Han | 3e217b6 | 2013-04-29 16:18:27 -0700 | [diff] [blame] | 281 | * @ops: the rtc operations structure |
| 282 | * @owner: the module owner |
| 283 | * |
| 284 | * @return a struct rtc on success, or an ERR_PTR on error |
| 285 | * |
| 286 | * Managed rtc_device_register(). The rtc_device returned from this function |
| 287 | * are automatically freed on driver detach. See rtc_device_register() |
| 288 | * for more information. |
| 289 | */ |
| 290 | |
Jingoo Han | 6636a99 | 2013-04-29 16:18:33 -0700 | [diff] [blame] | 291 | struct rtc_device *devm_rtc_device_register(struct device *dev, |
| 292 | const char *name, |
Jingoo Han | 3e217b6 | 2013-04-29 16:18:27 -0700 | [diff] [blame] | 293 | const struct rtc_class_ops *ops, |
| 294 | struct module *owner) |
| 295 | { |
| 296 | struct rtc_device **ptr, *rtc; |
| 297 | |
| 298 | ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL); |
| 299 | if (!ptr) |
| 300 | return ERR_PTR(-ENOMEM); |
| 301 | |
| 302 | rtc = rtc_device_register(name, dev, ops, owner); |
| 303 | if (!IS_ERR(rtc)) { |
| 304 | *ptr = rtc; |
| 305 | devres_add(dev, ptr); |
| 306 | } else { |
| 307 | devres_free(ptr); |
| 308 | } |
| 309 | |
| 310 | return rtc; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(devm_rtc_device_register); |
| 313 | |
| 314 | /** |
| 315 | * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister() |
| 316 | * @dev: the device to unregister |
| 317 | * @rtc: the RTC class device to unregister |
| 318 | * |
| 319 | * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this |
| 320 | * function will not need to be called and the resource management code will |
| 321 | * ensure that the resource is freed. |
| 322 | */ |
| 323 | void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc) |
| 324 | { |
| 325 | int rc; |
| 326 | |
| 327 | rc = devres_release(dev, devm_rtc_device_release, |
| 328 | devm_rtc_device_match, rtc); |
| 329 | WARN_ON(rc); |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(devm_rtc_device_unregister); |
| 332 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 333 | static int __init rtc_init(void) |
| 334 | { |
| 335 | rtc_class = class_create(THIS_MODULE, "rtc"); |
| 336 | if (IS_ERR(rtc_class)) { |
Jingoo Han | c100a5e | 2013-02-21 16:45:23 -0800 | [diff] [blame] | 337 | pr_err("couldn't create class\n"); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 338 | return PTR_ERR(rtc_class); |
| 339 | } |
Shuah Khan | 92e7f04 | 2013-07-03 15:07:59 -0700 | [diff] [blame] | 340 | rtc_class->pm = RTC_CLASS_DEV_PM_OPS; |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 341 | rtc_dev_init(); |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 342 | rtc_sysfs_init(rtc_class); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static void __exit rtc_exit(void) |
| 347 | { |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 348 | rtc_dev_exit(); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 349 | class_destroy(rtc_class); |
Jonathan Cameron | 6d03d06 | 2011-11-02 13:37:49 -0700 | [diff] [blame] | 350 | ida_destroy(&rtc_ida); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 351 | } |
| 352 | |
David Brownell | 818a867 | 2006-09-30 23:28:15 -0700 | [diff] [blame] | 353 | subsys_initcall(rtc_init); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 354 | module_exit(rtc_exit); |
| 355 | |
David Brownell | 818a867 | 2006-09-30 23:28:15 -0700 | [diff] [blame] | 356 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 357 | MODULE_DESCRIPTION("RTC class support"); |
| 358 | MODULE_LICENSE("GPL"); |