Skip to content

Commit

Permalink
[PATCH] RTC subsystem: class
Browse files Browse the repository at this point in the history
Add the basic RTC subsystem infrastructure to the kernel.

rtc/class.c - registration facilities for RTC drivers
rtc/interface.c - kernel/rtc interface functions
rtc/hctosys.c - snippet of code that copies hw clock to sw clock
		at bootup, if configured to do so.

Signed-off-by: Alessandro Zummo <[email protected]>
Acked-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Alessandro Zummo authored and Linus Torvalds committed Mar 27, 2006
1 parent 4079c39 commit 0c86edc
Show file tree
Hide file tree
Showing 8 changed files with 633 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -3741,10 +3741,11 @@ D: Mylex DAC960 PCI RAID driver
D: Miscellaneous kernel fixes

N: Alessandro Zummo
E: [email protected]
W: http://freepage.logicom.it/azummo/
E: [email protected]
D: CMI8330 support is sb_card.c
D: ISAPnP fixes in sb_card.c
D: ZyXEL omni.net lcd plus driver
D: RTC subsystem
S: Italy

N: Marc Zyngier
Expand Down
6 changes: 6 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,12 @@ M: [email protected]
L: [email protected]
S: Maintained

REAL TIME CLOCK (RTC) SUBSYSTEM
P: Alessandro Zummo
M: [email protected]
L: [email protected]
S: Maintained

REISERFS FILE SYSTEM
P: Hans Reiser
M: [email protected]
Expand Down
44 changes: 42 additions & 2 deletions drivers/rtc/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
#
\#
# RTC class/drivers configuration
#

menu "Real Time Clock"

config RTC_LIB
tristate
tristate

config RTC_CLASS
tristate "RTC class"
depends on EXPERIMENTAL
default n
select RTC_LIB
help
Generic RTC class support. If you say yes here, you will
be allowed to plug one or more RTCs to your system. You will
probably want to enable one of more of the interfaces below.

This driver can also be built as a module. If so, the module
will be called rtc-class.

config RTC_HCTOSYS
bool "Set system time from RTC on startup"
depends on RTC_CLASS = y
default y
help
If you say yes here, the system time will be set using
the value read from the specified RTC device. This is useful
in order to avoid unnecessary fschk runs.

config RTC_HCTOSYS_DEVICE
string "The RTC to read the time from"
depends on RTC_HCTOSYS = y
default "rtc0"
help
The RTC device that will be used as the source for
the system time, usually rtc0.

comment "RTC interfaces"
depends on RTC_CLASS

comment "RTC drivers"
depends on RTC_CLASS

endmenu
5 changes: 4 additions & 1 deletion drivers/rtc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
# Makefile for RTC class/drivers.
#

obj-$(CONFIG_RTC_LIB) += rtc-lib.o
obj-$(CONFIG_RTC_LIB) += rtc-lib.o
obj-$(CONFIG_RTC_HCTOSYS) += hctosys.o
obj-$(CONFIG_RTC_CLASS) += rtc-core.o
rtc-core-y := class.o interface.o
145 changes: 145 additions & 0 deletions drivers/rtc/class.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* RTC subsystem, base class
*
* Copyright (C) 2005 Tower Technologies
* Author: Alessandro Zummo <[email protected]>
*
* class skeleton from drivers/hwmon/hwmon.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#include <linux/module.h>
#include <linux/rtc.h>
#include <linux/kdev_t.h>
#include <linux/idr.h>

static DEFINE_IDR(rtc_idr);
static DEFINE_MUTEX(idr_lock);
struct class *rtc_class;

static void rtc_device_release(struct class_device *class_dev)
{
struct rtc_device *rtc = to_rtc_device(class_dev);
mutex_lock(&idr_lock);
idr_remove(&rtc_idr, rtc->id);
mutex_unlock(&idr_lock);
kfree(rtc);
}

/**
* rtc_device_register - register w/ RTC class
* @dev: the device to register
*
* rtc_device_unregister() must be called when the class device is no
* longer needed.
*
* Returns the pointer to the new struct class device.
*/
struct rtc_device *rtc_device_register(const char *name, struct device *dev,
struct rtc_class_ops *ops,
struct module *owner)
{
struct rtc_device *rtc;
int id, err;

if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
err = -ENOMEM;
goto exit;
}


mutex_lock(&idr_lock);
err = idr_get_new(&rtc_idr, NULL, &id);
mutex_unlock(&idr_lock);

if (err < 0)
goto exit;

id = id & MAX_ID_MASK;

rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
if (rtc == NULL) {
err = -ENOMEM;
goto exit_idr;
}

rtc->id = id;
rtc->ops = ops;
rtc->owner = owner;
rtc->class_dev.dev = dev;
rtc->class_dev.class = rtc_class;
rtc->class_dev.release = rtc_device_release;

mutex_init(&rtc->ops_lock);
spin_lock_init(&rtc->irq_lock);
spin_lock_init(&rtc->irq_task_lock);

strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id);

err = class_device_register(&rtc->class_dev);
if (err)
goto exit_kfree;

dev_info(dev, "rtc core: registered %s as %s\n",
rtc->name, rtc->class_dev.class_id);

return rtc;

exit_kfree:
kfree(rtc);

exit_idr:
idr_remove(&rtc_idr, id);

exit:
return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(rtc_device_register);


/**
* rtc_device_unregister - removes the previously registered RTC class device
*
* @rtc: the RTC class device to destroy
*/
void rtc_device_unregister(struct rtc_device *rtc)
{
mutex_lock(&rtc->ops_lock);
rtc->ops = NULL;
mutex_unlock(&rtc->ops_lock);
class_device_unregister(&rtc->class_dev);
}
EXPORT_SYMBOL_GPL(rtc_device_unregister);

int rtc_interface_register(struct class_interface *intf)
{
intf->class = rtc_class;
return class_interface_register(intf);
}
EXPORT_SYMBOL_GPL(rtc_interface_register);

static int __init rtc_init(void)
{
rtc_class = class_create(THIS_MODULE, "rtc");
if (IS_ERR(rtc_class)) {
printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
return PTR_ERR(rtc_class);
}
return 0;
}

static void __exit rtc_exit(void)
{
class_destroy(rtc_class);
}

module_init(rtc_init);
module_exit(rtc_exit);

MODULE_AUTHOR("Alessandro Zummo <[email protected]>");
MODULE_DESCRIPTION("RTC class support");
MODULE_LICENSE("GPL");
69 changes: 69 additions & 0 deletions drivers/rtc/hctosys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* RTC subsystem, initialize system time on startup
*
* Copyright (C) 2005 Tower Technologies
* Author: Alessandro Zummo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#include <linux/rtc.h>

/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
* whether it stores the most close value or the value with partial
* seconds truncated. However, it is important that we use it to store
* the truncated value. This is because otherwise it is necessary,
* in an rtc sync function, to read both xtime.tv_sec and
* xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
* of >32bits is not possible. So storing the most close value would
* slow down the sync API. So here we have the truncated value and
* the best guess is to add 0.5s.
*/

static int __init rtc_hctosys(void)
{
int err;
struct rtc_time tm;
struct class_device *class_dev = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);

if (class_dev == NULL) {
printk("%s: unable to open rtc device (%s)\n",
__FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
return -ENODEV;
}

err = rtc_read_time(class_dev, &tm);
if (err == 0) {
err = rtc_valid_tm(&tm);
if (err == 0) {
struct timespec tv;

tv.tv_nsec = NSEC_PER_SEC >> 1;

rtc_tm_to_time(&tm, &tv.tv_sec);

do_settimeofday(&tv);

dev_info(class_dev->dev,
"setting the system clock to "
"%d-%02d-%02d %02d:%02d:%02d (%u)\n",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
(unsigned int) tv.tv_sec);
}
else
dev_err(class_dev->dev,
"hctosys: invalid date/time\n");
}
else
dev_err(class_dev->dev,
"hctosys: unable to read the hardware clock\n");

rtc_class_close(class_dev);

return 0;
}

late_initcall(rtc_hctosys);
Loading

0 comments on commit 0c86edc

Please sign in to comment.