Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Dallas DS1302 RTC Support |
| 3 | * |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 4 | * Copyright (C) 2002 David McCullough |
| 5 | * Copyright (C) 2003 - 2007 Paul Mundt |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 8 | * License version 2. See the file "COPYING" in the main directory of |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 9 | * this archive for more details. |
| 10 | */ |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 11 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/platform_device.h> |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 16 | #include <linux/rtc.h> |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/bcd.h> |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 19 | |
| 20 | #define DRV_NAME "rtc-ds1302" |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 21 | #define DRV_VERSION "0.1.1" |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 22 | |
| 23 | #define RTC_CMD_READ 0x81 /* Read command */ |
| 24 | #define RTC_CMD_WRITE 0x80 /* Write command */ |
| 25 | |
| 26 | #define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */ |
| 27 | #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */ |
| 28 | #define RTC_ADDR_YEAR 0x06 /* Address of year register */ |
| 29 | #define RTC_ADDR_DAY 0x05 /* Address of day of week register */ |
| 30 | #define RTC_ADDR_MON 0x04 /* Address of month register */ |
| 31 | #define RTC_ADDR_DATE 0x03 /* Address of day of month register */ |
| 32 | #define RTC_ADDR_HOUR 0x02 /* Address of hour register */ |
| 33 | #define RTC_ADDR_MIN 0x01 /* Address of minute register */ |
| 34 | #define RTC_ADDR_SEC 0x00 /* Address of second register */ |
| 35 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_SH_SECUREEDGE5410 |
| 37 | #include <asm/rtc.h> |
Paul Mundt | f6eec8d | 2010-10-29 19:06:53 +0900 | [diff] [blame] | 38 | #include <mach/secureedge5410.h> |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 39 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 40 | #define RTC_RESET 0x1000 |
| 41 | #define RTC_IODATA 0x0800 |
| 42 | #define RTC_SCLK 0x0400 |
| 43 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 44 | #define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00) |
| 45 | #define get_dp() SECUREEDGE_READ_IOPORT() |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 46 | #define ds1302_set_tx() |
| 47 | #define ds1302_set_rx() |
| 48 | |
| 49 | static inline int ds1302_hw_init(void) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static inline void ds1302_reset(void) |
| 55 | { |
| 56 | set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK)); |
| 57 | } |
| 58 | |
| 59 | static 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 | |
| 65 | static inline void ds1302_start(void) |
| 66 | { |
| 67 | set_dp(get_dp() | RTC_RESET); |
| 68 | } |
| 69 | |
| 70 | static inline void ds1302_stop(void) |
| 71 | { |
| 72 | set_dp(get_dp() & ~RTC_RESET); |
| 73 | } |
| 74 | |
| 75 | static inline void ds1302_txbit(int bit) |
| 76 | { |
| 77 | set_dp((get_dp() & ~RTC_IODATA) | (bit ? RTC_IODATA : 0)); |
| 78 | } |
| 79 | |
| 80 | static inline int ds1302_rxbit(void) |
| 81 | { |
| 82 | return !!(get_dp() & RTC_IODATA); |
| 83 | } |
| 84 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 85 | #else |
| 86 | #error "Add support for your platform" |
| 87 | #endif |
| 88 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 89 | static void ds1302_sendbits(unsigned int val) |
| 90 | { |
| 91 | int i; |
| 92 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 93 | ds1302_set_tx(); |
| 94 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 95 | for (i = 8; (i); i--, val >>= 1) { |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 96 | ds1302_txbit(val & 0x1); |
| 97 | ds1302_clock(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | static unsigned int ds1302_recvbits(void) |
| 102 | { |
| 103 | unsigned int val; |
| 104 | int i; |
| 105 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 106 | ds1302_set_rx(); |
| 107 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 108 | for (i = 0, val = 0; (i < 8); i++) { |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 109 | val |= (ds1302_rxbit() << i); |
| 110 | ds1302_clock(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return val; |
| 114 | } |
| 115 | |
| 116 | static unsigned int ds1302_readbyte(unsigned int addr) |
| 117 | { |
| 118 | unsigned int val; |
| 119 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 120 | ds1302_reset(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 121 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 122 | ds1302_start(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 123 | ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ); |
| 124 | val = ds1302_recvbits(); |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 125 | ds1302_stop(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 126 | |
| 127 | return val; |
| 128 | } |
| 129 | |
| 130 | static void ds1302_writebyte(unsigned int addr, unsigned int val) |
| 131 | { |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 132 | ds1302_reset(); |
| 133 | |
| 134 | ds1302_start(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 135 | ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE); |
| 136 | ds1302_sendbits(val); |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 137 | ds1302_stop(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 141 | { |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 142 | tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC)); |
| 143 | tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN)); |
| 144 | tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR)); |
| 145 | tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY)); |
| 146 | tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE)); |
| 147 | tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1; |
| 148 | tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR)); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 149 | |
| 150 | if (tm->tm_year < 70) |
| 151 | tm->tm_year += 100; |
| 152 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 153 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
| 154 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
Harvey Harrison | 2a4e2b8 | 2008-04-28 02:12:00 -0700 | [diff] [blame] | 155 | __func__, |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 156 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
| 157 | tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); |
| 158 | |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 159 | return rtc_valid_tm(tm); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 163 | { |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 164 | /* Stop RTC */ |
| 165 | ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80); |
| 166 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 167 | ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec)); |
| 168 | ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min)); |
| 169 | ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour)); |
| 170 | ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday)); |
| 171 | ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday)); |
| 172 | ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1)); |
| 173 | ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100)); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 174 | |
| 175 | /* Start RTC */ |
| 176 | ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80); |
| 177 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 182 | unsigned long arg) |
| 183 | { |
| 184 | switch (cmd) { |
| 185 | #ifdef RTC_SET_CHARGE |
| 186 | case RTC_SET_CHARGE: |
| 187 | { |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 188 | int tcs_val; |
| 189 | |
| 190 | if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int))) |
| 191 | return -EFAULT; |
| 192 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 193 | ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf)); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | #endif |
| 197 | } |
| 198 | |
| 199 | return -ENOIOCTLCMD; |
| 200 | } |
| 201 | |
| 202 | static struct rtc_class_ops ds1302_rtc_ops = { |
| 203 | .read_time = ds1302_rtc_read_time, |
| 204 | .set_time = ds1302_rtc_set_time, |
| 205 | .ioctl = ds1302_rtc_ioctl, |
| 206 | }; |
| 207 | |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 208 | static int __init ds1302_rtc_probe(struct platform_device *pdev) |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 209 | { |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 210 | struct rtc_device *rtc; |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 211 | |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 212 | if (ds1302_hw_init()) { |
| 213 | dev_err(&pdev->dev, "Failed to init communication channel"); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 217 | /* Reset */ |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 218 | ds1302_reset(); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 219 | |
| 220 | /* Write a magic value to the DS1302 RAM, and see if it sticks. */ |
| 221 | ds1302_writebyte(RTC_ADDR_RAM0, 0x42); |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 222 | if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42) { |
| 223 | dev_err(&pdev->dev, "Failed to probe"); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 224 | return -ENODEV; |
Marc Zyngier | 72cc8e5 | 2010-05-24 14:33:47 -0700 | [diff] [blame] | 225 | } |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 226 | |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 227 | rtc = rtc_device_register("ds1302", &pdev->dev, |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 228 | &ds1302_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 229 | if (IS_ERR(rtc)) |
| 230 | return PTR_ERR(rtc); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 231 | |
| 232 | platform_set_drvdata(pdev, rtc); |
| 233 | |
| 234 | return 0; |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int __devexit ds1302_rtc_remove(struct platform_device *pdev) |
| 238 | { |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 239 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 240 | |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 241 | rtc_device_unregister(rtc); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 242 | platform_set_drvdata(pdev, NULL); |
| 243 | |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static struct platform_driver ds1302_platform_driver = { |
| 248 | .driver = { |
| 249 | .name = DRV_NAME, |
| 250 | .owner = THIS_MODULE, |
| 251 | }, |
Uwe Kleine-König | b9e05c6 | 2009-11-24 22:07:23 +0100 | [diff] [blame] | 252 | .remove = __devexit_p(ds1302_rtc_remove), |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | static int __init ds1302_rtc_init(void) |
| 256 | { |
Alessandro Zummo | 2bfc330 | 2009-08-20 12:31:49 +0900 | [diff] [blame] | 257 | return platform_driver_probe(&ds1302_platform_driver, ds1302_rtc_probe); |
Paul Mundt | 739d340 | 2008-02-06 01:38:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void __exit ds1302_rtc_exit(void) |
| 261 | { |
| 262 | platform_driver_unregister(&ds1302_platform_driver); |
| 263 | } |
| 264 | |
| 265 | module_init(ds1302_rtc_init); |
| 266 | module_exit(ds1302_rtc_exit); |
| 267 | |
| 268 | MODULE_DESCRIPTION("Dallas DS1302 RTC driver"); |
| 269 | MODULE_VERSION(DRV_VERSION); |
| 270 | MODULE_AUTHOR("Paul Mundt, David McCullough"); |
| 271 | MODULE_LICENSE("GPL v2"); |