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