aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-ds1302.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/rtc-ds1302.c')
-rw-r--r--drivers/rtc/rtc-ds1302.c153
1 files changed, 81 insertions, 72 deletions
diff --git a/drivers/rtc/rtc-ds1302.c b/drivers/rtc/rtc-ds1302.c
index 184556620778..f0d638922644 100644
--- a/drivers/rtc/rtc-ds1302.c
+++ b/drivers/rtc/rtc-ds1302.c
@@ -1,26 +1,24 @@
1/* 1/*
2 * Dallas DS1302 RTC Support 2 * Dallas DS1302 RTC Support
3 * 3 *
4 * Copyright (C) 2002 David McCullough 4 * Copyright (C) 2002 David McCullough
5 * Copyright (C) 2003 - 2007 Paul Mundt 5 * Copyright (C) 2003 - 2007 Paul Mundt
6 * 6 *
7 * This file is subject to the terms and conditions of the GNU General Public 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 8 * License version 2. See the file "COPYING" in the main directory of
9 * this archive for more details. 9 * this archive for more details.
10 */ 10 */
11
11#include <linux/init.h> 12#include <linux/init.h>
12#include <linux/module.h> 13#include <linux/module.h>
13#include <linux/kernel.h> 14#include <linux/kernel.h>
14#include <linux/platform_device.h> 15#include <linux/platform_device.h>
15#include <linux/time.h>
16#include <linux/rtc.h> 16#include <linux/rtc.h>
17#include <linux/spinlock.h>
18#include <linux/io.h> 17#include <linux/io.h>
19#include <linux/bcd.h> 18#include <linux/bcd.h>
20#include <asm/rtc.h>
21 19
22#define DRV_NAME "rtc-ds1302" 20#define DRV_NAME "rtc-ds1302"
23#define DRV_VERSION "0.1.0" 21#define DRV_VERSION "0.1.1"
24 22
25#define RTC_CMD_READ 0x81 /* Read command */ 23#define RTC_CMD_READ 0x81 /* Read command */
26#define RTC_CMD_WRITE 0x80 /* Write command */ 24#define RTC_CMD_WRITE 0x80 /* Write command */
@@ -35,32 +33,68 @@
35#define RTC_ADDR_MIN 0x01 /* Address of minute register */ 33#define RTC_ADDR_MIN 0x01 /* Address of minute register */
36#define RTC_ADDR_SEC 0x00 /* Address of second register */ 34#define RTC_ADDR_SEC 0x00 /* Address of second register */
37 35
36#ifdef CONFIG_SH_SECUREEDGE5410
37#include <asm/rtc.h>
38#include <mach/secureedge5410.h>
39
38#define RTC_RESET 0x1000 40#define RTC_RESET 0x1000
39#define RTC_IODATA 0x0800 41#define RTC_IODATA 0x0800
40#define RTC_SCLK 0x0400 42#define RTC_SCLK 0x0400
41 43
42#ifdef CONFIG_SH_SECUREEDGE5410
43#include <mach/snapgear.h>
44#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00) 44#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
45#define get_dp() SECUREEDGE_READ_IOPORT() 45#define get_dp() SECUREEDGE_READ_IOPORT()
46#define ds1302_set_tx()
47#define ds1302_set_rx()
48
49static inline int ds1302_hw_init(void)
50{
51 return 0;
52}
53
54static inline void ds1302_reset(void)
55{
56 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
57}
58
59static inline void ds1302_clock(void)
60{
61 set_dp(get_dp() | RTC_SCLK); /* clock high */
62 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
63}
64
65static inline void ds1302_start(void)
66{
67 set_dp(get_dp() | RTC_RESET);
68}
69
70static inline void ds1302_stop(void)
71{
72 set_dp(get_dp() & ~RTC_RESET);
73}
74
75static inline void ds1302_txbit(int bit)
76{
77 set_dp((get_dp() & ~RTC_IODATA) | (bit ? RTC_IODATA : 0));
78}
79
80static inline int ds1302_rxbit(void)
81{
82 return !!(get_dp() & RTC_IODATA);
83}
84
46#else 85#else
47#error "Add support for your platform" 86#error "Add support for your platform"
48#endif 87#endif
49 88
50struct ds1302_rtc {
51 struct rtc_device *rtc_dev;
52 spinlock_t lock;
53};
54
55static void ds1302_sendbits(unsigned int val) 89static void ds1302_sendbits(unsigned int val)
56{ 90{
57 int i; 91 int i;
58 92
93 ds1302_set_tx();
94
59 for (i = 8; (i); i--, val >>= 1) { 95 for (i = 8; (i); i--, val >>= 1) {
60 set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ? 96 ds1302_txbit(val & 0x1);
61 RTC_IODATA : 0)); 97 ds1302_clock();
62 set_dp(get_dp() | RTC_SCLK); /* clock high */
63 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
64 } 98 }
65} 99}
66 100
@@ -69,10 +103,11 @@ static unsigned int ds1302_recvbits(void)
69 unsigned int val; 103 unsigned int val;
70 int i; 104 int i;
71 105
106 ds1302_set_rx();
107
72 for (i = 0, val = 0; (i < 8); i++) { 108 for (i = 0, val = 0; (i < 8); i++) {
73 val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i); 109 val |= (ds1302_rxbit() << i);
74 set_dp(get_dp() | RTC_SCLK); /* clock high */ 110 ds1302_clock();
75 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
76 } 111 }
77 112
78 return val; 113 return val;
@@ -82,31 +117,28 @@ static unsigned int ds1302_readbyte(unsigned int addr)
82{ 117{
83 unsigned int val; 118 unsigned int val;
84 119
85 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK)); 120 ds1302_reset();
86 121
87 set_dp(get_dp() | RTC_RESET); 122 ds1302_start();
88 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ); 123 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
89 val = ds1302_recvbits(); 124 val = ds1302_recvbits();
90 set_dp(get_dp() & ~RTC_RESET); 125 ds1302_stop();
91 126
92 return val; 127 return val;
93} 128}
94 129
95static void ds1302_writebyte(unsigned int addr, unsigned int val) 130static void ds1302_writebyte(unsigned int addr, unsigned int val)
96{ 131{
97 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK)); 132 ds1302_reset();
98 set_dp(get_dp() | RTC_RESET); 133
134 ds1302_start();
99 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE); 135 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
100 ds1302_sendbits(val); 136 ds1302_sendbits(val);
101 set_dp(get_dp() & ~RTC_RESET); 137 ds1302_stop();
102} 138}
103 139
104static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm) 140static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
105{ 141{
106 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
107
108 spin_lock_irq(&rtc->lock);
109
110 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC)); 142 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
111 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN)); 143 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
112 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR)); 144 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
@@ -118,26 +150,17 @@ static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
118 if (tm->tm_year < 70) 150 if (tm->tm_year < 70)
119 tm->tm_year += 100; 151 tm->tm_year += 100;
120 152
121 spin_unlock_irq(&rtc->lock);
122
123 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 153 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
124 "mday=%d, mon=%d, year=%d, wday=%d\n", 154 "mday=%d, mon=%d, year=%d, wday=%d\n",
125 __func__, 155 __func__,
126 tm->tm_sec, tm->tm_min, tm->tm_hour, 156 tm->tm_sec, tm->tm_min, tm->tm_hour,
127 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); 157 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
128 158
129 if (rtc_valid_tm(tm) < 0) 159 return rtc_valid_tm(tm);
130 dev_err(dev, "invalid date\n");
131
132 return 0;
133} 160}
134 161
135static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm) 162static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
136{ 163{
137 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
138
139 spin_lock_irq(&rtc->lock);
140
141 /* Stop RTC */ 164 /* Stop RTC */
142 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80); 165 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
143 166
@@ -152,8 +175,6 @@ static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
152 /* Start RTC */ 175 /* Start RTC */
153 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80); 176 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
154 177
155 spin_unlock_irq(&rtc->lock);
156
157 return 0; 178 return 0;
158} 179}
159 180
@@ -164,15 +185,12 @@ static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
164#ifdef RTC_SET_CHARGE 185#ifdef RTC_SET_CHARGE
165 case RTC_SET_CHARGE: 186 case RTC_SET_CHARGE:
166 { 187 {
167 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
168 int tcs_val; 188 int tcs_val;
169 189
170 if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int))) 190 if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
171 return -EFAULT; 191 return -EFAULT;
172 192
173 spin_lock_irq(&rtc->lock);
174 ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf)); 193 ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
175 spin_unlock_irq(&rtc->lock);
176 return 0; 194 return 0;
177 } 195 }
178#endif 196#endif
@@ -187,50 +205,42 @@ static struct rtc_class_ops ds1302_rtc_ops = {
187 .ioctl = ds1302_rtc_ioctl, 205 .ioctl = ds1302_rtc_ioctl,
188}; 206};
189 207
190static int __devinit ds1302_rtc_probe(struct platform_device *pdev) 208static int __init ds1302_rtc_probe(struct platform_device *pdev)
191{ 209{
192 struct ds1302_rtc *rtc; 210 struct rtc_device *rtc;
193 int ret; 211
212 if (ds1302_hw_init()) {
213 dev_err(&pdev->dev, "Failed to init communication channel");
214 return -EINVAL;
215 }
194 216
195 /* Reset */ 217 /* Reset */
196 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK)); 218 ds1302_reset();
197 219
198 /* Write a magic value to the DS1302 RAM, and see if it sticks. */ 220 /* Write a magic value to the DS1302 RAM, and see if it sticks. */
199 ds1302_writebyte(RTC_ADDR_RAM0, 0x42); 221 ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
200 if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42) 222 if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42) {
223 dev_err(&pdev->dev, "Failed to probe");
201 return -ENODEV; 224 return -ENODEV;
225 }
202 226
203 rtc = kzalloc(sizeof(struct ds1302_rtc), GFP_KERNEL); 227 rtc = rtc_device_register("ds1302", &pdev->dev,
204 if (unlikely(!rtc))
205 return -ENOMEM;
206
207 spin_lock_init(&rtc->lock);
208 rtc->rtc_dev = rtc_device_register("ds1302", &pdev->dev,
209 &ds1302_rtc_ops, THIS_MODULE); 228 &ds1302_rtc_ops, THIS_MODULE);
210 if (IS_ERR(rtc->rtc_dev)) { 229 if (IS_ERR(rtc))
211 ret = PTR_ERR(rtc->rtc_dev); 230 return PTR_ERR(rtc);
212 goto out;
213 }
214 231
215 platform_set_drvdata(pdev, rtc); 232 platform_set_drvdata(pdev, rtc);
216 233
217 return 0; 234 return 0;
218out:
219 kfree(rtc);
220 return ret;
221} 235}
222 236
223static int __devexit ds1302_rtc_remove(struct platform_device *pdev) 237static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
224{ 238{
225 struct ds1302_rtc *rtc = platform_get_drvdata(pdev); 239 struct rtc_device *rtc = platform_get_drvdata(pdev);
226
227 if (likely(rtc->rtc_dev))
228 rtc_device_unregister(rtc->rtc_dev);
229 240
241 rtc_device_unregister(rtc);
230 platform_set_drvdata(pdev, NULL); 242 platform_set_drvdata(pdev, NULL);
231 243
232 kfree(rtc);
233
234 return 0; 244 return 0;
235} 245}
236 246
@@ -239,13 +249,12 @@ static struct platform_driver ds1302_platform_driver = {
239 .name = DRV_NAME, 249 .name = DRV_NAME,
240 .owner = THIS_MODULE, 250 .owner = THIS_MODULE,
241 }, 251 },
242 .probe = ds1302_rtc_probe,
243 .remove = __devexit_p(ds1302_rtc_remove), 252 .remove = __devexit_p(ds1302_rtc_remove),
244}; 253};
245 254
246static int __init ds1302_rtc_init(void) 255static int __init ds1302_rtc_init(void)
247{ 256{
248 return platform_driver_register(&ds1302_platform_driver); 257 return platform_driver_probe(&ds1302_platform_driver, ds1302_rtc_probe);
249} 258}
250 259
251static void __exit ds1302_rtc_exit(void) 260static void __exit ds1302_rtc_exit(void)