diff options
Diffstat (limited to 'drivers/rtc')
| -rw-r--r-- | drivers/rtc/Kconfig | 165 | ||||
| -rw-r--r-- | drivers/rtc/Makefile | 21 | ||||
| -rw-r--r-- | drivers/rtc/class.c | 145 | ||||
| -rw-r--r-- | drivers/rtc/hctosys.c | 69 | ||||
| -rw-r--r-- | drivers/rtc/interface.c | 277 | ||||
| -rw-r--r-- | drivers/rtc/rtc-dev.c | 382 | ||||
| -rw-r--r-- | drivers/rtc/rtc-ds1672.c | 233 | ||||
| -rw-r--r-- | drivers/rtc/rtc-ep93xx.c | 162 | ||||
| -rw-r--r-- | drivers/rtc/rtc-lib.c | 101 | ||||
| -rw-r--r-- | drivers/rtc/rtc-m48t86.c | 209 | ||||
| -rw-r--r-- | drivers/rtc/rtc-pcf8563.c | 353 | ||||
| -rw-r--r-- | drivers/rtc/rtc-proc.c | 162 | ||||
| -rw-r--r-- | drivers/rtc/rtc-rs5c372.c | 294 | ||||
| -rw-r--r-- | drivers/rtc/rtc-sa1100.c | 388 | ||||
| -rw-r--r-- | drivers/rtc/rtc-sysfs.c | 124 | ||||
| -rw-r--r-- | drivers/rtc/rtc-test.c | 204 | ||||
| -rw-r--r-- | drivers/rtc/rtc-x1205.c | 619 |
17 files changed, 3908 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig new file mode 100644 index 000000000000..929dd8090578 --- /dev/null +++ b/drivers/rtc/Kconfig | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | \# | ||
| 2 | # RTC class/drivers configuration | ||
| 3 | # | ||
| 4 | |||
| 5 | menu "Real Time Clock" | ||
| 6 | |||
| 7 | config RTC_LIB | ||
| 8 | tristate | ||
| 9 | |||
| 10 | config RTC_CLASS | ||
| 11 | tristate "RTC class" | ||
| 12 | depends on EXPERIMENTAL | ||
| 13 | default n | ||
| 14 | select RTC_LIB | ||
| 15 | help | ||
| 16 | Generic RTC class support. If you say yes here, you will | ||
| 17 | be allowed to plug one or more RTCs to your system. You will | ||
| 18 | probably want to enable one of more of the interfaces below. | ||
| 19 | |||
| 20 | This driver can also be built as a module. If so, the module | ||
| 21 | will be called rtc-class. | ||
| 22 | |||
| 23 | config RTC_HCTOSYS | ||
| 24 | bool "Set system time from RTC on startup" | ||
| 25 | depends on RTC_CLASS = y | ||
| 26 | default y | ||
| 27 | help | ||
| 28 | If you say yes here, the system time will be set using | ||
| 29 | the value read from the specified RTC device. This is useful | ||
| 30 | in order to avoid unnecessary fschk runs. | ||
| 31 | |||
| 32 | config RTC_HCTOSYS_DEVICE | ||
| 33 | string "The RTC to read the time from" | ||
| 34 | depends on RTC_HCTOSYS = y | ||
| 35 | default "rtc0" | ||
| 36 | help | ||
| 37 | The RTC device that will be used as the source for | ||
| 38 | the system time, usually rtc0. | ||
| 39 | |||
| 40 | comment "RTC interfaces" | ||
| 41 | depends on RTC_CLASS | ||
| 42 | |||
| 43 | config RTC_INTF_SYSFS | ||
| 44 | tristate "sysfs" | ||
| 45 | depends on RTC_CLASS && SYSFS | ||
| 46 | default RTC_CLASS | ||
| 47 | help | ||
| 48 | Say yes here if you want to use your RTC using the sysfs | ||
| 49 | interface, /sys/class/rtc/rtcX . | ||
| 50 | |||
| 51 | This driver can also be built as a module. If so, the module | ||
| 52 | will be called rtc-sysfs. | ||
| 53 | |||
| 54 | config RTC_INTF_PROC | ||
| 55 | tristate "proc" | ||
| 56 | depends on RTC_CLASS && PROC_FS | ||
| 57 | default RTC_CLASS | ||
| 58 | help | ||
| 59 | Say yes here if you want to use your RTC using the proc | ||
| 60 | interface, /proc/driver/rtc . | ||
| 61 | |||
| 62 | This driver can also be built as a module. If so, the module | ||
| 63 | will be called rtc-proc. | ||
| 64 | |||
| 65 | config RTC_INTF_DEV | ||
| 66 | tristate "dev" | ||
| 67 | depends on RTC_CLASS | ||
| 68 | default RTC_CLASS | ||
| 69 | help | ||
| 70 | Say yes here if you want to use your RTC using the dev | ||
| 71 | interface, /dev/rtc . | ||
| 72 | |||
| 73 | This driver can also be built as a module. If so, the module | ||
| 74 | will be called rtc-dev. | ||
| 75 | |||
| 76 | comment "RTC drivers" | ||
| 77 | depends on RTC_CLASS | ||
| 78 | |||
| 79 | config RTC_DRV_X1205 | ||
| 80 | tristate "Xicor/Intersil X1205" | ||
| 81 | depends on RTC_CLASS && I2C | ||
| 82 | help | ||
| 83 | If you say yes here you get support for the | ||
| 84 | Xicor/Intersil X1205 RTC chip. | ||
| 85 | |||
| 86 | This driver can also be built as a module. If so, the module | ||
| 87 | will be called rtc-x1205. | ||
| 88 | |||
| 89 | config RTC_DRV_DS1672 | ||
| 90 | tristate "Dallas/Maxim DS1672" | ||
| 91 | depends on RTC_CLASS && I2C | ||
| 92 | help | ||
| 93 | If you say yes here you get support for the | ||
| 94 | Dallas/Maxim DS1672 timekeeping chip. | ||
| 95 | |||
| 96 | This driver can also be built as a module. If so, the module | ||
| 97 | will be called rtc-ds1672. | ||
| 98 | |||
| 99 | config RTC_DRV_PCF8563 | ||
| 100 | tristate "Philips PCF8563/Epson RTC8564" | ||
| 101 | depends on RTC_CLASS && I2C | ||
| 102 | help | ||
| 103 | If you say yes here you get support for the | ||
| 104 | Philips PCF8563 RTC chip. The Epson RTC8564 | ||
| 105 | should work as well. | ||
| 106 | |||
| 107 | This driver can also be built as a module. If so, the module | ||
| 108 | will be called rtc-pcf8563. | ||
| 109 | |||
| 110 | config RTC_DRV_RS5C372 | ||
| 111 | tristate "Ricoh RS5C372A/B" | ||
| 112 | depends on RTC_CLASS && I2C | ||
| 113 | help | ||
| 114 | If you say yes here you get support for the | ||
| 115 | Ricoh RS5C372A and RS5C372B RTC chips. | ||
| 116 | |||
| 117 | This driver can also be built as a module. If so, the module | ||
| 118 | will be called rtc-rs5c372. | ||
| 119 | |||
| 120 | config RTC_DRV_M48T86 | ||
| 121 | tristate "ST M48T86/Dallas DS12887" | ||
| 122 | depends on RTC_CLASS | ||
| 123 | help | ||
| 124 | If you say Y here you will get support for the | ||
| 125 | ST M48T86 and Dallas DS12887 RTC chips. | ||
| 126 | |||
| 127 | This driver can also be built as a module. If so, the module | ||
| 128 | will be called rtc-m48t86. | ||
| 129 | |||
| 130 | config RTC_DRV_EP93XX | ||
| 131 | tristate "Cirrus Logic EP93XX" | ||
| 132 | depends on RTC_CLASS && ARCH_EP93XX | ||
| 133 | help | ||
| 134 | If you say yes here you get support for the | ||
| 135 | RTC embedded in the Cirrus Logic EP93XX processors. | ||
| 136 | |||
| 137 | This driver can also be built as a module. If so, the module | ||
| 138 | will be called rtc-ep93xx. | ||
| 139 | |||
| 140 | config RTC_DRV_SA1100 | ||
| 141 | tristate "SA11x0/PXA2xx" | ||
| 142 | depends on RTC_CLASS && (ARCH_SA1100 || ARCH_PXA) | ||
| 143 | help | ||
| 144 | If you say Y here you will get access to the real time clock | ||
| 145 | built into your SA11x0 or PXA2xx CPU. | ||
| 146 | |||
| 147 | To compile this driver as a module, choose M here: the | ||
| 148 | module will be called rtc-sa1100. | ||
| 149 | |||
| 150 | config RTC_DRV_TEST | ||
| 151 | tristate "Test driver/device" | ||
| 152 | depends on RTC_CLASS | ||
| 153 | help | ||
| 154 | If you say yes here you get support for the | ||
| 155 | RTC test driver. It's a software RTC which can be | ||
| 156 | used to test the RTC subsystem APIs. It gets | ||
| 157 | the time from the system clock. | ||
| 158 | You want this driver only if you are doing development | ||
| 159 | on the RTC subsystem. Please read the source code | ||
| 160 | for further details. | ||
| 161 | |||
| 162 | This driver can also be built as a module. If so, the module | ||
| 163 | will be called rtc-test. | ||
| 164 | |||
| 165 | endmenu | ||
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile new file mode 100644 index 000000000000..8d4c7fe88d58 --- /dev/null +++ b/drivers/rtc/Makefile | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | # | ||
| 2 | # Makefile for RTC class/drivers. | ||
| 3 | # | ||
| 4 | |||
| 5 | obj-$(CONFIG_RTC_LIB) += rtc-lib.o | ||
| 6 | obj-$(CONFIG_RTC_HCTOSYS) += hctosys.o | ||
| 7 | obj-$(CONFIG_RTC_CLASS) += rtc-core.o | ||
| 8 | rtc-core-y := class.o interface.o | ||
| 9 | |||
| 10 | obj-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o | ||
| 11 | obj-$(CONFIG_RTC_INTF_PROC) += rtc-proc.o | ||
| 12 | obj-$(CONFIG_RTC_INTF_DEV) += rtc-dev.o | ||
| 13 | |||
| 14 | obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o | ||
| 15 | obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o | ||
| 16 | obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o | ||
| 17 | obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o | ||
| 18 | obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o | ||
| 19 | obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o | ||
| 20 | obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o | ||
| 21 | obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o | ||
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 | |||
| 19 | static DEFINE_IDR(rtc_idr); | ||
| 20 | static DEFINE_MUTEX(idr_lock); | ||
| 21 | struct class *rtc_class; | ||
| 22 | |||
| 23 | static 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 | */ | ||
| 41 | struct 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 | |||
| 92 | exit_kfree: | ||
| 93 | kfree(rtc); | ||
| 94 | |||
| 95 | exit_idr: | ||
| 96 | idr_remove(&rtc_idr, id); | ||
| 97 | |||
| 98 | exit: | ||
| 99 | return ERR_PTR(err); | ||
| 100 | } | ||
| 101 | EXPORT_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 | */ | ||
| 109 | void 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 | } | ||
| 116 | EXPORT_SYMBOL_GPL(rtc_device_unregister); | ||
| 117 | |||
| 118 | int rtc_interface_register(struct class_interface *intf) | ||
| 119 | { | ||
| 120 | intf->class = rtc_class; | ||
| 121 | return class_interface_register(intf); | ||
| 122 | } | ||
| 123 | EXPORT_SYMBOL_GPL(rtc_interface_register); | ||
| 124 | |||
| 125 | static 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 | |||
| 135 | static void __exit rtc_exit(void) | ||
| 136 | { | ||
| 137 | class_destroy(rtc_class); | ||
| 138 | } | ||
| 139 | |||
| 140 | module_init(rtc_init); | ||
| 141 | module_exit(rtc_exit); | ||
| 142 | |||
| 143 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towerteh.it>"); | ||
| 144 | MODULE_DESCRIPTION("RTC class support"); | ||
| 145 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/hctosys.c b/drivers/rtc/hctosys.c new file mode 100644 index 000000000000..d02fe9a0001f --- /dev/null +++ b/drivers/rtc/hctosys.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* | ||
| 2 | * RTC subsystem, initialize system time on startup | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/rtc.h> | ||
| 13 | |||
| 14 | /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary | ||
| 15 | * whether it stores the most close value or the value with partial | ||
| 16 | * seconds truncated. However, it is important that we use it to store | ||
| 17 | * the truncated value. This is because otherwise it is necessary, | ||
| 18 | * in an rtc sync function, to read both xtime.tv_sec and | ||
| 19 | * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read | ||
| 20 | * of >32bits is not possible. So storing the most close value would | ||
| 21 | * slow down the sync API. So here we have the truncated value and | ||
| 22 | * the best guess is to add 0.5s. | ||
| 23 | */ | ||
| 24 | |||
| 25 | static int __init rtc_hctosys(void) | ||
| 26 | { | ||
| 27 | int err; | ||
| 28 | struct rtc_time tm; | ||
| 29 | struct class_device *class_dev = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); | ||
| 30 | |||
| 31 | if (class_dev == NULL) { | ||
| 32 | printk("%s: unable to open rtc device (%s)\n", | ||
| 33 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); | ||
| 34 | return -ENODEV; | ||
| 35 | } | ||
| 36 | |||
| 37 | err = rtc_read_time(class_dev, &tm); | ||
| 38 | if (err == 0) { | ||
| 39 | err = rtc_valid_tm(&tm); | ||
| 40 | if (err == 0) { | ||
| 41 | struct timespec tv; | ||
| 42 | |||
| 43 | tv.tv_nsec = NSEC_PER_SEC >> 1; | ||
| 44 | |||
| 45 | rtc_tm_to_time(&tm, &tv.tv_sec); | ||
| 46 | |||
| 47 | do_settimeofday(&tv); | ||
| 48 | |||
| 49 | dev_info(class_dev->dev, | ||
| 50 | "setting the system clock to " | ||
| 51 | "%d-%02d-%02d %02d:%02d:%02d (%u)\n", | ||
| 52 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | ||
| 53 | tm.tm_hour, tm.tm_min, tm.tm_sec, | ||
| 54 | (unsigned int) tv.tv_sec); | ||
| 55 | } | ||
| 56 | else | ||
| 57 | dev_err(class_dev->dev, | ||
| 58 | "hctosys: invalid date/time\n"); | ||
| 59 | } | ||
| 60 | else | ||
| 61 | dev_err(class_dev->dev, | ||
| 62 | "hctosys: unable to read the hardware clock\n"); | ||
| 63 | |||
| 64 | rtc_class_close(class_dev); | ||
| 65 | |||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | late_initcall(rtc_hctosys); | ||
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c new file mode 100644 index 000000000000..56e490709b87 --- /dev/null +++ b/drivers/rtc/interface.c | |||
| @@ -0,0 +1,277 @@ | |||
| 1 | /* | ||
| 2 | * RTC subsystem, interface functions | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * based on arch/arm/common/rtctime.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/rtc.h> | ||
| 15 | |||
| 16 | int rtc_read_time(struct class_device *class_dev, struct rtc_time *tm) | ||
| 17 | { | ||
| 18 | int err; | ||
| 19 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 20 | |||
| 21 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
| 22 | if (err) | ||
| 23 | return -EBUSY; | ||
| 24 | |||
| 25 | if (!rtc->ops) | ||
| 26 | err = -ENODEV; | ||
| 27 | else if (!rtc->ops->read_time) | ||
| 28 | err = -EINVAL; | ||
| 29 | else { | ||
| 30 | memset(tm, 0, sizeof(struct rtc_time)); | ||
| 31 | err = rtc->ops->read_time(class_dev->dev, tm); | ||
| 32 | } | ||
| 33 | |||
| 34 | mutex_unlock(&rtc->ops_lock); | ||
| 35 | return err; | ||
| 36 | } | ||
| 37 | EXPORT_SYMBOL_GPL(rtc_read_time); | ||
| 38 | |||
| 39 | int rtc_set_time(struct class_device *class_dev, struct rtc_time *tm) | ||
| 40 | { | ||
| 41 | int err; | ||
| 42 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 43 | |||
| 44 | err = rtc_valid_tm(tm); | ||
| 45 | if (err != 0) | ||
| 46 | return err; | ||
| 47 | |||
| 48 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
| 49 | if (err) | ||
| 50 | return -EBUSY; | ||
| 51 | |||
| 52 | if (!rtc->ops) | ||
| 53 | err = -ENODEV; | ||
| 54 | else if (!rtc->ops->set_time) | ||
| 55 | err = -EINVAL; | ||
| 56 | else | ||
| 57 | err = rtc->ops->set_time(class_dev->dev, tm); | ||
| 58 | |||
| 59 | mutex_unlock(&rtc->ops_lock); | ||
| 60 | return err; | ||
| 61 | } | ||
| 62 | EXPORT_SYMBOL_GPL(rtc_set_time); | ||
| 63 | |||
| 64 | int rtc_set_mmss(struct class_device *class_dev, unsigned long secs) | ||
| 65 | { | ||
| 66 | int err; | ||
| 67 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 68 | |||
| 69 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
| 70 | if (err) | ||
| 71 | return -EBUSY; | ||
| 72 | |||
| 73 | if (!rtc->ops) | ||
| 74 | err = -ENODEV; | ||
| 75 | else if (rtc->ops->set_mmss) | ||
| 76 | err = rtc->ops->set_mmss(class_dev->dev, secs); | ||
| 77 | else if (rtc->ops->read_time && rtc->ops->set_time) { | ||
| 78 | struct rtc_time new, old; | ||
| 79 | |||
| 80 | err = rtc->ops->read_time(class_dev->dev, &old); | ||
| 81 | if (err == 0) { | ||
| 82 | rtc_time_to_tm(secs, &new); | ||
| 83 | |||
| 84 | /* | ||
| 85 | * avoid writing when we're going to change the day of | ||
| 86 | * the month. We will retry in the next minute. This | ||
| 87 | * basically means that if the RTC must not drift | ||
| 88 | * by more than 1 minute in 11 minutes. | ||
| 89 | */ | ||
| 90 | if (!((old.tm_hour == 23 && old.tm_min == 59) || | ||
| 91 | (new.tm_hour == 23 && new.tm_min == 59))) | ||
| 92 | err = rtc->ops->set_time(class_dev->dev, &new); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | else | ||
| 96 | err = -EINVAL; | ||
| 97 | |||
| 98 | mutex_unlock(&rtc->ops_lock); | ||
| 99 | |||
| 100 | return err; | ||
| 101 | } | ||
| 102 | EXPORT_SYMBOL_GPL(rtc_set_mmss); | ||
| 103 | |||
| 104 | int rtc_read_alarm(struct class_device *class_dev, struct rtc_wkalrm *alarm) | ||
| 105 | { | ||
| 106 | int err; | ||
| 107 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 108 | |||
| 109 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
| 110 | if (err) | ||
| 111 | return -EBUSY; | ||
| 112 | |||
| 113 | if (rtc->ops == NULL) | ||
| 114 | err = -ENODEV; | ||
| 115 | else if (!rtc->ops->read_alarm) | ||
| 116 | err = -EINVAL; | ||
| 117 | else { | ||
| 118 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); | ||
| 119 | err = rtc->ops->read_alarm(class_dev->dev, alarm); | ||
| 120 | } | ||
| 121 | |||
| 122 | mutex_unlock(&rtc->ops_lock); | ||
| 123 | return err; | ||
| 124 | } | ||
| 125 | EXPORT_SYMBOL_GPL(rtc_read_alarm); | ||
| 126 | |||
| 127 | int rtc_set_alarm(struct class_device *class_dev, struct rtc_wkalrm *alarm) | ||
| 128 | { | ||
| 129 | int err; | ||
| 130 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 131 | |||
| 132 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
| 133 | if (err) | ||
| 134 | return -EBUSY; | ||
| 135 | |||
| 136 | if (!rtc->ops) | ||
| 137 | err = -ENODEV; | ||
| 138 | else if (!rtc->ops->set_alarm) | ||
| 139 | err = -EINVAL; | ||
| 140 | else | ||
| 141 | err = rtc->ops->set_alarm(class_dev->dev, alarm); | ||
| 142 | |||
| 143 | mutex_unlock(&rtc->ops_lock); | ||
| 144 | return err; | ||
| 145 | } | ||
| 146 | EXPORT_SYMBOL_GPL(rtc_set_alarm); | ||
| 147 | |||
| 148 | void rtc_update_irq(struct class_device *class_dev, | ||
| 149 | unsigned long num, unsigned long events) | ||
| 150 | { | ||
| 151 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 152 | |||
| 153 | spin_lock(&rtc->irq_lock); | ||
| 154 | rtc->irq_data = (rtc->irq_data + (num << 8)) | events; | ||
| 155 | spin_unlock(&rtc->irq_lock); | ||
| 156 | |||
| 157 | spin_lock(&rtc->irq_task_lock); | ||
| 158 | if (rtc->irq_task) | ||
| 159 | rtc->irq_task->func(rtc->irq_task->private_data); | ||
| 160 | spin_unlock(&rtc->irq_task_lock); | ||
| 161 | |||
| 162 | wake_up_interruptible(&rtc->irq_queue); | ||
| 163 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); | ||
| 164 | } | ||
| 165 | EXPORT_SYMBOL_GPL(rtc_update_irq); | ||
| 166 | |||
| 167 | struct class_device *rtc_class_open(char *name) | ||
| 168 | { | ||
| 169 | struct class_device *class_dev = NULL, | ||
| 170 | *class_dev_tmp; | ||
| 171 | |||
| 172 | down(&rtc_class->sem); | ||
| 173 | list_for_each_entry(class_dev_tmp, &rtc_class->children, node) { | ||
| 174 | if (strncmp(class_dev_tmp->class_id, name, BUS_ID_SIZE) == 0) { | ||
| 175 | class_dev = class_dev_tmp; | ||
| 176 | break; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | if (class_dev) { | ||
| 181 | if (!try_module_get(to_rtc_device(class_dev)->owner)) | ||
| 182 | class_dev = NULL; | ||
| 183 | } | ||
| 184 | up(&rtc_class->sem); | ||
| 185 | |||
| 186 | return class_dev; | ||
| 187 | } | ||
| 188 | EXPORT_SYMBOL_GPL(rtc_class_open); | ||
| 189 | |||
| 190 | void rtc_class_close(struct class_device *class_dev) | ||
| 191 | { | ||
| 192 | module_put(to_rtc_device(class_dev)->owner); | ||
| 193 | } | ||
| 194 | EXPORT_SYMBOL_GPL(rtc_class_close); | ||
| 195 | |||
| 196 | int rtc_irq_register(struct class_device *class_dev, struct rtc_task *task) | ||
| 197 | { | ||
| 198 | int retval = -EBUSY; | ||
| 199 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 200 | |||
| 201 | if (task == NULL || task->func == NULL) | ||
| 202 | return -EINVAL; | ||
| 203 | |||
| 204 | spin_lock(&rtc->irq_task_lock); | ||
| 205 | if (rtc->irq_task == NULL) { | ||
| 206 | rtc->irq_task = task; | ||
| 207 | retval = 0; | ||
| 208 | } | ||
| 209 | spin_unlock(&rtc->irq_task_lock); | ||
| 210 | |||
| 211 | return retval; | ||
| 212 | } | ||
| 213 | EXPORT_SYMBOL_GPL(rtc_irq_register); | ||
| 214 | |||
| 215 | void rtc_irq_unregister(struct class_device *class_dev, struct rtc_task *task) | ||
| 216 | { | ||
| 217 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 218 | |||
| 219 | spin_lock(&rtc->irq_task_lock); | ||
| 220 | if (rtc->irq_task == task) | ||
| 221 | rtc->irq_task = NULL; | ||
| 222 | spin_unlock(&rtc->irq_task_lock); | ||
| 223 | } | ||
| 224 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); | ||
| 225 | |||
| 226 | int rtc_irq_set_state(struct class_device *class_dev, struct rtc_task *task, int enabled) | ||
| 227 | { | ||
| 228 | int err = 0; | ||
| 229 | unsigned long flags; | ||
| 230 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 231 | |||
| 232 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | ||
| 233 | if (rtc->irq_task != task) | ||
| 234 | err = -ENXIO; | ||
| 235 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | ||
| 236 | |||
| 237 | if (err == 0) | ||
| 238 | err = rtc->ops->irq_set_state(class_dev->dev, enabled); | ||
| 239 | |||
| 240 | return err; | ||
| 241 | } | ||
| 242 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); | ||
| 243 | |||
| 244 | int rtc_irq_set_freq(struct class_device *class_dev, struct rtc_task *task, int freq) | ||
| 245 | { | ||
| 246 | int err = 0, tmp = 0; | ||
| 247 | unsigned long flags; | ||
| 248 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 249 | |||
| 250 | /* allowed range is 2-8192 */ | ||
| 251 | if (freq < 2 || freq > 8192) | ||
| 252 | return -EINVAL; | ||
| 253 | /* | ||
| 254 | FIXME: this does not belong here, will move where appropriate | ||
| 255 | at a later stage. It cannot hurt right now, trust me :) | ||
| 256 | if ((freq > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE))) | ||
| 257 | return -EACCES; | ||
| 258 | */ | ||
| 259 | /* check if freq is a power of 2 */ | ||
| 260 | while (freq > (1 << tmp)) | ||
| 261 | tmp++; | ||
| 262 | |||
| 263 | if (freq != (1 << tmp)) | ||
| 264 | return -EINVAL; | ||
| 265 | |||
| 266 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | ||
| 267 | if (rtc->irq_task != task) | ||
| 268 | err = -ENXIO; | ||
| 269 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | ||
| 270 | |||
| 271 | if (err == 0) { | ||
| 272 | err = rtc->ops->irq_set_freq(class_dev->dev, freq); | ||
| 273 | if (err == 0) | ||
| 274 | rtc->irq_freq = freq; | ||
| 275 | } | ||
| 276 | return err; | ||
| 277 | } | ||
diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c new file mode 100644 index 000000000000..b1e3e6179e56 --- /dev/null +++ b/drivers/rtc/rtc-dev.c | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | /* | ||
| 2 | * RTC subsystem, dev interface | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * based on arch/arm/common/rtctime.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 | |||
| 17 | static struct class *rtc_dev_class; | ||
| 18 | static dev_t rtc_devt; | ||
| 19 | |||
| 20 | #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */ | ||
| 21 | |||
| 22 | static int rtc_dev_open(struct inode *inode, struct file *file) | ||
| 23 | { | ||
| 24 | int err; | ||
| 25 | struct rtc_device *rtc = container_of(inode->i_cdev, | ||
| 26 | struct rtc_device, char_dev); | ||
| 27 | struct rtc_class_ops *ops = rtc->ops; | ||
| 28 | |||
| 29 | /* We keep the lock as long as the device is in use | ||
| 30 | * and return immediately if busy | ||
| 31 | */ | ||
| 32 | if (!(mutex_trylock(&rtc->char_lock))) | ||
| 33 | return -EBUSY; | ||
| 34 | |||
| 35 | file->private_data = &rtc->class_dev; | ||
| 36 | |||
| 37 | err = ops->open ? ops->open(rtc->class_dev.dev) : 0; | ||
| 38 | if (err == 0) { | ||
| 39 | spin_lock_irq(&rtc->irq_lock); | ||
| 40 | rtc->irq_data = 0; | ||
| 41 | spin_unlock_irq(&rtc->irq_lock); | ||
| 42 | |||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | /* something has gone wrong, release the lock */ | ||
| 47 | mutex_unlock(&rtc->char_lock); | ||
| 48 | return err; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | static ssize_t | ||
| 53 | rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | ||
| 54 | { | ||
| 55 | struct rtc_device *rtc = to_rtc_device(file->private_data); | ||
| 56 | |||
| 57 | DECLARE_WAITQUEUE(wait, current); | ||
| 58 | unsigned long data; | ||
| 59 | ssize_t ret; | ||
| 60 | |||
| 61 | if (count < sizeof(unsigned long)) | ||
| 62 | return -EINVAL; | ||
| 63 | |||
| 64 | add_wait_queue(&rtc->irq_queue, &wait); | ||
| 65 | do { | ||
| 66 | __set_current_state(TASK_INTERRUPTIBLE); | ||
| 67 | |||
| 68 | spin_lock_irq(&rtc->irq_lock); | ||
| 69 | data = rtc->irq_data; | ||
| 70 | rtc->irq_data = 0; | ||
| 71 | spin_unlock_irq(&rtc->irq_lock); | ||
| 72 | |||
| 73 | if (data != 0) { | ||
| 74 | ret = 0; | ||
| 75 | break; | ||
| 76 | } | ||
| 77 | if (file->f_flags & O_NONBLOCK) { | ||
| 78 | ret = -EAGAIN; | ||
| 79 | break; | ||
| 80 | } | ||
| 81 | if (signal_pending(current)) { | ||
| 82 | ret = -ERESTARTSYS; | ||
| 83 | break; | ||
| 84 | } | ||
| 85 | schedule(); | ||
| 86 | } while (1); | ||
| 87 | set_current_state(TASK_RUNNING); | ||
| 88 | remove_wait_queue(&rtc->irq_queue, &wait); | ||
| 89 | |||
| 90 | if (ret == 0) { | ||
| 91 | /* Check for any data updates */ | ||
| 92 | if (rtc->ops->read_callback) | ||
| 93 | data = rtc->ops->read_callback(rtc->class_dev.dev, data); | ||
| 94 | |||
| 95 | ret = put_user(data, (unsigned long __user *)buf); | ||
| 96 | if (ret == 0) | ||
| 97 | ret = sizeof(unsigned long); | ||
| 98 | } | ||
| 99 | return ret; | ||
| 100 | } | ||
| 101 | |||
| 102 | static unsigned int rtc_dev_poll(struct file *file, poll_table *wait) | ||
| 103 | { | ||
| 104 | struct rtc_device *rtc = to_rtc_device(file->private_data); | ||
| 105 | unsigned long data; | ||
| 106 | |||
| 107 | poll_wait(file, &rtc->irq_queue, wait); | ||
| 108 | |||
| 109 | data = rtc->irq_data; | ||
| 110 | |||
| 111 | return (data != 0) ? (POLLIN | POLLRDNORM) : 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | static int rtc_dev_ioctl(struct inode *inode, struct file *file, | ||
| 115 | unsigned int cmd, unsigned long arg) | ||
| 116 | { | ||
| 117 | int err = 0; | ||
| 118 | struct class_device *class_dev = file->private_data; | ||
| 119 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 120 | struct rtc_class_ops *ops = rtc->ops; | ||
| 121 | struct rtc_time tm; | ||
| 122 | struct rtc_wkalrm alarm; | ||
| 123 | void __user *uarg = (void __user *) arg; | ||
| 124 | |||
| 125 | /* avoid conflicting IRQ users */ | ||
| 126 | if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) { | ||
| 127 | spin_lock(&rtc->irq_task_lock); | ||
| 128 | if (rtc->irq_task) | ||
| 129 | err = -EBUSY; | ||
| 130 | spin_unlock(&rtc->irq_task_lock); | ||
| 131 | |||
| 132 | if (err < 0) | ||
| 133 | return err; | ||
| 134 | } | ||
| 135 | |||
| 136 | /* try the driver's ioctl interface */ | ||
| 137 | if (ops->ioctl) { | ||
| 138 | err = ops->ioctl(class_dev->dev, cmd, arg); | ||
| 139 | if (err != -EINVAL) | ||
| 140 | return err; | ||
| 141 | } | ||
| 142 | |||
| 143 | /* if the driver does not provide the ioctl interface | ||
| 144 | * or if that particular ioctl was not implemented | ||
| 145 | * (-EINVAL), we will try to emulate here. | ||
| 146 | */ | ||
| 147 | |||
| 148 | switch (cmd) { | ||
| 149 | case RTC_ALM_READ: | ||
| 150 | err = rtc_read_alarm(class_dev, &alarm); | ||
| 151 | if (err < 0) | ||
| 152 | return err; | ||
| 153 | |||
| 154 | if (copy_to_user(uarg, &alarm.time, sizeof(tm))) | ||
| 155 | return -EFAULT; | ||
| 156 | break; | ||
| 157 | |||
| 158 | case RTC_ALM_SET: | ||
| 159 | if (copy_from_user(&alarm.time, uarg, sizeof(tm))) | ||
| 160 | return -EFAULT; | ||
| 161 | |||
| 162 | alarm.enabled = 0; | ||
| 163 | alarm.pending = 0; | ||
| 164 | alarm.time.tm_mday = -1; | ||
| 165 | alarm.time.tm_mon = -1; | ||
| 166 | alarm.time.tm_year = -1; | ||
| 167 | alarm.time.tm_wday = -1; | ||
| 168 | alarm.time.tm_yday = -1; | ||
| 169 | alarm.time.tm_isdst = -1; | ||
| 170 | err = rtc_set_alarm(class_dev, &alarm); | ||
| 171 | break; | ||
| 172 | |||
| 173 | case RTC_RD_TIME: | ||
| 174 | err = rtc_read_time(class_dev, &tm); | ||
| 175 | if (err < 0) | ||
| 176 | return err; | ||
| 177 | |||
| 178 | if (copy_to_user(uarg, &tm, sizeof(tm))) | ||
| 179 | return -EFAULT; | ||
| 180 | break; | ||
| 181 | |||
| 182 | case RTC_SET_TIME: | ||
| 183 | if (!capable(CAP_SYS_TIME)) | ||
| 184 | return -EACCES; | ||
| 185 | |||
| 186 | if (copy_from_user(&tm, uarg, sizeof(tm))) | ||
| 187 | return -EFAULT; | ||
| 188 | |||
| 189 | err = rtc_set_time(class_dev, &tm); | ||
| 190 | break; | ||
| 191 | #if 0 | ||
| 192 | case RTC_EPOCH_SET: | ||
| 193 | #ifndef rtc_epoch | ||
| 194 | /* | ||
| 195 | * There were no RTC clocks before 1900. | ||
| 196 | */ | ||
| 197 | if (arg < 1900) { | ||
| 198 | err = -EINVAL; | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | if (!capable(CAP_SYS_TIME)) { | ||
| 202 | err = -EACCES; | ||
| 203 | break; | ||
| 204 | } | ||
| 205 | rtc_epoch = arg; | ||
| 206 | err = 0; | ||
| 207 | #endif | ||
| 208 | break; | ||
| 209 | |||
| 210 | case RTC_EPOCH_READ: | ||
| 211 | err = put_user(rtc_epoch, (unsigned long __user *)uarg); | ||
| 212 | break; | ||
| 213 | #endif | ||
| 214 | case RTC_WKALM_SET: | ||
| 215 | if (copy_from_user(&alarm, uarg, sizeof(alarm))) | ||
| 216 | return -EFAULT; | ||
| 217 | |||
| 218 | err = rtc_set_alarm(class_dev, &alarm); | ||
| 219 | break; | ||
| 220 | |||
| 221 | case RTC_WKALM_RD: | ||
| 222 | err = rtc_read_alarm(class_dev, &alarm); | ||
| 223 | if (err < 0) | ||
| 224 | return err; | ||
| 225 | |||
| 226 | if (copy_to_user(uarg, &alarm, sizeof(alarm))) | ||
| 227 | return -EFAULT; | ||
| 228 | break; | ||
| 229 | |||
| 230 | default: | ||
| 231 | err = -EINVAL; | ||
| 232 | break; | ||
| 233 | } | ||
| 234 | |||
| 235 | return err; | ||
| 236 | } | ||
| 237 | |||
| 238 | static int rtc_dev_release(struct inode *inode, struct file *file) | ||
| 239 | { | ||
| 240 | struct rtc_device *rtc = to_rtc_device(file->private_data); | ||
| 241 | |||
| 242 | if (rtc->ops->release) | ||
| 243 | rtc->ops->release(rtc->class_dev.dev); | ||
| 244 | |||
| 245 | mutex_unlock(&rtc->char_lock); | ||
| 246 | return 0; | ||
| 247 | } | ||
| 248 | |||
| 249 | static int rtc_dev_fasync(int fd, struct file *file, int on) | ||
| 250 | { | ||
| 251 | struct rtc_device *rtc = to_rtc_device(file->private_data); | ||
| 252 | return fasync_helper(fd, file, on, &rtc->async_queue); | ||
| 253 | } | ||
| 254 | |||
| 255 | static struct file_operations rtc_dev_fops = { | ||
| 256 | .owner = THIS_MODULE, | ||
| 257 | .llseek = no_llseek, | ||
| 258 | .read = rtc_dev_read, | ||
| 259 | .poll = rtc_dev_poll, | ||
| 260 | .ioctl = rtc_dev_ioctl, | ||
| 261 | .open = rtc_dev_open, | ||
| 262 | .release = rtc_dev_release, | ||
| 263 | .fasync = rtc_dev_fasync, | ||
| 264 | }; | ||
| 265 | |||
| 266 | /* insertion/removal hooks */ | ||
| 267 | |||
| 268 | static int rtc_dev_add_device(struct class_device *class_dev, | ||
| 269 | struct class_interface *class_intf) | ||
| 270 | { | ||
| 271 | int err = 0; | ||
| 272 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 273 | |||
| 274 | if (rtc->id >= RTC_DEV_MAX) { | ||
| 275 | dev_err(class_dev->dev, "too many RTCs\n"); | ||
| 276 | return -EINVAL; | ||
| 277 | } | ||
| 278 | |||
| 279 | mutex_init(&rtc->char_lock); | ||
| 280 | spin_lock_init(&rtc->irq_lock); | ||
| 281 | init_waitqueue_head(&rtc->irq_queue); | ||
| 282 | |||
| 283 | cdev_init(&rtc->char_dev, &rtc_dev_fops); | ||
| 284 | rtc->char_dev.owner = rtc->owner; | ||
| 285 | |||
| 286 | if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) { | ||
| 287 | cdev_del(&rtc->char_dev); | ||
| 288 | dev_err(class_dev->dev, | ||
| 289 | "failed to add char device %d:%d\n", | ||
| 290 | MAJOR(rtc_devt), rtc->id); | ||
| 291 | return -ENODEV; | ||
| 292 | } | ||
| 293 | |||
| 294 | rtc->rtc_dev = class_device_create(rtc_dev_class, NULL, | ||
| 295 | MKDEV(MAJOR(rtc_devt), rtc->id), | ||
| 296 | class_dev->dev, "rtc%d", rtc->id); | ||
| 297 | if (IS_ERR(rtc->rtc_dev)) { | ||
| 298 | dev_err(class_dev->dev, "cannot create rtc_dev device\n"); | ||
| 299 | err = PTR_ERR(rtc->rtc_dev); | ||
| 300 | goto err_cdev_del; | ||
| 301 | } | ||
| 302 | |||
| 303 | dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n", | ||
| 304 | MAJOR(rtc->rtc_dev->devt), | ||
| 305 | MINOR(rtc->rtc_dev->devt)); | ||
| 306 | |||
| 307 | return 0; | ||
| 308 | |||
| 309 | err_cdev_del: | ||
| 310 | |||
| 311 | cdev_del(&rtc->char_dev); | ||
| 312 | return err; | ||
| 313 | } | ||
| 314 | |||
| 315 | static void rtc_dev_remove_device(struct class_device *class_dev, | ||
| 316 | struct class_interface *class_intf) | ||
| 317 | { | ||
| 318 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 319 | |||
| 320 | if (rtc->rtc_dev) { | ||
| 321 | dev_dbg(class_dev->dev, "removing char %d:%d\n", | ||
| 322 | MAJOR(rtc->rtc_dev->devt), | ||
| 323 | MINOR(rtc->rtc_dev->devt)); | ||
| 324 | |||
| 325 | class_device_unregister(rtc->rtc_dev); | ||
| 326 | cdev_del(&rtc->char_dev); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | /* interface registration */ | ||
| 331 | |||
| 332 | static struct class_interface rtc_dev_interface = { | ||
| 333 | .add = &rtc_dev_add_device, | ||
| 334 | .remove = &rtc_dev_remove_device, | ||
| 335 | }; | ||
| 336 | |||
| 337 | static int __init rtc_dev_init(void) | ||
| 338 | { | ||
| 339 | int err; | ||
| 340 | |||
| 341 | rtc_dev_class = class_create(THIS_MODULE, "rtc-dev"); | ||
| 342 | if (IS_ERR(rtc_dev_class)) | ||
| 343 | return PTR_ERR(rtc_dev_class); | ||
| 344 | |||
| 345 | err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); | ||
| 346 | if (err < 0) { | ||
| 347 | printk(KERN_ERR "%s: failed to allocate char dev region\n", | ||
| 348 | __FILE__); | ||
| 349 | goto err_destroy_class; | ||
| 350 | } | ||
| 351 | |||
| 352 | err = rtc_interface_register(&rtc_dev_interface); | ||
| 353 | if (err < 0) { | ||
| 354 | printk(KERN_ERR "%s: failed to register the interface\n", | ||
| 355 | __FILE__); | ||
| 356 | goto err_unregister_chrdev; | ||
| 357 | } | ||
| 358 | |||
| 359 | return 0; | ||
| 360 | |||
| 361 | err_unregister_chrdev: | ||
| 362 | unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); | ||
| 363 | |||
| 364 | err_destroy_class: | ||
| 365 | class_destroy(rtc_dev_class); | ||
| 366 | |||
| 367 | return err; | ||
| 368 | } | ||
| 369 | |||
| 370 | static void __exit rtc_dev_exit(void) | ||
| 371 | { | ||
| 372 | class_interface_unregister(&rtc_dev_interface); | ||
| 373 | class_destroy(rtc_dev_class); | ||
| 374 | unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); | ||
| 375 | } | ||
| 376 | |||
| 377 | module_init(rtc_dev_init); | ||
| 378 | module_exit(rtc_dev_exit); | ||
| 379 | |||
| 380 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 381 | MODULE_DESCRIPTION("RTC class dev interface"); | ||
| 382 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-ds1672.c b/drivers/rtc/rtc-ds1672.c new file mode 100644 index 000000000000..358695a416f3 --- /dev/null +++ b/drivers/rtc/rtc-ds1672.c | |||
| @@ -0,0 +1,233 @@ | |||
| 1 | /* | ||
| 2 | * An rtc/i2c driver for the Dallas DS1672 | ||
| 3 | * Copyright 2005 Alessandro Zummo | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License version 2 as | ||
| 7 | * published by the Free Software Foundation. | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <linux/module.h> | ||
| 11 | #include <linux/i2c.h> | ||
| 12 | #include <linux/rtc.h> | ||
| 13 | |||
| 14 | #define DRV_VERSION "0.2" | ||
| 15 | |||
| 16 | /* Addresses to scan: none. This chip cannot be detected. */ | ||
| 17 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
| 18 | |||
| 19 | /* Insmod parameters */ | ||
| 20 | I2C_CLIENT_INSMOD; | ||
| 21 | |||
| 22 | /* Registers */ | ||
| 23 | |||
| 24 | #define DS1672_REG_CNT_BASE 0 | ||
| 25 | #define DS1672_REG_CONTROL 4 | ||
| 26 | #define DS1672_REG_TRICKLE 5 | ||
| 27 | |||
| 28 | |||
| 29 | /* Prototypes */ | ||
| 30 | static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind); | ||
| 31 | |||
| 32 | /* | ||
| 33 | * In the routines that deal directly with the ds1672 hardware, we use | ||
| 34 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch | ||
| 35 | * Epoch is initialized as 2000. Time is set to UTC. | ||
| 36 | */ | ||
| 37 | static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 38 | { | ||
| 39 | unsigned long time; | ||
| 40 | unsigned char addr = DS1672_REG_CNT_BASE; | ||
| 41 | unsigned char buf[4]; | ||
| 42 | |||
| 43 | struct i2c_msg msgs[] = { | ||
| 44 | { client->addr, 0, 1, &addr }, /* setup read ptr */ | ||
| 45 | { client->addr, I2C_M_RD, 4, buf }, /* read date */ | ||
| 46 | }; | ||
| 47 | |||
| 48 | /* read date registers */ | ||
| 49 | if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { | ||
| 50 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 51 | return -EIO; | ||
| 52 | } | ||
| 53 | |||
| 54 | dev_dbg(&client->dev, | ||
| 55 | "%s: raw read data - counters=%02x,%02x,%02x,%02x\n" | ||
| 56 | __FUNCTION__, | ||
| 57 | buf[0], buf[1], buf[2], buf[3]); | ||
| 58 | |||
| 59 | time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; | ||
| 60 | |||
| 61 | rtc_time_to_tm(time, tm); | ||
| 62 | |||
| 63 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | ||
| 64 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 65 | __FUNCTION__, | ||
| 66 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 67 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 68 | |||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs) | ||
| 73 | { | ||
| 74 | int xfer; | ||
| 75 | unsigned char buf[5]; | ||
| 76 | |||
| 77 | buf[0] = DS1672_REG_CNT_BASE; | ||
| 78 | buf[1] = secs & 0x000000FF; | ||
| 79 | buf[2] = (secs & 0x0000FF00) >> 8; | ||
| 80 | buf[3] = (secs & 0x00FF0000) >> 16; | ||
| 81 | buf[4] = (secs & 0xFF000000) >> 24; | ||
| 82 | |||
| 83 | xfer = i2c_master_send(client, buf, 5); | ||
| 84 | if (xfer != 5) { | ||
| 85 | dev_err(&client->dev, "%s: send: %d\n", __FUNCTION__, xfer); | ||
| 86 | return -EIO; | ||
| 87 | } | ||
| 88 | |||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | static int ds1672_set_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 93 | { | ||
| 94 | unsigned long secs; | ||
| 95 | |||
| 96 | dev_dbg(&client->dev, | ||
| 97 | "%s: secs=%d, mins=%d, hours=%d, ", | ||
| 98 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 99 | __FUNCTION__, | ||
| 100 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 101 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 102 | |||
| 103 | rtc_tm_to_time(tm, &secs); | ||
| 104 | |||
| 105 | return ds1672_set_mmss(client, secs); | ||
| 106 | } | ||
| 107 | |||
| 108 | static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 109 | { | ||
| 110 | return ds1672_get_datetime(to_i2c_client(dev), tm); | ||
| 111 | } | ||
| 112 | |||
| 113 | static int ds1672_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 114 | { | ||
| 115 | return ds1672_set_datetime(to_i2c_client(dev), tm); | ||
| 116 | } | ||
| 117 | |||
| 118 | static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs) | ||
| 119 | { | ||
| 120 | return ds1672_set_mmss(to_i2c_client(dev), secs); | ||
| 121 | } | ||
| 122 | |||
| 123 | static struct rtc_class_ops ds1672_rtc_ops = { | ||
| 124 | .read_time = ds1672_rtc_read_time, | ||
| 125 | .set_time = ds1672_rtc_set_time, | ||
| 126 | .set_mmss = ds1672_rtc_set_mmss, | ||
| 127 | }; | ||
| 128 | |||
| 129 | static int ds1672_attach(struct i2c_adapter *adapter) | ||
| 130 | { | ||
| 131 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 132 | return i2c_probe(adapter, &addr_data, ds1672_probe); | ||
| 133 | } | ||
| 134 | |||
| 135 | static int ds1672_detach(struct i2c_client *client) | ||
| 136 | { | ||
| 137 | int err; | ||
| 138 | struct rtc_device *rtc = i2c_get_clientdata(client); | ||
| 139 | |||
| 140 | dev_dbg(&client->dev, "%s\n", __FUNCTION__); | ||
| 141 | |||
| 142 | if (rtc) | ||
| 143 | rtc_device_unregister(rtc); | ||
| 144 | |||
| 145 | if ((err = i2c_detach_client(client))) | ||
| 146 | return err; | ||
| 147 | |||
| 148 | kfree(client); | ||
| 149 | |||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | static struct i2c_driver ds1672_driver = { | ||
| 154 | .driver = { | ||
| 155 | .name = "ds1672", | ||
| 156 | }, | ||
| 157 | .id = I2C_DRIVERID_DS1672, | ||
| 158 | .attach_adapter = &ds1672_attach, | ||
| 159 | .detach_client = &ds1672_detach, | ||
| 160 | }; | ||
| 161 | |||
| 162 | static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind) | ||
| 163 | { | ||
| 164 | int err = 0; | ||
| 165 | struct i2c_client *client; | ||
| 166 | struct rtc_device *rtc; | ||
| 167 | |||
| 168 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 169 | |||
| 170 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | ||
| 171 | err = -ENODEV; | ||
| 172 | goto exit; | ||
| 173 | } | ||
| 174 | |||
| 175 | if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { | ||
| 176 | err = -ENOMEM; | ||
| 177 | goto exit; | ||
| 178 | } | ||
| 179 | |||
| 180 | /* I2C client */ | ||
| 181 | client->addr = address; | ||
| 182 | client->driver = &ds1672_driver; | ||
| 183 | client->adapter = adapter; | ||
| 184 | |||
| 185 | strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE); | ||
| 186 | |||
| 187 | /* Inform the i2c layer */ | ||
| 188 | if ((err = i2c_attach_client(client))) | ||
| 189 | goto exit_kfree; | ||
| 190 | |||
| 191 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | ||
| 192 | |||
| 193 | rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev, | ||
| 194 | &ds1672_rtc_ops, THIS_MODULE); | ||
| 195 | |||
| 196 | if (IS_ERR(rtc)) { | ||
| 197 | err = PTR_ERR(rtc); | ||
| 198 | dev_err(&client->dev, | ||
| 199 | "unable to register the class device\n"); | ||
| 200 | goto exit_detach; | ||
| 201 | } | ||
| 202 | |||
| 203 | i2c_set_clientdata(client, rtc); | ||
| 204 | |||
| 205 | return 0; | ||
| 206 | |||
| 207 | exit_detach: | ||
| 208 | i2c_detach_client(client); | ||
| 209 | |||
| 210 | exit_kfree: | ||
| 211 | kfree(client); | ||
| 212 | |||
| 213 | exit: | ||
| 214 | return err; | ||
| 215 | } | ||
| 216 | |||
| 217 | static int __init ds1672_init(void) | ||
| 218 | { | ||
| 219 | return i2c_add_driver(&ds1672_driver); | ||
| 220 | } | ||
| 221 | |||
| 222 | static void __exit ds1672_exit(void) | ||
| 223 | { | ||
| 224 | i2c_del_driver(&ds1672_driver); | ||
| 225 | } | ||
| 226 | |||
| 227 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 228 | MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver"); | ||
| 229 | MODULE_LICENSE("GPL"); | ||
| 230 | MODULE_VERSION(DRV_VERSION); | ||
| 231 | |||
| 232 | module_init(ds1672_init); | ||
| 233 | module_exit(ds1672_exit); | ||
diff --git a/drivers/rtc/rtc-ep93xx.c b/drivers/rtc/rtc-ep93xx.c new file mode 100644 index 000000000000..0dd80ea686a9 --- /dev/null +++ b/drivers/rtc/rtc-ep93xx.c | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | /* | ||
| 2 | * A driver for the RTC embedded in the Cirrus Logic EP93XX processors | ||
| 3 | * Copyright (c) 2006 Tower Technologies | ||
| 4 | * | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/rtc.h> | ||
| 14 | #include <linux/platform_device.h> | ||
| 15 | #include <asm/hardware.h> | ||
| 16 | |||
| 17 | #define EP93XX_RTC_REG(x) (EP93XX_RTC_BASE + (x)) | ||
| 18 | #define EP93XX_RTC_DATA EP93XX_RTC_REG(0x0000) | ||
| 19 | #define EP93XX_RTC_LOAD EP93XX_RTC_REG(0x000C) | ||
| 20 | #define EP93XX_RTC_SWCOMP EP93XX_RTC_REG(0x0108) | ||
| 21 | |||
| 22 | #define DRV_VERSION "0.2" | ||
| 23 | |||
| 24 | static int ep93xx_get_swcomp(struct device *dev, unsigned short *preload, | ||
| 25 | unsigned short *delete) | ||
| 26 | { | ||
| 27 | unsigned short comp = __raw_readl(EP93XX_RTC_SWCOMP); | ||
| 28 | |||
| 29 | if (preload) | ||
| 30 | *preload = comp & 0xffff; | ||
| 31 | |||
| 32 | if (delete) | ||
| 33 | *delete = (comp >> 16) & 0x1f; | ||
| 34 | |||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 39 | { | ||
| 40 | unsigned long time = __raw_readl(EP93XX_RTC_DATA); | ||
| 41 | |||
| 42 | rtc_time_to_tm(time, tm); | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs) | ||
| 47 | { | ||
| 48 | __raw_writel(secs + 1, EP93XX_RTC_LOAD); | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 53 | { | ||
| 54 | int err; | ||
| 55 | unsigned long secs; | ||
| 56 | |||
| 57 | err = rtc_tm_to_time(tm, &secs); | ||
| 58 | if (err != 0) | ||
| 59 | return err; | ||
| 60 | |||
| 61 | return ep93xx_rtc_set_mmss(dev, secs); | ||
| 62 | } | ||
| 63 | |||
| 64 | static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 65 | { | ||
| 66 | unsigned short preload, delete; | ||
| 67 | |||
| 68 | ep93xx_get_swcomp(dev, &preload, &delete); | ||
| 69 | |||
| 70 | seq_printf(seq, "24hr\t\t: yes\n"); | ||
| 71 | seq_printf(seq, "preload\t\t: %d\n", preload); | ||
| 72 | seq_printf(seq, "delete\t\t: %d\n", delete); | ||
| 73 | |||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | static struct rtc_class_ops ep93xx_rtc_ops = { | ||
| 78 | .read_time = ep93xx_rtc_read_time, | ||
| 79 | .set_time = ep93xx_rtc_set_time, | ||
| 80 | .set_mmss = ep93xx_rtc_set_mmss, | ||
| 81 | .proc = ep93xx_rtc_proc, | ||
| 82 | }; | ||
| 83 | |||
| 84 | static ssize_t ep93xx_sysfs_show_comp_preload(struct device *dev, | ||
| 85 | struct device_attribute *attr, char *buf) | ||
| 86 | { | ||
| 87 | unsigned short preload; | ||
| 88 | |||
| 89 | ep93xx_get_swcomp(dev, &preload, NULL); | ||
| 90 | |||
| 91 | return sprintf(buf, "%d\n", preload); | ||
| 92 | } | ||
| 93 | static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_sysfs_show_comp_preload, NULL); | ||
| 94 | |||
| 95 | static ssize_t ep93xx_sysfs_show_comp_delete(struct device *dev, | ||
| 96 | struct device_attribute *attr, char *buf) | ||
| 97 | { | ||
| 98 | unsigned short delete; | ||
| 99 | |||
| 100 | ep93xx_get_swcomp(dev, NULL, &delete); | ||
| 101 | |||
| 102 | return sprintf(buf, "%d\n", delete); | ||
| 103 | } | ||
| 104 | static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_sysfs_show_comp_delete, NULL); | ||
| 105 | |||
| 106 | |||
| 107 | static int __devinit ep93xx_rtc_probe(struct platform_device *dev) | ||
| 108 | { | ||
| 109 | struct rtc_device *rtc = rtc_device_register("ep93xx", | ||
| 110 | &dev->dev, &ep93xx_rtc_ops, THIS_MODULE); | ||
| 111 | |||
| 112 | if (IS_ERR(rtc)) { | ||
| 113 | dev_err(&dev->dev, "unable to register\n"); | ||
| 114 | return PTR_ERR(rtc); | ||
| 115 | } | ||
| 116 | |||
| 117 | platform_set_drvdata(dev, rtc); | ||
| 118 | |||
| 119 | device_create_file(&dev->dev, &dev_attr_comp_preload); | ||
| 120 | device_create_file(&dev->dev, &dev_attr_comp_delete); | ||
| 121 | |||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | |||
| 125 | static int __devexit ep93xx_rtc_remove(struct platform_device *dev) | ||
| 126 | { | ||
| 127 | struct rtc_device *rtc = platform_get_drvdata(dev); | ||
| 128 | |||
| 129 | if (rtc) | ||
| 130 | rtc_device_unregister(rtc); | ||
| 131 | |||
| 132 | platform_set_drvdata(dev, NULL); | ||
| 133 | |||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | static struct platform_driver ep93xx_rtc_platform_driver = { | ||
| 138 | .driver = { | ||
| 139 | .name = "ep93xx-rtc", | ||
| 140 | .owner = THIS_MODULE, | ||
| 141 | }, | ||
| 142 | .probe = ep93xx_rtc_probe, | ||
| 143 | .remove = __devexit_p(ep93xx_rtc_remove), | ||
| 144 | }; | ||
| 145 | |||
| 146 | static int __init ep93xx_rtc_init(void) | ||
| 147 | { | ||
| 148 | return platform_driver_register(&ep93xx_rtc_platform_driver); | ||
| 149 | } | ||
| 150 | |||
| 151 | static void __exit ep93xx_rtc_exit(void) | ||
| 152 | { | ||
| 153 | platform_driver_unregister(&ep93xx_rtc_platform_driver); | ||
| 154 | } | ||
| 155 | |||
| 156 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 157 | MODULE_DESCRIPTION("EP93XX RTC driver"); | ||
| 158 | MODULE_LICENSE("GPL"); | ||
| 159 | MODULE_VERSION(DRV_VERSION); | ||
| 160 | |||
| 161 | module_init(ep93xx_rtc_init); | ||
| 162 | module_exit(ep93xx_rtc_exit); | ||
diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c new file mode 100644 index 000000000000..cfedc1d28ee1 --- /dev/null +++ b/drivers/rtc/rtc-lib.c | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | /* | ||
| 2 | * rtc and date/time utility functions | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005-06 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * based on arch/arm/common/rtctime.c and other bits | ||
| 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 | |||
| 17 | static const unsigned char rtc_days_in_month[] = { | ||
| 18 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | ||
| 19 | }; | ||
| 20 | |||
| 21 | #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400) | ||
| 22 | #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400)) | ||
| 23 | |||
| 24 | int rtc_month_days(unsigned int month, unsigned int year) | ||
| 25 | { | ||
| 26 | return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1); | ||
| 27 | } | ||
| 28 | EXPORT_SYMBOL(rtc_month_days); | ||
| 29 | |||
| 30 | /* | ||
| 31 | * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. | ||
| 32 | */ | ||
| 33 | void rtc_time_to_tm(unsigned long time, struct rtc_time *tm) | ||
| 34 | { | ||
| 35 | register int days, month, year; | ||
| 36 | |||
| 37 | days = time / 86400; | ||
| 38 | time -= days * 86400; | ||
| 39 | |||
| 40 | /* day of the week, 1970-01-01 was a Thursday */ | ||
| 41 | tm->tm_wday = (days + 4) % 7; | ||
| 42 | |||
| 43 | year = 1970 + days / 365; | ||
| 44 | days -= (year - 1970) * 365 | ||
| 45 | + LEAPS_THRU_END_OF(year - 1) | ||
| 46 | - LEAPS_THRU_END_OF(1970 - 1); | ||
| 47 | if (days < 0) { | ||
| 48 | year -= 1; | ||
| 49 | days += 365 + LEAP_YEAR(year); | ||
| 50 | } | ||
| 51 | tm->tm_year = year - 1900; | ||
| 52 | tm->tm_yday = days + 1; | ||
| 53 | |||
| 54 | for (month = 0; month < 11; month++) { | ||
| 55 | int newdays; | ||
| 56 | |||
| 57 | newdays = days - rtc_month_days(month, year); | ||
| 58 | if (newdays < 0) | ||
| 59 | break; | ||
| 60 | days = newdays; | ||
| 61 | } | ||
| 62 | tm->tm_mon = month; | ||
| 63 | tm->tm_mday = days + 1; | ||
| 64 | |||
| 65 | tm->tm_hour = time / 3600; | ||
| 66 | time -= tm->tm_hour * 3600; | ||
| 67 | tm->tm_min = time / 60; | ||
| 68 | tm->tm_sec = time - tm->tm_min * 60; | ||
| 69 | } | ||
| 70 | EXPORT_SYMBOL(rtc_time_to_tm); | ||
| 71 | |||
| 72 | /* | ||
| 73 | * Does the rtc_time represent a valid date/time? | ||
| 74 | */ | ||
| 75 | int rtc_valid_tm(struct rtc_time *tm) | ||
| 76 | { | ||
| 77 | if (tm->tm_year < 70 | ||
| 78 | || tm->tm_mon >= 12 | ||
| 79 | || tm->tm_mday < 1 | ||
| 80 | || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900) | ||
| 81 | || tm->tm_hour >= 24 | ||
| 82 | || tm->tm_min >= 60 | ||
| 83 | || tm->tm_sec >= 60) | ||
| 84 | return -EINVAL; | ||
| 85 | |||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | EXPORT_SYMBOL(rtc_valid_tm); | ||
| 89 | |||
| 90 | /* | ||
| 91 | * Convert Gregorian date to seconds since 01-01-1970 00:00:00. | ||
| 92 | */ | ||
| 93 | int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time) | ||
| 94 | { | ||
| 95 | *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, | ||
| 96 | tm->tm_hour, tm->tm_min, tm->tm_sec); | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | EXPORT_SYMBOL(rtc_tm_to_time); | ||
| 100 | |||
| 101 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-m48t86.c b/drivers/rtc/rtc-m48t86.c new file mode 100644 index 000000000000..db445c872b1b --- /dev/null +++ b/drivers/rtc/rtc-m48t86.c | |||
| @@ -0,0 +1,209 @@ | |||
| 1 | /* | ||
| 2 | * ST M48T86 / Dallas DS12887 RTC driver | ||
| 3 | * Copyright (c) 2006 Tower Technologies | ||
| 4 | * | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | * | ||
| 11 | * This drivers only supports the clock running in BCD and 24H mode. | ||
| 12 | * If it will be ever adapted to binary and 12H mode, care must be taken | ||
| 13 | * to not introduce bugs. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include <linux/module.h> | ||
| 17 | #include <linux/rtc.h> | ||
| 18 | #include <linux/platform_device.h> | ||
| 19 | #include <linux/m48t86.h> | ||
| 20 | #include <linux/bcd.h> | ||
| 21 | |||
| 22 | #define M48T86_REG_SEC 0x00 | ||
| 23 | #define M48T86_REG_SECALRM 0x01 | ||
| 24 | #define M48T86_REG_MIN 0x02 | ||
| 25 | #define M48T86_REG_MINALRM 0x03 | ||
| 26 | #define M48T86_REG_HOUR 0x04 | ||
| 27 | #define M48T86_REG_HOURALRM 0x05 | ||
| 28 | #define M48T86_REG_DOW 0x06 /* 1 = sunday */ | ||
| 29 | #define M48T86_REG_DOM 0x07 | ||
| 30 | #define M48T86_REG_MONTH 0x08 /* 1 - 12 */ | ||
| 31 | #define M48T86_REG_YEAR 0x09 /* 0 - 99 */ | ||
| 32 | #define M48T86_REG_A 0x0A | ||
| 33 | #define M48T86_REG_B 0x0B | ||
| 34 | #define M48T86_REG_C 0x0C | ||
| 35 | #define M48T86_REG_D 0x0D | ||
| 36 | |||
| 37 | #define M48T86_REG_B_H24 (1 << 1) | ||
| 38 | #define M48T86_REG_B_DM (1 << 2) | ||
| 39 | #define M48T86_REG_B_SET (1 << 7) | ||
| 40 | #define M48T86_REG_D_VRT (1 << 7) | ||
| 41 | |||
| 42 | #define DRV_VERSION "0.1" | ||
| 43 | |||
| 44 | |||
| 45 | static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 46 | { | ||
| 47 | unsigned char reg; | ||
| 48 | struct platform_device *pdev = to_platform_device(dev); | ||
| 49 | struct m48t86_ops *ops = pdev->dev.platform_data; | ||
| 50 | |||
| 51 | reg = ops->readb(M48T86_REG_B); | ||
| 52 | |||
| 53 | if (reg & M48T86_REG_B_DM) { | ||
| 54 | /* data (binary) mode */ | ||
| 55 | tm->tm_sec = ops->readb(M48T86_REG_SEC); | ||
| 56 | tm->tm_min = ops->readb(M48T86_REG_MIN); | ||
| 57 | tm->tm_hour = ops->readb(M48T86_REG_HOUR) & 0x3F; | ||
| 58 | tm->tm_mday = ops->readb(M48T86_REG_DOM); | ||
| 59 | /* tm_mon is 0-11 */ | ||
| 60 | tm->tm_mon = ops->readb(M48T86_REG_MONTH) - 1; | ||
| 61 | tm->tm_year = ops->readb(M48T86_REG_YEAR) + 100; | ||
| 62 | tm->tm_wday = ops->readb(M48T86_REG_DOW); | ||
| 63 | } else { | ||
| 64 | /* bcd mode */ | ||
| 65 | tm->tm_sec = BCD2BIN(ops->readb(M48T86_REG_SEC)); | ||
| 66 | tm->tm_min = BCD2BIN(ops->readb(M48T86_REG_MIN)); | ||
| 67 | tm->tm_hour = BCD2BIN(ops->readb(M48T86_REG_HOUR) & 0x3F); | ||
| 68 | tm->tm_mday = BCD2BIN(ops->readb(M48T86_REG_DOM)); | ||
| 69 | /* tm_mon is 0-11 */ | ||
| 70 | tm->tm_mon = BCD2BIN(ops->readb(M48T86_REG_MONTH)) - 1; | ||
| 71 | tm->tm_year = BCD2BIN(ops->readb(M48T86_REG_YEAR)) + 100; | ||
| 72 | tm->tm_wday = BCD2BIN(ops->readb(M48T86_REG_DOW)); | ||
| 73 | } | ||
| 74 | |||
| 75 | /* correct the hour if the clock is in 12h mode */ | ||
| 76 | if (!(reg & M48T86_REG_B_H24)) | ||
| 77 | if (ops->readb(M48T86_REG_HOUR) & 0x80) | ||
| 78 | tm->tm_hour += 12; | ||
| 79 | |||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 84 | { | ||
| 85 | unsigned char reg; | ||
| 86 | struct platform_device *pdev = to_platform_device(dev); | ||
| 87 | struct m48t86_ops *ops = pdev->dev.platform_data; | ||
| 88 | |||
| 89 | reg = ops->readb(M48T86_REG_B); | ||
| 90 | |||
| 91 | /* update flag and 24h mode */ | ||
| 92 | reg |= M48T86_REG_B_SET | M48T86_REG_B_H24; | ||
| 93 | ops->writeb(reg, M48T86_REG_B); | ||
| 94 | |||
| 95 | if (reg & M48T86_REG_B_DM) { | ||
| 96 | /* data (binary) mode */ | ||
| 97 | ops->writeb(tm->tm_sec, M48T86_REG_SEC); | ||
| 98 | ops->writeb(tm->tm_min, M48T86_REG_MIN); | ||
| 99 | ops->writeb(tm->tm_hour, M48T86_REG_HOUR); | ||
| 100 | ops->writeb(tm->tm_mday, M48T86_REG_DOM); | ||
| 101 | ops->writeb(tm->tm_mon + 1, M48T86_REG_MONTH); | ||
| 102 | ops->writeb(tm->tm_year % 100, M48T86_REG_YEAR); | ||
| 103 | ops->writeb(tm->tm_wday, M48T86_REG_DOW); | ||
| 104 | } else { | ||
| 105 | /* bcd mode */ | ||
| 106 | ops->writeb(BIN2BCD(tm->tm_sec), M48T86_REG_SEC); | ||
| 107 | ops->writeb(BIN2BCD(tm->tm_min), M48T86_REG_MIN); | ||
| 108 | ops->writeb(BIN2BCD(tm->tm_hour), M48T86_REG_HOUR); | ||
| 109 | ops->writeb(BIN2BCD(tm->tm_mday), M48T86_REG_DOM); | ||
| 110 | ops->writeb(BIN2BCD(tm->tm_mon + 1), M48T86_REG_MONTH); | ||
| 111 | ops->writeb(BIN2BCD(tm->tm_year % 100), M48T86_REG_YEAR); | ||
| 112 | ops->writeb(BIN2BCD(tm->tm_wday), M48T86_REG_DOW); | ||
| 113 | } | ||
| 114 | |||
| 115 | /* update ended */ | ||
| 116 | reg &= ~M48T86_REG_B_SET; | ||
| 117 | ops->writeb(reg, M48T86_REG_B); | ||
| 118 | |||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 123 | { | ||
| 124 | unsigned char reg; | ||
| 125 | struct platform_device *pdev = to_platform_device(dev); | ||
| 126 | struct m48t86_ops *ops = pdev->dev.platform_data; | ||
| 127 | |||
| 128 | reg = ops->readb(M48T86_REG_B); | ||
| 129 | |||
| 130 | seq_printf(seq, "24hr\t\t: %s\n", | ||
| 131 | (reg & M48T86_REG_B_H24) ? "yes" : "no"); | ||
| 132 | |||
| 133 | seq_printf(seq, "mode\t\t: %s\n", | ||
| 134 | (reg & M48T86_REG_B_DM) ? "binary" : "bcd"); | ||
| 135 | |||
| 136 | reg = ops->readb(M48T86_REG_D); | ||
| 137 | |||
| 138 | seq_printf(seq, "battery\t\t: %s\n", | ||
| 139 | (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted"); | ||
| 140 | |||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | |||
| 144 | static struct rtc_class_ops m48t86_rtc_ops = { | ||
| 145 | .read_time = m48t86_rtc_read_time, | ||
| 146 | .set_time = m48t86_rtc_set_time, | ||
| 147 | .proc = m48t86_rtc_proc, | ||
| 148 | }; | ||
| 149 | |||
| 150 | static int __devinit m48t86_rtc_probe(struct platform_device *dev) | ||
| 151 | { | ||
| 152 | unsigned char reg; | ||
| 153 | struct m48t86_ops *ops = dev->dev.platform_data; | ||
| 154 | struct rtc_device *rtc = rtc_device_register("m48t86", | ||
| 155 | &dev->dev, &m48t86_rtc_ops, THIS_MODULE); | ||
| 156 | |||
| 157 | if (IS_ERR(rtc)) { | ||
| 158 | dev_err(&dev->dev, "unable to register\n"); | ||
| 159 | return PTR_ERR(rtc); | ||
| 160 | } | ||
| 161 | |||
| 162 | platform_set_drvdata(dev, rtc); | ||
| 163 | |||
| 164 | /* read battery status */ | ||
| 165 | reg = ops->readb(M48T86_REG_D); | ||
| 166 | dev_info(&dev->dev, "battery %s\n", | ||
| 167 | (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted"); | ||
| 168 | |||
| 169 | return 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | static int __devexit m48t86_rtc_remove(struct platform_device *dev) | ||
| 173 | { | ||
| 174 | struct rtc_device *rtc = platform_get_drvdata(dev); | ||
| 175 | |||
| 176 | if (rtc) | ||
| 177 | rtc_device_unregister(rtc); | ||
| 178 | |||
| 179 | platform_set_drvdata(dev, NULL); | ||
| 180 | |||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | static struct platform_driver m48t86_rtc_platform_driver = { | ||
| 185 | .driver = { | ||
| 186 | .name = "rtc-m48t86", | ||
| 187 | .owner = THIS_MODULE, | ||
| 188 | }, | ||
| 189 | .probe = m48t86_rtc_probe, | ||
| 190 | .remove = __devexit_p(m48t86_rtc_remove), | ||
| 191 | }; | ||
| 192 | |||
| 193 | static int __init m48t86_rtc_init(void) | ||
| 194 | { | ||
| 195 | return platform_driver_register(&m48t86_rtc_platform_driver); | ||
| 196 | } | ||
| 197 | |||
| 198 | static void __exit m48t86_rtc_exit(void) | ||
| 199 | { | ||
| 200 | platform_driver_unregister(&m48t86_rtc_platform_driver); | ||
| 201 | } | ||
| 202 | |||
| 203 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 204 | MODULE_DESCRIPTION("M48T86 RTC driver"); | ||
| 205 | MODULE_LICENSE("GPL"); | ||
| 206 | MODULE_VERSION(DRV_VERSION); | ||
| 207 | |||
| 208 | module_init(m48t86_rtc_init); | ||
| 209 | module_exit(m48t86_rtc_exit); | ||
diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c new file mode 100644 index 000000000000..d857d45bdbe8 --- /dev/null +++ b/drivers/rtc/rtc-pcf8563.c | |||
| @@ -0,0 +1,353 @@ | |||
| 1 | /* | ||
| 2 | * An I2C driver for the Philips PCF8563 RTC | ||
| 3 | * Copyright 2005-06 Tower Technologies | ||
| 4 | * | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * Maintainers: http://www.nslu2-linux.org/ | ||
| 7 | * | ||
| 8 | * based on the other drivers in this same directory. | ||
| 9 | * | ||
| 10 | * http://www.semiconductors.philips.com/acrobat/datasheets/PCF8563-04.pdf | ||
| 11 | * | ||
| 12 | * This program is free software; you can redistribute it and/or modify | ||
| 13 | * it under the terms of the GNU General Public License version 2 as | ||
| 14 | * published by the Free Software Foundation. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/i2c.h> | ||
| 18 | #include <linux/bcd.h> | ||
| 19 | #include <linux/rtc.h> | ||
| 20 | |||
| 21 | #define DRV_VERSION "0.4.2" | ||
| 22 | |||
| 23 | /* Addresses to scan: none | ||
| 24 | * This chip cannot be reliably autodetected. An empty eeprom | ||
| 25 | * located at 0x51 will pass the validation routine due to | ||
| 26 | * the way the registers are implemented. | ||
| 27 | */ | ||
| 28 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
| 29 | |||
| 30 | /* Module parameters */ | ||
| 31 | I2C_CLIENT_INSMOD; | ||
| 32 | |||
| 33 | #define PCF8563_REG_ST1 0x00 /* status */ | ||
| 34 | #define PCF8563_REG_ST2 0x01 | ||
| 35 | |||
| 36 | #define PCF8563_REG_SC 0x02 /* datetime */ | ||
| 37 | #define PCF8563_REG_MN 0x03 | ||
| 38 | #define PCF8563_REG_HR 0x04 | ||
| 39 | #define PCF8563_REG_DM 0x05 | ||
| 40 | #define PCF8563_REG_DW 0x06 | ||
| 41 | #define PCF8563_REG_MO 0x07 | ||
| 42 | #define PCF8563_REG_YR 0x08 | ||
| 43 | |||
| 44 | #define PCF8563_REG_AMN 0x09 /* alarm */ | ||
| 45 | #define PCF8563_REG_AHR 0x0A | ||
| 46 | #define PCF8563_REG_ADM 0x0B | ||
| 47 | #define PCF8563_REG_ADW 0x0C | ||
| 48 | |||
| 49 | #define PCF8563_REG_CLKO 0x0D /* clock out */ | ||
| 50 | #define PCF8563_REG_TMRC 0x0E /* timer control */ | ||
| 51 | #define PCF8563_REG_TMR 0x0F /* timer */ | ||
| 52 | |||
| 53 | #define PCF8563_SC_LV 0x80 /* low voltage */ | ||
| 54 | #define PCF8563_MO_C 0x80 /* century */ | ||
| 55 | |||
| 56 | static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind); | ||
| 57 | static int pcf8563_detach(struct i2c_client *client); | ||
| 58 | |||
| 59 | /* | ||
| 60 | * In the routines that deal directly with the pcf8563 hardware, we use | ||
| 61 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. | ||
| 62 | */ | ||
| 63 | static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 64 | { | ||
| 65 | unsigned char buf[13] = { PCF8563_REG_ST1 }; | ||
| 66 | |||
| 67 | struct i2c_msg msgs[] = { | ||
| 68 | { client->addr, 0, 1, buf }, /* setup read ptr */ | ||
| 69 | { client->addr, I2C_M_RD, 13, buf }, /* read status + date */ | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* read registers */ | ||
| 73 | if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { | ||
| 74 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 75 | return -EIO; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) | ||
| 79 | dev_info(&client->dev, | ||
| 80 | "low voltage detected, date/time is not reliable.\n"); | ||
| 81 | |||
| 82 | dev_dbg(&client->dev, | ||
| 83 | "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, " | ||
| 84 | "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", | ||
| 85 | __FUNCTION__, | ||
| 86 | buf[0], buf[1], buf[2], buf[3], | ||
| 87 | buf[4], buf[5], buf[6], buf[7], | ||
| 88 | buf[8]); | ||
| 89 | |||
| 90 | |||
| 91 | tm->tm_sec = BCD2BIN(buf[PCF8563_REG_SC] & 0x7F); | ||
| 92 | tm->tm_min = BCD2BIN(buf[PCF8563_REG_MN] & 0x7F); | ||
| 93 | tm->tm_hour = BCD2BIN(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */ | ||
| 94 | tm->tm_mday = BCD2BIN(buf[PCF8563_REG_DM] & 0x3F); | ||
| 95 | tm->tm_wday = buf[PCF8563_REG_DW] & 0x07; | ||
| 96 | tm->tm_mon = BCD2BIN(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ | ||
| 97 | tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR]) | ||
| 98 | + (buf[PCF8563_REG_MO] & PCF8563_MO_C ? 100 : 0); | ||
| 99 | |||
| 100 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | ||
| 101 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 102 | __FUNCTION__, | ||
| 103 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 104 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 105 | |||
| 106 | /* the clock can give out invalid datetime, but we cannot return | ||
| 107 | * -EINVAL otherwise hwclock will refuse to set the time on bootup. | ||
| 108 | */ | ||
| 109 | if (rtc_valid_tm(tm) < 0) | ||
| 110 | dev_err(&client->dev, "retrieved date/time is not valid.\n"); | ||
| 111 | |||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 116 | { | ||
| 117 | int i, err; | ||
| 118 | unsigned char buf[9]; | ||
| 119 | |||
| 120 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " | ||
| 121 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 122 | __FUNCTION__, | ||
| 123 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 124 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 125 | |||
| 126 | /* hours, minutes and seconds */ | ||
| 127 | buf[PCF8563_REG_SC] = BIN2BCD(tm->tm_sec); | ||
| 128 | buf[PCF8563_REG_MN] = BIN2BCD(tm->tm_min); | ||
| 129 | buf[PCF8563_REG_HR] = BIN2BCD(tm->tm_hour); | ||
| 130 | |||
| 131 | buf[PCF8563_REG_DM] = BIN2BCD(tm->tm_mday); | ||
| 132 | |||
| 133 | /* month, 1 - 12 */ | ||
| 134 | buf[PCF8563_REG_MO] = BIN2BCD(tm->tm_mon + 1); | ||
| 135 | |||
| 136 | /* year and century */ | ||
| 137 | buf[PCF8563_REG_YR] = BIN2BCD(tm->tm_year % 100); | ||
| 138 | if (tm->tm_year / 100) | ||
| 139 | buf[PCF8563_REG_MO] |= PCF8563_MO_C; | ||
| 140 | |||
| 141 | buf[PCF8563_REG_DW] = tm->tm_wday & 0x07; | ||
| 142 | |||
| 143 | /* write register's data */ | ||
| 144 | for (i = 0; i < 7; i++) { | ||
| 145 | unsigned char data[2] = { PCF8563_REG_SC + i, | ||
| 146 | buf[PCF8563_REG_SC + i] }; | ||
| 147 | |||
| 148 | err = i2c_master_send(client, data, sizeof(data)); | ||
| 149 | if (err != sizeof(data)) { | ||
| 150 | dev_err(&client->dev, | ||
| 151 | "%s: err=%d addr=%02x, data=%02x\n", | ||
| 152 | __FUNCTION__, err, data[0], data[1]); | ||
| 153 | return -EIO; | ||
| 154 | } | ||
| 155 | }; | ||
| 156 | |||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | struct pcf8563_limit | ||
| 161 | { | ||
| 162 | unsigned char reg; | ||
| 163 | unsigned char mask; | ||
| 164 | unsigned char min; | ||
| 165 | unsigned char max; | ||
| 166 | }; | ||
| 167 | |||
| 168 | static int pcf8563_validate_client(struct i2c_client *client) | ||
| 169 | { | ||
| 170 | int i; | ||
| 171 | |||
| 172 | static const struct pcf8563_limit pattern[] = { | ||
| 173 | /* register, mask, min, max */ | ||
| 174 | { PCF8563_REG_SC, 0x7F, 0, 59 }, | ||
| 175 | { PCF8563_REG_MN, 0x7F, 0, 59 }, | ||
| 176 | { PCF8563_REG_HR, 0x3F, 0, 23 }, | ||
| 177 | { PCF8563_REG_DM, 0x3F, 0, 31 }, | ||
| 178 | { PCF8563_REG_MO, 0x1F, 0, 12 }, | ||
| 179 | }; | ||
| 180 | |||
| 181 | /* check limits (only registers with bcd values) */ | ||
| 182 | for (i = 0; i < ARRAY_SIZE(pattern); i++) { | ||
| 183 | int xfer; | ||
| 184 | unsigned char value; | ||
| 185 | unsigned char buf = pattern[i].reg; | ||
| 186 | |||
| 187 | struct i2c_msg msgs[] = { | ||
| 188 | { client->addr, 0, 1, &buf }, | ||
| 189 | { client->addr, I2C_M_RD, 1, &buf }, | ||
| 190 | }; | ||
| 191 | |||
| 192 | xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | ||
| 193 | |||
| 194 | if (xfer != ARRAY_SIZE(msgs)) { | ||
| 195 | dev_err(&client->adapter->dev, | ||
| 196 | "%s: could not read register 0x%02X\n", | ||
| 197 | __FUNCTION__, pattern[i].reg); | ||
| 198 | |||
| 199 | return -EIO; | ||
| 200 | } | ||
| 201 | |||
| 202 | value = BCD2BIN(buf & pattern[i].mask); | ||
| 203 | |||
| 204 | if (value > pattern[i].max || | ||
| 205 | value < pattern[i].min) { | ||
| 206 | dev_dbg(&client->adapter->dev, | ||
| 207 | "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, " | ||
| 208 | "max=%d, value=%d, raw=0x%02X\n", | ||
| 209 | __FUNCTION__, i, pattern[i].reg, pattern[i].mask, | ||
| 210 | pattern[i].min, pattern[i].max, | ||
| 211 | value, buf); | ||
| 212 | |||
| 213 | return -ENODEV; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | return 0; | ||
| 218 | } | ||
| 219 | |||
| 220 | static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 221 | { | ||
| 222 | return pcf8563_get_datetime(to_i2c_client(dev), tm); | ||
| 223 | } | ||
| 224 | |||
| 225 | static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 226 | { | ||
| 227 | return pcf8563_set_datetime(to_i2c_client(dev), tm); | ||
| 228 | } | ||
| 229 | |||
| 230 | static int pcf8563_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 231 | { | ||
| 232 | seq_printf(seq, "24hr\t\t: yes\n"); | ||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | |||
| 236 | static struct rtc_class_ops pcf8563_rtc_ops = { | ||
| 237 | .proc = pcf8563_rtc_proc, | ||
| 238 | .read_time = pcf8563_rtc_read_time, | ||
| 239 | .set_time = pcf8563_rtc_set_time, | ||
| 240 | }; | ||
| 241 | |||
| 242 | static int pcf8563_attach(struct i2c_adapter *adapter) | ||
| 243 | { | ||
| 244 | return i2c_probe(adapter, &addr_data, pcf8563_probe); | ||
| 245 | } | ||
| 246 | |||
| 247 | static struct i2c_driver pcf8563_driver = { | ||
| 248 | .driver = { | ||
| 249 | .name = "pcf8563", | ||
| 250 | }, | ||
| 251 | .id = I2C_DRIVERID_PCF8563, | ||
| 252 | .attach_adapter = &pcf8563_attach, | ||
| 253 | .detach_client = &pcf8563_detach, | ||
| 254 | }; | ||
| 255 | |||
| 256 | static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind) | ||
| 257 | { | ||
| 258 | struct i2c_client *client; | ||
| 259 | struct rtc_device *rtc; | ||
| 260 | |||
| 261 | int err = 0; | ||
| 262 | |||
| 263 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 264 | |||
| 265 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | ||
| 266 | err = -ENODEV; | ||
| 267 | goto exit; | ||
| 268 | } | ||
| 269 | |||
| 270 | if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { | ||
| 271 | err = -ENOMEM; | ||
| 272 | goto exit; | ||
| 273 | } | ||
| 274 | |||
| 275 | client->addr = address; | ||
| 276 | client->driver = &pcf8563_driver; | ||
| 277 | client->adapter = adapter; | ||
| 278 | |||
| 279 | strlcpy(client->name, pcf8563_driver.driver.name, I2C_NAME_SIZE); | ||
| 280 | |||
| 281 | /* Verify the chip is really an PCF8563 */ | ||
| 282 | if (kind < 0) { | ||
| 283 | if (pcf8563_validate_client(client) < 0) { | ||
| 284 | err = -ENODEV; | ||
| 285 | goto exit_kfree; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | /* Inform the i2c layer */ | ||
| 290 | if ((err = i2c_attach_client(client))) | ||
| 291 | goto exit_kfree; | ||
| 292 | |||
| 293 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | ||
| 294 | |||
| 295 | rtc = rtc_device_register(pcf8563_driver.driver.name, &client->dev, | ||
| 296 | &pcf8563_rtc_ops, THIS_MODULE); | ||
| 297 | |||
| 298 | if (IS_ERR(rtc)) { | ||
| 299 | err = PTR_ERR(rtc); | ||
| 300 | dev_err(&client->dev, | ||
| 301 | "unable to register the class device\n"); | ||
| 302 | goto exit_detach; | ||
| 303 | } | ||
| 304 | |||
| 305 | i2c_set_clientdata(client, rtc); | ||
| 306 | |||
| 307 | return 0; | ||
| 308 | |||
| 309 | exit_detach: | ||
| 310 | i2c_detach_client(client); | ||
| 311 | |||
| 312 | exit_kfree: | ||
| 313 | kfree(client); | ||
| 314 | |||
| 315 | exit: | ||
| 316 | return err; | ||
| 317 | } | ||
| 318 | |||
| 319 | static int pcf8563_detach(struct i2c_client *client) | ||
| 320 | { | ||
| 321 | int err; | ||
| 322 | struct rtc_device *rtc = i2c_get_clientdata(client); | ||
| 323 | |||
| 324 | dev_dbg(&client->dev, "%s\n", __FUNCTION__); | ||
| 325 | |||
| 326 | if (rtc) | ||
| 327 | rtc_device_unregister(rtc); | ||
| 328 | |||
| 329 | if ((err = i2c_detach_client(client))) | ||
| 330 | return err; | ||
| 331 | |||
| 332 | kfree(client); | ||
| 333 | |||
| 334 | return 0; | ||
| 335 | } | ||
| 336 | |||
| 337 | static int __init pcf8563_init(void) | ||
| 338 | { | ||
| 339 | return i2c_add_driver(&pcf8563_driver); | ||
| 340 | } | ||
| 341 | |||
| 342 | static void __exit pcf8563_exit(void) | ||
| 343 | { | ||
| 344 | i2c_del_driver(&pcf8563_driver); | ||
| 345 | } | ||
| 346 | |||
| 347 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 348 | MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver"); | ||
| 349 | MODULE_LICENSE("GPL"); | ||
| 350 | MODULE_VERSION(DRV_VERSION); | ||
| 351 | |||
| 352 | module_init(pcf8563_init); | ||
| 353 | module_exit(pcf8563_exit); | ||
diff --git a/drivers/rtc/rtc-proc.c b/drivers/rtc/rtc-proc.c new file mode 100644 index 000000000000..90b8a97a0919 --- /dev/null +++ b/drivers/rtc/rtc-proc.c | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | /* | ||
| 2 | * RTC subsystem, proc interface | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005-06 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * based on arch/arm/common/rtctime.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/proc_fs.h> | ||
| 17 | #include <linux/seq_file.h> | ||
| 18 | |||
| 19 | static struct class_device *rtc_dev = NULL; | ||
| 20 | static DEFINE_MUTEX(rtc_lock); | ||
| 21 | |||
| 22 | static int rtc_proc_show(struct seq_file *seq, void *offset) | ||
| 23 | { | ||
| 24 | int err; | ||
| 25 | struct class_device *class_dev = seq->private; | ||
| 26 | struct rtc_class_ops *ops = to_rtc_device(class_dev)->ops; | ||
| 27 | struct rtc_wkalrm alrm; | ||
| 28 | struct rtc_time tm; | ||
| 29 | |||
| 30 | err = rtc_read_time(class_dev, &tm); | ||
| 31 | if (err == 0) { | ||
| 32 | seq_printf(seq, | ||
| 33 | "rtc_time\t: %02d:%02d:%02d\n" | ||
| 34 | "rtc_date\t: %04d-%02d-%02d\n", | ||
| 35 | tm.tm_hour, tm.tm_min, tm.tm_sec, | ||
| 36 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); | ||
| 37 | } | ||
| 38 | |||
| 39 | err = rtc_read_alarm(class_dev, &alrm); | ||
| 40 | if (err == 0) { | ||
| 41 | seq_printf(seq, "alrm_time\t: "); | ||
| 42 | if ((unsigned int)alrm.time.tm_hour <= 24) | ||
| 43 | seq_printf(seq, "%02d:", alrm.time.tm_hour); | ||
| 44 | else | ||
| 45 | seq_printf(seq, "**:"); | ||
| 46 | if ((unsigned int)alrm.time.tm_min <= 59) | ||
| 47 | seq_printf(seq, "%02d:", alrm.time.tm_min); | ||
| 48 | else | ||
| 49 | seq_printf(seq, "**:"); | ||
| 50 | if ((unsigned int)alrm.time.tm_sec <= 59) | ||
| 51 | seq_printf(seq, "%02d\n", alrm.time.tm_sec); | ||
| 52 | else | ||
| 53 | seq_printf(seq, "**\n"); | ||
| 54 | |||
| 55 | seq_printf(seq, "alrm_date\t: "); | ||
| 56 | if ((unsigned int)alrm.time.tm_year <= 200) | ||
| 57 | seq_printf(seq, "%04d-", alrm.time.tm_year + 1900); | ||
| 58 | else | ||
| 59 | seq_printf(seq, "****-"); | ||
| 60 | if ((unsigned int)alrm.time.tm_mon <= 11) | ||
| 61 | seq_printf(seq, "%02d-", alrm.time.tm_mon + 1); | ||
| 62 | else | ||
| 63 | seq_printf(seq, "**-"); | ||
| 64 | if ((unsigned int)alrm.time.tm_mday <= 31) | ||
| 65 | seq_printf(seq, "%02d\n", alrm.time.tm_mday); | ||
| 66 | else | ||
| 67 | seq_printf(seq, "**\n"); | ||
| 68 | seq_printf(seq, "alrm_wakeup\t: %s\n", | ||
| 69 | alrm.enabled ? "yes" : "no"); | ||
| 70 | seq_printf(seq, "alrm_pending\t: %s\n", | ||
| 71 | alrm.pending ? "yes" : "no"); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (ops->proc) | ||
| 75 | ops->proc(class_dev->dev, seq); | ||
| 76 | |||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | static int rtc_proc_open(struct inode *inode, struct file *file) | ||
| 81 | { | ||
| 82 | struct class_device *class_dev = PDE(inode)->data; | ||
| 83 | |||
| 84 | if (!try_module_get(THIS_MODULE)) | ||
| 85 | return -ENODEV; | ||
| 86 | |||
| 87 | return single_open(file, rtc_proc_show, class_dev); | ||
| 88 | } | ||
| 89 | |||
| 90 | static int rtc_proc_release(struct inode *inode, struct file *file) | ||
| 91 | { | ||
| 92 | int res = single_release(inode, file); | ||
| 93 | module_put(THIS_MODULE); | ||
| 94 | return res; | ||
| 95 | } | ||
| 96 | |||
| 97 | static struct file_operations rtc_proc_fops = { | ||
| 98 | .open = rtc_proc_open, | ||
| 99 | .read = seq_read, | ||
| 100 | .llseek = seq_lseek, | ||
| 101 | .release = rtc_proc_release, | ||
| 102 | }; | ||
| 103 | |||
| 104 | static int rtc_proc_add_device(struct class_device *class_dev, | ||
| 105 | struct class_interface *class_intf) | ||
| 106 | { | ||
| 107 | mutex_lock(&rtc_lock); | ||
| 108 | if (rtc_dev == NULL) { | ||
| 109 | struct proc_dir_entry *ent; | ||
| 110 | |||
| 111 | rtc_dev = class_dev; | ||
| 112 | |||
| 113 | ent = create_proc_entry("driver/rtc", 0, NULL); | ||
| 114 | if (ent) { | ||
| 115 | struct rtc_device *rtc = to_rtc_device(class_dev); | ||
| 116 | |||
| 117 | ent->proc_fops = &rtc_proc_fops; | ||
| 118 | ent->owner = rtc->owner; | ||
| 119 | ent->data = class_dev; | ||
| 120 | |||
| 121 | dev_info(class_dev->dev, "rtc intf: proc\n"); | ||
| 122 | } | ||
| 123 | else | ||
| 124 | rtc_dev = NULL; | ||
| 125 | } | ||
| 126 | mutex_unlock(&rtc_lock); | ||
| 127 | |||
| 128 | return 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | static void rtc_proc_remove_device(struct class_device *class_dev, | ||
| 132 | struct class_interface *class_intf) | ||
| 133 | { | ||
| 134 | mutex_lock(&rtc_lock); | ||
| 135 | if (rtc_dev == class_dev) { | ||
| 136 | remove_proc_entry("driver/rtc", NULL); | ||
| 137 | rtc_dev = NULL; | ||
| 138 | } | ||
| 139 | mutex_unlock(&rtc_lock); | ||
| 140 | } | ||
| 141 | |||
| 142 | static struct class_interface rtc_proc_interface = { | ||
| 143 | .add = &rtc_proc_add_device, | ||
| 144 | .remove = &rtc_proc_remove_device, | ||
| 145 | }; | ||
| 146 | |||
| 147 | static int __init rtc_proc_init(void) | ||
| 148 | { | ||
| 149 | return rtc_interface_register(&rtc_proc_interface); | ||
| 150 | } | ||
| 151 | |||
| 152 | static void __exit rtc_proc_exit(void) | ||
| 153 | { | ||
| 154 | class_interface_unregister(&rtc_proc_interface); | ||
| 155 | } | ||
| 156 | |||
| 157 | module_init(rtc_proc_init); | ||
| 158 | module_exit(rtc_proc_exit); | ||
| 159 | |||
| 160 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 161 | MODULE_DESCRIPTION("RTC class proc interface"); | ||
| 162 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c new file mode 100644 index 000000000000..396c8681f66c --- /dev/null +++ b/drivers/rtc/rtc-rs5c372.c | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | /* | ||
| 2 | * An I2C driver for the Ricoh RS5C372 RTC | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net> | ||
| 5 | * Copyright (C) 2006 Tower Technologies | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/i2c.h> | ||
| 13 | #include <linux/rtc.h> | ||
| 14 | #include <linux/bcd.h> | ||
| 15 | |||
| 16 | #define DRV_VERSION "0.2" | ||
| 17 | |||
| 18 | /* Addresses to scan */ | ||
| 19 | static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END }; | ||
| 20 | |||
| 21 | /* Insmod parameters */ | ||
| 22 | I2C_CLIENT_INSMOD; | ||
| 23 | |||
| 24 | #define RS5C372_REG_SECS 0 | ||
| 25 | #define RS5C372_REG_MINS 1 | ||
| 26 | #define RS5C372_REG_HOURS 2 | ||
| 27 | #define RS5C372_REG_WDAY 3 | ||
| 28 | #define RS5C372_REG_DAY 4 | ||
| 29 | #define RS5C372_REG_MONTH 5 | ||
| 30 | #define RS5C372_REG_YEAR 6 | ||
| 31 | #define RS5C372_REG_TRIM 7 | ||
| 32 | |||
| 33 | #define RS5C372_TRIM_XSL 0x80 | ||
| 34 | #define RS5C372_TRIM_MASK 0x7F | ||
| 35 | |||
| 36 | #define RS5C372_REG_BASE 0 | ||
| 37 | |||
| 38 | static int rs5c372_attach(struct i2c_adapter *adapter); | ||
| 39 | static int rs5c372_detach(struct i2c_client *client); | ||
| 40 | static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind); | ||
| 41 | |||
| 42 | static struct i2c_driver rs5c372_driver = { | ||
| 43 | .driver = { | ||
| 44 | .name = "rs5c372", | ||
| 45 | }, | ||
| 46 | .attach_adapter = &rs5c372_attach, | ||
| 47 | .detach_client = &rs5c372_detach, | ||
| 48 | }; | ||
| 49 | |||
| 50 | static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 51 | { | ||
| 52 | unsigned char buf[7] = { RS5C372_REG_BASE }; | ||
| 53 | |||
| 54 | /* this implements the 1st reading method, according | ||
| 55 | * to the datasheet. buf[0] is initialized with | ||
| 56 | * address ptr and transmission format register. | ||
| 57 | */ | ||
| 58 | struct i2c_msg msgs[] = { | ||
| 59 | { client->addr, 0, 1, buf }, | ||
| 60 | { client->addr, I2C_M_RD, 7, buf }, | ||
| 61 | }; | ||
| 62 | |||
| 63 | if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { | ||
| 64 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 65 | return -EIO; | ||
| 66 | } | ||
| 67 | |||
| 68 | tm->tm_sec = BCD2BIN(buf[RS5C372_REG_SECS] & 0x7f); | ||
| 69 | tm->tm_min = BCD2BIN(buf[RS5C372_REG_MINS] & 0x7f); | ||
| 70 | tm->tm_hour = BCD2BIN(buf[RS5C372_REG_HOURS] & 0x3f); | ||
| 71 | tm->tm_wday = BCD2BIN(buf[RS5C372_REG_WDAY] & 0x07); | ||
| 72 | tm->tm_mday = BCD2BIN(buf[RS5C372_REG_DAY] & 0x3f); | ||
| 73 | |||
| 74 | /* tm->tm_mon is zero-based */ | ||
| 75 | tm->tm_mon = BCD2BIN(buf[RS5C372_REG_MONTH] & 0x1f) - 1; | ||
| 76 | |||
| 77 | /* year is 1900 + tm->tm_year */ | ||
| 78 | tm->tm_year = BCD2BIN(buf[RS5C372_REG_YEAR]) + 100; | ||
| 79 | |||
| 80 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | ||
| 81 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 82 | __FUNCTION__, | ||
| 83 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 84 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 85 | |||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
| 90 | { | ||
| 91 | unsigned char buf[8] = { RS5C372_REG_BASE }; | ||
| 92 | |||
| 93 | dev_dbg(&client->dev, | ||
| 94 | "%s: secs=%d, mins=%d, hours=%d ", | ||
| 95 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 96 | __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 97 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 98 | |||
| 99 | buf[1] = BIN2BCD(tm->tm_sec); | ||
| 100 | buf[2] = BIN2BCD(tm->tm_min); | ||
| 101 | buf[3] = BIN2BCD(tm->tm_hour); | ||
| 102 | buf[4] = BIN2BCD(tm->tm_wday); | ||
| 103 | buf[5] = BIN2BCD(tm->tm_mday); | ||
| 104 | buf[6] = BIN2BCD(tm->tm_mon + 1); | ||
| 105 | buf[7] = BIN2BCD(tm->tm_year - 100); | ||
| 106 | |||
| 107 | if ((i2c_master_send(client, buf, 8)) != 8) { | ||
| 108 | dev_err(&client->dev, "%s: write error\n", __FUNCTION__); | ||
| 109 | return -EIO; | ||
| 110 | } | ||
| 111 | |||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim) | ||
| 116 | { | ||
| 117 | unsigned char buf = RS5C372_REG_TRIM; | ||
| 118 | |||
| 119 | struct i2c_msg msgs[] = { | ||
| 120 | { client->addr, 0, 1, &buf }, | ||
| 121 | { client->addr, I2C_M_RD, 1, &buf }, | ||
| 122 | }; | ||
| 123 | |||
| 124 | if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { | ||
| 125 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 126 | return -EIO; | ||
| 127 | } | ||
| 128 | |||
| 129 | dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, trim); | ||
| 130 | |||
| 131 | if (osc) | ||
| 132 | *osc = (buf & RS5C372_TRIM_XSL) ? 32000 : 32768; | ||
| 133 | |||
| 134 | if (trim) | ||
| 135 | *trim = buf & RS5C372_TRIM_MASK; | ||
| 136 | |||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 141 | { | ||
| 142 | return rs5c372_get_datetime(to_i2c_client(dev), tm); | ||
| 143 | } | ||
| 144 | |||
| 145 | static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 146 | { | ||
| 147 | return rs5c372_set_datetime(to_i2c_client(dev), tm); | ||
| 148 | } | ||
| 149 | |||
| 150 | static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 151 | { | ||
| 152 | int err, osc, trim; | ||
| 153 | |||
| 154 | seq_printf(seq, "24hr\t\t: yes\n"); | ||
| 155 | |||
| 156 | if ((err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim)) == 0) { | ||
| 157 | seq_printf(seq, "%d.%03d KHz\n", osc / 1000, osc % 1000); | ||
| 158 | seq_printf(seq, "trim\t: %d\n", trim); | ||
| 159 | } | ||
| 160 | |||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | |||
| 164 | static struct rtc_class_ops rs5c372_rtc_ops = { | ||
| 165 | .proc = rs5c372_rtc_proc, | ||
| 166 | .read_time = rs5c372_rtc_read_time, | ||
| 167 | .set_time = rs5c372_rtc_set_time, | ||
| 168 | }; | ||
| 169 | |||
| 170 | static ssize_t rs5c372_sysfs_show_trim(struct device *dev, | ||
| 171 | struct device_attribute *attr, char *buf) | ||
| 172 | { | ||
| 173 | int trim; | ||
| 174 | |||
| 175 | if (rs5c372_get_trim(to_i2c_client(dev), NULL, &trim) == 0) | ||
| 176 | return sprintf(buf, "0x%2x\n", trim); | ||
| 177 | |||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL); | ||
| 181 | |||
| 182 | static ssize_t rs5c372_sysfs_show_osc(struct device *dev, | ||
| 183 | struct device_attribute *attr, char *buf) | ||
| 184 | { | ||
| 185 | int osc; | ||
| 186 | |||
| 187 | if (rs5c372_get_trim(to_i2c_client(dev), &osc, NULL) == 0) | ||
| 188 | return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000); | ||
| 189 | |||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL); | ||
| 193 | |||
| 194 | static int rs5c372_attach(struct i2c_adapter *adapter) | ||
| 195 | { | ||
| 196 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 197 | return i2c_probe(adapter, &addr_data, rs5c372_probe); | ||
| 198 | } | ||
| 199 | |||
| 200 | static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) | ||
| 201 | { | ||
| 202 | int err = 0; | ||
| 203 | struct i2c_client *client; | ||
| 204 | struct rtc_device *rtc; | ||
| 205 | |||
| 206 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 207 | |||
| 208 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | ||
| 209 | err = -ENODEV; | ||
| 210 | goto exit; | ||
| 211 | } | ||
| 212 | |||
| 213 | if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { | ||
| 214 | err = -ENOMEM; | ||
| 215 | goto exit; | ||
| 216 | } | ||
| 217 | |||
| 218 | /* I2C client */ | ||
| 219 | client->addr = address; | ||
| 220 | client->driver = &rs5c372_driver; | ||
| 221 | client->adapter = adapter; | ||
| 222 | |||
| 223 | strlcpy(client->name, rs5c372_driver.driver.name, I2C_NAME_SIZE); | ||
| 224 | |||
| 225 | /* Inform the i2c layer */ | ||
| 226 | if ((err = i2c_attach_client(client))) | ||
| 227 | goto exit_kfree; | ||
| 228 | |||
| 229 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | ||
| 230 | |||
| 231 | rtc = rtc_device_register(rs5c372_driver.driver.name, &client->dev, | ||
| 232 | &rs5c372_rtc_ops, THIS_MODULE); | ||
| 233 | |||
| 234 | if (IS_ERR(rtc)) { | ||
| 235 | err = PTR_ERR(rtc); | ||
| 236 | dev_err(&client->dev, | ||
| 237 | "unable to register the class device\n"); | ||
| 238 | goto exit_detach; | ||
| 239 | } | ||
| 240 | |||
| 241 | i2c_set_clientdata(client, rtc); | ||
| 242 | |||
| 243 | device_create_file(&client->dev, &dev_attr_trim); | ||
| 244 | device_create_file(&client->dev, &dev_attr_osc); | ||
| 245 | |||
| 246 | return 0; | ||
| 247 | |||
| 248 | exit_detach: | ||
| 249 | i2c_detach_client(client); | ||
| 250 | |||
| 251 | exit_kfree: | ||
| 252 | kfree(client); | ||
| 253 | |||
| 254 | exit: | ||
| 255 | return err; | ||
| 256 | } | ||
| 257 | |||
| 258 | static int rs5c372_detach(struct i2c_client *client) | ||
| 259 | { | ||
| 260 | int err; | ||
| 261 | struct rtc_device *rtc = i2c_get_clientdata(client); | ||
| 262 | |||
| 263 | dev_dbg(&client->dev, "%s\n", __FUNCTION__); | ||
| 264 | |||
| 265 | if (rtc) | ||
| 266 | rtc_device_unregister(rtc); | ||
| 267 | |||
| 268 | if ((err = i2c_detach_client(client))) | ||
| 269 | return err; | ||
| 270 | |||
| 271 | kfree(client); | ||
| 272 | |||
| 273 | return 0; | ||
| 274 | } | ||
| 275 | |||
| 276 | static __init int rs5c372_init(void) | ||
| 277 | { | ||
| 278 | return i2c_add_driver(&rs5c372_driver); | ||
| 279 | } | ||
| 280 | |||
| 281 | static __exit void rs5c372_exit(void) | ||
| 282 | { | ||
| 283 | i2c_del_driver(&rs5c372_driver); | ||
| 284 | } | ||
| 285 | |||
| 286 | module_init(rs5c372_init); | ||
| 287 | module_exit(rs5c372_exit); | ||
| 288 | |||
| 289 | MODULE_AUTHOR( | ||
| 290 | "Pavel Mironchik <pmironchik@optifacio.net>, " | ||
| 291 | "Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 292 | MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver"); | ||
| 293 | MODULE_LICENSE("GPL"); | ||
| 294 | MODULE_VERSION(DRV_VERSION); | ||
diff --git a/drivers/rtc/rtc-sa1100.c b/drivers/rtc/rtc-sa1100.c new file mode 100644 index 000000000000..83b2bb480a16 --- /dev/null +++ b/drivers/rtc/rtc-sa1100.c | |||
| @@ -0,0 +1,388 @@ | |||
| 1 | /* | ||
| 2 | * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx | ||
| 3 | * | ||
| 4 | * Copyright (c) 2000 Nils Faerber | ||
| 5 | * | ||
| 6 | * Based on rtc.c by Paul Gortmaker | ||
| 7 | * | ||
| 8 | * Original Driver by Nils Faerber <nils@kernelconcepts.de> | ||
| 9 | * | ||
| 10 | * Modifications from: | ||
| 11 | * CIH <cih@coventive.com> | ||
| 12 | * Nicolas Pitre <nico@cam.org> | ||
| 13 | * Andrew Christian <andrew.christian@hp.com> | ||
| 14 | * | ||
| 15 | * Converted to the RTC subsystem and Driver Model | ||
| 16 | * by Richard Purdie <rpurdie@rpsys.net> | ||
| 17 | * | ||
| 18 | * This program is free software; you can redistribute it and/or | ||
| 19 | * modify it under the terms of the GNU General Public License | ||
| 20 | * as published by the Free Software Foundation; either version | ||
| 21 | * 2 of the License, or (at your option) any later version. | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <linux/platform_device.h> | ||
| 25 | #include <linux/module.h> | ||
| 26 | #include <linux/rtc.h> | ||
| 27 | #include <linux/init.h> | ||
| 28 | #include <linux/fs.h> | ||
| 29 | #include <linux/interrupt.h> | ||
| 30 | #include <linux/string.h> | ||
| 31 | #include <linux/pm.h> | ||
| 32 | |||
| 33 | #include <asm/bitops.h> | ||
| 34 | #include <asm/hardware.h> | ||
| 35 | #include <asm/irq.h> | ||
| 36 | #include <asm/rtc.h> | ||
| 37 | |||
| 38 | #ifdef CONFIG_ARCH_PXA | ||
| 39 | #include <asm/arch/pxa-regs.h> | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #define TIMER_FREQ CLOCK_TICK_RATE | ||
| 43 | #define RTC_DEF_DIVIDER 32768 - 1 | ||
| 44 | #define RTC_DEF_TRIM 0 | ||
| 45 | |||
| 46 | static unsigned long rtc_freq = 1024; | ||
| 47 | static struct rtc_time rtc_alarm; | ||
| 48 | static spinlock_t sa1100_rtc_lock = SPIN_LOCK_UNLOCKED; | ||
| 49 | |||
| 50 | static int rtc_update_alarm(struct rtc_time *alrm) | ||
| 51 | { | ||
| 52 | struct rtc_time alarm_tm, now_tm; | ||
| 53 | unsigned long now, time; | ||
| 54 | int ret; | ||
| 55 | |||
| 56 | do { | ||
| 57 | now = RCNR; | ||
| 58 | rtc_time_to_tm(now, &now_tm); | ||
| 59 | rtc_next_alarm_time(&alarm_tm, &now_tm, alrm); | ||
| 60 | ret = rtc_tm_to_time(&alarm_tm, &time); | ||
| 61 | if (ret != 0) | ||
| 62 | break; | ||
| 63 | |||
| 64 | RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL); | ||
| 65 | RTAR = time; | ||
| 66 | } while (now != RCNR); | ||
| 67 | |||
| 68 | return ret; | ||
| 69 | } | ||
| 70 | |||
| 71 | static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id, | ||
| 72 | struct pt_regs *regs) | ||
| 73 | { | ||
| 74 | struct platform_device *pdev = to_platform_device(dev_id); | ||
| 75 | struct rtc_device *rtc = platform_get_drvdata(pdev); | ||
| 76 | unsigned int rtsr; | ||
| 77 | unsigned long events = 0; | ||
| 78 | |||
| 79 | spin_lock(&sa1100_rtc_lock); | ||
| 80 | |||
| 81 | rtsr = RTSR; | ||
| 82 | /* clear interrupt sources */ | ||
| 83 | RTSR = 0; | ||
| 84 | RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2); | ||
| 85 | |||
| 86 | /* clear alarm interrupt if it has occurred */ | ||
| 87 | if (rtsr & RTSR_AL) | ||
| 88 | rtsr &= ~RTSR_ALE; | ||
| 89 | RTSR = rtsr & (RTSR_ALE | RTSR_HZE); | ||
| 90 | |||
| 91 | /* update irq data & counter */ | ||
| 92 | if (rtsr & RTSR_AL) | ||
| 93 | events |= RTC_AF | RTC_IRQF; | ||
| 94 | if (rtsr & RTSR_HZ) | ||
| 95 | events |= RTC_UF | RTC_IRQF; | ||
| 96 | |||
| 97 | rtc_update_irq(&rtc->class_dev, 1, events); | ||
| 98 | |||
| 99 | if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm)) | ||
| 100 | rtc_update_alarm(&rtc_alarm); | ||
| 101 | |||
| 102 | spin_unlock(&sa1100_rtc_lock); | ||
| 103 | |||
| 104 | return IRQ_HANDLED; | ||
| 105 | } | ||
| 106 | |||
| 107 | static int rtc_timer1_count; | ||
| 108 | |||
| 109 | static irqreturn_t timer1_interrupt(int irq, void *dev_id, | ||
| 110 | struct pt_regs *regs) | ||
| 111 | { | ||
| 112 | struct platform_device *pdev = to_platform_device(dev_id); | ||
| 113 | struct rtc_device *rtc = platform_get_drvdata(pdev); | ||
| 114 | |||
| 115 | /* | ||
| 116 | * If we match for the first time, rtc_timer1_count will be 1. | ||
| 117 | * Otherwise, we wrapped around (very unlikely but | ||
| 118 | * still possible) so compute the amount of missed periods. | ||
| 119 | * The match reg is updated only when the data is actually retrieved | ||
| 120 | * to avoid unnecessary interrupts. | ||
| 121 | */ | ||
| 122 | OSSR = OSSR_M1; /* clear match on timer1 */ | ||
| 123 | |||
| 124 | rtc_update_irq(&rtc->class_dev, rtc_timer1_count, RTC_PF | RTC_IRQF); | ||
| 125 | |||
| 126 | if (rtc_timer1_count == 1) | ||
| 127 | rtc_timer1_count = (rtc_freq * ((1<<30)/(TIMER_FREQ>>2))); | ||
| 128 | |||
| 129 | return IRQ_HANDLED; | ||
| 130 | } | ||
| 131 | |||
| 132 | static int sa1100_rtc_read_callback(struct device *dev, int data) | ||
| 133 | { | ||
| 134 | if (data & RTC_PF) { | ||
| 135 | /* interpolate missed periods and set match for the next */ | ||
| 136 | unsigned long period = TIMER_FREQ/rtc_freq; | ||
| 137 | unsigned long oscr = OSCR; | ||
| 138 | unsigned long osmr1 = OSMR1; | ||
| 139 | unsigned long missed = (oscr - osmr1)/period; | ||
| 140 | data += missed << 8; | ||
| 141 | OSSR = OSSR_M1; /* clear match on timer 1 */ | ||
| 142 | OSMR1 = osmr1 + (missed + 1)*period; | ||
| 143 | /* Ensure we didn't miss another match in the mean time. | ||
| 144 | * Here we compare (match - OSCR) 8 instead of 0 -- | ||
| 145 | * see comment in pxa_timer_interrupt() for explanation. | ||
| 146 | */ | ||
| 147 | while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) { | ||
| 148 | data += 0x100; | ||
| 149 | OSSR = OSSR_M1; /* clear match on timer 1 */ | ||
| 150 | OSMR1 = osmr1 + period; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | return data; | ||
| 154 | } | ||
| 155 | |||
| 156 | static int sa1100_rtc_open(struct device *dev) | ||
| 157 | { | ||
| 158 | int ret; | ||
| 159 | |||
| 160 | ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, SA_INTERRUPT, | ||
| 161 | "rtc 1Hz", dev); | ||
| 162 | if (ret) { | ||
| 163 | printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_RTC1Hz); | ||
| 164 | goto fail_ui; | ||
| 165 | } | ||
| 166 | ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, SA_INTERRUPT, | ||
| 167 | "rtc Alrm", dev); | ||
| 168 | if (ret) { | ||
| 169 | printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_RTCAlrm); | ||
| 170 | goto fail_ai; | ||
| 171 | } | ||
| 172 | ret = request_irq(IRQ_OST1, timer1_interrupt, SA_INTERRUPT, | ||
| 173 | "rtc timer", dev); | ||
| 174 | if (ret) { | ||
| 175 | printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_OST1); | ||
| 176 | goto fail_pi; | ||
| 177 | } | ||
| 178 | return 0; | ||
| 179 | |||
| 180 | fail_pi: | ||
| 181 | free_irq(IRQ_RTCAlrm, NULL); | ||
| 182 | fail_ai: | ||
| 183 | free_irq(IRQ_RTC1Hz, NULL); | ||
| 184 | fail_ui: | ||
| 185 | return ret; | ||
| 186 | } | ||
| 187 | |||
| 188 | static void sa1100_rtc_release(struct device *dev) | ||
| 189 | { | ||
| 190 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 191 | RTSR = 0; | ||
| 192 | OIER &= ~OIER_E1; | ||
| 193 | OSSR = OSSR_M1; | ||
| 194 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 195 | |||
| 196 | free_irq(IRQ_OST1, dev); | ||
| 197 | free_irq(IRQ_RTCAlrm, dev); | ||
| 198 | free_irq(IRQ_RTC1Hz, dev); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd, | ||
| 203 | unsigned long arg) | ||
| 204 | { | ||
| 205 | switch(cmd) { | ||
| 206 | case RTC_AIE_OFF: | ||
| 207 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 208 | RTSR &= ~RTSR_ALE; | ||
| 209 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 210 | return 0; | ||
| 211 | case RTC_AIE_ON: | ||
| 212 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 213 | RTSR |= RTSR_ALE; | ||
| 214 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 215 | return 0; | ||
| 216 | case RTC_UIE_OFF: | ||
| 217 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 218 | RTSR &= ~RTSR_HZE; | ||
| 219 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 220 | return 0; | ||
| 221 | case RTC_UIE_ON: | ||
| 222 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 223 | RTSR |= RTSR_HZE; | ||
| 224 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 225 | return 0; | ||
| 226 | case RTC_PIE_OFF: | ||
| 227 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 228 | OIER &= ~OIER_E1; | ||
| 229 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 230 | return 0; | ||
| 231 | case RTC_PIE_ON: | ||
| 232 | if ((rtc_freq > 64) && !capable(CAP_SYS_RESOURCE)) | ||
| 233 | return -EACCES; | ||
| 234 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 235 | OSMR1 = TIMER_FREQ/rtc_freq + OSCR; | ||
| 236 | OIER |= OIER_E1; | ||
| 237 | rtc_timer1_count = 1; | ||
| 238 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 239 | return 0; | ||
| 240 | case RTC_IRQP_READ: | ||
| 241 | return put_user(rtc_freq, (unsigned long *)arg); | ||
| 242 | case RTC_IRQP_SET: | ||
| 243 | if (arg < 1 || arg > TIMER_FREQ) | ||
| 244 | return -EINVAL; | ||
| 245 | if ((arg > 64) && (!capable(CAP_SYS_RESOURCE))) | ||
| 246 | return -EACCES; | ||
| 247 | rtc_freq = arg; | ||
| 248 | return 0; | ||
| 249 | } | ||
| 250 | return -EINVAL; | ||
| 251 | } | ||
| 252 | |||
| 253 | static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 254 | { | ||
| 255 | rtc_time_to_tm(RCNR, tm); | ||
| 256 | return 0; | ||
| 257 | } | ||
| 258 | |||
| 259 | static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 260 | { | ||
| 261 | unsigned long time; | ||
| 262 | int ret; | ||
| 263 | |||
| 264 | ret = rtc_tm_to_time(tm, &time); | ||
| 265 | if (ret == 0) | ||
| 266 | RCNR = time; | ||
| 267 | return ret; | ||
| 268 | } | ||
| 269 | |||
| 270 | static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 271 | { | ||
| 272 | memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time)); | ||
| 273 | alrm->pending = RTSR & RTSR_AL ? 1 : 0; | ||
| 274 | return 0; | ||
| 275 | } | ||
| 276 | |||
| 277 | static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 278 | { | ||
| 279 | int ret; | ||
| 280 | |||
| 281 | spin_lock_irq(&sa1100_rtc_lock); | ||
| 282 | ret = rtc_update_alarm(&alrm->time); | ||
| 283 | if (ret == 0) { | ||
| 284 | memcpy(&rtc_alarm, &alrm->time, sizeof(struct rtc_time)); | ||
| 285 | |||
| 286 | if (alrm->enabled) | ||
| 287 | enable_irq_wake(IRQ_RTCAlrm); | ||
| 288 | else | ||
| 289 | disable_irq_wake(IRQ_RTCAlrm); | ||
| 290 | } | ||
| 291 | spin_unlock_irq(&sa1100_rtc_lock); | ||
| 292 | |||
| 293 | return ret; | ||
| 294 | } | ||
| 295 | |||
| 296 | static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 297 | { | ||
| 298 | seq_printf(seq, "trim/divider\t: 0x%08x\n", RTTR); | ||
| 299 | seq_printf(seq, "alarm_IRQ\t: %s\n", | ||
| 300 | (RTSR & RTSR_ALE) ? "yes" : "no" ); | ||
| 301 | seq_printf(seq, "update_IRQ\t: %s\n", | ||
| 302 | (RTSR & RTSR_HZE) ? "yes" : "no"); | ||
| 303 | seq_printf(seq, "periodic_IRQ\t: %s\n", | ||
| 304 | (OIER & OIER_E1) ? "yes" : "no"); | ||
| 305 | seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq); | ||
| 306 | |||
| 307 | return 0; | ||
| 308 | } | ||
| 309 | |||
| 310 | static struct rtc_class_ops sa1100_rtc_ops = { | ||
| 311 | .open = sa1100_rtc_open, | ||
| 312 | .read_callback = sa1100_rtc_read_callback, | ||
| 313 | .release = sa1100_rtc_release, | ||
| 314 | .ioctl = sa1100_rtc_ioctl, | ||
| 315 | .read_time = sa1100_rtc_read_time, | ||
| 316 | .set_time = sa1100_rtc_set_time, | ||
| 317 | .read_alarm = sa1100_rtc_read_alarm, | ||
| 318 | .set_alarm = sa1100_rtc_set_alarm, | ||
| 319 | .proc = sa1100_rtc_proc, | ||
| 320 | }; | ||
| 321 | |||
| 322 | static int sa1100_rtc_probe(struct platform_device *pdev) | ||
| 323 | { | ||
| 324 | struct rtc_device *rtc; | ||
| 325 | |||
| 326 | /* | ||
| 327 | * According to the manual we should be able to let RTTR be zero | ||
| 328 | * and then a default diviser for a 32.768KHz clock is used. | ||
| 329 | * Apparently this doesn't work, at least for my SA1110 rev 5. | ||
| 330 | * If the clock divider is uninitialized then reset it to the | ||
| 331 | * default value to get the 1Hz clock. | ||
| 332 | */ | ||
| 333 | if (RTTR == 0) { | ||
| 334 | RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); | ||
| 335 | printk(KERN_WARNING "rtc: warning: initializing default clock divider/trim value\n"); | ||
| 336 | /* The current RTC value probably doesn't make sense either */ | ||
| 337 | RCNR = 0; | ||
| 338 | } | ||
| 339 | |||
| 340 | rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops, | ||
| 341 | THIS_MODULE); | ||
| 342 | |||
| 343 | if (IS_ERR(rtc)) { | ||
| 344 | dev_err(&pdev->dev, "Unable to register the RTC device\n"); | ||
| 345 | return PTR_ERR(rtc); | ||
| 346 | } | ||
| 347 | |||
| 348 | platform_set_drvdata(pdev, rtc); | ||
| 349 | |||
| 350 | dev_info(&pdev->dev, "SA11xx/PXA2xx RTC Registered\n"); | ||
| 351 | |||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | static int sa1100_rtc_remove(struct platform_device *pdev) | ||
| 356 | { | ||
| 357 | struct rtc_device *rtc = platform_get_drvdata(pdev); | ||
| 358 | |||
| 359 | if (rtc) | ||
| 360 | rtc_device_unregister(rtc); | ||
| 361 | |||
| 362 | return 0; | ||
| 363 | } | ||
| 364 | |||
| 365 | static struct platform_driver sa1100_rtc_driver = { | ||
| 366 | .probe = sa1100_rtc_probe, | ||
| 367 | .remove = sa1100_rtc_remove, | ||
| 368 | .driver = { | ||
| 369 | .name = "sa1100-rtc", | ||
| 370 | }, | ||
| 371 | }; | ||
| 372 | |||
| 373 | static int __init sa1100_rtc_init(void) | ||
| 374 | { | ||
| 375 | return platform_driver_register(&sa1100_rtc_driver); | ||
| 376 | } | ||
| 377 | |||
| 378 | static void __exit sa1100_rtc_exit(void) | ||
| 379 | { | ||
| 380 | platform_driver_unregister(&sa1100_rtc_driver); | ||
| 381 | } | ||
| 382 | |||
| 383 | module_init(sa1100_rtc_init); | ||
| 384 | module_exit(sa1100_rtc_exit); | ||
| 385 | |||
| 386 | MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>"); | ||
| 387 | MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)"); | ||
| 388 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c new file mode 100644 index 000000000000..7c1f3d2e53c4 --- /dev/null +++ b/drivers/rtc/rtc-sysfs.c | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | /* | ||
| 2 | * RTC subsystem, sysfs interface | ||
| 3 | * | ||
| 4 | * Copyright (C) 2005 Tower Technologies | ||
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/rtc.h> | ||
| 14 | |||
| 15 | /* device attributes */ | ||
| 16 | |||
| 17 | static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf) | ||
| 18 | { | ||
| 19 | return sprintf(buf, "%s\n", to_rtc_device(dev)->name); | ||
| 20 | } | ||
| 21 | static CLASS_DEVICE_ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL); | ||
| 22 | |||
| 23 | static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf) | ||
| 24 | { | ||
| 25 | ssize_t retval; | ||
| 26 | struct rtc_time tm; | ||
| 27 | |||
| 28 | retval = rtc_read_time(dev, &tm); | ||
| 29 | if (retval == 0) { | ||
| 30 | retval = sprintf(buf, "%04d-%02d-%02d\n", | ||
| 31 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); | ||
| 32 | } | ||
| 33 | |||
| 34 | return retval; | ||
| 35 | } | ||
| 36 | static CLASS_DEVICE_ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL); | ||
| 37 | |||
| 38 | static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf) | ||
| 39 | { | ||
| 40 | ssize_t retval; | ||
| 41 | struct rtc_time tm; | ||
| 42 | |||
| 43 | retval = rtc_read_time(dev, &tm); | ||
| 44 | if (retval == 0) { | ||
| 45 | retval = sprintf(buf, "%02d:%02d:%02d\n", | ||
| 46 | tm.tm_hour, tm.tm_min, tm.tm_sec); | ||
| 47 | } | ||
| 48 | |||
| 49 | return retval; | ||
| 50 | } | ||
| 51 | static CLASS_DEVICE_ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL); | ||
| 52 | |||
| 53 | static ssize_t rtc_sysfs_show_since_epoch(struct class_device *dev, char *buf) | ||
| 54 | { | ||
| 55 | ssize_t retval; | ||
| 56 | struct rtc_time tm; | ||
| 57 | |||
| 58 | retval = rtc_read_time(dev, &tm); | ||
| 59 | if (retval == 0) { | ||
| 60 | unsigned long time; | ||
| 61 | rtc_tm_to_time(&tm, &time); | ||
| 62 | retval = sprintf(buf, "%lu\n", time); | ||
| 63 | } | ||
| 64 | |||
| 65 | return retval; | ||
| 66 | } | ||
| 67 | static CLASS_DEVICE_ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL); | ||
| 68 | |||
| 69 | static struct attribute *rtc_attrs[] = { | ||
| 70 | &class_device_attr_name.attr, | ||
| 71 | &class_device_attr_date.attr, | ||
| 72 | &class_device_attr_time.attr, | ||
| 73 | &class_device_attr_since_epoch.attr, | ||
| 74 | NULL, | ||
| 75 | }; | ||
| 76 | |||
| 77 | static struct attribute_group rtc_attr_group = { | ||
| 78 | .attrs = rtc_attrs, | ||
| 79 | }; | ||
| 80 | |||
| 81 | static int __devinit rtc_sysfs_add_device(struct class_device *class_dev, | ||
| 82 | struct class_interface *class_intf) | ||
| 83 | { | ||
| 84 | int err; | ||
| 85 | |||
| 86 | dev_info(class_dev->dev, "rtc intf: sysfs\n"); | ||
| 87 | |||
| 88 | err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group); | ||
| 89 | if (err) | ||
| 90 | dev_err(class_dev->dev, | ||
| 91 | "failed to create sysfs attributes\n"); | ||
| 92 | |||
| 93 | return err; | ||
| 94 | } | ||
| 95 | |||
| 96 | static void rtc_sysfs_remove_device(struct class_device *class_dev, | ||
| 97 | struct class_interface *class_intf) | ||
| 98 | { | ||
| 99 | sysfs_remove_group(&class_dev->kobj, &rtc_attr_group); | ||
| 100 | } | ||
| 101 | |||
| 102 | /* interface registration */ | ||
| 103 | |||
| 104 | static struct class_interface rtc_sysfs_interface = { | ||
| 105 | .add = &rtc_sysfs_add_device, | ||
| 106 | .remove = &rtc_sysfs_remove_device, | ||
| 107 | }; | ||
| 108 | |||
| 109 | static int __init rtc_sysfs_init(void) | ||
| 110 | { | ||
| 111 | return rtc_interface_register(&rtc_sysfs_interface); | ||
| 112 | } | ||
| 113 | |||
| 114 | static void __exit rtc_sysfs_exit(void) | ||
| 115 | { | ||
| 116 | class_interface_unregister(&rtc_sysfs_interface); | ||
| 117 | } | ||
| 118 | |||
| 119 | module_init(rtc_sysfs_init); | ||
| 120 | module_exit(rtc_sysfs_exit); | ||
| 121 | |||
| 122 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 123 | MODULE_DESCRIPTION("RTC class sysfs interface"); | ||
| 124 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-test.c b/drivers/rtc/rtc-test.c new file mode 100644 index 000000000000..43d107487820 --- /dev/null +++ b/drivers/rtc/rtc-test.c | |||
| @@ -0,0 +1,204 @@ | |||
| 1 | /* | ||
| 2 | * An RTC test device/driver | ||
| 3 | * Copyright (C) 2005 Tower Technologies | ||
| 4 | * Author: Alessandro Zummo <a.zummo@towertech.it> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License version 2 as | ||
| 8 | * published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <linux/module.h> | ||
| 12 | #include <linux/err.h> | ||
| 13 | #include <linux/rtc.h> | ||
| 14 | #include <linux/platform_device.h> | ||
| 15 | |||
| 16 | static struct platform_device *test0 = NULL, *test1 = NULL; | ||
| 17 | |||
| 18 | static int test_rtc_read_alarm(struct device *dev, | ||
| 19 | struct rtc_wkalrm *alrm) | ||
| 20 | { | ||
| 21 | return 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | static int test_rtc_set_alarm(struct device *dev, | ||
| 25 | struct rtc_wkalrm *alrm) | ||
| 26 | { | ||
| 27 | return 0; | ||
| 28 | } | ||
| 29 | |||
| 30 | static int test_rtc_read_time(struct device *dev, | ||
| 31 | struct rtc_time *tm) | ||
| 32 | { | ||
| 33 | rtc_time_to_tm(get_seconds(), tm); | ||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | static int test_rtc_set_time(struct device *dev, | ||
| 38 | struct rtc_time *tm) | ||
| 39 | { | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | static int test_rtc_set_mmss(struct device *dev, unsigned long secs) | ||
| 44 | { | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | |||
| 48 | static int test_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 49 | { | ||
| 50 | struct platform_device *plat_dev = to_platform_device(dev); | ||
| 51 | |||
| 52 | seq_printf(seq, "24hr\t\t: yes\n"); | ||
| 53 | seq_printf(seq, "test\t\t: yes\n"); | ||
| 54 | seq_printf(seq, "id\t\t: %d\n", plat_dev->id); | ||
| 55 | |||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | static int test_rtc_ioctl(struct device *dev, unsigned int cmd, | ||
| 60 | unsigned long arg) | ||
| 61 | { | ||
| 62 | /* We do support interrupts, they're generated | ||
| 63 | * using the sysfs interface. | ||
| 64 | */ | ||
| 65 | switch (cmd) { | ||
| 66 | case RTC_PIE_ON: | ||
| 67 | case RTC_PIE_OFF: | ||
| 68 | case RTC_UIE_ON: | ||
| 69 | case RTC_UIE_OFF: | ||
| 70 | case RTC_AIE_ON: | ||
| 71 | case RTC_AIE_OFF: | ||
| 72 | return 0; | ||
| 73 | |||
| 74 | default: | ||
| 75 | return -EINVAL; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | static struct rtc_class_ops test_rtc_ops = { | ||
| 80 | .proc = test_rtc_proc, | ||
| 81 | .read_time = test_rtc_read_time, | ||
| 82 | .set_time = test_rtc_set_time, | ||
| 83 | .read_alarm = test_rtc_read_alarm, | ||
| 84 | .set_alarm = test_rtc_set_alarm, | ||
| 85 | .set_mmss = test_rtc_set_mmss, | ||
| 86 | .ioctl = test_rtc_ioctl, | ||
| 87 | }; | ||
| 88 | |||
| 89 | static ssize_t test_irq_show(struct device *dev, | ||
| 90 | struct device_attribute *attr, char *buf) | ||
| 91 | { | ||
| 92 | return sprintf(buf, "%d\n", 42); | ||
| 93 | } | ||
| 94 | static ssize_t test_irq_store(struct device *dev, | ||
| 95 | struct device_attribute *attr, | ||
| 96 | const char *buf, size_t count) | ||
| 97 | { | ||
| 98 | int retval; | ||
| 99 | struct platform_device *plat_dev = to_platform_device(dev); | ||
| 100 | struct rtc_device *rtc = platform_get_drvdata(plat_dev); | ||
| 101 | |||
| 102 | retval = count; | ||
| 103 | if (strncmp(buf, "tick", 4) == 0) | ||
| 104 | rtc_update_irq(&rtc->class_dev, 1, RTC_PF | RTC_IRQF); | ||
| 105 | else if (strncmp(buf, "alarm", 5) == 0) | ||
| 106 | rtc_update_irq(&rtc->class_dev, 1, RTC_AF | RTC_IRQF); | ||
| 107 | else if (strncmp(buf, "update", 6) == 0) | ||
| 108 | rtc_update_irq(&rtc->class_dev, 1, RTC_UF | RTC_IRQF); | ||
| 109 | else | ||
| 110 | retval = -EINVAL; | ||
| 111 | |||
| 112 | return retval; | ||
| 113 | } | ||
| 114 | static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store); | ||
| 115 | |||
| 116 | static int test_probe(struct platform_device *plat_dev) | ||
| 117 | { | ||
| 118 | int err; | ||
| 119 | struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev, | ||
| 120 | &test_rtc_ops, THIS_MODULE); | ||
| 121 | if (IS_ERR(rtc)) { | ||
| 122 | err = PTR_ERR(rtc); | ||
| 123 | dev_err(&plat_dev->dev, | ||
| 124 | "unable to register the class device\n"); | ||
| 125 | return err; | ||
| 126 | } | ||
| 127 | device_create_file(&plat_dev->dev, &dev_attr_irq); | ||
| 128 | |||
| 129 | platform_set_drvdata(plat_dev, rtc); | ||
| 130 | |||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | static int __devexit test_remove(struct platform_device *plat_dev) | ||
| 135 | { | ||
| 136 | struct rtc_device *rtc = platform_get_drvdata(plat_dev); | ||
| 137 | |||
| 138 | rtc_device_unregister(rtc); | ||
| 139 | device_remove_file(&plat_dev->dev, &dev_attr_irq); | ||
| 140 | |||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | |||
| 144 | static struct platform_driver test_drv = { | ||
| 145 | .probe = test_probe, | ||
| 146 | .remove = __devexit_p(test_remove), | ||
| 147 | .driver = { | ||
| 148 | .name = "rtc-test", | ||
| 149 | .owner = THIS_MODULE, | ||
| 150 | }, | ||
| 151 | }; | ||
| 152 | |||
| 153 | static int __init test_init(void) | ||
| 154 | { | ||
| 155 | int err; | ||
| 156 | |||
| 157 | if ((err = platform_driver_register(&test_drv))) | ||
| 158 | return err; | ||
| 159 | |||
| 160 | if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) { | ||
| 161 | err = -ENOMEM; | ||
| 162 | goto exit_driver_unregister; | ||
| 163 | } | ||
| 164 | |||
| 165 | if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) { | ||
| 166 | err = -ENOMEM; | ||
| 167 | goto exit_free_test0; | ||
| 168 | } | ||
| 169 | |||
| 170 | if ((err = platform_device_add(test0))) | ||
| 171 | goto exit_free_test1; | ||
| 172 | |||
| 173 | if ((err = platform_device_add(test1))) | ||
| 174 | goto exit_device_unregister; | ||
| 175 | |||
| 176 | return 0; | ||
| 177 | |||
| 178 | exit_device_unregister: | ||
| 179 | platform_device_unregister(test0); | ||
| 180 | |||
| 181 | exit_free_test1: | ||
| 182 | platform_device_put(test1); | ||
| 183 | |||
| 184 | exit_free_test0: | ||
| 185 | platform_device_put(test0); | ||
| 186 | |||
| 187 | exit_driver_unregister: | ||
| 188 | platform_driver_unregister(&test_drv); | ||
| 189 | return err; | ||
| 190 | } | ||
| 191 | |||
| 192 | static void __exit test_exit(void) | ||
| 193 | { | ||
| 194 | platform_device_unregister(test0); | ||
| 195 | platform_device_unregister(test1); | ||
| 196 | platform_driver_unregister(&test_drv); | ||
| 197 | } | ||
| 198 | |||
| 199 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 200 | MODULE_DESCRIPTION("RTC test driver/device"); | ||
| 201 | MODULE_LICENSE("GPL"); | ||
| 202 | |||
| 203 | module_init(test_init); | ||
| 204 | module_exit(test_exit); | ||
diff --git a/drivers/rtc/rtc-x1205.c b/drivers/rtc/rtc-x1205.c new file mode 100644 index 000000000000..621d17afc0d9 --- /dev/null +++ b/drivers/rtc/rtc-x1205.c | |||
| @@ -0,0 +1,619 @@ | |||
| 1 | /* | ||
| 2 | * An i2c driver for the Xicor/Intersil X1205 RTC | ||
| 3 | * Copyright 2004 Karen Spearel | ||
| 4 | * Copyright 2005 Alessandro Zummo | ||
| 5 | * | ||
| 6 | * please send all reports to: | ||
| 7 | * Karen Spearel <kas111 at gmail dot com> | ||
| 8 | * Alessandro Zummo <a.zummo@towertech.it> | ||
| 9 | * | ||
| 10 | * based on a lot of other RTC drivers. | ||
| 11 | * | ||
| 12 | * This program is free software; you can redistribute it and/or modify | ||
| 13 | * it under the terms of the GNU General Public License version 2 as | ||
| 14 | * published by the Free Software Foundation. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/i2c.h> | ||
| 18 | #include <linux/bcd.h> | ||
| 19 | #include <linux/rtc.h> | ||
| 20 | #include <linux/delay.h> | ||
| 21 | |||
| 22 | #define DRV_VERSION "1.0.6" | ||
| 23 | |||
| 24 | /* Addresses to scan: none. This chip is located at | ||
| 25 | * 0x6f and uses a two bytes register addressing. | ||
| 26 | * Two bytes need to be written to read a single register, | ||
| 27 | * while most other chips just require one and take the second | ||
| 28 | * one as the data to be written. To prevent corrupting | ||
| 29 | * unknown chips, the user must explicitely set the probe parameter. | ||
| 30 | */ | ||
| 31 | |||
| 32 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
| 33 | |||
| 34 | /* Insmod parameters */ | ||
| 35 | I2C_CLIENT_INSMOD; | ||
| 36 | |||
| 37 | /* offsets into CCR area */ | ||
| 38 | |||
| 39 | #define CCR_SEC 0 | ||
| 40 | #define CCR_MIN 1 | ||
| 41 | #define CCR_HOUR 2 | ||
| 42 | #define CCR_MDAY 3 | ||
| 43 | #define CCR_MONTH 4 | ||
| 44 | #define CCR_YEAR 5 | ||
| 45 | #define CCR_WDAY 6 | ||
| 46 | #define CCR_Y2K 7 | ||
| 47 | |||
| 48 | #define X1205_REG_SR 0x3F /* status register */ | ||
| 49 | #define X1205_REG_Y2K 0x37 | ||
| 50 | #define X1205_REG_DW 0x36 | ||
| 51 | #define X1205_REG_YR 0x35 | ||
| 52 | #define X1205_REG_MO 0x34 | ||
| 53 | #define X1205_REG_DT 0x33 | ||
| 54 | #define X1205_REG_HR 0x32 | ||
| 55 | #define X1205_REG_MN 0x31 | ||
| 56 | #define X1205_REG_SC 0x30 | ||
| 57 | #define X1205_REG_DTR 0x13 | ||
| 58 | #define X1205_REG_ATR 0x12 | ||
| 59 | #define X1205_REG_INT 0x11 | ||
| 60 | #define X1205_REG_0 0x10 | ||
| 61 | #define X1205_REG_Y2K1 0x0F | ||
| 62 | #define X1205_REG_DWA1 0x0E | ||
| 63 | #define X1205_REG_YRA1 0x0D | ||
| 64 | #define X1205_REG_MOA1 0x0C | ||
| 65 | #define X1205_REG_DTA1 0x0B | ||
| 66 | #define X1205_REG_HRA1 0x0A | ||
| 67 | #define X1205_REG_MNA1 0x09 | ||
| 68 | #define X1205_REG_SCA1 0x08 | ||
| 69 | #define X1205_REG_Y2K0 0x07 | ||
| 70 | #define X1205_REG_DWA0 0x06 | ||
| 71 | #define X1205_REG_YRA0 0x05 | ||
| 72 | #define X1205_REG_MOA0 0x04 | ||
| 73 | #define X1205_REG_DTA0 0x03 | ||
| 74 | #define X1205_REG_HRA0 0x02 | ||
| 75 | #define X1205_REG_MNA0 0x01 | ||
| 76 | #define X1205_REG_SCA0 0x00 | ||
| 77 | |||
| 78 | #define X1205_CCR_BASE 0x30 /* Base address of CCR */ | ||
| 79 | #define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */ | ||
| 80 | |||
| 81 | #define X1205_SR_RTCF 0x01 /* Clock failure */ | ||
| 82 | #define X1205_SR_WEL 0x02 /* Write Enable Latch */ | ||
| 83 | #define X1205_SR_RWEL 0x04 /* Register Write Enable */ | ||
| 84 | |||
| 85 | #define X1205_DTR_DTR0 0x01 | ||
| 86 | #define X1205_DTR_DTR1 0x02 | ||
| 87 | #define X1205_DTR_DTR2 0x04 | ||
| 88 | |||
| 89 | #define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */ | ||
| 90 | |||
| 91 | /* Prototypes */ | ||
| 92 | static int x1205_attach(struct i2c_adapter *adapter); | ||
| 93 | static int x1205_detach(struct i2c_client *client); | ||
| 94 | static int x1205_probe(struct i2c_adapter *adapter, int address, int kind); | ||
| 95 | |||
| 96 | static struct i2c_driver x1205_driver = { | ||
| 97 | .driver = { | ||
| 98 | .name = "x1205", | ||
| 99 | }, | ||
| 100 | .id = I2C_DRIVERID_X1205, | ||
| 101 | .attach_adapter = &x1205_attach, | ||
| 102 | .detach_client = &x1205_detach, | ||
| 103 | }; | ||
| 104 | |||
| 105 | /* | ||
| 106 | * In the routines that deal directly with the x1205 hardware, we use | ||
| 107 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch | ||
| 108 | * Epoch is initialized as 2000. Time is set to UTC. | ||
| 109 | */ | ||
| 110 | static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm, | ||
| 111 | unsigned char reg_base) | ||
| 112 | { | ||
| 113 | unsigned char dt_addr[2] = { 0, reg_base }; | ||
| 114 | |||
| 115 | unsigned char buf[8]; | ||
| 116 | |||
| 117 | struct i2c_msg msgs[] = { | ||
| 118 | { client->addr, 0, 2, dt_addr }, /* setup read ptr */ | ||
| 119 | { client->addr, I2C_M_RD, 8, buf }, /* read date */ | ||
| 120 | }; | ||
| 121 | |||
| 122 | /* read date registers */ | ||
| 123 | if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { | ||
| 124 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 125 | return -EIO; | ||
| 126 | } | ||
| 127 | |||
| 128 | dev_dbg(&client->dev, | ||
| 129 | "%s: raw read data - sec=%02x, min=%02x, hr=%02x, " | ||
| 130 | "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n", | ||
| 131 | __FUNCTION__, | ||
| 132 | buf[0], buf[1], buf[2], buf[3], | ||
| 133 | buf[4], buf[5], buf[6], buf[7]); | ||
| 134 | |||
| 135 | tm->tm_sec = BCD2BIN(buf[CCR_SEC]); | ||
| 136 | tm->tm_min = BCD2BIN(buf[CCR_MIN]); | ||
| 137 | tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */ | ||
| 138 | tm->tm_mday = BCD2BIN(buf[CCR_MDAY]); | ||
| 139 | tm->tm_mon = BCD2BIN(buf[CCR_MONTH]) - 1; /* mon is 0-11 */ | ||
| 140 | tm->tm_year = BCD2BIN(buf[CCR_YEAR]) | ||
| 141 | + (BCD2BIN(buf[CCR_Y2K]) * 100) - 1900; | ||
| 142 | tm->tm_wday = buf[CCR_WDAY]; | ||
| 143 | |||
| 144 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | ||
| 145 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 146 | __FUNCTION__, | ||
| 147 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
| 148 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 149 | |||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | static int x1205_get_status(struct i2c_client *client, unsigned char *sr) | ||
| 154 | { | ||
| 155 | static unsigned char sr_addr[2] = { 0, X1205_REG_SR }; | ||
| 156 | |||
| 157 | struct i2c_msg msgs[] = { | ||
| 158 | { client->addr, 0, 2, sr_addr }, /* setup read ptr */ | ||
| 159 | { client->addr, I2C_M_RD, 1, sr }, /* read status */ | ||
| 160 | }; | ||
| 161 | |||
| 162 | /* read status register */ | ||
| 163 | if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { | ||
| 164 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 165 | return -EIO; | ||
| 166 | } | ||
| 167 | |||
| 168 | return 0; | ||
| 169 | } | ||
| 170 | |||
| 171 | static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, | ||
| 172 | int datetoo, u8 reg_base) | ||
| 173 | { | ||
| 174 | int i, xfer; | ||
| 175 | unsigned char buf[8]; | ||
| 176 | |||
| 177 | static const unsigned char wel[3] = { 0, X1205_REG_SR, | ||
| 178 | X1205_SR_WEL }; | ||
| 179 | |||
| 180 | static const unsigned char rwel[3] = { 0, X1205_REG_SR, | ||
| 181 | X1205_SR_WEL | X1205_SR_RWEL }; | ||
| 182 | |||
| 183 | static const unsigned char diswe[3] = { 0, X1205_REG_SR, 0 }; | ||
| 184 | |||
| 185 | dev_dbg(&client->dev, | ||
| 186 | "%s: secs=%d, mins=%d, hours=%d\n", | ||
| 187 | __FUNCTION__, | ||
| 188 | tm->tm_sec, tm->tm_min, tm->tm_hour); | ||
| 189 | |||
| 190 | buf[CCR_SEC] = BIN2BCD(tm->tm_sec); | ||
| 191 | buf[CCR_MIN] = BIN2BCD(tm->tm_min); | ||
| 192 | |||
| 193 | /* set hour and 24hr bit */ | ||
| 194 | buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL; | ||
| 195 | |||
| 196 | /* should we also set the date? */ | ||
| 197 | if (datetoo) { | ||
| 198 | dev_dbg(&client->dev, | ||
| 199 | "%s: mday=%d, mon=%d, year=%d, wday=%d\n", | ||
| 200 | __FUNCTION__, | ||
| 201 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
| 202 | |||
| 203 | buf[CCR_MDAY] = BIN2BCD(tm->tm_mday); | ||
| 204 | |||
| 205 | /* month, 1 - 12 */ | ||
| 206 | buf[CCR_MONTH] = BIN2BCD(tm->tm_mon + 1); | ||
| 207 | |||
| 208 | /* year, since the rtc epoch*/ | ||
| 209 | buf[CCR_YEAR] = BIN2BCD(tm->tm_year % 100); | ||
| 210 | buf[CCR_WDAY] = tm->tm_wday & 0x07; | ||
| 211 | buf[CCR_Y2K] = BIN2BCD(tm->tm_year / 100); | ||
| 212 | } | ||
| 213 | |||
| 214 | /* this sequence is required to unlock the chip */ | ||
| 215 | if ((xfer = i2c_master_send(client, wel, 3)) != 3) { | ||
| 216 | dev_err(&client->dev, "%s: wel - %d\n", __FUNCTION__, xfer); | ||
| 217 | return -EIO; | ||
| 218 | } | ||
| 219 | |||
| 220 | if ((xfer = i2c_master_send(client, rwel, 3)) != 3) { | ||
| 221 | dev_err(&client->dev, "%s: rwel - %d\n", __FUNCTION__, xfer); | ||
| 222 | return -EIO; | ||
| 223 | } | ||
| 224 | |||
| 225 | /* write register's data */ | ||
| 226 | for (i = 0; i < (datetoo ? 8 : 3); i++) { | ||
| 227 | unsigned char rdata[3] = { 0, reg_base + i, buf[i] }; | ||
| 228 | |||
| 229 | xfer = i2c_master_send(client, rdata, 3); | ||
| 230 | if (xfer != 3) { | ||
| 231 | dev_err(&client->dev, | ||
| 232 | "%s: xfer=%d addr=%02x, data=%02x\n", | ||
| 233 | __FUNCTION__, | ||
| 234 | xfer, rdata[1], rdata[2]); | ||
| 235 | return -EIO; | ||
| 236 | } | ||
| 237 | }; | ||
| 238 | |||
| 239 | /* disable further writes */ | ||
| 240 | if ((xfer = i2c_master_send(client, diswe, 3)) != 3) { | ||
| 241 | dev_err(&client->dev, "%s: diswe - %d\n", __FUNCTION__, xfer); | ||
| 242 | return -EIO; | ||
| 243 | } | ||
| 244 | |||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | |||
| 248 | static int x1205_fix_osc(struct i2c_client *client) | ||
| 249 | { | ||
| 250 | int err; | ||
| 251 | struct rtc_time tm; | ||
| 252 | |||
| 253 | tm.tm_hour = tm.tm_min = tm.tm_sec = 0; | ||
| 254 | |||
| 255 | if ((err = x1205_set_datetime(client, &tm, 0, X1205_CCR_BASE)) < 0) | ||
| 256 | dev_err(&client->dev, | ||
| 257 | "unable to restart the oscillator\n"); | ||
| 258 | |||
| 259 | return err; | ||
| 260 | } | ||
| 261 | |||
| 262 | static int x1205_get_dtrim(struct i2c_client *client, int *trim) | ||
| 263 | { | ||
| 264 | unsigned char dtr; | ||
| 265 | static unsigned char dtr_addr[2] = { 0, X1205_REG_DTR }; | ||
| 266 | |||
| 267 | struct i2c_msg msgs[] = { | ||
| 268 | { client->addr, 0, 2, dtr_addr }, /* setup read ptr */ | ||
| 269 | { client->addr, I2C_M_RD, 1, &dtr }, /* read dtr */ | ||
| 270 | }; | ||
| 271 | |||
| 272 | /* read dtr register */ | ||
| 273 | if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { | ||
| 274 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 275 | return -EIO; | ||
| 276 | } | ||
| 277 | |||
| 278 | dev_dbg(&client->dev, "%s: raw dtr=%x\n", __FUNCTION__, dtr); | ||
| 279 | |||
| 280 | *trim = 0; | ||
| 281 | |||
| 282 | if (dtr & X1205_DTR_DTR0) | ||
| 283 | *trim += 20; | ||
| 284 | |||
| 285 | if (dtr & X1205_DTR_DTR1) | ||
| 286 | *trim += 10; | ||
| 287 | |||
| 288 | if (dtr & X1205_DTR_DTR2) | ||
| 289 | *trim = -*trim; | ||
| 290 | |||
| 291 | return 0; | ||
| 292 | } | ||
| 293 | |||
| 294 | static int x1205_get_atrim(struct i2c_client *client, int *trim) | ||
| 295 | { | ||
| 296 | s8 atr; | ||
| 297 | static unsigned char atr_addr[2] = { 0, X1205_REG_ATR }; | ||
| 298 | |||
| 299 | struct i2c_msg msgs[] = { | ||
| 300 | { client->addr, 0, 2, atr_addr }, /* setup read ptr */ | ||
| 301 | { client->addr, I2C_M_RD, 1, &atr }, /* read atr */ | ||
| 302 | }; | ||
| 303 | |||
| 304 | /* read atr register */ | ||
| 305 | if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { | ||
| 306 | dev_err(&client->dev, "%s: read error\n", __FUNCTION__); | ||
| 307 | return -EIO; | ||
| 308 | } | ||
| 309 | |||
| 310 | dev_dbg(&client->dev, "%s: raw atr=%x\n", __FUNCTION__, atr); | ||
| 311 | |||
| 312 | /* atr is a two's complement value on 6 bits, | ||
| 313 | * perform sign extension. The formula is | ||
| 314 | * Catr = (atr * 0.25pF) + 11.00pF. | ||
| 315 | */ | ||
| 316 | if (atr & 0x20) | ||
| 317 | atr |= 0xC0; | ||
| 318 | |||
| 319 | dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __FUNCTION__, atr, atr); | ||
| 320 | |||
| 321 | *trim = (atr * 250) + 11000; | ||
| 322 | |||
| 323 | dev_dbg(&client->dev, "%s: real=%d\n", __FUNCTION__, *trim); | ||
| 324 | |||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | |||
| 328 | struct x1205_limit | ||
| 329 | { | ||
| 330 | unsigned char reg, mask, min, max; | ||
| 331 | }; | ||
| 332 | |||
| 333 | static int x1205_validate_client(struct i2c_client *client) | ||
| 334 | { | ||
| 335 | int i, xfer; | ||
| 336 | |||
| 337 | /* Probe array. We will read the register at the specified | ||
| 338 | * address and check if the given bits are zero. | ||
| 339 | */ | ||
| 340 | static const unsigned char probe_zero_pattern[] = { | ||
| 341 | /* register, mask */ | ||
| 342 | X1205_REG_SR, 0x18, | ||
| 343 | X1205_REG_DTR, 0xF8, | ||
| 344 | X1205_REG_ATR, 0xC0, | ||
| 345 | X1205_REG_INT, 0x18, | ||
| 346 | X1205_REG_0, 0xFF, | ||
| 347 | }; | ||
| 348 | |||
| 349 | static const struct x1205_limit probe_limits_pattern[] = { | ||
| 350 | /* register, mask, min, max */ | ||
| 351 | { X1205_REG_Y2K, 0xFF, 19, 20 }, | ||
| 352 | { X1205_REG_DW, 0xFF, 0, 6 }, | ||
| 353 | { X1205_REG_YR, 0xFF, 0, 99 }, | ||
| 354 | { X1205_REG_MO, 0xFF, 0, 12 }, | ||
| 355 | { X1205_REG_DT, 0xFF, 0, 31 }, | ||
| 356 | { X1205_REG_HR, 0x7F, 0, 23 }, | ||
| 357 | { X1205_REG_MN, 0xFF, 0, 59 }, | ||
| 358 | { X1205_REG_SC, 0xFF, 0, 59 }, | ||
| 359 | { X1205_REG_Y2K1, 0xFF, 19, 20 }, | ||
| 360 | { X1205_REG_Y2K0, 0xFF, 19, 20 }, | ||
| 361 | }; | ||
| 362 | |||
| 363 | /* check that registers have bits a 0 where expected */ | ||
| 364 | for (i = 0; i < ARRAY_SIZE(probe_zero_pattern); i += 2) { | ||
| 365 | unsigned char buf; | ||
| 366 | |||
| 367 | unsigned char addr[2] = { 0, probe_zero_pattern[i] }; | ||
| 368 | |||
| 369 | struct i2c_msg msgs[2] = { | ||
| 370 | { client->addr, 0, 2, addr }, | ||
| 371 | { client->addr, I2C_M_RD, 1, &buf }, | ||
| 372 | }; | ||
| 373 | |||
| 374 | if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { | ||
| 375 | dev_err(&client->adapter->dev, | ||
| 376 | "%s: could not read register %x\n", | ||
| 377 | __FUNCTION__, probe_zero_pattern[i]); | ||
| 378 | |||
| 379 | return -EIO; | ||
| 380 | } | ||
| 381 | |||
| 382 | if ((buf & probe_zero_pattern[i+1]) != 0) { | ||
| 383 | dev_err(&client->adapter->dev, | ||
| 384 | "%s: register=%02x, zero pattern=%d, value=%x\n", | ||
| 385 | __FUNCTION__, probe_zero_pattern[i], i, buf); | ||
| 386 | |||
| 387 | return -ENODEV; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | /* check limits (only registers with bcd values) */ | ||
| 392 | for (i = 0; i < ARRAY_SIZE(probe_limits_pattern); i++) { | ||
| 393 | unsigned char reg, value; | ||
| 394 | |||
| 395 | unsigned char addr[2] = { 0, probe_limits_pattern[i].reg }; | ||
| 396 | |||
| 397 | struct i2c_msg msgs[2] = { | ||
| 398 | { client->addr, 0, 2, addr }, | ||
| 399 | { client->addr, I2C_M_RD, 1, ® }, | ||
| 400 | }; | ||
| 401 | |||
| 402 | if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { | ||
| 403 | dev_err(&client->adapter->dev, | ||
| 404 | "%s: could not read register %x\n", | ||
| 405 | __FUNCTION__, probe_limits_pattern[i].reg); | ||
| 406 | |||
| 407 | return -EIO; | ||
| 408 | } | ||
| 409 | |||
| 410 | value = BCD2BIN(reg & probe_limits_pattern[i].mask); | ||
| 411 | |||
| 412 | if (value > probe_limits_pattern[i].max || | ||
| 413 | value < probe_limits_pattern[i].min) { | ||
| 414 | dev_dbg(&client->adapter->dev, | ||
| 415 | "%s: register=%x, lim pattern=%d, value=%d\n", | ||
| 416 | __FUNCTION__, probe_limits_pattern[i].reg, | ||
| 417 | i, value); | ||
| 418 | |||
| 419 | return -ENODEV; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | return 0; | ||
| 424 | } | ||
| 425 | |||
| 426 | static int x1205_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 427 | { | ||
| 428 | return x1205_get_datetime(to_i2c_client(dev), | ||
| 429 | &alrm->time, X1205_ALM0_BASE); | ||
| 430 | } | ||
| 431 | |||
| 432 | static int x1205_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 433 | { | ||
| 434 | return x1205_set_datetime(to_i2c_client(dev), | ||
| 435 | &alrm->time, 1, X1205_ALM0_BASE); | ||
| 436 | } | ||
| 437 | |||
| 438 | static int x1205_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 439 | { | ||
| 440 | return x1205_get_datetime(to_i2c_client(dev), | ||
| 441 | tm, X1205_CCR_BASE); | ||
| 442 | } | ||
| 443 | |||
| 444 | static int x1205_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 445 | { | ||
| 446 | return x1205_set_datetime(to_i2c_client(dev), | ||
| 447 | tm, 1, X1205_CCR_BASE); | ||
| 448 | } | ||
| 449 | |||
| 450 | static int x1205_rtc_proc(struct device *dev, struct seq_file *seq) | ||
| 451 | { | ||
| 452 | int err, dtrim, atrim; | ||
| 453 | |||
| 454 | seq_printf(seq, "24hr\t\t: yes\n"); | ||
| 455 | |||
| 456 | if ((err = x1205_get_dtrim(to_i2c_client(dev), &dtrim)) == 0) | ||
| 457 | seq_printf(seq, "digital_trim\t: %d ppm\n", dtrim); | ||
| 458 | |||
| 459 | if ((err = x1205_get_atrim(to_i2c_client(dev), &atrim)) == 0) | ||
| 460 | seq_printf(seq, "analog_trim\t: %d.%02d pF\n", | ||
| 461 | atrim / 1000, atrim % 1000); | ||
| 462 | return 0; | ||
| 463 | } | ||
| 464 | |||
| 465 | static struct rtc_class_ops x1205_rtc_ops = { | ||
| 466 | .proc = x1205_rtc_proc, | ||
| 467 | .read_time = x1205_rtc_read_time, | ||
| 468 | .set_time = x1205_rtc_set_time, | ||
| 469 | .read_alarm = x1205_rtc_read_alarm, | ||
| 470 | .set_alarm = x1205_rtc_set_alarm, | ||
| 471 | }; | ||
| 472 | |||
| 473 | static ssize_t x1205_sysfs_show_atrim(struct device *dev, | ||
| 474 | struct device_attribute *attr, char *buf) | ||
| 475 | { | ||
| 476 | int atrim; | ||
| 477 | |||
| 478 | if (x1205_get_atrim(to_i2c_client(dev), &atrim) == 0) | ||
| 479 | return sprintf(buf, "%d.%02d pF\n", | ||
| 480 | atrim / 1000, atrim % 1000); | ||
| 481 | return 0; | ||
| 482 | } | ||
| 483 | static DEVICE_ATTR(atrim, S_IRUGO, x1205_sysfs_show_atrim, NULL); | ||
| 484 | |||
| 485 | static ssize_t x1205_sysfs_show_dtrim(struct device *dev, | ||
| 486 | struct device_attribute *attr, char *buf) | ||
| 487 | { | ||
| 488 | int dtrim; | ||
| 489 | |||
| 490 | if (x1205_get_dtrim(to_i2c_client(dev), &dtrim) == 0) | ||
| 491 | return sprintf(buf, "%d ppm\n", dtrim); | ||
| 492 | |||
| 493 | return 0; | ||
| 494 | } | ||
| 495 | static DEVICE_ATTR(dtrim, S_IRUGO, x1205_sysfs_show_dtrim, NULL); | ||
| 496 | |||
| 497 | static int x1205_attach(struct i2c_adapter *adapter) | ||
| 498 | { | ||
| 499 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 500 | return i2c_probe(adapter, &addr_data, x1205_probe); | ||
| 501 | } | ||
| 502 | |||
| 503 | static int x1205_probe(struct i2c_adapter *adapter, int address, int kind) | ||
| 504 | { | ||
| 505 | int err = 0; | ||
| 506 | unsigned char sr; | ||
| 507 | struct i2c_client *client; | ||
| 508 | struct rtc_device *rtc; | ||
| 509 | |||
| 510 | dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); | ||
| 511 | |||
| 512 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | ||
| 513 | err = -ENODEV; | ||
| 514 | goto exit; | ||
| 515 | } | ||
| 516 | |||
| 517 | if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { | ||
| 518 | err = -ENOMEM; | ||
| 519 | goto exit; | ||
| 520 | } | ||
| 521 | |||
| 522 | /* I2C client */ | ||
| 523 | client->addr = address; | ||
| 524 | client->driver = &x1205_driver; | ||
| 525 | client->adapter = adapter; | ||
| 526 | |||
| 527 | strlcpy(client->name, x1205_driver.driver.name, I2C_NAME_SIZE); | ||
| 528 | |||
| 529 | /* Verify the chip is really an X1205 */ | ||
| 530 | if (kind < 0) { | ||
| 531 | if (x1205_validate_client(client) < 0) { | ||
| 532 | err = -ENODEV; | ||
| 533 | goto exit_kfree; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | /* Inform the i2c layer */ | ||
| 538 | if ((err = i2c_attach_client(client))) | ||
| 539 | goto exit_kfree; | ||
| 540 | |||
| 541 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | ||
| 542 | |||
| 543 | rtc = rtc_device_register(x1205_driver.driver.name, &client->dev, | ||
| 544 | &x1205_rtc_ops, THIS_MODULE); | ||
| 545 | |||
| 546 | if (IS_ERR(rtc)) { | ||
| 547 | err = PTR_ERR(rtc); | ||
| 548 | dev_err(&client->dev, | ||
| 549 | "unable to register the class device\n"); | ||
| 550 | goto exit_detach; | ||
| 551 | } | ||
| 552 | |||
| 553 | i2c_set_clientdata(client, rtc); | ||
| 554 | |||
| 555 | /* Check for power failures and eventualy enable the osc */ | ||
| 556 | if ((err = x1205_get_status(client, &sr)) == 0) { | ||
| 557 | if (sr & X1205_SR_RTCF) { | ||
| 558 | dev_err(&client->dev, | ||
| 559 | "power failure detected, " | ||
| 560 | "please set the clock\n"); | ||
| 561 | udelay(50); | ||
| 562 | x1205_fix_osc(client); | ||
| 563 | } | ||
| 564 | } | ||
| 565 | else | ||
| 566 | dev_err(&client->dev, "couldn't read status\n"); | ||
| 567 | |||
| 568 | device_create_file(&client->dev, &dev_attr_atrim); | ||
| 569 | device_create_file(&client->dev, &dev_attr_dtrim); | ||
| 570 | |||
| 571 | return 0; | ||
| 572 | |||
| 573 | exit_detach: | ||
| 574 | i2c_detach_client(client); | ||
| 575 | |||
| 576 | exit_kfree: | ||
| 577 | kfree(client); | ||
| 578 | |||
| 579 | exit: | ||
| 580 | return err; | ||
| 581 | } | ||
| 582 | |||
| 583 | static int x1205_detach(struct i2c_client *client) | ||
| 584 | { | ||
| 585 | int err; | ||
| 586 | struct rtc_device *rtc = i2c_get_clientdata(client); | ||
| 587 | |||
| 588 | dev_dbg(&client->dev, "%s\n", __FUNCTION__); | ||
| 589 | |||
| 590 | if (rtc) | ||
| 591 | rtc_device_unregister(rtc); | ||
| 592 | |||
| 593 | if ((err = i2c_detach_client(client))) | ||
| 594 | return err; | ||
| 595 | |||
| 596 | kfree(client); | ||
| 597 | |||
| 598 | return 0; | ||
| 599 | } | ||
| 600 | |||
| 601 | static int __init x1205_init(void) | ||
| 602 | { | ||
| 603 | return i2c_add_driver(&x1205_driver); | ||
| 604 | } | ||
| 605 | |||
| 606 | static void __exit x1205_exit(void) | ||
| 607 | { | ||
| 608 | i2c_del_driver(&x1205_driver); | ||
| 609 | } | ||
| 610 | |||
| 611 | MODULE_AUTHOR( | ||
| 612 | "Karen Spearel <kas111 at gmail dot com>, " | ||
| 613 | "Alessandro Zummo <a.zummo@towertech.it>"); | ||
| 614 | MODULE_DESCRIPTION("Xicor/Intersil X1205 RTC driver"); | ||
| 615 | MODULE_LICENSE("GPL"); | ||
| 616 | MODULE_VERSION(DRV_VERSION); | ||
| 617 | |||
| 618 | module_init(x1205_init); | ||
| 619 | module_exit(x1205_exit); | ||
