Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * A RTC driver for the Simtek STK17TA8 |
| 3 | * |
Martyn Welch | ab4364d | 2010-04-14 12:08:28 +0100 | [diff] [blame] | 4 | * By Thomas Hommel <thomas.hommel@ge.com> |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 5 | * |
| 6 | * Based on the DS1553 driver from |
| 7 | * Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/bcd.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/gfp.h> |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/jiffies.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/io.h> |
| 24 | |
| 25 | #define DRV_VERSION "0.1" |
| 26 | |
| 27 | #define RTC_REG_SIZE 0x20000 |
| 28 | #define RTC_OFFSET 0x1fff0 |
| 29 | |
| 30 | #define RTC_FLAGS (RTC_OFFSET + 0) |
| 31 | #define RTC_CENTURY (RTC_OFFSET + 1) |
| 32 | #define RTC_SECONDS_ALARM (RTC_OFFSET + 2) |
| 33 | #define RTC_MINUTES_ALARM (RTC_OFFSET + 3) |
| 34 | #define RTC_HOURS_ALARM (RTC_OFFSET + 4) |
| 35 | #define RTC_DATE_ALARM (RTC_OFFSET + 5) |
| 36 | #define RTC_INTERRUPTS (RTC_OFFSET + 6) |
| 37 | #define RTC_WATCHDOG (RTC_OFFSET + 7) |
| 38 | #define RTC_CALIBRATION (RTC_OFFSET + 8) |
| 39 | #define RTC_SECONDS (RTC_OFFSET + 9) |
| 40 | #define RTC_MINUTES (RTC_OFFSET + 10) |
| 41 | #define RTC_HOURS (RTC_OFFSET + 11) |
| 42 | #define RTC_DAY (RTC_OFFSET + 12) |
| 43 | #define RTC_DATE (RTC_OFFSET + 13) |
| 44 | #define RTC_MONTH (RTC_OFFSET + 14) |
| 45 | #define RTC_YEAR (RTC_OFFSET + 15) |
| 46 | |
| 47 | #define RTC_SECONDS_MASK 0x7f |
| 48 | #define RTC_DAY_MASK 0x07 |
| 49 | #define RTC_CAL_MASK 0x3f |
| 50 | |
| 51 | /* Bits in the Calibration register */ |
| 52 | #define RTC_STOP 0x80 |
| 53 | |
| 54 | /* Bits in the Flags register */ |
| 55 | #define RTC_FLAGS_AF 0x40 |
| 56 | #define RTC_FLAGS_PF 0x20 |
| 57 | #define RTC_WRITE 0x02 |
| 58 | #define RTC_READ 0x01 |
| 59 | |
| 60 | /* Bits in the Interrupts register */ |
| 61 | #define RTC_INTS_AIE 0x40 |
| 62 | |
| 63 | struct rtc_plat_data { |
| 64 | struct rtc_device *rtc; |
| 65 | void __iomem *ioaddr; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 66 | unsigned long last_jiffies; |
| 67 | int irq; |
| 68 | unsigned int irqen; |
| 69 | int alrm_sec; |
| 70 | int alrm_min; |
| 71 | int alrm_hour; |
| 72 | int alrm_mday; |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 73 | spinlock_t lock; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 77 | { |
| 78 | struct platform_device *pdev = to_platform_device(dev); |
| 79 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 80 | void __iomem *ioaddr = pdata->ioaddr; |
| 81 | u8 flags; |
| 82 | |
| 83 | flags = readb(pdata->ioaddr + RTC_FLAGS); |
| 84 | writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS); |
| 85 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 86 | writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR); |
| 87 | writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH); |
| 88 | writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); |
| 89 | writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE); |
| 90 | writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS); |
| 91 | writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES); |
| 92 | writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); |
| 93 | writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 94 | |
| 95 | writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 100 | { |
| 101 | struct platform_device *pdev = to_platform_device(dev); |
| 102 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 103 | void __iomem *ioaddr = pdata->ioaddr; |
| 104 | unsigned int year, month, day, hour, minute, second, week; |
| 105 | unsigned int century; |
| 106 | u8 flags; |
| 107 | |
| 108 | /* give enough time to update RTC in case of continuous read */ |
| 109 | if (pdata->last_jiffies == jiffies) |
| 110 | msleep(1); |
| 111 | pdata->last_jiffies = jiffies; |
| 112 | |
| 113 | flags = readb(pdata->ioaddr + RTC_FLAGS); |
| 114 | writeb(flags | RTC_READ, ioaddr + RTC_FLAGS); |
| 115 | second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK; |
| 116 | minute = readb(ioaddr + RTC_MINUTES); |
| 117 | hour = readb(ioaddr + RTC_HOURS); |
| 118 | day = readb(ioaddr + RTC_DATE); |
| 119 | week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK; |
| 120 | month = readb(ioaddr + RTC_MONTH); |
| 121 | year = readb(ioaddr + RTC_YEAR); |
| 122 | century = readb(ioaddr + RTC_CENTURY); |
| 123 | writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 124 | tm->tm_sec = bcd2bin(second); |
| 125 | tm->tm_min = bcd2bin(minute); |
| 126 | tm->tm_hour = bcd2bin(hour); |
| 127 | tm->tm_mday = bcd2bin(day); |
| 128 | tm->tm_wday = bcd2bin(week); |
| 129 | tm->tm_mon = bcd2bin(month) - 1; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 130 | /* year is 1900 + tm->tm_year */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 131 | tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 132 | |
| 133 | if (rtc_valid_tm(tm) < 0) { |
| 134 | dev_err(dev, "retrieved date/time is not valid.\n"); |
| 135 | rtc_time_to_tm(0, tm); |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata) |
| 141 | { |
| 142 | void __iomem *ioaddr = pdata->ioaddr; |
| 143 | unsigned long irqflags; |
| 144 | u8 flags; |
| 145 | |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 146 | spin_lock_irqsave(&pdata->lock, irqflags); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 147 | |
| 148 | flags = readb(ioaddr + RTC_FLAGS); |
| 149 | writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); |
| 150 | |
| 151 | writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 152 | 0x80 : bin2bcd(pdata->alrm_mday), |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 153 | ioaddr + RTC_DATE_ALARM); |
| 154 | writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 155 | 0x80 : bin2bcd(pdata->alrm_hour), |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 156 | ioaddr + RTC_HOURS_ALARM); |
| 157 | writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 158 | 0x80 : bin2bcd(pdata->alrm_min), |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 159 | ioaddr + RTC_MINUTES_ALARM); |
| 160 | writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 161 | 0x80 : bin2bcd(pdata->alrm_sec), |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 162 | ioaddr + RTC_SECONDS_ALARM); |
| 163 | writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS); |
| 164 | readb(ioaddr + RTC_FLAGS); /* clear interrupts */ |
| 165 | writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS); |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 166 | spin_unlock_irqrestore(&pdata->lock, irqflags); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 170 | { |
| 171 | struct platform_device *pdev = to_platform_device(dev); |
| 172 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 173 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 174 | if (pdata->irq <= 0) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | pdata->alrm_mday = alrm->time.tm_mday; |
| 177 | pdata->alrm_hour = alrm->time.tm_hour; |
| 178 | pdata->alrm_min = alrm->time.tm_min; |
| 179 | pdata->alrm_sec = alrm->time.tm_sec; |
| 180 | if (alrm->enabled) |
| 181 | pdata->irqen |= RTC_AF; |
| 182 | stk17ta8_rtc_update_alarm(pdata); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 187 | { |
| 188 | struct platform_device *pdev = to_platform_device(dev); |
| 189 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 190 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 191 | if (pdata->irq <= 0) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; |
| 194 | alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; |
| 195 | alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; |
| 196 | alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; |
| 197 | alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id) |
| 202 | { |
| 203 | struct platform_device *pdev = dev_id; |
| 204 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 205 | void __iomem *ioaddr = pdata->ioaddr; |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 206 | unsigned long events = 0; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 207 | |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 208 | spin_lock(&pdata->lock); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 209 | /* read and clear interrupt */ |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 210 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { |
| 211 | events = RTC_IRQF; |
| 212 | if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) |
| 213 | events |= RTC_UF; |
| 214 | else |
| 215 | events |= RTC_AF; |
| 216 | if (likely(pdata->rtc)) |
| 217 | rtc_update_irq(pdata->rtc, 1, events); |
| 218 | } |
| 219 | spin_unlock(&pdata->lock); |
| 220 | return events ? IRQ_HANDLED : IRQ_NONE; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 223 | static int stk17ta8_rtc_alarm_irq_enable(struct device *dev, |
| 224 | unsigned int enabled) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 225 | { |
| 226 | struct platform_device *pdev = to_platform_device(dev); |
| 227 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 228 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 229 | if (pdata->irq <= 0) |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 230 | return -EINVAL; |
| 231 | if (enabled) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 232 | pdata->irqen |= RTC_AF; |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 233 | else |
| 234 | pdata->irqen &= ~RTC_AF; |
| 235 | stk17ta8_rtc_update_alarm(pdata); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static const struct rtc_class_ops stk17ta8_rtc_ops = { |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 240 | .read_time = stk17ta8_rtc_read_time, |
| 241 | .set_time = stk17ta8_rtc_set_time, |
| 242 | .read_alarm = stk17ta8_rtc_read_alarm, |
| 243 | .set_alarm = stk17ta8_rtc_set_alarm, |
| 244 | .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable, |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 247 | static ssize_t stk17ta8_nvram_read(struct file *filp, struct kobject *kobj, |
Al Viro | c98dbe5 | 2007-07-26 17:32:49 +0100 | [diff] [blame] | 248 | struct bin_attribute *attr, char *buf, |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 249 | loff_t pos, size_t size) |
| 250 | { |
Atsushi Nemoto | 50e49be | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 251 | struct device *dev = container_of(kobj, struct device, kobj); |
| 252 | struct platform_device *pdev = to_platform_device(dev); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 253 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 254 | void __iomem *ioaddr = pdata->ioaddr; |
| 255 | ssize_t count; |
| 256 | |
| 257 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) |
| 258 | *buf++ = readb(ioaddr + pos++); |
| 259 | return count; |
| 260 | } |
| 261 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 262 | static ssize_t stk17ta8_nvram_write(struct file *filp, struct kobject *kobj, |
Al Viro | c98dbe5 | 2007-07-26 17:32:49 +0100 | [diff] [blame] | 263 | struct bin_attribute *attr, char *buf, |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 264 | loff_t pos, size_t size) |
| 265 | { |
Atsushi Nemoto | 50e49be | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 266 | struct device *dev = container_of(kobj, struct device, kobj); |
| 267 | struct platform_device *pdev = to_platform_device(dev); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 268 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 269 | void __iomem *ioaddr = pdata->ioaddr; |
| 270 | ssize_t count; |
| 271 | |
| 272 | for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) |
| 273 | writeb(*buf++, ioaddr + pos++); |
| 274 | return count; |
| 275 | } |
| 276 | |
| 277 | static struct bin_attribute stk17ta8_nvram_attr = { |
| 278 | .attr = { |
| 279 | .name = "nvram", |
David Brownell | a4b1d50 | 2007-11-14 16:58:30 -0800 | [diff] [blame] | 280 | .mode = S_IRUGO | S_IWUSR, |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 281 | }, |
| 282 | .size = RTC_OFFSET, |
| 283 | .read = stk17ta8_nvram_read, |
| 284 | .write = stk17ta8_nvram_write, |
| 285 | }; |
| 286 | |
Uwe Kleine-König | 81e627e | 2009-07-11 22:53:05 +0200 | [diff] [blame] | 287 | static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 288 | { |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 289 | struct resource *res; |
| 290 | unsigned int cal; |
| 291 | unsigned int flags; |
| 292 | struct rtc_plat_data *pdata; |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 293 | void __iomem *ioaddr; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 294 | int ret = 0; |
| 295 | |
| 296 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 297 | if (!res) |
| 298 | return -ENODEV; |
| 299 | |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 300 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 301 | if (!pdata) |
| 302 | return -ENOMEM; |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 303 | if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE, |
| 304 | pdev->name)) |
| 305 | return -EBUSY; |
| 306 | ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE); |
| 307 | if (!ioaddr) |
| 308 | return -ENOMEM; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 309 | pdata->ioaddr = ioaddr; |
| 310 | pdata->irq = platform_get_irq(pdev, 0); |
| 311 | |
| 312 | /* turn RTC on if it was not on */ |
| 313 | cal = readb(ioaddr + RTC_CALIBRATION); |
| 314 | if (cal & RTC_STOP) { |
| 315 | cal &= RTC_CAL_MASK; |
| 316 | flags = readb(ioaddr + RTC_FLAGS); |
| 317 | writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); |
| 318 | writeb(cal, ioaddr + RTC_CALIBRATION); |
| 319 | writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS); |
| 320 | } |
| 321 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF) |
| 322 | dev_warn(&pdev->dev, "voltage-low detected.\n"); |
| 323 | |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 324 | spin_lock_init(&pdata->lock); |
| 325 | pdata->last_jiffies = jiffies; |
| 326 | platform_set_drvdata(pdev, pdata); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 327 | if (pdata->irq > 0) { |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 328 | writeb(0, ioaddr + RTC_INTERRUPTS); |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 329 | if (devm_request_irq(&pdev->dev, pdata->irq, |
| 330 | stk17ta8_rtc_interrupt, |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 331 | IRQF_DISABLED | IRQF_SHARED, |
| 332 | pdev->name, pdev) < 0) { |
| 333 | dev_warn(&pdev->dev, "interrupt not available.\n"); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 334 | pdata->irq = 0; |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 338 | pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 339 | &stk17ta8_rtc_ops, THIS_MODULE); |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 340 | if (IS_ERR(pdata->rtc)) |
| 341 | return PTR_ERR(pdata->rtc); |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 342 | |
| 343 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr); |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 344 | if (ret) |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 345 | rtc_device_unregister(pdata->rtc); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static int __devexit stk17ta8_rtc_remove(struct platform_device *pdev) |
| 350 | { |
| 351 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 352 | |
| 353 | sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr); |
| 354 | rtc_device_unregister(pdata->rtc); |
Atsushi Nemoto | 3151520 | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 355 | if (pdata->irq > 0) |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 356 | writeb(0, pdata->ioaddr + RTC_INTERRUPTS); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 360 | /* work with hotplug and coldplug */ |
| 361 | MODULE_ALIAS("platform:stk17ta8"); |
| 362 | |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 363 | static struct platform_driver stk17ta8_rtc_driver = { |
| 364 | .probe = stk17ta8_rtc_probe, |
| 365 | .remove = __devexit_p(stk17ta8_rtc_remove), |
| 366 | .driver = { |
| 367 | .name = "stk17ta8", |
| 368 | .owner = THIS_MODULE, |
| 369 | }, |
| 370 | }; |
| 371 | |
| 372 | static __init int stk17ta8_init(void) |
| 373 | { |
| 374 | return platform_driver_register(&stk17ta8_rtc_driver); |
| 375 | } |
| 376 | |
| 377 | static __exit void stk17ta8_exit(void) |
| 378 | { |
Hannes Eder | 277835a | 2008-11-28 17:11:47 +0100 | [diff] [blame] | 379 | platform_driver_unregister(&stk17ta8_rtc_driver); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | module_init(stk17ta8_init); |
| 383 | module_exit(stk17ta8_exit); |
| 384 | |
Martyn Welch | ab4364d | 2010-04-14 12:08:28 +0100 | [diff] [blame] | 385 | MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>"); |
Thomas Hommel | 0296411 | 2007-07-21 04:37:58 -0700 | [diff] [blame] | 386 | MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver"); |
| 387 | MODULE_LICENSE("GPL"); |
| 388 | MODULE_VERSION(DRV_VERSION); |