Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH On-Chip RTC Support |
| 3 | * |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 4 | * Copyright (C) 2006, 2007, 2008 Paul Mundt |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 5 | * Copyright (C) 2006 Jamie Lenehan |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 6 | * Copyright (C) 2008 Angelo Castello |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 7 | * |
| 8 | * Based on the old arch/sh/kernel/cpu/rtc.c by: |
| 9 | * |
| 10 | * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org> |
| 11 | * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka |
| 12 | * |
| 13 | * This file is subject to the terms and conditions of the GNU General Public |
| 14 | * License. See the file "COPYING" in the main directory of this archive |
| 15 | * for more details. |
| 16 | */ |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/bcd.h> |
| 20 | #include <linux/rtc.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/spinlock.h> |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 26 | #include <linux/io.h> |
Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 27 | #include <linux/log2.h> |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 28 | #include <asm/rtc.h> |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 29 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 30 | #define DRV_NAME "sh-rtc" |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 31 | #define DRV_VERSION "0.2.0" |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 32 | |
| 33 | #define RTC_REG(r) ((r) * rtc_reg_size) |
| 34 | |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 35 | #define R64CNT RTC_REG(0) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 36 | |
| 37 | #define RSECCNT RTC_REG(1) /* RTC sec */ |
| 38 | #define RMINCNT RTC_REG(2) /* RTC min */ |
| 39 | #define RHRCNT RTC_REG(3) /* RTC hour */ |
| 40 | #define RWKCNT RTC_REG(4) /* RTC week */ |
| 41 | #define RDAYCNT RTC_REG(5) /* RTC day */ |
| 42 | #define RMONCNT RTC_REG(6) /* RTC month */ |
| 43 | #define RYRCNT RTC_REG(7) /* RTC year */ |
| 44 | #define RSECAR RTC_REG(8) /* ALARM sec */ |
| 45 | #define RMINAR RTC_REG(9) /* ALARM min */ |
| 46 | #define RHRAR RTC_REG(10) /* ALARM hour */ |
| 47 | #define RWKAR RTC_REG(11) /* ALARM week */ |
| 48 | #define RDAYAR RTC_REG(12) /* ALARM day */ |
| 49 | #define RMONAR RTC_REG(13) /* ALARM month */ |
| 50 | #define RCR1 RTC_REG(14) /* Control */ |
| 51 | #define RCR2 RTC_REG(15) /* Control */ |
| 52 | |
Paul Mundt | ff1b750 | 2007-11-26 17:56:31 +0900 | [diff] [blame] | 53 | /* |
| 54 | * Note on RYRAR and RCR3: Up until this point most of the register |
| 55 | * definitions are consistent across all of the available parts. However, |
| 56 | * the placement of the optional RYRAR and RCR3 (the RYRAR control |
| 57 | * register used to control RYRCNT/RYRAR compare) varies considerably |
| 58 | * across various parts, occasionally being mapped in to a completely |
| 59 | * unrelated address space. For proper RYRAR support a separate resource |
| 60 | * would have to be handed off, but as this is purely optional in |
| 61 | * practice, we simply opt not to support it, thereby keeping the code |
| 62 | * quite a bit more simplified. |
| 63 | */ |
| 64 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 65 | /* ALARM Bits - or with BCD encoded value */ |
| 66 | #define AR_ENB 0x80 /* Enable for alarm cmp */ |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 67 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 68 | /* Period Bits */ |
| 69 | #define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */ |
| 70 | #define PF_COUNT 0x200 /* Half periodic counter */ |
| 71 | #define PF_OXS 0x400 /* Periodic One x Second */ |
| 72 | #define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */ |
| 73 | #define PF_MASK 0xf00 |
| 74 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 75 | /* RCR1 Bits */ |
| 76 | #define RCR1_CF 0x80 /* Carry Flag */ |
| 77 | #define RCR1_CIE 0x10 /* Carry Interrupt Enable */ |
| 78 | #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */ |
| 79 | #define RCR1_AF 0x01 /* Alarm Flag */ |
| 80 | |
| 81 | /* RCR2 Bits */ |
| 82 | #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */ |
| 83 | #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */ |
| 84 | #define RCR2_RTCEN 0x08 /* ENable RTC */ |
| 85 | #define RCR2_ADJ 0x04 /* ADJustment (30-second) */ |
| 86 | #define RCR2_RESET 0x02 /* Reset bit */ |
| 87 | #define RCR2_START 0x01 /* Start bit */ |
| 88 | |
| 89 | struct sh_rtc { |
| 90 | void __iomem *regbase; |
| 91 | unsigned long regsize; |
| 92 | struct resource *res; |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 93 | int alarm_irq; |
| 94 | int periodic_irq; |
| 95 | int carry_irq; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 96 | struct rtc_device *rtc_dev; |
| 97 | spinlock_t lock; |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 98 | unsigned long capabilities; /* See asm-sh/rtc.h for cap bits */ |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 99 | unsigned short periodic_freq; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 100 | }; |
| 101 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 102 | static int __sh_rtc_interrupt(struct sh_rtc *rtc) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 103 | { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 104 | unsigned int tmp, pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 105 | |
| 106 | tmp = readb(rtc->regbase + RCR1); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 107 | pending = tmp & RCR1_CF; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 108 | tmp &= ~RCR1_CF; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 109 | writeb(tmp, rtc->regbase + RCR1); |
| 110 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 111 | /* Users have requested One x Second IRQ */ |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 112 | if (pending && rtc->periodic_freq & PF_OXS) |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 113 | rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 114 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 115 | return pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 118 | static int __sh_rtc_alarm(struct sh_rtc *rtc) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 119 | { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 120 | unsigned int tmp, pending; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 121 | |
| 122 | tmp = readb(rtc->regbase + RCR1); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 123 | pending = tmp & RCR1_AF; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 124 | tmp &= ~(RCR1_AF | RCR1_AIE); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 125 | writeb(tmp, rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 126 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 127 | if (pending) |
| 128 | rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 129 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 130 | return pending; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 133 | static int __sh_rtc_periodic(struct sh_rtc *rtc) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 134 | { |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 135 | struct rtc_device *rtc_dev = rtc->rtc_dev; |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 136 | struct rtc_task *irq_task; |
| 137 | unsigned int tmp, pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 138 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 139 | tmp = readb(rtc->regbase + RCR2); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 140 | pending = tmp & RCR2_PEF; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 141 | tmp &= ~RCR2_PEF; |
| 142 | writeb(tmp, rtc->regbase + RCR2); |
| 143 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 144 | if (!pending) |
| 145 | return 0; |
| 146 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 147 | /* Half period enabled than one skipped and the next notified */ |
| 148 | if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT)) |
| 149 | rtc->periodic_freq &= ~PF_COUNT; |
| 150 | else { |
| 151 | if (rtc->periodic_freq & PF_HP) |
| 152 | rtc->periodic_freq |= PF_COUNT; |
| 153 | if (rtc->periodic_freq & PF_KOU) { |
| 154 | spin_lock(&rtc_dev->irq_task_lock); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 155 | irq_task = rtc_dev->irq_task; |
| 156 | if (irq_task) |
| 157 | irq_task->func(irq_task->private_data); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 158 | spin_unlock(&rtc_dev->irq_task_lock); |
| 159 | } else |
| 160 | rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF); |
| 161 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 162 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 163 | return pending; |
| 164 | } |
| 165 | |
| 166 | static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id) |
| 167 | { |
| 168 | struct sh_rtc *rtc = dev_id; |
| 169 | int ret; |
| 170 | |
| 171 | spin_lock(&rtc->lock); |
| 172 | ret = __sh_rtc_interrupt(rtc); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 173 | spin_unlock(&rtc->lock); |
| 174 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 175 | return IRQ_RETVAL(ret); |
| 176 | } |
| 177 | |
| 178 | static irqreturn_t sh_rtc_alarm(int irq, void *dev_id) |
| 179 | { |
| 180 | struct sh_rtc *rtc = dev_id; |
| 181 | int ret; |
| 182 | |
| 183 | spin_lock(&rtc->lock); |
| 184 | ret = __sh_rtc_alarm(rtc); |
| 185 | spin_unlock(&rtc->lock); |
| 186 | |
| 187 | return IRQ_RETVAL(ret); |
| 188 | } |
| 189 | |
| 190 | static irqreturn_t sh_rtc_periodic(int irq, void *dev_id) |
| 191 | { |
| 192 | struct sh_rtc *rtc = dev_id; |
| 193 | int ret; |
| 194 | |
| 195 | spin_lock(&rtc->lock); |
| 196 | ret = __sh_rtc_periodic(rtc); |
| 197 | spin_unlock(&rtc->lock); |
| 198 | |
| 199 | return IRQ_RETVAL(ret); |
| 200 | } |
| 201 | |
| 202 | static irqreturn_t sh_rtc_shared(int irq, void *dev_id) |
| 203 | { |
| 204 | struct sh_rtc *rtc = dev_id; |
| 205 | int ret; |
| 206 | |
| 207 | spin_lock(&rtc->lock); |
| 208 | ret = __sh_rtc_interrupt(rtc); |
| 209 | ret |= __sh_rtc_alarm(rtc); |
| 210 | ret |= __sh_rtc_periodic(rtc); |
| 211 | spin_unlock(&rtc->lock); |
| 212 | |
| 213 | return IRQ_RETVAL(ret); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static inline void sh_rtc_setpie(struct device *dev, unsigned int enable) |
| 217 | { |
| 218 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 219 | unsigned int tmp; |
| 220 | |
| 221 | spin_lock_irq(&rtc->lock); |
| 222 | |
| 223 | tmp = readb(rtc->regbase + RCR2); |
| 224 | |
| 225 | if (enable) { |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 226 | tmp &= ~RCR2_PEF; /* Clear PES bit */ |
| 227 | tmp |= (rtc->periodic_freq & ~PF_HP); /* Set PES2-0 */ |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 228 | } else |
| 229 | tmp &= ~(RCR2_PESMASK | RCR2_PEF); |
| 230 | |
| 231 | writeb(tmp, rtc->regbase + RCR2); |
| 232 | |
| 233 | spin_unlock_irq(&rtc->lock); |
| 234 | } |
| 235 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 236 | static inline int sh_rtc_setfreq(struct device *dev, unsigned int freq) |
| 237 | { |
| 238 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 239 | int tmp, ret = 0; |
| 240 | |
| 241 | spin_lock_irq(&rtc->lock); |
| 242 | tmp = rtc->periodic_freq & PF_MASK; |
| 243 | |
| 244 | switch (freq) { |
| 245 | case 0: |
| 246 | rtc->periodic_freq = 0x00; |
| 247 | break; |
| 248 | case 1: |
| 249 | rtc->periodic_freq = 0x60; |
| 250 | break; |
| 251 | case 2: |
| 252 | rtc->periodic_freq = 0x50; |
| 253 | break; |
| 254 | case 4: |
| 255 | rtc->periodic_freq = 0x40; |
| 256 | break; |
| 257 | case 8: |
| 258 | rtc->periodic_freq = 0x30 | PF_HP; |
| 259 | break; |
| 260 | case 16: |
| 261 | rtc->periodic_freq = 0x30; |
| 262 | break; |
| 263 | case 32: |
| 264 | rtc->periodic_freq = 0x20 | PF_HP; |
| 265 | break; |
| 266 | case 64: |
| 267 | rtc->periodic_freq = 0x20; |
| 268 | break; |
| 269 | case 128: |
| 270 | rtc->periodic_freq = 0x10 | PF_HP; |
| 271 | break; |
| 272 | case 256: |
| 273 | rtc->periodic_freq = 0x10; |
| 274 | break; |
| 275 | default: |
| 276 | ret = -ENOTSUPP; |
| 277 | } |
| 278 | |
| 279 | if (ret == 0) { |
| 280 | rtc->periodic_freq |= tmp; |
| 281 | rtc->rtc_dev->irq_freq = freq; |
| 282 | } |
| 283 | |
| 284 | spin_unlock_irq(&rtc->lock); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 288 | static inline void sh_rtc_setaie(struct device *dev, unsigned int enable) |
| 289 | { |
| 290 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 291 | unsigned int tmp; |
| 292 | |
| 293 | spin_lock_irq(&rtc->lock); |
| 294 | |
| 295 | tmp = readb(rtc->regbase + RCR1); |
| 296 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 297 | if (!enable) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 298 | tmp &= ~RCR1_AIE; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 299 | else |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 300 | tmp |= RCR1_AIE; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 301 | |
| 302 | writeb(tmp, rtc->regbase + RCR1); |
| 303 | |
| 304 | spin_unlock_irq(&rtc->lock); |
| 305 | } |
| 306 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 307 | static int sh_rtc_proc(struct device *dev, struct seq_file *seq) |
| 308 | { |
| 309 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 310 | unsigned int tmp; |
| 311 | |
| 312 | tmp = readb(rtc->regbase + RCR1); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 313 | seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 314 | |
| 315 | tmp = readb(rtc->regbase + RCR2); |
| 316 | seq_printf(seq, "periodic_IRQ\t: %s\n", |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 317 | (tmp & RCR2_PESMASK) ? "yes" : "no"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 323 | { |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 324 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 325 | unsigned int ret = 0; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 326 | |
| 327 | switch (cmd) { |
| 328 | case RTC_PIE_OFF: |
| 329 | case RTC_PIE_ON: |
| 330 | sh_rtc_setpie(dev, cmd == RTC_PIE_ON); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 331 | break; |
| 332 | case RTC_AIE_OFF: |
| 333 | case RTC_AIE_ON: |
| 334 | sh_rtc_setaie(dev, cmd == RTC_AIE_ON); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 335 | break; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 336 | case RTC_UIE_OFF: |
| 337 | rtc->periodic_freq &= ~PF_OXS; |
| 338 | break; |
| 339 | case RTC_UIE_ON: |
| 340 | rtc->periodic_freq |= PF_OXS; |
| 341 | break; |
| 342 | case RTC_IRQP_READ: |
| 343 | ret = put_user(rtc->rtc_dev->irq_freq, |
| 344 | (unsigned long __user *)arg); |
| 345 | break; |
| 346 | case RTC_IRQP_SET: |
| 347 | ret = sh_rtc_setfreq(dev, arg); |
| 348 | break; |
| 349 | default: |
| 350 | ret = -ENOIOCTLCMD; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 357 | { |
| 358 | struct platform_device *pdev = to_platform_device(dev); |
| 359 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 360 | unsigned int sec128, sec2, yr, yr100, cf_bit; |
| 361 | |
| 362 | do { |
| 363 | unsigned int tmp; |
| 364 | |
| 365 | spin_lock_irq(&rtc->lock); |
| 366 | |
| 367 | tmp = readb(rtc->regbase + RCR1); |
| 368 | tmp &= ~RCR1_CF; /* Clear CF-bit */ |
| 369 | tmp |= RCR1_CIE; |
| 370 | writeb(tmp, rtc->regbase + RCR1); |
| 371 | |
| 372 | sec128 = readb(rtc->regbase + R64CNT); |
| 373 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 374 | tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT)); |
| 375 | tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT)); |
| 376 | tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT)); |
| 377 | tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT)); |
| 378 | tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT)); |
| 379 | tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 380 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 381 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
| 382 | yr = readw(rtc->regbase + RYRCNT); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 383 | yr100 = bcd2bin(yr >> 8); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 384 | yr &= 0xff; |
| 385 | } else { |
| 386 | yr = readb(rtc->regbase + RYRCNT); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 387 | yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 388 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 389 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 390 | tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 391 | |
| 392 | sec2 = readb(rtc->regbase + R64CNT); |
| 393 | cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF; |
| 394 | |
| 395 | spin_unlock_irq(&rtc->lock); |
| 396 | } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0); |
| 397 | |
| 398 | #if RTC_BIT_INVERTED != 0 |
| 399 | if ((sec128 & RTC_BIT_INVERTED)) |
| 400 | tm->tm_sec--; |
| 401 | #endif |
| 402 | |
Paul Mundt | 435c55d | 2007-05-08 11:56:27 +0900 | [diff] [blame] | 403 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 404 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
Harvey Harrison | 2a4e2b8 | 2008-04-28 02:12:00 -0700 | [diff] [blame] | 405 | __func__, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 406 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 407 | tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 408 | |
Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 409 | if (rtc_valid_tm(tm) < 0) { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 410 | dev_err(dev, "invalid date\n"); |
Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 411 | rtc_time_to_tm(0, tm); |
| 412 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 418 | { |
| 419 | struct platform_device *pdev = to_platform_device(dev); |
| 420 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 421 | unsigned int tmp; |
| 422 | int year; |
| 423 | |
| 424 | spin_lock_irq(&rtc->lock); |
| 425 | |
| 426 | /* Reset pre-scaler & stop RTC */ |
| 427 | tmp = readb(rtc->regbase + RCR2); |
| 428 | tmp |= RCR2_RESET; |
Markus Brunner | 699bc66 | 2007-07-26 17:31:28 +0900 | [diff] [blame] | 429 | tmp &= ~RCR2_START; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 430 | writeb(tmp, rtc->regbase + RCR2); |
| 431 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 432 | writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT); |
| 433 | writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT); |
| 434 | writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT); |
| 435 | writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT); |
| 436 | writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT); |
| 437 | writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 438 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 439 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 440 | year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) | |
| 441 | bin2bcd(tm->tm_year % 100); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 442 | writew(year, rtc->regbase + RYRCNT); |
| 443 | } else { |
| 444 | year = tm->tm_year % 100; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 445 | writeb(bin2bcd(year), rtc->regbase + RYRCNT); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 446 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 447 | |
| 448 | /* Start RTC */ |
| 449 | tmp = readb(rtc->regbase + RCR2); |
| 450 | tmp &= ~RCR2_RESET; |
| 451 | tmp |= RCR2_RTCEN | RCR2_START; |
| 452 | writeb(tmp, rtc->regbase + RCR2); |
| 453 | |
| 454 | spin_unlock_irq(&rtc->lock); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 459 | static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off) |
| 460 | { |
| 461 | unsigned int byte; |
| 462 | int value = 0xff; /* return 0xff for ignored values */ |
| 463 | |
| 464 | byte = readb(rtc->regbase + reg_off); |
| 465 | if (byte & AR_ENB) { |
| 466 | byte &= ~AR_ENB; /* strip the enable bit */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 467 | value = bcd2bin(byte); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return value; |
| 471 | } |
| 472 | |
| 473 | static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 474 | { |
| 475 | struct platform_device *pdev = to_platform_device(dev); |
| 476 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 477 | struct rtc_time *tm = &wkalrm->time; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 478 | |
| 479 | spin_lock_irq(&rtc->lock); |
| 480 | |
| 481 | tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR); |
| 482 | tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR); |
| 483 | tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR); |
| 484 | tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR); |
| 485 | tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR); |
| 486 | tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR); |
| 487 | if (tm->tm_mon > 0) |
| 488 | tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ |
| 489 | tm->tm_year = 0xffff; |
| 490 | |
David Brownell | 0d103e9 | 2007-01-10 23:15:32 -0800 | [diff] [blame] | 491 | wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0; |
| 492 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 493 | spin_unlock_irq(&rtc->lock); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc, |
| 499 | int value, int reg_off) |
| 500 | { |
| 501 | /* < 0 for a value that is ignored */ |
| 502 | if (value < 0) |
| 503 | writeb(0, rtc->regbase + reg_off); |
| 504 | else |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 505 | writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 506 | } |
| 507 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 508 | static int sh_rtc_check_alarm(struct rtc_time *tm) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 509 | { |
| 510 | /* |
| 511 | * The original rtc says anything > 0xc0 is "don't care" or "match |
| 512 | * all" - most users use 0xff but rtc-dev uses -1 for the same thing. |
| 513 | * The original rtc doesn't support years - some things use -1 and |
| 514 | * some 0xffff. We use -1 to make out tests easier. |
| 515 | */ |
| 516 | if (tm->tm_year == 0xffff) |
| 517 | tm->tm_year = -1; |
| 518 | if (tm->tm_mon >= 0xff) |
| 519 | tm->tm_mon = -1; |
| 520 | if (tm->tm_mday >= 0xff) |
| 521 | tm->tm_mday = -1; |
| 522 | if (tm->tm_wday >= 0xff) |
| 523 | tm->tm_wday = -1; |
| 524 | if (tm->tm_hour >= 0xff) |
| 525 | tm->tm_hour = -1; |
| 526 | if (tm->tm_min >= 0xff) |
| 527 | tm->tm_min = -1; |
| 528 | if (tm->tm_sec >= 0xff) |
| 529 | tm->tm_sec = -1; |
| 530 | |
| 531 | if (tm->tm_year > 9999 || |
| 532 | tm->tm_mon >= 12 || |
| 533 | tm->tm_mday == 0 || tm->tm_mday >= 32 || |
| 534 | tm->tm_wday >= 7 || |
| 535 | tm->tm_hour >= 24 || |
| 536 | tm->tm_min >= 60 || |
| 537 | tm->tm_sec >= 60) |
| 538 | return -EINVAL; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 544 | { |
| 545 | struct platform_device *pdev = to_platform_device(dev); |
| 546 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 547 | unsigned int rcr1; |
| 548 | struct rtc_time *tm = &wkalrm->time; |
| 549 | int mon, err; |
| 550 | |
| 551 | err = sh_rtc_check_alarm(tm); |
| 552 | if (unlikely(err < 0)) |
| 553 | return err; |
| 554 | |
| 555 | spin_lock_irq(&rtc->lock); |
| 556 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 557 | /* disable alarm interrupt and clear the alarm flag */ |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 558 | rcr1 = readb(rtc->regbase + RCR1); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 559 | rcr1 &= ~(RCR1_AF | RCR1_AIE); |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 560 | writeb(rcr1, rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 561 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 562 | /* set alarm time */ |
| 563 | sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR); |
| 564 | sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR); |
| 565 | sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR); |
| 566 | sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR); |
| 567 | sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR); |
| 568 | mon = tm->tm_mon; |
| 569 | if (mon >= 0) |
| 570 | mon += 1; |
| 571 | sh_rtc_write_alarm_value(rtc, mon, RMONAR); |
| 572 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 573 | if (wkalrm->enabled) { |
| 574 | rcr1 |= RCR1_AIE; |
| 575 | writeb(rcr1, rtc->regbase + RCR1); |
| 576 | } |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 577 | |
| 578 | spin_unlock_irq(&rtc->lock); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 583 | static int sh_rtc_irq_set_state(struct device *dev, int enabled) |
| 584 | { |
| 585 | struct platform_device *pdev = to_platform_device(dev); |
| 586 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 587 | |
| 588 | if (enabled) { |
| 589 | rtc->periodic_freq |= PF_KOU; |
| 590 | return sh_rtc_ioctl(dev, RTC_PIE_ON, 0); |
| 591 | } else { |
| 592 | rtc->periodic_freq &= ~PF_KOU; |
| 593 | return sh_rtc_ioctl(dev, RTC_PIE_OFF, 0); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | static int sh_rtc_irq_set_freq(struct device *dev, int freq) |
| 598 | { |
Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 599 | if (!is_power_of_2(freq)) |
| 600 | return -EINVAL; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 601 | return sh_rtc_ioctl(dev, RTC_IRQP_SET, freq); |
| 602 | } |
| 603 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 604 | static struct rtc_class_ops sh_rtc_ops = { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 605 | .ioctl = sh_rtc_ioctl, |
| 606 | .read_time = sh_rtc_read_time, |
| 607 | .set_time = sh_rtc_set_time, |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 608 | .read_alarm = sh_rtc_read_alarm, |
| 609 | .set_alarm = sh_rtc_set_alarm, |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 610 | .irq_set_state = sh_rtc_irq_set_state, |
| 611 | .irq_set_freq = sh_rtc_irq_set_freq, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 612 | .proc = sh_rtc_proc, |
| 613 | }; |
| 614 | |
| 615 | static int __devinit sh_rtc_probe(struct platform_device *pdev) |
| 616 | { |
| 617 | struct sh_rtc *rtc; |
| 618 | struct resource *res; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 619 | unsigned int tmp; |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 620 | int ret; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 621 | |
| 622 | rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL); |
| 623 | if (unlikely(!rtc)) |
| 624 | return -ENOMEM; |
| 625 | |
| 626 | spin_lock_init(&rtc->lock); |
| 627 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 628 | /* get periodic/carry/alarm irqs */ |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 629 | ret = platform_get_irq(pdev, 0); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 630 | if (unlikely(ret <= 0)) { |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 631 | ret = -ENOENT; |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 632 | dev_err(&pdev->dev, "No IRQ resource\n"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 633 | goto err_badres; |
| 634 | } |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 635 | rtc->periodic_irq = ret; |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 636 | rtc->carry_irq = platform_get_irq(pdev, 1); |
| 637 | rtc->alarm_irq = platform_get_irq(pdev, 2); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 638 | |
| 639 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 640 | if (unlikely(res == NULL)) { |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 641 | ret = -ENOENT; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 642 | dev_err(&pdev->dev, "No IO resource\n"); |
| 643 | goto err_badres; |
| 644 | } |
| 645 | |
| 646 | rtc->regsize = res->end - res->start + 1; |
| 647 | |
| 648 | rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name); |
| 649 | if (unlikely(!rtc->res)) { |
| 650 | ret = -EBUSY; |
| 651 | goto err_badres; |
| 652 | } |
| 653 | |
Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 654 | rtc->regbase = ioremap_nocache(rtc->res->start, rtc->regsize); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 655 | if (unlikely(!rtc->regbase)) { |
| 656 | ret = -EINVAL; |
| 657 | goto err_badmap; |
| 658 | } |
| 659 | |
| 660 | rtc->rtc_dev = rtc_device_register("sh", &pdev->dev, |
| 661 | &sh_rtc_ops, THIS_MODULE); |
Paul Mundt | 29dd0da | 2007-11-07 14:58:09 +0900 | [diff] [blame] | 662 | if (IS_ERR(rtc->rtc_dev)) { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 663 | ret = PTR_ERR(rtc->rtc_dev); |
Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 664 | goto err_unmap; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 665 | } |
| 666 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 667 | rtc->capabilities = RTC_DEF_CAPABILITIES; |
| 668 | if (pdev->dev.platform_data) { |
| 669 | struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data; |
| 670 | |
| 671 | /* |
| 672 | * Some CPUs have special capabilities in addition to the |
| 673 | * default set. Add those in here. |
| 674 | */ |
| 675 | rtc->capabilities |= pinfo->capabilities; |
| 676 | } |
| 677 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 678 | rtc->rtc_dev->max_user_freq = 256; |
| 679 | rtc->rtc_dev->irq_freq = 1; |
| 680 | rtc->periodic_freq = 0x60; |
| 681 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 682 | platform_set_drvdata(pdev, rtc); |
| 683 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 684 | if (rtc->carry_irq <= 0) { |
| 685 | /* register shared periodic/carry/alarm irq */ |
| 686 | ret = request_irq(rtc->periodic_irq, sh_rtc_shared, |
| 687 | IRQF_DISABLED, "sh-rtc", rtc); |
| 688 | if (unlikely(ret)) { |
| 689 | dev_err(&pdev->dev, |
| 690 | "request IRQ failed with %d, IRQ %d\n", ret, |
| 691 | rtc->periodic_irq); |
| 692 | goto err_unmap; |
| 693 | } |
| 694 | } else { |
| 695 | /* register periodic/carry/alarm irqs */ |
| 696 | ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, |
| 697 | IRQF_DISABLED, "sh-rtc period", rtc); |
| 698 | if (unlikely(ret)) { |
| 699 | dev_err(&pdev->dev, |
| 700 | "request period IRQ failed with %d, IRQ %d\n", |
| 701 | ret, rtc->periodic_irq); |
| 702 | goto err_unmap; |
| 703 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 704 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 705 | ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, |
| 706 | IRQF_DISABLED, "sh-rtc carry", rtc); |
| 707 | if (unlikely(ret)) { |
| 708 | dev_err(&pdev->dev, |
| 709 | "request carry IRQ failed with %d, IRQ %d\n", |
| 710 | ret, rtc->carry_irq); |
| 711 | free_irq(rtc->periodic_irq, rtc); |
| 712 | goto err_unmap; |
| 713 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 714 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 715 | ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, |
| 716 | IRQF_DISABLED, "sh-rtc alarm", rtc); |
| 717 | if (unlikely(ret)) { |
| 718 | dev_err(&pdev->dev, |
| 719 | "request alarm IRQ failed with %d, IRQ %d\n", |
| 720 | ret, rtc->alarm_irq); |
| 721 | free_irq(rtc->carry_irq, rtc); |
| 722 | free_irq(rtc->periodic_irq, rtc); |
| 723 | goto err_unmap; |
| 724 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | tmp = readb(rtc->regbase + RCR1); |
| 728 | tmp &= ~RCR1_CF; |
| 729 | tmp |= RCR1_CIE; |
| 730 | writeb(tmp, rtc->regbase + RCR1); |
| 731 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 732 | return 0; |
| 733 | |
Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 734 | err_unmap: |
| 735 | iounmap(rtc->regbase); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 736 | err_badmap: |
| 737 | release_resource(rtc->res); |
| 738 | err_badres: |
| 739 | kfree(rtc); |
| 740 | |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | static int __devexit sh_rtc_remove(struct platform_device *pdev) |
| 745 | { |
| 746 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 747 | |
| 748 | if (likely(rtc->rtc_dev)) |
| 749 | rtc_device_unregister(rtc->rtc_dev); |
| 750 | |
| 751 | sh_rtc_setpie(&pdev->dev, 0); |
| 752 | sh_rtc_setaie(&pdev->dev, 0); |
| 753 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 754 | free_irq(rtc->periodic_irq, rtc); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame^] | 755 | if (rtc->carry_irq > 0) { |
| 756 | free_irq(rtc->carry_irq, rtc); |
| 757 | free_irq(rtc->alarm_irq, rtc); |
| 758 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 759 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 760 | release_resource(rtc->res); |
| 761 | |
Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 762 | iounmap(rtc->regbase); |
| 763 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 764 | platform_set_drvdata(pdev, NULL); |
| 765 | |
| 766 | kfree(rtc); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | static struct platform_driver sh_rtc_platform_driver = { |
| 771 | .driver = { |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 772 | .name = DRV_NAME, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 773 | .owner = THIS_MODULE, |
| 774 | }, |
| 775 | .probe = sh_rtc_probe, |
| 776 | .remove = __devexit_p(sh_rtc_remove), |
| 777 | }; |
| 778 | |
| 779 | static int __init sh_rtc_init(void) |
| 780 | { |
| 781 | return platform_driver_register(&sh_rtc_platform_driver); |
| 782 | } |
| 783 | |
| 784 | static void __exit sh_rtc_exit(void) |
| 785 | { |
| 786 | platform_driver_unregister(&sh_rtc_platform_driver); |
| 787 | } |
| 788 | |
| 789 | module_init(sh_rtc_init); |
| 790 | module_exit(sh_rtc_exit); |
| 791 | |
| 792 | MODULE_DESCRIPTION("SuperH on-chip RTC driver"); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 793 | MODULE_VERSION(DRV_VERSION); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 794 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, " |
| 795 | "Jamie Lenehan <lenehan@twibble.org>, " |
| 796 | "Angelo Castello <angelo.castello@st.com>"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 797 | MODULE_LICENSE("GPL"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 798 | MODULE_ALIAS("platform:" DRV_NAME); |