[NETFILTER]: remove remaining ASSERT_{READ,WRITE}_LOCK
[hh.org.git] / drivers / rtc / rtc-ds1742.c
blob6273a3d240a2afd2f4a57179d38219f6259797a1
1 /*
2 * An rtc driver for the Dallas DS1742
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
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 */
11 #include <linux/bcd.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/rtc.h>
17 #include <linux/platform_device.h>
18 #include <linux/io.h>
20 #define DRV_VERSION "0.2"
22 #define RTC_REG_SIZE 0x800
23 #define RTC_OFFSET 0x7f8
25 #define RTC_CONTROL (RTC_OFFSET + 0)
26 #define RTC_CENTURY (RTC_OFFSET + 0)
27 #define RTC_SECONDS (RTC_OFFSET + 1)
28 #define RTC_MINUTES (RTC_OFFSET + 2)
29 #define RTC_HOURS (RTC_OFFSET + 3)
30 #define RTC_DAY (RTC_OFFSET + 4)
31 #define RTC_DATE (RTC_OFFSET + 5)
32 #define RTC_MONTH (RTC_OFFSET + 6)
33 #define RTC_YEAR (RTC_OFFSET + 7)
35 #define RTC_CENTURY_MASK 0x3f
36 #define RTC_SECONDS_MASK 0x7f
37 #define RTC_DAY_MASK 0x07
39 /* Bits in the Control/Century register */
40 #define RTC_WRITE 0x80
41 #define RTC_READ 0x40
43 /* Bits in the Seconds register */
44 #define RTC_STOP 0x80
46 /* Bits in the Day register */
47 #define RTC_BATT_FLAG 0x80
49 struct rtc_plat_data {
50 struct rtc_device *rtc;
51 void __iomem *ioaddr;
52 unsigned long baseaddr;
53 unsigned long last_jiffies;
56 static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
58 struct platform_device *pdev = to_platform_device(dev);
59 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
60 void __iomem *ioaddr = pdata->ioaddr;
61 u8 century;
63 century = BIN2BCD((tm->tm_year + 1900) / 100);
65 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
67 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR);
68 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH);
69 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
70 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE);
71 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS);
72 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES);
73 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
75 /* RTC_CENTURY and RTC_CONTROL share same register */
76 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
77 writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
78 return 0;
81 static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
83 struct platform_device *pdev = to_platform_device(dev);
84 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
85 void __iomem *ioaddr = pdata->ioaddr;
86 unsigned int year, month, day, hour, minute, second, week;
87 unsigned int century;
89 /* give enough time to update RTC in case of continuous read */
90 if (pdata->last_jiffies == jiffies)
91 msleep(1);
92 pdata->last_jiffies = jiffies;
93 writeb(RTC_READ, ioaddr + RTC_CONTROL);
94 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
95 minute = readb(ioaddr + RTC_MINUTES);
96 hour = readb(ioaddr + RTC_HOURS);
97 day = readb(ioaddr + RTC_DATE);
98 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
99 month = readb(ioaddr + RTC_MONTH);
100 year = readb(ioaddr + RTC_YEAR);
101 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
102 writeb(0, ioaddr + RTC_CONTROL);
103 tm->tm_sec = BCD2BIN(second);
104 tm->tm_min = BCD2BIN(minute);
105 tm->tm_hour = BCD2BIN(hour);
106 tm->tm_mday = BCD2BIN(day);
107 tm->tm_wday = BCD2BIN(week);
108 tm->tm_mon = BCD2BIN(month) - 1;
109 /* year is 1900 + tm->tm_year */
110 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900;
112 if (rtc_valid_tm(tm) < 0) {
113 dev_err(dev, "retrieved date/time is not valid.\n");
114 rtc_time_to_tm(0, tm);
116 return 0;
119 static const struct rtc_class_ops ds1742_rtc_ops = {
120 .read_time = ds1742_rtc_read_time,
121 .set_time = ds1742_rtc_set_time,
124 static ssize_t ds1742_nvram_read(struct kobject *kobj, char *buf,
125 loff_t pos, size_t size)
127 struct platform_device *pdev =
128 to_platform_device(container_of(kobj, struct device, kobj));
129 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
130 void __iomem *ioaddr = pdata->ioaddr;
131 ssize_t count;
133 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
134 *buf++ = readb(ioaddr + pos++);
135 return count;
138 static ssize_t ds1742_nvram_write(struct kobject *kobj, char *buf,
139 loff_t pos, size_t size)
141 struct platform_device *pdev =
142 to_platform_device(container_of(kobj, struct device, kobj));
143 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
144 void __iomem *ioaddr = pdata->ioaddr;
145 ssize_t count;
147 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
148 writeb(*buf++, ioaddr + pos++);
149 return count;
152 static struct bin_attribute ds1742_nvram_attr = {
153 .attr = {
154 .name = "nvram",
155 .mode = S_IRUGO | S_IWUGO,
156 .owner = THIS_MODULE,
158 .size = RTC_OFFSET,
159 .read = ds1742_nvram_read,
160 .write = ds1742_nvram_write,
163 static int __init ds1742_rtc_probe(struct platform_device *pdev)
165 struct rtc_device *rtc;
166 struct resource *res;
167 unsigned int cen, sec;
168 struct rtc_plat_data *pdata = NULL;
169 void __iomem *ioaddr = NULL;
170 int ret = 0;
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 if (!res)
174 return -ENODEV;
175 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
176 if (!pdata)
177 return -ENOMEM;
178 if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
179 ret = -EBUSY;
180 goto out;
182 pdata->baseaddr = res->start;
183 ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
184 if (!ioaddr) {
185 ret = -ENOMEM;
186 goto out;
188 pdata->ioaddr = ioaddr;
190 /* turn RTC on if it was not on */
191 sec = readb(ioaddr + RTC_SECONDS);
192 if (sec & RTC_STOP) {
193 sec &= RTC_SECONDS_MASK;
194 cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
195 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
196 writeb(sec, ioaddr + RTC_SECONDS);
197 writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
199 if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
200 dev_warn(&pdev->dev, "voltage-low detected.\n");
202 rtc = rtc_device_register(pdev->name, &pdev->dev,
203 &ds1742_rtc_ops, THIS_MODULE);
204 if (IS_ERR(rtc)) {
205 ret = PTR_ERR(rtc);
206 goto out;
208 pdata->rtc = rtc;
209 pdata->last_jiffies = jiffies;
210 platform_set_drvdata(pdev, pdata);
211 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr);
212 if (ret)
213 goto out;
214 return 0;
215 out:
216 if (pdata->rtc)
217 rtc_device_unregister(pdata->rtc);
218 if (ioaddr)
219 iounmap(ioaddr);
220 if (pdata->baseaddr)
221 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
222 kfree(pdata);
223 return ret;
226 static int __devexit ds1742_rtc_remove(struct platform_device *pdev)
228 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
230 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr);
231 rtc_device_unregister(pdata->rtc);
232 iounmap(pdata->ioaddr);
233 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
234 kfree(pdata);
235 return 0;
238 static struct platform_driver ds1742_rtc_driver = {
239 .probe = ds1742_rtc_probe,
240 .remove = __devexit_p(ds1742_rtc_remove),
241 .driver = {
242 .name = "ds1742",
243 .owner = THIS_MODULE,
247 static __init int ds1742_init(void)
249 return platform_driver_register(&ds1742_rtc_driver);
252 static __exit void ds1742_exit(void)
254 return platform_driver_unregister(&ds1742_rtc_driver);
257 module_init(ds1742_init);
258 module_exit(ds1742_exit);
260 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
261 MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
262 MODULE_LICENSE("GPL");
263 MODULE_VERSION(DRV_VERSION);