2 * RTC subsystem, base class
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * class skeleton from drivers/hwmon/hwmon.c
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.
14 #include <linux/module.h>
15 #include <linux/rtc.h>
16 #include <linux/kdev_t.h>
17 #include <linux/idr.h>
19 static DEFINE_IDR(rtc_idr);
20 static DEFINE_MUTEX(idr_lock);
21 struct class *rtc_class;
23 static void rtc_device_release(struct class_device *class_dev)
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);
33 * rtc_device_register - register w/ RTC class
34 * @dev: the device to register
36 * rtc_device_unregister() must be called when the class device is no
39 * Returns the pointer to the new struct class device.
41 struct rtc_device *rtc_device_register(const char *name, struct device *dev,
42 const struct rtc_class_ops *ops,
45 struct rtc_device *rtc;
48 if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
54 mutex_lock(&idr_lock);
55 err = idr_get_new(&rtc_idr, NULL, &id);
56 mutex_unlock(&idr_lock);
61 id = id & MAX_ID_MASK;
63 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
72 rtc->max_user_freq = 64;
73 rtc->class_dev.dev = dev;
74 rtc->class_dev.class = rtc_class;
75 rtc->class_dev.release = rtc_device_release;
77 mutex_init(&rtc->ops_lock);
78 spin_lock_init(&rtc->irq_lock);
79 spin_lock_init(&rtc->irq_task_lock);
81 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
82 snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id);
84 err = class_device_register(&rtc->class_dev);
88 dev_info(dev, "rtc core: registered %s as %s\n",
89 rtc->name, rtc->class_dev.class_id);
97 mutex_lock(&idr_lock);
98 idr_remove(&rtc_idr, id);
99 mutex_unlock(&idr_lock);
102 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
106 EXPORT_SYMBOL_GPL(rtc_device_register);
110 * rtc_device_unregister - removes the previously registered RTC class device
112 * @rtc: the RTC class device to destroy
114 void rtc_device_unregister(struct rtc_device *rtc)
116 mutex_lock(&rtc->ops_lock);
118 mutex_unlock(&rtc->ops_lock);
119 class_device_unregister(&rtc->class_dev);
121 EXPORT_SYMBOL_GPL(rtc_device_unregister);
123 int rtc_interface_register(struct class_interface *intf)
125 intf->class = rtc_class;
126 return class_interface_register(intf);
128 EXPORT_SYMBOL_GPL(rtc_interface_register);
130 static int __init rtc_init(void)
132 rtc_class = class_create(THIS_MODULE, "rtc");
133 if (IS_ERR(rtc_class)) {
134 printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
135 return PTR_ERR(rtc_class);
140 static void __exit rtc_exit(void)
142 class_destroy(rtc_class);
145 subsys_initcall(rtc_init);
146 module_exit(rtc_exit);
148 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
149 MODULE_DESCRIPTION("RTC class support");
150 MODULE_LICENSE("GPL");