]>
Commit | Line | Data |
---|---|---|
5ec3e4b7 AN |
1 | /* |
2 | * An rtc driver for the Dallas DS1742 | |
3 | * | |
4 | * Copyright (C) 2006 Atsushi Nemoto <[email protected]> | |
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/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> | |
19 | ||
20 | #define DRV_VERSION "0.1" | |
21 | ||
22 | #define RTC_REG_SIZE 0x800 | |
23 | #define RTC_OFFSET 0x7f8 | |
24 | ||
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) | |
34 | ||
35 | #define RTC_CENTURY_MASK 0x3f | |
36 | #define RTC_SECONDS_MASK 0x7f | |
37 | #define RTC_DAY_MASK 0x07 | |
38 | ||
39 | /* Bits in the Control/Century register */ | |
40 | #define RTC_WRITE 0x80 | |
41 | #define RTC_READ 0x40 | |
42 | ||
43 | /* Bits in the Seconds register */ | |
44 | #define RTC_STOP 0x80 | |
45 | ||
46 | /* Bits in the Day register */ | |
47 | #define RTC_BATT_FLAG 0x80 | |
48 | ||
49 | struct rtc_plat_data { | |
50 | struct rtc_device *rtc; | |
51 | void __iomem *ioaddr; | |
52 | unsigned long baseaddr; | |
53 | unsigned long last_jiffies; | |
54 | }; | |
55 | ||
56 | static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
57 | { | |
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; | |
62 | ||
63 | century = BIN2BCD((tm->tm_year + 1900) / 100); | |
64 | ||
65 | writeb(RTC_WRITE, ioaddr + RTC_CONTROL); | |
66 | ||
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); | |
74 | ||
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; | |
79 | } | |
80 | ||
81 | static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
82 | { | |
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; | |
88 | ||
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; | |
111 | ||
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); | |
115 | } | |
116 | return 0; | |
117 | } | |
118 | ||
119 | static struct rtc_class_ops ds1742_rtc_ops = { | |
120 | .read_time = ds1742_rtc_read_time, | |
121 | .set_time = ds1742_rtc_set_time, | |
122 | }; | |
123 | ||
124 | static ssize_t ds1742_nvram_read(struct kobject *kobj, char *buf, | |
125 | loff_t pos, size_t size) | |
126 | { | |
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; | |
132 | ||
133 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) | |
134 | *buf++ = readb(ioaddr + pos++); | |
135 | return count; | |
136 | } | |
137 | ||
138 | static ssize_t ds1742_nvram_write(struct kobject *kobj, char *buf, | |
139 | loff_t pos, size_t size) | |
140 | { | |
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; | |
146 | ||
147 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) | |
148 | writeb(*buf++, ioaddr + pos++); | |
149 | return count; | |
150 | } | |
151 | ||
152 | static struct bin_attribute ds1742_nvram_attr = { | |
153 | .attr = { | |
154 | .name = "nvram", | |
155 | .mode = S_IRUGO | S_IWUGO, | |
156 | .owner = THIS_MODULE, | |
157 | }, | |
158 | .size = RTC_OFFSET, | |
159 | .read = ds1742_nvram_read, | |
160 | .write = ds1742_nvram_write, | |
161 | }; | |
162 | ||
163 | static int __init ds1742_rtc_probe(struct platform_device *pdev) | |
164 | { | |
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; | |
171 | ||
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; | |
181 | } | |
182 | pdata->baseaddr = res->start; | |
183 | ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE); | |
184 | if (!ioaddr) { | |
185 | ret = -ENOMEM; | |
186 | goto out; | |
187 | } | |
188 | pdata->ioaddr = ioaddr; | |
189 | ||
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); | |
198 | } | |
199 | if (readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG) | |
200 | dev_warn(&pdev->dev, "voltage-low detected.\n"); | |
201 | ||
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; | |
207 | } | |
208 | pdata->rtc = rtc; | |
209 | pdata->last_jiffies = jiffies; | |
210 | platform_set_drvdata(pdev, pdata); | |
211 | sysfs_create_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr); | |
212 | return 0; | |
213 | out: | |
214 | if (ioaddr) | |
215 | iounmap(ioaddr); | |
216 | if (pdata->baseaddr) | |
217 | release_mem_region(pdata->baseaddr, RTC_REG_SIZE); | |
218 | kfree(pdata); | |
219 | return ret; | |
220 | } | |
221 | ||
222 | static int __devexit ds1742_rtc_remove(struct platform_device *pdev) | |
223 | { | |
224 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
225 | ||
226 | sysfs_remove_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr); | |
227 | rtc_device_unregister(pdata->rtc); | |
228 | iounmap(pdata->ioaddr); | |
229 | release_mem_region(pdata->baseaddr, RTC_REG_SIZE); | |
230 | kfree(pdata); | |
231 | return 0; | |
232 | } | |
233 | ||
234 | static struct platform_driver ds1742_rtc_driver = { | |
235 | .probe = ds1742_rtc_probe, | |
236 | .remove = __devexit_p(ds1742_rtc_remove), | |
237 | .driver = { | |
238 | .name = "ds1742", | |
239 | .owner = THIS_MODULE, | |
240 | }, | |
241 | }; | |
242 | ||
243 | static __init int ds1742_init(void) | |
244 | { | |
245 | return platform_driver_register(&ds1742_rtc_driver); | |
246 | } | |
247 | ||
248 | static __exit void ds1742_exit(void) | |
249 | { | |
250 | return platform_driver_unregister(&ds1742_rtc_driver); | |
251 | } | |
252 | ||
253 | module_init(ds1742_init); | |
254 | module_exit(ds1742_exit); | |
255 | ||
256 | MODULE_AUTHOR("Atsushi Nemoto <[email protected]>"); | |
257 | MODULE_DESCRIPTION("Dallas DS1742 RTC driver"); | |
258 | MODULE_LICENSE("GPL"); | |
259 | MODULE_VERSION(DRV_VERSION); |