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