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