aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/class.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/class.c')
-rw-r--r--drivers/rtc/class.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
new file mode 100644
index 000000000000..8533936d50d8
--- /dev/null
+++ b/drivers/rtc/class.c
@@ -0,0 +1,145 @@
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
14#include <linux/module.h>
15#include <linux/rtc.h>
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
18
19static DEFINE_IDR(rtc_idr);
20static DEFINE_MUTEX(idr_lock);
21struct class *rtc_class;
22
23static void rtc_device_release(struct class_device *class_dev)
24{
25 struct rtc_device *rtc = to_rtc_device(class_dev);
26 mutex_lock(&idr_lock);
27 idr_remove(&rtc_idr, rtc->id);
28 mutex_unlock(&idr_lock);
29 kfree(rtc);
30}
31
32/**
33 * rtc_device_register - register w/ RTC class
34 * @dev: the device to register
35 *
36 * rtc_device_unregister() must be called when the class device is no
37 * longer needed.
38 *
39 * Returns the pointer to the new struct class device.
40 */
41struct rtc_device *rtc_device_register(const char *name, struct device *dev,
42 struct rtc_class_ops *ops,
43 struct module *owner)
44{
45 struct rtc_device *rtc;
46 int id, err;
47
48 if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
49 err = -ENOMEM;
50 goto exit;
51 }
52
53
54 mutex_lock(&idr_lock);
55 err = idr_get_new(&rtc_idr, NULL, &id);
56 mutex_unlock(&idr_lock);
57
58 if (err < 0)
59 goto exit;
60
61 id = id & MAX_ID_MASK;
62
63 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
64 if (rtc == NULL) {
65 err = -ENOMEM;
66 goto exit_idr;
67 }
68
69 rtc->id = id;
70 rtc->ops = ops;
71 rtc->owner = owner;
72 rtc->class_dev.dev = dev;
73 rtc->class_dev.class = rtc_class;
74 rtc->class_dev.release = rtc_device_release;
75
76 mutex_init(&rtc->ops_lock);
77 spin_lock_init(&rtc->irq_lock);
78 spin_lock_init(&rtc->irq_task_lock);
79
80 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
81 snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id);
82
83 err = class_device_register(&rtc->class_dev);
84 if (err)
85 goto exit_kfree;
86
87 dev_info(dev, "rtc core: registered %s as %s\n",
88 rtc->name, rtc->class_dev.class_id);
89
90 return rtc;
91
92exit_kfree:
93 kfree(rtc);
94
95exit_idr:
96 idr_remove(&rtc_idr, id);
97
98exit:
99 return ERR_PTR(err);
100}
101EXPORT_SYMBOL_GPL(rtc_device_register);
102
103
104/**
105 * rtc_device_unregister - removes the previously registered RTC class device
106 *
107 * @rtc: the RTC class device to destroy
108 */
109void rtc_device_unregister(struct rtc_device *rtc)
110{
111 mutex_lock(&rtc->ops_lock);
112 rtc->ops = NULL;
113 mutex_unlock(&rtc->ops_lock);
114 class_device_unregister(&rtc->class_dev);
115}
116EXPORT_SYMBOL_GPL(rtc_device_unregister);
117
118int rtc_interface_register(struct class_interface *intf)
119{
120 intf->class = rtc_class;
121 return class_interface_register(intf);
122}
123EXPORT_SYMBOL_GPL(rtc_interface_register);
124
125static int __init rtc_init(void)
126{
127 rtc_class = class_create(THIS_MODULE, "rtc");
128 if (IS_ERR(rtc_class)) {
129 printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
130 return PTR_ERR(rtc_class);
131 }
132 return 0;
133}
134
135static void __exit rtc_exit(void)
136{
137 class_destroy(rtc_class);
138}
139
140module_init(rtc_init);
141module_exit(rtc_exit);
142
143MODULE_AUTHOR("Alessandro Zummo <a.zummo@towerteh.it>");
144MODULE_DESCRIPTION("RTC class support");
145MODULE_LICENSE("GPL");