Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / rtc / rtc-ds1302.c
blob184556620778c5da5f8ec45dc3144d3b611a47ca
1 /*
2 * Dallas DS1302 RTC Support
4 * Copyright (C) 2002 David McCullough
5 * Copyright (C) 2003 - 2007 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License version 2. See the file "COPYING" in the main directory of
9 * this archive for more details.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/time.h>
16 #include <linux/rtc.h>
17 #include <linux/spinlock.h>
18 #include <linux/io.h>
19 #include <linux/bcd.h>
20 #include <asm/rtc.h>
22 #define DRV_NAME "rtc-ds1302"
23 #define DRV_VERSION "0.1.0"
25 #define RTC_CMD_READ 0x81 /* Read command */
26 #define RTC_CMD_WRITE 0x80 /* Write command */
28 #define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */
29 #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */
30 #define RTC_ADDR_YEAR 0x06 /* Address of year register */
31 #define RTC_ADDR_DAY 0x05 /* Address of day of week register */
32 #define RTC_ADDR_MON 0x04 /* Address of month register */
33 #define RTC_ADDR_DATE 0x03 /* Address of day of month register */
34 #define RTC_ADDR_HOUR 0x02 /* Address of hour register */
35 #define RTC_ADDR_MIN 0x01 /* Address of minute register */
36 #define RTC_ADDR_SEC 0x00 /* Address of second register */
38 #define RTC_RESET 0x1000
39 #define RTC_IODATA 0x0800
40 #define RTC_SCLK 0x0400
42 #ifdef CONFIG_SH_SECUREEDGE5410
43 #include <mach/snapgear.h>
44 #define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
45 #define get_dp() SECUREEDGE_READ_IOPORT()
46 #else
47 #error "Add support for your platform"
48 #endif
50 struct ds1302_rtc {
51 struct rtc_device *rtc_dev;
52 spinlock_t lock;
55 static void ds1302_sendbits(unsigned int val)
57 int i;
59 for (i = 8; (i); i--, val >>= 1) {
60 set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ?
61 RTC_IODATA : 0));
62 set_dp(get_dp() | RTC_SCLK); /* clock high */
63 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
67 static unsigned int ds1302_recvbits(void)
69 unsigned int val;
70 int i;
72 for (i = 0, val = 0; (i < 8); i++) {
73 val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i);
74 set_dp(get_dp() | RTC_SCLK); /* clock high */
75 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
78 return val;
81 static unsigned int ds1302_readbyte(unsigned int addr)
83 unsigned int val;
85 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
87 set_dp(get_dp() | RTC_RESET);
88 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
89 val = ds1302_recvbits();
90 set_dp(get_dp() & ~RTC_RESET);
92 return val;
95 static void ds1302_writebyte(unsigned int addr, unsigned int val)
97 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
98 set_dp(get_dp() | RTC_RESET);
99 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
100 ds1302_sendbits(val);
101 set_dp(get_dp() & ~RTC_RESET);
104 static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
106 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
108 spin_lock_irq(&rtc->lock);
110 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
111 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
112 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
113 tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
114 tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
115 tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
116 tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
118 if (tm->tm_year < 70)
119 tm->tm_year += 100;
121 spin_unlock_irq(&rtc->lock);
123 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
124 "mday=%d, mon=%d, year=%d, wday=%d\n",
125 __func__,
126 tm->tm_sec, tm->tm_min, tm->tm_hour,
127 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
129 if (rtc_valid_tm(tm) < 0)
130 dev_err(dev, "invalid date\n");
132 return 0;
135 static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
137 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
139 spin_lock_irq(&rtc->lock);
141 /* Stop RTC */
142 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
144 ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
145 ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
146 ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
147 ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
148 ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
149 ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
150 ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
152 /* Start RTC */
153 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
155 spin_unlock_irq(&rtc->lock);
157 return 0;
160 static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
161 unsigned long arg)
163 switch (cmd) {
164 #ifdef RTC_SET_CHARGE
165 case RTC_SET_CHARGE:
167 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
168 int tcs_val;
170 if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
171 return -EFAULT;
173 spin_lock_irq(&rtc->lock);
174 ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
175 spin_unlock_irq(&rtc->lock);
176 return 0;
178 #endif
181 return -ENOIOCTLCMD;
184 static struct rtc_class_ops ds1302_rtc_ops = {
185 .read_time = ds1302_rtc_read_time,
186 .set_time = ds1302_rtc_set_time,
187 .ioctl = ds1302_rtc_ioctl,
190 static int __devinit ds1302_rtc_probe(struct platform_device *pdev)
192 struct ds1302_rtc *rtc;
193 int ret;
195 /* Reset */
196 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
198 /* Write a magic value to the DS1302 RAM, and see if it sticks. */
199 ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
200 if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42)
201 return -ENODEV;
203 rtc = kzalloc(sizeof(struct ds1302_rtc), GFP_KERNEL);
204 if (unlikely(!rtc))
205 return -ENOMEM;
207 spin_lock_init(&rtc->lock);
208 rtc->rtc_dev = rtc_device_register("ds1302", &pdev->dev,
209 &ds1302_rtc_ops, THIS_MODULE);
210 if (IS_ERR(rtc->rtc_dev)) {
211 ret = PTR_ERR(rtc->rtc_dev);
212 goto out;
215 platform_set_drvdata(pdev, rtc);
217 return 0;
218 out:
219 kfree(rtc);
220 return ret;
223 static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
225 struct ds1302_rtc *rtc = platform_get_drvdata(pdev);
227 if (likely(rtc->rtc_dev))
228 rtc_device_unregister(rtc->rtc_dev);
230 platform_set_drvdata(pdev, NULL);
232 kfree(rtc);
234 return 0;
237 static struct platform_driver ds1302_platform_driver = {
238 .driver = {
239 .name = DRV_NAME,
240 .owner = THIS_MODULE,
242 .probe = ds1302_rtc_probe,
243 .remove = __devexit_p(ds1302_rtc_remove),
246 static int __init ds1302_rtc_init(void)
248 return platform_driver_register(&ds1302_platform_driver);
251 static void __exit ds1302_rtc_exit(void)
253 platform_driver_unregister(&ds1302_platform_driver);
256 module_init(ds1302_rtc_init);
257 module_exit(ds1302_rtc_exit);
259 MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
260 MODULE_VERSION(DRV_VERSION);
261 MODULE_AUTHOR("Paul Mundt, David McCullough");
262 MODULE_LICENSE("GPL v2");