Linux 2.6.33-rc8
[linux-2.6/lguest.git] / drivers / rtc / rtc-rp5c01.c
blobe1313feb060f0bde073b18b05050d763a405a2ae
1 /*
2 * Ricoh RP5C01 RTC Driver
4 * Copyright 2009 Geert Uytterhoeven
6 * Based on the A3000 TOD code in arch/m68k/amiga/config.c
7 * Copyright (C) 1993 Hamish Macdonald
8 */
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/rtc.h>
17 enum {
18 RP5C01_1_SECOND = 0x0, /* MODE 00 */
19 RP5C01_10_SECOND = 0x1, /* MODE 00 */
20 RP5C01_1_MINUTE = 0x2, /* MODE 00 and MODE 01 */
21 RP5C01_10_MINUTE = 0x3, /* MODE 00 and MODE 01 */
22 RP5C01_1_HOUR = 0x4, /* MODE 00 and MODE 01 */
23 RP5C01_10_HOUR = 0x5, /* MODE 00 and MODE 01 */
24 RP5C01_DAY_OF_WEEK = 0x6, /* MODE 00 and MODE 01 */
25 RP5C01_1_DAY = 0x7, /* MODE 00 and MODE 01 */
26 RP5C01_10_DAY = 0x8, /* MODE 00 and MODE 01 */
27 RP5C01_1_MONTH = 0x9, /* MODE 00 */
28 RP5C01_10_MONTH = 0xa, /* MODE 00 */
29 RP5C01_1_YEAR = 0xb, /* MODE 00 */
30 RP5C01_10_YEAR = 0xc, /* MODE 00 */
32 RP5C01_12_24_SELECT = 0xa, /* MODE 01 */
33 RP5C01_LEAP_YEAR = 0xb, /* MODE 01 */
35 RP5C01_MODE = 0xd, /* all modes */
36 RP5C01_TEST = 0xe, /* all modes */
37 RP5C01_RESET = 0xf, /* all modes */
40 #define RP5C01_12_24_SELECT_12 (0 << 0)
41 #define RP5C01_12_24_SELECT_24 (1 << 0)
43 #define RP5C01_10_HOUR_AM (0 << 1)
44 #define RP5C01_10_HOUR_PM (1 << 1)
46 #define RP5C01_MODE_TIMER_EN (1 << 3) /* timer enable */
47 #define RP5C01_MODE_ALARM_EN (1 << 2) /* alarm enable */
49 #define RP5C01_MODE_MODE_MASK (3 << 0)
50 #define RP5C01_MODE_MODE00 (0 << 0) /* time */
51 #define RP5C01_MODE_MODE01 (1 << 0) /* alarm, 12h/24h, leap year */
52 #define RP5C01_MODE_RAM_BLOCK10 (2 << 0) /* RAM 4 bits x 13 */
53 #define RP5C01_MODE_RAM_BLOCK11 (3 << 0) /* RAM 4 bits x 13 */
55 #define RP5C01_RESET_1HZ_PULSE (1 << 3)
56 #define RP5C01_RESET_16HZ_PULSE (1 << 2)
57 #define RP5C01_RESET_SECOND (1 << 1) /* reset divider stages for */
58 /* seconds or smaller units */
59 #define RP5C01_RESET_ALARM (1 << 0) /* reset all alarm registers */
62 struct rp5c01_priv {
63 u32 __iomem *regs;
64 struct rtc_device *rtc;
67 static inline unsigned int rp5c01_read(struct rp5c01_priv *priv,
68 unsigned int reg)
70 return __raw_readl(&priv->regs[reg]) & 0xf;
73 static inline void rp5c01_write(struct rp5c01_priv *priv, unsigned int val,
74 unsigned int reg)
76 return __raw_writel(val, &priv->regs[reg]);
79 static void rp5c01_lock(struct rp5c01_priv *priv)
81 rp5c01_write(priv, RP5C01_MODE_MODE00, RP5C01_MODE);
84 static void rp5c01_unlock(struct rp5c01_priv *priv)
86 rp5c01_write(priv, RP5C01_MODE_TIMER_EN | RP5C01_MODE_MODE01,
87 RP5C01_MODE);
90 static int rp5c01_read_time(struct device *dev, struct rtc_time *tm)
92 struct rp5c01_priv *priv = dev_get_drvdata(dev);
94 rp5c01_lock(priv);
96 tm->tm_sec = rp5c01_read(priv, RP5C01_10_SECOND) * 10 +
97 rp5c01_read(priv, RP5C01_1_SECOND);
98 tm->tm_min = rp5c01_read(priv, RP5C01_10_MINUTE) * 10 +
99 rp5c01_read(priv, RP5C01_1_MINUTE);
100 tm->tm_hour = rp5c01_read(priv, RP5C01_10_HOUR) * 10 +
101 rp5c01_read(priv, RP5C01_1_HOUR);
102 tm->tm_mday = rp5c01_read(priv, RP5C01_10_DAY) * 10 +
103 rp5c01_read(priv, RP5C01_1_DAY);
104 tm->tm_wday = rp5c01_read(priv, RP5C01_DAY_OF_WEEK);
105 tm->tm_mon = rp5c01_read(priv, RP5C01_10_MONTH) * 10 +
106 rp5c01_read(priv, RP5C01_1_MONTH) - 1;
107 tm->tm_year = rp5c01_read(priv, RP5C01_10_YEAR) * 10 +
108 rp5c01_read(priv, RP5C01_1_YEAR);
109 if (tm->tm_year <= 69)
110 tm->tm_year += 100;
112 rp5c01_unlock(priv);
114 return rtc_valid_tm(tm);
117 static int rp5c01_set_time(struct device *dev, struct rtc_time *tm)
119 struct rp5c01_priv *priv = dev_get_drvdata(dev);
121 rp5c01_lock(priv);
123 rp5c01_write(priv, tm->tm_sec / 10, RP5C01_10_SECOND);
124 rp5c01_write(priv, tm->tm_sec % 10, RP5C01_1_SECOND);
125 rp5c01_write(priv, tm->tm_min / 10, RP5C01_10_MINUTE);
126 rp5c01_write(priv, tm->tm_min % 10, RP5C01_1_MINUTE);
127 rp5c01_write(priv, tm->tm_hour / 10, RP5C01_10_HOUR);
128 rp5c01_write(priv, tm->tm_hour % 10, RP5C01_1_HOUR);
129 rp5c01_write(priv, tm->tm_mday / 10, RP5C01_10_DAY);
130 rp5c01_write(priv, tm->tm_mday % 10, RP5C01_1_DAY);
131 if (tm->tm_wday != -1)
132 rp5c01_write(priv, tm->tm_wday, RP5C01_DAY_OF_WEEK);
133 rp5c01_write(priv, (tm->tm_mon + 1) / 10, RP5C01_10_MONTH);
134 rp5c01_write(priv, (tm->tm_mon + 1) % 10, RP5C01_1_MONTH);
135 if (tm->tm_year >= 100)
136 tm->tm_year -= 100;
137 rp5c01_write(priv, tm->tm_year / 10, RP5C01_10_YEAR);
138 rp5c01_write(priv, tm->tm_year % 10, RP5C01_1_YEAR);
140 rp5c01_unlock(priv);
141 return 0;
144 static const struct rtc_class_ops rp5c01_rtc_ops = {
145 .read_time = rp5c01_read_time,
146 .set_time = rp5c01_set_time,
149 static int __init rp5c01_rtc_probe(struct platform_device *dev)
151 struct resource *res;
152 struct rp5c01_priv *priv;
153 struct rtc_device *rtc;
154 int error;
156 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
157 if (!res)
158 return -ENODEV;
160 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
161 if (!priv)
162 return -ENOMEM;
164 priv->regs = ioremap(res->start, resource_size(res));
165 if (!priv->regs) {
166 error = -ENOMEM;
167 goto out_free_priv;
170 rtc = rtc_device_register("rtc-rp5c01", &dev->dev, &rp5c01_rtc_ops,
171 THIS_MODULE);
172 if (IS_ERR(rtc)) {
173 error = PTR_ERR(rtc);
174 goto out_unmap;
177 priv->rtc = rtc;
178 platform_set_drvdata(dev, priv);
179 return 0;
181 out_unmap:
182 iounmap(priv->regs);
183 out_free_priv:
184 kfree(priv);
185 return error;
188 static int __exit rp5c01_rtc_remove(struct platform_device *dev)
190 struct rp5c01_priv *priv = platform_get_drvdata(dev);
192 rtc_device_unregister(priv->rtc);
193 iounmap(priv->regs);
194 kfree(priv);
195 return 0;
198 static struct platform_driver rp5c01_rtc_driver = {
199 .driver = {
200 .name = "rtc-rp5c01",
201 .owner = THIS_MODULE,
203 .remove = __exit_p(rp5c01_rtc_remove),
206 static int __init rp5c01_rtc_init(void)
208 return platform_driver_probe(&rp5c01_rtc_driver, rp5c01_rtc_probe);
211 static void __exit rp5c01_rtc_fini(void)
213 platform_driver_unregister(&rp5c01_rtc_driver);
216 module_init(rp5c01_rtc_init);
217 module_exit(rp5c01_rtc_fini);
219 MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
220 MODULE_LICENSE("GPL");
221 MODULE_DESCRIPTION("Ricoh RP5C01 RTC driver");
222 MODULE_ALIAS("platform:rtc-rp5c01");