Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ST M48T59 RTC driver |
| 3 | * |
| 4 | * Copyright (c) 2007 Wind River Systems, Inc. |
| 5 | * |
| 6 | * Author: Mark Zhan <rongkai.zhan@windriver.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/rtc/m48t59.h> |
| 21 | #include <linux/bcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 23 | |
| 24 | #ifndef NO_IRQ |
| 25 | #define NO_IRQ (-1) |
| 26 | #endif |
| 27 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 28 | #define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg)) |
| 29 | #define M48T59_WRITE(val, reg) \ |
| 30 | (pdata->write_byte(dev, pdata->offset + reg, val)) |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 31 | |
| 32 | #define M48T59_SET_BITS(mask, reg) \ |
| 33 | M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg)) |
| 34 | #define M48T59_CLEAR_BITS(mask, reg) \ |
| 35 | M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg)) |
| 36 | |
| 37 | struct m48t59_private { |
| 38 | void __iomem *ioaddr; |
Mark Zhan | 0439208 | 2007-07-31 00:39:47 -0700 | [diff] [blame] | 39 | int irq; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 40 | struct rtc_device *rtc; |
| 41 | spinlock_t lock; /* serialize the NVRAM and RTC access */ |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * This is the generic access method when the chip is memory-mapped |
| 46 | */ |
| 47 | static void |
| 48 | m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val) |
| 49 | { |
| 50 | struct platform_device *pdev = to_platform_device(dev); |
| 51 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 52 | |
| 53 | writeb(val, m48t59->ioaddr+ofs); |
| 54 | } |
| 55 | |
| 56 | static u8 |
| 57 | m48t59_mem_readb(struct device *dev, u32 ofs) |
| 58 | { |
| 59 | struct platform_device *pdev = to_platform_device(dev); |
| 60 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 61 | |
| 62 | return readb(m48t59->ioaddr+ofs); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * NOTE: M48T59 only uses BCD mode |
| 67 | */ |
| 68 | static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 69 | { |
| 70 | struct platform_device *pdev = to_platform_device(dev); |
| 71 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 72 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 73 | unsigned long flags; |
| 74 | u8 val; |
| 75 | |
| 76 | spin_lock_irqsave(&m48t59->lock, flags); |
| 77 | /* Issue the READ command */ |
| 78 | M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL); |
| 79 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 80 | tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR)); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 81 | /* tm_mon is 0-11 */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 82 | tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1; |
| 83 | tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY)); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 84 | |
| 85 | val = M48T59_READ(M48T59_WDAY); |
Robert Reif | 833be4e | 2008-09-09 19:38:17 -0700 | [diff] [blame] | 86 | if ((pdata->type == M48T59RTC_TYPE_M48T59) && |
| 87 | (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) { |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 88 | dev_dbg(dev, "Century bit is enabled\n"); |
| 89 | tm->tm_year += 100; /* one century */ |
| 90 | } |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_SPARC |
| 92 | /* Sun SPARC machines count years since 1968 */ |
| 93 | tm->tm_year += 68; |
| 94 | #endif |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 95 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 96 | tm->tm_wday = bcd2bin(val & 0x07); |
| 97 | tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F); |
| 98 | tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F); |
| 99 | tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 100 | |
| 101 | /* Clear the READ bit */ |
| 102 | M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL); |
| 103 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 104 | |
| 105 | dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n", |
| 106 | tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, |
| 107 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 112 | { |
| 113 | struct platform_device *pdev = to_platform_device(dev); |
| 114 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 115 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 116 | unsigned long flags; |
| 117 | u8 val = 0; |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 118 | int year = tm->tm_year; |
| 119 | |
| 120 | #ifdef CONFIG_SPARC |
| 121 | /* Sun SPARC machines count years since 1968 */ |
| 122 | year -= 68; |
| 123 | #endif |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 124 | |
| 125 | dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n", |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 126 | year + 1900, tm->tm_mon, tm->tm_mday, |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 127 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 128 | |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 129 | if (year < 0) |
| 130 | return -EINVAL; |
| 131 | |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 132 | spin_lock_irqsave(&m48t59->lock, flags); |
| 133 | /* Issue the WRITE command */ |
| 134 | M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL); |
| 135 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 136 | M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC); |
| 137 | M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN); |
| 138 | M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR); |
| 139 | M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 140 | /* tm_mon is 0-11 */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 141 | M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH); |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 142 | M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 143 | |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 144 | if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100)) |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 145 | val = (M48T59_WDAY_CEB | M48T59_WDAY_CB); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 146 | val |= (bin2bcd(tm->tm_wday) & 0x07); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 147 | M48T59_WRITE(val, M48T59_WDAY); |
| 148 | |
| 149 | /* Clear the WRITE bit */ |
| 150 | M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL); |
| 151 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Read alarm time and date in RTC |
| 157 | */ |
| 158 | static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 159 | { |
| 160 | struct platform_device *pdev = to_platform_device(dev); |
| 161 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 162 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 163 | struct rtc_time *tm = &alrm->time; |
| 164 | unsigned long flags; |
| 165 | u8 val; |
| 166 | |
| 167 | /* If no irq, we don't support ALARM */ |
| 168 | if (m48t59->irq == NO_IRQ) |
| 169 | return -EIO; |
| 170 | |
| 171 | spin_lock_irqsave(&m48t59->lock, flags); |
| 172 | /* Issue the READ command */ |
| 173 | M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL); |
| 174 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 175 | tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR)); |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_SPARC |
| 177 | /* Sun SPARC machines count years since 1968 */ |
| 178 | tm->tm_year += 68; |
| 179 | #endif |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 180 | /* tm_mon is 0-11 */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 181 | tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 182 | |
| 183 | val = M48T59_READ(M48T59_WDAY); |
| 184 | if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) |
| 185 | tm->tm_year += 100; /* one century */ |
| 186 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 187 | tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE)); |
| 188 | tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR)); |
| 189 | tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN)); |
| 190 | tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC)); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 191 | |
| 192 | /* Clear the READ bit */ |
| 193 | M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL); |
| 194 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 195 | |
| 196 | dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n", |
| 197 | tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, |
| 198 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Set alarm time and date in RTC |
| 204 | */ |
| 205 | static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 206 | { |
| 207 | struct platform_device *pdev = to_platform_device(dev); |
| 208 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 209 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 210 | struct rtc_time *tm = &alrm->time; |
| 211 | u8 mday, hour, min, sec; |
| 212 | unsigned long flags; |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 213 | int year = tm->tm_year; |
| 214 | |
| 215 | #ifdef CONFIG_SPARC |
| 216 | /* Sun SPARC machines count years since 1968 */ |
| 217 | year -= 68; |
| 218 | #endif |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 219 | |
| 220 | /* If no irq, we don't support ALARM */ |
| 221 | if (m48t59->irq == NO_IRQ) |
| 222 | return -EIO; |
| 223 | |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 224 | if (year < 0) |
| 225 | return -EINVAL; |
| 226 | |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 227 | /* |
| 228 | * 0xff means "always match" |
| 229 | */ |
| 230 | mday = tm->tm_mday; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 231 | mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 232 | if (mday == 0xff) |
| 233 | mday = M48T59_READ(M48T59_MDAY); |
| 234 | |
| 235 | hour = tm->tm_hour; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 236 | hour = (hour < 24) ? bin2bcd(hour) : 0x00; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 237 | |
| 238 | min = tm->tm_min; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 239 | min = (min < 60) ? bin2bcd(min) : 0x00; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 240 | |
| 241 | sec = tm->tm_sec; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 242 | sec = (sec < 60) ? bin2bcd(sec) : 0x00; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 243 | |
| 244 | spin_lock_irqsave(&m48t59->lock, flags); |
| 245 | /* Issue the WRITE command */ |
| 246 | M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL); |
| 247 | |
| 248 | M48T59_WRITE(mday, M48T59_ALARM_DATE); |
| 249 | M48T59_WRITE(hour, M48T59_ALARM_HOUR); |
| 250 | M48T59_WRITE(min, M48T59_ALARM_MIN); |
| 251 | M48T59_WRITE(sec, M48T59_ALARM_SEC); |
| 252 | |
| 253 | /* Clear the WRITE bit */ |
| 254 | M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL); |
| 255 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 256 | |
| 257 | dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n", |
Krzysztof Helt | 12a9ee3 | 2008-10-29 15:35:24 -0700 | [diff] [blame] | 258 | year + 1900, tm->tm_mon, tm->tm_mday, |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 259 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Handle commands from user-space |
| 265 | */ |
| 266 | static int m48t59_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 267 | unsigned long arg) |
| 268 | { |
| 269 | struct platform_device *pdev = to_platform_device(dev); |
| 270 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 271 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 272 | unsigned long flags; |
| 273 | int ret = 0; |
| 274 | |
| 275 | spin_lock_irqsave(&m48t59->lock, flags); |
| 276 | switch (cmd) { |
| 277 | case RTC_AIE_OFF: /* alarm interrupt off */ |
| 278 | M48T59_WRITE(0x00, M48T59_INTR); |
| 279 | break; |
| 280 | case RTC_AIE_ON: /* alarm interrupt on */ |
| 281 | M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR); |
| 282 | break; |
| 283 | default: |
| 284 | ret = -ENOIOCTLCMD; |
| 285 | break; |
| 286 | } |
| 287 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq) |
| 293 | { |
| 294 | struct platform_device *pdev = to_platform_device(dev); |
| 295 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 296 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 297 | unsigned long flags; |
| 298 | u8 val; |
| 299 | |
| 300 | spin_lock_irqsave(&m48t59->lock, flags); |
| 301 | val = M48T59_READ(M48T59_FLAGS); |
| 302 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 303 | |
| 304 | seq_printf(seq, "battery\t\t: %s\n", |
| 305 | (val & M48T59_FLAGS_BF) ? "low" : "normal"); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * IRQ handler for the RTC |
| 311 | */ |
| 312 | static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id) |
| 313 | { |
| 314 | struct device *dev = (struct device *)dev_id; |
| 315 | struct platform_device *pdev = to_platform_device(dev); |
| 316 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 317 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 318 | u8 event; |
| 319 | |
| 320 | spin_lock(&m48t59->lock); |
| 321 | event = M48T59_READ(M48T59_FLAGS); |
| 322 | spin_unlock(&m48t59->lock); |
| 323 | |
| 324 | if (event & M48T59_FLAGS_AF) { |
| 325 | rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF)); |
| 326 | return IRQ_HANDLED; |
| 327 | } |
| 328 | |
| 329 | return IRQ_NONE; |
| 330 | } |
| 331 | |
| 332 | static const struct rtc_class_ops m48t59_rtc_ops = { |
| 333 | .ioctl = m48t59_rtc_ioctl, |
| 334 | .read_time = m48t59_rtc_read_time, |
| 335 | .set_time = m48t59_rtc_set_time, |
| 336 | .read_alarm = m48t59_rtc_readalarm, |
| 337 | .set_alarm = m48t59_rtc_setalarm, |
| 338 | .proc = m48t59_rtc_proc, |
| 339 | }; |
| 340 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 341 | static const struct rtc_class_ops m48t02_rtc_ops = { |
| 342 | .read_time = m48t59_rtc_read_time, |
| 343 | .set_time = m48t59_rtc_set_time, |
| 344 | }; |
| 345 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 346 | static ssize_t m48t59_nvram_read(struct file *filp, struct kobject *kobj, |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 347 | struct bin_attribute *bin_attr, |
| 348 | char *buf, loff_t pos, size_t size) |
| 349 | { |
| 350 | struct device *dev = container_of(kobj, struct device, kobj); |
| 351 | struct platform_device *pdev = to_platform_device(dev); |
| 352 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 353 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 354 | ssize_t cnt = 0; |
| 355 | unsigned long flags; |
| 356 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 357 | for (; size > 0 && pos < pdata->offset; cnt++, size--) { |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 358 | spin_lock_irqsave(&m48t59->lock, flags); |
| 359 | *buf++ = M48T59_READ(cnt); |
| 360 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 361 | } |
| 362 | |
| 363 | return cnt; |
| 364 | } |
| 365 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 366 | static ssize_t m48t59_nvram_write(struct file *filp, struct kobject *kobj, |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 367 | struct bin_attribute *bin_attr, |
| 368 | char *buf, loff_t pos, size_t size) |
| 369 | { |
| 370 | struct device *dev = container_of(kobj, struct device, kobj); |
| 371 | struct platform_device *pdev = to_platform_device(dev); |
| 372 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 373 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
| 374 | ssize_t cnt = 0; |
| 375 | unsigned long flags; |
| 376 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 377 | for (; size > 0 && pos < pdata->offset; cnt++, size--) { |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 378 | spin_lock_irqsave(&m48t59->lock, flags); |
| 379 | M48T59_WRITE(*buf++, cnt); |
| 380 | spin_unlock_irqrestore(&m48t59->lock, flags); |
| 381 | } |
| 382 | |
| 383 | return cnt; |
| 384 | } |
| 385 | |
| 386 | static struct bin_attribute m48t59_nvram_attr = { |
| 387 | .attr = { |
| 388 | .name = "nvram", |
David Brownell | a4b1d50 | 2007-11-14 16:58:30 -0800 | [diff] [blame] | 389 | .mode = S_IRUGO | S_IWUSR, |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 390 | }, |
| 391 | .read = m48t59_nvram_read, |
| 392 | .write = m48t59_nvram_write, |
| 393 | }; |
| 394 | |
| 395 | static int __devinit m48t59_rtc_probe(struct platform_device *pdev) |
| 396 | { |
| 397 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
| 398 | struct m48t59_private *m48t59 = NULL; |
| 399 | struct resource *res; |
| 400 | int ret = -ENOMEM; |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 401 | char *name; |
| 402 | const struct rtc_class_ops *ops; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 403 | |
| 404 | /* This chip could be memory-mapped or I/O-mapped */ |
| 405 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 406 | if (!res) { |
| 407 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 408 | if (!res) |
| 409 | return -EINVAL; |
| 410 | } |
| 411 | |
| 412 | if (res->flags & IORESOURCE_IO) { |
| 413 | /* If we are I/O-mapped, the platform should provide |
| 414 | * the operations accessing chip registers. |
| 415 | */ |
| 416 | if (!pdata || !pdata->write_byte || !pdata->read_byte) |
| 417 | return -EINVAL; |
| 418 | } else if (res->flags & IORESOURCE_MEM) { |
| 419 | /* we are memory-mapped */ |
| 420 | if (!pdata) { |
| 421 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 422 | if (!pdata) |
| 423 | return -ENOMEM; |
| 424 | /* Ensure we only kmalloc platform data once */ |
| 425 | pdev->dev.platform_data = pdata; |
| 426 | } |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 427 | if (!pdata->type) |
| 428 | pdata->type = M48T59RTC_TYPE_M48T59; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 429 | |
| 430 | /* Try to use the generic memory read/write ops */ |
| 431 | if (!pdata->write_byte) |
| 432 | pdata->write_byte = m48t59_mem_writeb; |
| 433 | if (!pdata->read_byte) |
| 434 | pdata->read_byte = m48t59_mem_readb; |
| 435 | } |
| 436 | |
| 437 | m48t59 = kzalloc(sizeof(*m48t59), GFP_KERNEL); |
| 438 | if (!m48t59) |
| 439 | return -ENOMEM; |
| 440 | |
Krzysztof Helt | 64151ad | 2008-09-03 15:41:57 -0700 | [diff] [blame] | 441 | m48t59->ioaddr = pdata->ioaddr; |
| 442 | |
| 443 | if (!m48t59->ioaddr) { |
| 444 | /* ioaddr not mapped externally */ |
| 445 | m48t59->ioaddr = ioremap(res->start, res->end - res->start + 1); |
| 446 | if (!m48t59->ioaddr) |
| 447 | goto out; |
| 448 | } |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 449 | |
| 450 | /* Try to get irq number. We also can work in |
| 451 | * the mode without IRQ. |
| 452 | */ |
| 453 | m48t59->irq = platform_get_irq(pdev, 0); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 454 | if (m48t59->irq <= 0) |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 455 | m48t59->irq = NO_IRQ; |
| 456 | |
| 457 | if (m48t59->irq != NO_IRQ) { |
| 458 | ret = request_irq(m48t59->irq, m48t59_rtc_interrupt, |
| 459 | IRQF_SHARED, "rtc-m48t59", &pdev->dev); |
| 460 | if (ret) |
| 461 | goto out; |
| 462 | } |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 463 | switch (pdata->type) { |
| 464 | case M48T59RTC_TYPE_M48T59: |
| 465 | name = "m48t59"; |
| 466 | ops = &m48t59_rtc_ops; |
| 467 | pdata->offset = 0x1ff0; |
| 468 | break; |
| 469 | case M48T59RTC_TYPE_M48T02: |
| 470 | name = "m48t02"; |
| 471 | ops = &m48t02_rtc_ops; |
| 472 | pdata->offset = 0x7f0; |
| 473 | break; |
| 474 | case M48T59RTC_TYPE_M48T08: |
| 475 | name = "m48t08"; |
| 476 | ops = &m48t02_rtc_ops; |
| 477 | pdata->offset = 0x1ff0; |
| 478 | break; |
| 479 | default: |
| 480 | dev_err(&pdev->dev, "Unknown RTC type\n"); |
| 481 | ret = -ENODEV; |
| 482 | goto out; |
| 483 | } |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 484 | |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 485 | spin_lock_init(&m48t59->lock); |
| 486 | platform_set_drvdata(pdev, m48t59); |
| 487 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 488 | m48t59->rtc = rtc_device_register(name, &pdev->dev, ops, THIS_MODULE); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 489 | if (IS_ERR(m48t59->rtc)) { |
| 490 | ret = PTR_ERR(m48t59->rtc); |
| 491 | goto out; |
| 492 | } |
| 493 | |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 494 | m48t59_nvram_attr.size = pdata->offset; |
| 495 | |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 496 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr); |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 497 | if (ret) { |
| 498 | rtc_device_unregister(m48t59->rtc); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 499 | goto out; |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 500 | } |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 501 | |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 502 | return 0; |
| 503 | |
| 504 | out: |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 505 | if (m48t59->irq != NO_IRQ) |
| 506 | free_irq(m48t59->irq, &pdev->dev); |
| 507 | if (m48t59->ioaddr) |
| 508 | iounmap(m48t59->ioaddr); |
| 509 | if (m48t59) |
| 510 | kfree(m48t59); |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static int __devexit m48t59_rtc_remove(struct platform_device *pdev) |
| 515 | { |
| 516 | struct m48t59_private *m48t59 = platform_get_drvdata(pdev); |
Krzysztof Helt | 64151ad | 2008-09-03 15:41:57 -0700 | [diff] [blame] | 517 | struct m48t59_plat_data *pdata = pdev->dev.platform_data; |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 518 | |
| 519 | sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr); |
| 520 | if (!IS_ERR(m48t59->rtc)) |
| 521 | rtc_device_unregister(m48t59->rtc); |
Krzysztof Helt | 64151ad | 2008-09-03 15:41:57 -0700 | [diff] [blame] | 522 | if (m48t59->ioaddr && !pdata->ioaddr) |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 523 | iounmap(m48t59->ioaddr); |
| 524 | if (m48t59->irq != NO_IRQ) |
| 525 | free_irq(m48t59->irq, &pdev->dev); |
| 526 | platform_set_drvdata(pdev, NULL); |
| 527 | kfree(m48t59); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 531 | /* work with hotplug and coldplug */ |
| 532 | MODULE_ALIAS("platform:rtc-m48t59"); |
| 533 | |
Randy Dunlap | 1273092 | 2007-11-05 14:51:00 -0800 | [diff] [blame] | 534 | static struct platform_driver m48t59_rtc_driver = { |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 535 | .driver = { |
| 536 | .name = "rtc-m48t59", |
| 537 | .owner = THIS_MODULE, |
| 538 | }, |
| 539 | .probe = m48t59_rtc_probe, |
| 540 | .remove = __devexit_p(m48t59_rtc_remove), |
| 541 | }; |
| 542 | |
| 543 | static int __init m48t59_rtc_init(void) |
| 544 | { |
Randy Dunlap | 1273092 | 2007-11-05 14:51:00 -0800 | [diff] [blame] | 545 | return platform_driver_register(&m48t59_rtc_driver); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static void __exit m48t59_rtc_exit(void) |
| 549 | { |
Randy Dunlap | 1273092 | 2007-11-05 14:51:00 -0800 | [diff] [blame] | 550 | platform_driver_unregister(&m48t59_rtc_driver); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | module_init(m48t59_rtc_init); |
| 554 | module_exit(m48t59_rtc_exit); |
| 555 | |
| 556 | MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>"); |
Krzysztof Helt | 94fe742 | 2008-09-03 15:12:34 -0700 | [diff] [blame] | 557 | MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver"); |
Mark Zhan | 2e774c7 | 2007-07-17 04:05:05 -0700 | [diff] [blame] | 558 | MODULE_LICENSE("GPL"); |