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> |
Paul Gortmaker | 2113852 | 2011-05-27 09:57:25 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 22 | |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 23 | #define DRV_VERSION "0.3" |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 24 | |
| 25 | #define RTC_REG_SIZE 0x2000 |
| 26 | #define RTC_OFFSET 0x1ff0 |
| 27 | |
| 28 | #define RTC_FLAGS (RTC_OFFSET + 0) |
| 29 | #define RTC_SECONDS_ALARM (RTC_OFFSET + 2) |
| 30 | #define RTC_MINUTES_ALARM (RTC_OFFSET + 3) |
| 31 | #define RTC_HOURS_ALARM (RTC_OFFSET + 4) |
| 32 | #define RTC_DATE_ALARM (RTC_OFFSET + 5) |
| 33 | #define RTC_INTERRUPTS (RTC_OFFSET + 6) |
| 34 | #define RTC_WATCHDOG (RTC_OFFSET + 7) |
| 35 | #define RTC_CONTROL (RTC_OFFSET + 8) |
| 36 | #define RTC_CENTURY (RTC_OFFSET + 8) |
| 37 | #define RTC_SECONDS (RTC_OFFSET + 9) |
| 38 | #define RTC_MINUTES (RTC_OFFSET + 10) |
| 39 | #define RTC_HOURS (RTC_OFFSET + 11) |
| 40 | #define RTC_DAY (RTC_OFFSET + 12) |
| 41 | #define RTC_DATE (RTC_OFFSET + 13) |
| 42 | #define RTC_MONTH (RTC_OFFSET + 14) |
| 43 | #define RTC_YEAR (RTC_OFFSET + 15) |
| 44 | |
| 45 | #define RTC_CENTURY_MASK 0x3f |
| 46 | #define RTC_SECONDS_MASK 0x7f |
| 47 | #define RTC_DAY_MASK 0x07 |
| 48 | |
| 49 | /* Bits in the Control/Century register */ |
| 50 | #define RTC_WRITE 0x80 |
| 51 | #define RTC_READ 0x40 |
| 52 | |
| 53 | /* Bits in the Seconds register */ |
| 54 | #define RTC_STOP 0x80 |
| 55 | |
| 56 | /* Bits in the Flags register */ |
| 57 | #define RTC_FLAGS_AF 0x40 |
| 58 | #define RTC_FLAGS_BLF 0x10 |
| 59 | |
| 60 | /* Bits in the Interrupts register */ |
| 61 | #define RTC_INTS_AE 0x80 |
| 62 | |
| 63 | struct rtc_plat_data { |
| 64 | struct rtc_device *rtc; |
| 65 | void __iomem *ioaddr; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -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 | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 73 | spinlock_t lock; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static int ds1553_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 century; |
| 82 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 83 | century = bin2bcd((tm->tm_year + 1900) / 100); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 84 | |
| 85 | writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL); |
| 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); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 94 | |
| 95 | /* RTC_CENTURY and RTC_CONTROL share same register */ |
| 96 | writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY); |
| 97 | writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 102 | { |
| 103 | struct platform_device *pdev = to_platform_device(dev); |
| 104 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 105 | void __iomem *ioaddr = pdata->ioaddr; |
| 106 | unsigned int year, month, day, hour, minute, second, week; |
| 107 | unsigned int century; |
| 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 | writeb(RTC_READ, ioaddr + RTC_CONTROL); |
| 114 | second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK; |
| 115 | minute = readb(ioaddr + RTC_MINUTES); |
| 116 | hour = readb(ioaddr + RTC_HOURS); |
| 117 | day = readb(ioaddr + RTC_DATE); |
| 118 | week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK; |
| 119 | month = readb(ioaddr + RTC_MONTH); |
| 120 | year = readb(ioaddr + RTC_YEAR); |
| 121 | century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; |
| 122 | writeb(0, ioaddr + RTC_CONTROL); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 123 | tm->tm_sec = bcd2bin(second); |
| 124 | tm->tm_min = bcd2bin(minute); |
| 125 | tm->tm_hour = bcd2bin(hour); |
| 126 | tm->tm_mday = bcd2bin(day); |
| 127 | tm->tm_wday = bcd2bin(week); |
| 128 | tm->tm_mon = bcd2bin(month) - 1; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 129 | /* year is 1900 + tm->tm_year */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 130 | tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 131 | |
| 132 | if (rtc_valid_tm(tm) < 0) { |
| 133 | dev_err(dev, "retrieved date/time is not valid.\n"); |
| 134 | rtc_time_to_tm(0, tm); |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata) |
| 140 | { |
| 141 | void __iomem *ioaddr = pdata->ioaddr; |
| 142 | unsigned long flags; |
| 143 | |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 144 | spin_lock_irqsave(&pdata->lock, flags); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 145 | writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 146 | 0x80 : bin2bcd(pdata->alrm_mday), |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 147 | ioaddr + RTC_DATE_ALARM); |
| 148 | writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 149 | 0x80 : bin2bcd(pdata->alrm_hour), |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 150 | ioaddr + RTC_HOURS_ALARM); |
| 151 | writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 152 | 0x80 : bin2bcd(pdata->alrm_min), |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 153 | ioaddr + RTC_MINUTES_ALARM); |
| 154 | writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 155 | 0x80 : bin2bcd(pdata->alrm_sec), |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 156 | ioaddr + RTC_SECONDS_ALARM); |
| 157 | writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS); |
| 158 | readb(ioaddr + RTC_FLAGS); /* clear interrupts */ |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 159 | spin_unlock_irqrestore(&pdata->lock, flags); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 163 | { |
| 164 | struct platform_device *pdev = to_platform_device(dev); |
| 165 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 166 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 167 | if (pdata->irq <= 0) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 168 | return -EINVAL; |
| 169 | pdata->alrm_mday = alrm->time.tm_mday; |
| 170 | pdata->alrm_hour = alrm->time.tm_hour; |
| 171 | pdata->alrm_min = alrm->time.tm_min; |
| 172 | pdata->alrm_sec = alrm->time.tm_sec; |
| 173 | if (alrm->enabled) |
| 174 | pdata->irqen |= RTC_AF; |
| 175 | ds1553_rtc_update_alarm(pdata); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 180 | { |
| 181 | struct platform_device *pdev = to_platform_device(dev); |
| 182 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 183 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 184 | if (pdata->irq <= 0) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; |
| 187 | alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; |
| 188 | alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; |
| 189 | alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; |
| 190 | alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; |
| 191 | return 0; |
| 192 | } |
| 193 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 194 | static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 195 | { |
| 196 | struct platform_device *pdev = dev_id; |
| 197 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 198 | void __iomem *ioaddr = pdata->ioaddr; |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 199 | unsigned long events = 0; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 200 | |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 201 | spin_lock(&pdata->lock); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 202 | /* read and clear interrupt */ |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 203 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { |
| 204 | events = RTC_IRQF; |
| 205 | if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) |
| 206 | events |= RTC_UF; |
| 207 | else |
| 208 | events |= RTC_AF; |
Alexander Shiyan | 0d71915 | 2014-04-03 14:50:20 -0700 | [diff] [blame] | 209 | rtc_update_irq(pdata->rtc, 1, events); |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 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 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 230 | static const struct rtc_class_ops ds1553_rtc_ops = { |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 231 | .read_time = ds1553_rtc_read_time, |
| 232 | .set_time = ds1553_rtc_set_time, |
| 233 | .read_alarm = ds1553_rtc_read_alarm, |
| 234 | .set_alarm = ds1553_rtc_set_alarm, |
| 235 | .alarm_irq_enable = ds1553_rtc_alarm_irq_enable, |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 238 | static ssize_t ds1553_nvram_read(struct file *filp, struct kobject *kobj, |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 239 | struct bin_attribute *bin_attr, |
| 240 | char *buf, loff_t pos, size_t size) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 241 | { |
Atsushi Nemoto | 50e49be | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 242 | struct device *dev = container_of(kobj, struct device, kobj); |
| 243 | struct platform_device *pdev = to_platform_device(dev); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 244 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 245 | void __iomem *ioaddr = pdata->ioaddr; |
| 246 | ssize_t count; |
| 247 | |
Vladimir Zapolskiy | a9118d7 | 2015-07-27 00:48:31 +0300 | [diff] [blame] | 248 | for (count = 0; count < size; count++) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 249 | *buf++ = readb(ioaddr + pos++); |
| 250 | return count; |
| 251 | } |
| 252 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 253 | static ssize_t ds1553_nvram_write(struct file *filp, struct kobject *kobj, |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 254 | struct bin_attribute *bin_attr, |
| 255 | char *buf, loff_t pos, size_t size) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 256 | { |
Atsushi Nemoto | 50e49be | 2009-12-15 16:46:04 -0800 | [diff] [blame] | 257 | struct device *dev = container_of(kobj, struct device, kobj); |
| 258 | struct platform_device *pdev = to_platform_device(dev); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 259 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 260 | void __iomem *ioaddr = pdata->ioaddr; |
| 261 | ssize_t count; |
| 262 | |
Vladimir Zapolskiy | a9118d7 | 2015-07-27 00:48:31 +0300 | [diff] [blame] | 263 | for (count = 0; count < size; count++) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 264 | writeb(*buf++, ioaddr + pos++); |
| 265 | return count; |
| 266 | } |
| 267 | |
| 268 | static struct bin_attribute ds1553_nvram_attr = { |
| 269 | .attr = { |
| 270 | .name = "nvram", |
David Brownell | a4b1d50 | 2007-11-14 16:58:30 -0800 | [diff] [blame] | 271 | .mode = S_IRUGO | S_IWUSR, |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 272 | }, |
| 273 | .size = RTC_OFFSET, |
| 274 | .read = ds1553_nvram_read, |
| 275 | .write = ds1553_nvram_write, |
| 276 | }; |
| 277 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 278 | static int ds1553_rtc_probe(struct platform_device *pdev) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 279 | { |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 280 | struct resource *res; |
| 281 | unsigned int cen, sec; |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 282 | struct rtc_plat_data *pdata; |
| 283 | void __iomem *ioaddr; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 284 | int ret = 0; |
| 285 | |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 286 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 287 | if (!pdata) |
| 288 | return -ENOMEM; |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 289 | |
Julia Lawall | 7c1d69e | 2013-09-11 14:24:27 -0700 | [diff] [blame] | 290 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 291 | ioaddr = devm_ioremap_resource(&pdev->dev, res); |
| 292 | if (IS_ERR(ioaddr)) |
| 293 | return PTR_ERR(ioaddr); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 294 | pdata->ioaddr = ioaddr; |
| 295 | pdata->irq = platform_get_irq(pdev, 0); |
| 296 | |
| 297 | /* turn RTC on if it was not on */ |
| 298 | sec = readb(ioaddr + RTC_SECONDS); |
| 299 | if (sec & RTC_STOP) { |
| 300 | sec &= RTC_SECONDS_MASK; |
| 301 | cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; |
| 302 | writeb(RTC_WRITE, ioaddr + RTC_CONTROL); |
| 303 | writeb(sec, ioaddr + RTC_SECONDS); |
| 304 | writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL); |
| 305 | } |
| 306 | if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF) |
| 307 | dev_warn(&pdev->dev, "voltage-low detected.\n"); |
| 308 | |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 309 | spin_lock_init(&pdata->lock); |
| 310 | pdata->last_jiffies = jiffies; |
| 311 | platform_set_drvdata(pdev, pdata); |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 312 | |
| 313 | pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, |
| 314 | &ds1553_rtc_ops, THIS_MODULE); |
| 315 | if (IS_ERR(pdata->rtc)) |
| 316 | return PTR_ERR(pdata->rtc); |
| 317 | |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 318 | if (pdata->irq > 0) { |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 319 | writeb(0, ioaddr + RTC_INTERRUPTS); |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 320 | if (devm_request_irq(&pdev->dev, pdata->irq, |
| 321 | ds1553_rtc_interrupt, |
Yong Zhang | 2f6e5f9 | 2012-03-23 15:02:34 -0700 | [diff] [blame] | 322 | 0, pdev->name, pdev) < 0) { |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 323 | dev_warn(&pdev->dev, "interrupt not available.\n"); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 324 | pdata->irq = 0; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Atsushi Nemoto | 391b1fe | 2006-09-30 23:28:18 -0700 | [diff] [blame] | 328 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 329 | if (ret) |
| 330 | dev_err(&pdev->dev, "unable to create sysfs file: %s\n", |
| 331 | ds1553_nvram_attr.attr.name); |
Jingoo Han | c1be915 | 2013-04-29 16:19:00 -0700 | [diff] [blame] | 332 | |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 333 | return 0; |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 336 | static int ds1553_rtc_remove(struct platform_device *pdev) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 337 | { |
| 338 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 339 | |
| 340 | sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); |
Atsushi Nemoto | 618161f | 2009-12-15 16:46:01 -0800 | [diff] [blame] | 341 | if (pdata->irq > 0) |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 342 | writeb(0, pdata->ioaddr + RTC_INTERRUPTS); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 346 | /* work with hotplug and coldplug */ |
| 347 | MODULE_ALIAS("platform:rtc-ds1553"); |
| 348 | |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 349 | static struct platform_driver ds1553_rtc_driver = { |
| 350 | .probe = ds1553_rtc_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 351 | .remove = ds1553_rtc_remove, |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 352 | .driver = { |
Atsushi Nemoto | e7634c2 | 2007-10-16 01:28:17 -0700 | [diff] [blame] | 353 | .name = "rtc-ds1553", |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 354 | }, |
| 355 | }; |
| 356 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 357 | module_platform_driver(ds1553_rtc_driver); |
Atsushi Nemoto | 9bf5b4f | 2006-06-25 05:48:28 -0700 | [diff] [blame] | 358 | |
| 359 | MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); |
| 360 | MODULE_DESCRIPTION("Dallas DS1553 RTC driver"); |
| 361 | MODULE_LICENSE("GPL"); |
| 362 | MODULE_VERSION(DRV_VERSION); |