Kuninori Morimoto | 4ff6f02 | 2018-11-08 06:43:22 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SuperH On-Chip RTC Support |
| 4 | * |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 5 | * Copyright (C) 2006 - 2009 Paul Mundt |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 6 | * Copyright (C) 2006 Jamie Lenehan |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 7 | * Copyright (C) 2008 Angelo Castello |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 8 | * |
| 9 | * Based on the old arch/sh/kernel/cpu/rtc.c by: |
| 10 | * |
| 11 | * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org> |
| 12 | * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 13 | */ |
| 14 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 15 | #include <linux/mod_devicetable.h> |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/bcd.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/spinlock.h> |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 24 | #include <linux/io.h> |
Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 25 | #include <linux/log2.h> |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 26 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 28 | #ifdef CONFIG_SUPERH |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 29 | #include <asm/rtc.h> |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 30 | #else |
| 31 | /* Default values for RZ/A RTC */ |
| 32 | #define rtc_reg_size sizeof(u16) |
| 33 | #define RTC_BIT_INVERTED 0 /* no chip bugs */ |
| 34 | #define RTC_CAP_4_DIGIT_YEAR (1 << 0) |
| 35 | #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR |
| 36 | #endif |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 37 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 38 | #define DRV_NAME "sh-rtc" |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 39 | |
| 40 | #define RTC_REG(r) ((r) * rtc_reg_size) |
| 41 | |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 42 | #define R64CNT RTC_REG(0) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 43 | |
| 44 | #define RSECCNT RTC_REG(1) /* RTC sec */ |
| 45 | #define RMINCNT RTC_REG(2) /* RTC min */ |
| 46 | #define RHRCNT RTC_REG(3) /* RTC hour */ |
| 47 | #define RWKCNT RTC_REG(4) /* RTC week */ |
| 48 | #define RDAYCNT RTC_REG(5) /* RTC day */ |
| 49 | #define RMONCNT RTC_REG(6) /* RTC month */ |
| 50 | #define RYRCNT RTC_REG(7) /* RTC year */ |
| 51 | #define RSECAR RTC_REG(8) /* ALARM sec */ |
| 52 | #define RMINAR RTC_REG(9) /* ALARM min */ |
| 53 | #define RHRAR RTC_REG(10) /* ALARM hour */ |
| 54 | #define RWKAR RTC_REG(11) /* ALARM week */ |
| 55 | #define RDAYAR RTC_REG(12) /* ALARM day */ |
| 56 | #define RMONAR RTC_REG(13) /* ALARM month */ |
| 57 | #define RCR1 RTC_REG(14) /* Control */ |
| 58 | #define RCR2 RTC_REG(15) /* Control */ |
| 59 | |
Paul Mundt | ff1b750 | 2007-11-26 17:56:31 +0900 | [diff] [blame] | 60 | /* |
| 61 | * Note on RYRAR and RCR3: Up until this point most of the register |
| 62 | * definitions are consistent across all of the available parts. However, |
| 63 | * the placement of the optional RYRAR and RCR3 (the RYRAR control |
| 64 | * register used to control RYRCNT/RYRAR compare) varies considerably |
| 65 | * across various parts, occasionally being mapped in to a completely |
| 66 | * unrelated address space. For proper RYRAR support a separate resource |
| 67 | * would have to be handed off, but as this is purely optional in |
| 68 | * practice, we simply opt not to support it, thereby keeping the code |
| 69 | * quite a bit more simplified. |
| 70 | */ |
| 71 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 72 | /* ALARM Bits - or with BCD encoded value */ |
| 73 | #define AR_ENB 0x80 /* Enable for alarm cmp */ |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 74 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 75 | /* Period Bits */ |
| 76 | #define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */ |
| 77 | #define PF_COUNT 0x200 /* Half periodic counter */ |
| 78 | #define PF_OXS 0x400 /* Periodic One x Second */ |
| 79 | #define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */ |
| 80 | #define PF_MASK 0xf00 |
| 81 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 82 | /* RCR1 Bits */ |
| 83 | #define RCR1_CF 0x80 /* Carry Flag */ |
| 84 | #define RCR1_CIE 0x10 /* Carry Interrupt Enable */ |
| 85 | #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */ |
| 86 | #define RCR1_AF 0x01 /* Alarm Flag */ |
| 87 | |
| 88 | /* RCR2 Bits */ |
| 89 | #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */ |
| 90 | #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */ |
| 91 | #define RCR2_RTCEN 0x08 /* ENable RTC */ |
| 92 | #define RCR2_ADJ 0x04 /* ADJustment (30-second) */ |
| 93 | #define RCR2_RESET 0x02 /* Reset bit */ |
| 94 | #define RCR2_START 0x01 /* Start bit */ |
| 95 | |
| 96 | struct sh_rtc { |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 97 | void __iomem *regbase; |
| 98 | unsigned long regsize; |
| 99 | struct resource *res; |
| 100 | int alarm_irq; |
| 101 | int periodic_irq; |
| 102 | int carry_irq; |
| 103 | struct clk *clk; |
| 104 | struct rtc_device *rtc_dev; |
| 105 | spinlock_t lock; |
| 106 | unsigned long capabilities; /* See asm/rtc.h for cap bits */ |
| 107 | unsigned short periodic_freq; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 108 | }; |
| 109 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 110 | static int __sh_rtc_interrupt(struct sh_rtc *rtc) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 111 | { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 112 | unsigned int tmp, pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 113 | |
| 114 | tmp = readb(rtc->regbase + RCR1); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 115 | pending = tmp & RCR1_CF; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 116 | tmp &= ~RCR1_CF; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 117 | writeb(tmp, rtc->regbase + RCR1); |
| 118 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 119 | /* Users have requested One x Second IRQ */ |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 120 | if (pending && rtc->periodic_freq & PF_OXS) |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 121 | rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 122 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 123 | return pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 126 | static int __sh_rtc_alarm(struct sh_rtc *rtc) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 127 | { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 128 | unsigned int tmp, pending; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 129 | |
| 130 | tmp = readb(rtc->regbase + RCR1); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 131 | pending = tmp & RCR1_AF; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 132 | tmp &= ~(RCR1_AF | RCR1_AIE); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 133 | writeb(tmp, rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 134 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 135 | if (pending) |
| 136 | rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 137 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 138 | return pending; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 141 | static int __sh_rtc_periodic(struct sh_rtc *rtc) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 142 | { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 143 | unsigned int tmp, pending; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 144 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 145 | tmp = readb(rtc->regbase + RCR2); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 146 | pending = tmp & RCR2_PEF; |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 147 | tmp &= ~RCR2_PEF; |
| 148 | writeb(tmp, rtc->regbase + RCR2); |
| 149 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 150 | if (!pending) |
| 151 | return 0; |
| 152 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 153 | /* Half period enabled than one skipped and the next notified */ |
| 154 | if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT)) |
| 155 | rtc->periodic_freq &= ~PF_COUNT; |
| 156 | else { |
| 157 | if (rtc->periodic_freq & PF_HP) |
| 158 | rtc->periodic_freq |= PF_COUNT; |
Alexandre Belloni | ec623ff | 2018-07-25 16:45:39 +0200 | [diff] [blame] | 159 | rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 160 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 161 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 162 | return pending; |
| 163 | } |
| 164 | |
| 165 | static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id) |
| 166 | { |
| 167 | struct sh_rtc *rtc = dev_id; |
| 168 | int ret; |
| 169 | |
| 170 | spin_lock(&rtc->lock); |
| 171 | ret = __sh_rtc_interrupt(rtc); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 172 | spin_unlock(&rtc->lock); |
| 173 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 174 | return IRQ_RETVAL(ret); |
| 175 | } |
| 176 | |
| 177 | static irqreturn_t sh_rtc_alarm(int irq, void *dev_id) |
| 178 | { |
| 179 | struct sh_rtc *rtc = dev_id; |
| 180 | int ret; |
| 181 | |
| 182 | spin_lock(&rtc->lock); |
| 183 | ret = __sh_rtc_alarm(rtc); |
| 184 | spin_unlock(&rtc->lock); |
| 185 | |
| 186 | return IRQ_RETVAL(ret); |
| 187 | } |
| 188 | |
| 189 | static irqreturn_t sh_rtc_periodic(int irq, void *dev_id) |
| 190 | { |
| 191 | struct sh_rtc *rtc = dev_id; |
| 192 | int ret; |
| 193 | |
| 194 | spin_lock(&rtc->lock); |
| 195 | ret = __sh_rtc_periodic(rtc); |
| 196 | spin_unlock(&rtc->lock); |
| 197 | |
| 198 | return IRQ_RETVAL(ret); |
| 199 | } |
| 200 | |
| 201 | static irqreturn_t sh_rtc_shared(int irq, void *dev_id) |
| 202 | { |
| 203 | struct sh_rtc *rtc = dev_id; |
| 204 | int ret; |
| 205 | |
| 206 | spin_lock(&rtc->lock); |
| 207 | ret = __sh_rtc_interrupt(rtc); |
| 208 | ret |= __sh_rtc_alarm(rtc); |
| 209 | ret |= __sh_rtc_periodic(rtc); |
| 210 | spin_unlock(&rtc->lock); |
| 211 | |
| 212 | return IRQ_RETVAL(ret); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 213 | } |
| 214 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 215 | static inline void sh_rtc_setaie(struct device *dev, unsigned int enable) |
| 216 | { |
| 217 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 218 | unsigned int tmp; |
| 219 | |
| 220 | spin_lock_irq(&rtc->lock); |
| 221 | |
| 222 | tmp = readb(rtc->regbase + RCR1); |
| 223 | |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 224 | if (enable) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 225 | tmp |= RCR1_AIE; |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 226 | else |
| 227 | tmp &= ~RCR1_AIE; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 228 | |
| 229 | writeb(tmp, rtc->regbase + RCR1); |
| 230 | |
| 231 | spin_unlock_irq(&rtc->lock); |
| 232 | } |
| 233 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 234 | static int sh_rtc_proc(struct device *dev, struct seq_file *seq) |
| 235 | { |
| 236 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 237 | unsigned int tmp; |
| 238 | |
| 239 | tmp = readb(rtc->regbase + RCR1); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 240 | 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] | 241 | |
| 242 | tmp = readb(rtc->regbase + RCR2); |
| 243 | seq_printf(seq, "periodic_IRQ\t: %s\n", |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 244 | (tmp & RCR2_PESMASK) ? "yes" : "no"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Magnus Damm | 9cd88b9 | 2009-03-19 10:05:58 +0000 | [diff] [blame] | 249 | static inline void sh_rtc_setcie(struct device *dev, unsigned int enable) |
| 250 | { |
| 251 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 252 | unsigned int tmp; |
| 253 | |
| 254 | spin_lock_irq(&rtc->lock); |
| 255 | |
| 256 | tmp = readb(rtc->regbase + RCR1); |
| 257 | |
| 258 | if (!enable) |
| 259 | tmp &= ~RCR1_CIE; |
| 260 | else |
| 261 | tmp |= RCR1_CIE; |
| 262 | |
| 263 | writeb(tmp, rtc->regbase + RCR1); |
| 264 | |
| 265 | spin_unlock_irq(&rtc->lock); |
| 266 | } |
| 267 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 268 | static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 269 | { |
| 270 | sh_rtc_setaie(dev, enabled); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 274 | static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 275 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 276 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 277 | unsigned int sec128, sec2, yr, yr100, cf_bit; |
| 278 | |
Alexandre Belloni | 9852023 | 2019-03-20 12:30:08 +0100 | [diff] [blame] | 279 | if (!(readb(rtc->regbase + RCR2) & RCR2_RTCEN)) |
| 280 | return -EINVAL; |
| 281 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 282 | do { |
| 283 | unsigned int tmp; |
| 284 | |
| 285 | spin_lock_irq(&rtc->lock); |
| 286 | |
| 287 | tmp = readb(rtc->regbase + RCR1); |
| 288 | tmp &= ~RCR1_CF; /* Clear CF-bit */ |
| 289 | tmp |= RCR1_CIE; |
| 290 | writeb(tmp, rtc->regbase + RCR1); |
| 291 | |
| 292 | sec128 = readb(rtc->regbase + R64CNT); |
| 293 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 294 | tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT)); |
| 295 | tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT)); |
| 296 | tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT)); |
| 297 | tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT)); |
| 298 | tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT)); |
| 299 | tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 300 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 301 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
| 302 | yr = readw(rtc->regbase + RYRCNT); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 303 | yr100 = bcd2bin(yr >> 8); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 304 | yr &= 0xff; |
| 305 | } else { |
| 306 | yr = readb(rtc->regbase + RYRCNT); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 307 | yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 308 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 309 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 310 | tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 311 | |
| 312 | sec2 = readb(rtc->regbase + R64CNT); |
| 313 | cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF; |
| 314 | |
| 315 | spin_unlock_irq(&rtc->lock); |
| 316 | } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0); |
| 317 | |
| 318 | #if RTC_BIT_INVERTED != 0 |
| 319 | if ((sec128 & RTC_BIT_INVERTED)) |
| 320 | tm->tm_sec--; |
| 321 | #endif |
| 322 | |
Magnus Damm | 9cd88b9 | 2009-03-19 10:05:58 +0000 | [diff] [blame] | 323 | /* only keep the carry interrupt enabled if UIE is on */ |
| 324 | if (!(rtc->periodic_freq & PF_OXS)) |
| 325 | sh_rtc_setcie(dev, 0); |
| 326 | |
Paul Mundt | 435c55d | 2007-05-08 11:56:27 +0900 | [diff] [blame] | 327 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 328 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
Harvey Harrison | 2a4e2b878 | 2008-04-28 02:12:00 -0700 | [diff] [blame] | 329 | __func__, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 330 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 331 | 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] | 332 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 333 | return 0; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 337 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 338 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 339 | unsigned int tmp; |
| 340 | int year; |
| 341 | |
| 342 | spin_lock_irq(&rtc->lock); |
| 343 | |
| 344 | /* Reset pre-scaler & stop RTC */ |
| 345 | tmp = readb(rtc->regbase + RCR2); |
| 346 | tmp |= RCR2_RESET; |
Markus Brunner | 699bc66 | 2007-07-26 17:31:28 +0900 | [diff] [blame] | 347 | tmp &= ~RCR2_START; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 348 | writeb(tmp, rtc->regbase + RCR2); |
| 349 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 350 | writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT); |
| 351 | writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT); |
| 352 | writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT); |
| 353 | writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT); |
| 354 | writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT); |
| 355 | writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 356 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 357 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 358 | year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) | |
| 359 | bin2bcd(tm->tm_year % 100); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 360 | writew(year, rtc->regbase + RYRCNT); |
| 361 | } else { |
| 362 | year = tm->tm_year % 100; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 363 | writeb(bin2bcd(year), rtc->regbase + RYRCNT); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 364 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 365 | |
| 366 | /* Start RTC */ |
| 367 | tmp = readb(rtc->regbase + RCR2); |
| 368 | tmp &= ~RCR2_RESET; |
| 369 | tmp |= RCR2_RTCEN | RCR2_START; |
| 370 | writeb(tmp, rtc->regbase + RCR2); |
| 371 | |
| 372 | spin_unlock_irq(&rtc->lock); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 377 | static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off) |
| 378 | { |
| 379 | unsigned int byte; |
Geert Uytterhoeven | 15d82d2 | 2019-03-20 11:32:14 +0100 | [diff] [blame] | 380 | int value = -1; /* return -1 for ignored values */ |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 381 | |
| 382 | byte = readb(rtc->regbase + reg_off); |
| 383 | if (byte & AR_ENB) { |
| 384 | byte &= ~AR_ENB; /* strip the enable bit */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 385 | value = bcd2bin(byte); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return value; |
| 389 | } |
| 390 | |
| 391 | static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 392 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 393 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 394 | struct rtc_time *tm = &wkalrm->time; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 395 | |
| 396 | spin_lock_irq(&rtc->lock); |
| 397 | |
| 398 | tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR); |
| 399 | tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR); |
| 400 | tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR); |
| 401 | tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR); |
| 402 | tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR); |
| 403 | tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR); |
| 404 | if (tm->tm_mon > 0) |
| 405 | tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 406 | |
David Brownell | 0d103e9 | 2007-01-10 23:15:32 -0800 | [diff] [blame] | 407 | wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0; |
| 408 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 409 | spin_unlock_irq(&rtc->lock); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc, |
| 415 | int value, int reg_off) |
| 416 | { |
| 417 | /* < 0 for a value that is ignored */ |
| 418 | if (value < 0) |
| 419 | writeb(0, rtc->regbase + reg_off); |
| 420 | else |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 421 | writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 424 | static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 425 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 426 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 427 | unsigned int rcr1; |
| 428 | struct rtc_time *tm = &wkalrm->time; |
Uwe Kleine-König | 8441189 | 2016-06-28 10:43:48 +0200 | [diff] [blame] | 429 | int mon; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 430 | |
| 431 | spin_lock_irq(&rtc->lock); |
| 432 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 433 | /* disable alarm interrupt and clear the alarm flag */ |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 434 | rcr1 = readb(rtc->regbase + RCR1); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 435 | rcr1 &= ~(RCR1_AF | RCR1_AIE); |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 436 | writeb(rcr1, rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 437 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 438 | /* set alarm time */ |
| 439 | sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR); |
| 440 | sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR); |
| 441 | sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR); |
| 442 | sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR); |
| 443 | sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR); |
| 444 | mon = tm->tm_mon; |
| 445 | if (mon >= 0) |
| 446 | mon += 1; |
| 447 | sh_rtc_write_alarm_value(rtc, mon, RMONAR); |
| 448 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 449 | if (wkalrm->enabled) { |
| 450 | rcr1 |= RCR1_AIE; |
| 451 | writeb(rcr1, rtc->regbase + RCR1); |
| 452 | } |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 453 | |
| 454 | spin_unlock_irq(&rtc->lock); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Bhumika Goyal | 8bc57e7 | 2017-01-05 22:25:05 +0530 | [diff] [blame] | 459 | static const struct rtc_class_ops sh_rtc_ops = { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 460 | .read_time = sh_rtc_read_time, |
| 461 | .set_time = sh_rtc_set_time, |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 462 | .read_alarm = sh_rtc_read_alarm, |
| 463 | .set_alarm = sh_rtc_set_alarm, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 464 | .proc = sh_rtc_proc, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 465 | .alarm_irq_enable = sh_rtc_alarm_irq_enable, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 466 | }; |
| 467 | |
Alessandro Zummo | 5c9740a | 2009-08-20 13:25:11 +0900 | [diff] [blame] | 468 | static int __init sh_rtc_probe(struct platform_device *pdev) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 469 | { |
| 470 | struct sh_rtc *rtc; |
| 471 | struct resource *res; |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 472 | char clk_name[6]; |
| 473 | int clk_id, ret; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 474 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 475 | rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 476 | if (unlikely(!rtc)) |
| 477 | return -ENOMEM; |
| 478 | |
| 479 | spin_lock_init(&rtc->lock); |
| 480 | |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 481 | /* get periodic/carry/alarm irqs */ |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 482 | ret = platform_get_irq(pdev, 0); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 483 | if (unlikely(ret <= 0)) { |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 484 | dev_err(&pdev->dev, "No IRQ resource\n"); |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 485 | return -ENOENT; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 486 | } |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 487 | |
roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 488 | rtc->periodic_irq = ret; |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 489 | rtc->carry_irq = platform_get_irq(pdev, 1); |
| 490 | rtc->alarm_irq = platform_get_irq(pdev, 2); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 491 | |
| 492 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 493 | if (!res) |
| 494 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 495 | if (unlikely(res == NULL)) { |
| 496 | dev_err(&pdev->dev, "No IO resource\n"); |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 497 | return -ENOENT; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 498 | } |
| 499 | |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 500 | rtc->regsize = resource_size(res); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 501 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 502 | rtc->res = devm_request_mem_region(&pdev->dev, res->start, |
| 503 | rtc->regsize, pdev->name); |
| 504 | if (unlikely(!rtc->res)) |
| 505 | return -EBUSY; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 506 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 507 | rtc->regbase = devm_ioremap_nocache(&pdev->dev, rtc->res->start, |
| 508 | rtc->regsize); |
| 509 | if (unlikely(!rtc->regbase)) |
| 510 | return -EINVAL; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 511 | |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 512 | if (!pdev->dev.of_node) { |
| 513 | clk_id = pdev->id; |
| 514 | /* With a single device, the clock id is still "rtc0" */ |
| 515 | if (clk_id < 0) |
| 516 | clk_id = 0; |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 517 | |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 518 | snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id); |
| 519 | } else |
| 520 | snprintf(clk_name, sizeof(clk_name), "fck"); |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 521 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 522 | rtc->clk = devm_clk_get(&pdev->dev, clk_name); |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 523 | if (IS_ERR(rtc->clk)) { |
| 524 | /* |
| 525 | * No error handling for rtc->clk intentionally, not all |
| 526 | * platforms will have a unique clock for the RTC, and |
| 527 | * the clk API can handle the struct clk pointer being |
| 528 | * NULL. |
| 529 | */ |
| 530 | rtc->clk = NULL; |
| 531 | } |
| 532 | |
Alexandre Belloni | 1097998 | 2019-03-20 12:30:09 +0100 | [diff] [blame] | 533 | rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev); |
| 534 | if (IS_ERR(rtc->rtc_dev)) |
| 535 | return PTR_ERR(rtc->rtc_dev); |
| 536 | |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 537 | clk_enable(rtc->clk); |
| 538 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 539 | rtc->capabilities = RTC_DEF_CAPABILITIES; |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 540 | |
| 541 | #ifdef CONFIG_SUPERH |
Jingoo Han | e58c18d | 2013-11-12 15:10:52 -0800 | [diff] [blame] | 542 | if (dev_get_platdata(&pdev->dev)) { |
| 543 | struct sh_rtc_platform_info *pinfo = |
| 544 | dev_get_platdata(&pdev->dev); |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * Some CPUs have special capabilities in addition to the |
| 548 | * default set. Add those in here. |
| 549 | */ |
| 550 | rtc->capabilities |= pinfo->capabilities; |
| 551 | } |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 552 | #endif |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 553 | |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 554 | if (rtc->carry_irq <= 0) { |
| 555 | /* register shared periodic/carry/alarm irq */ |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 556 | ret = devm_request_irq(&pdev->dev, rtc->periodic_irq, |
| 557 | sh_rtc_shared, 0, "sh-rtc", rtc); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 558 | if (unlikely(ret)) { |
| 559 | dev_err(&pdev->dev, |
| 560 | "request IRQ failed with %d, IRQ %d\n", ret, |
| 561 | rtc->periodic_irq); |
| 562 | goto err_unmap; |
| 563 | } |
| 564 | } else { |
| 565 | /* register periodic/carry/alarm irqs */ |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 566 | ret = devm_request_irq(&pdev->dev, rtc->periodic_irq, |
| 567 | sh_rtc_periodic, 0, "sh-rtc period", rtc); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 568 | if (unlikely(ret)) { |
| 569 | dev_err(&pdev->dev, |
| 570 | "request period IRQ failed with %d, IRQ %d\n", |
| 571 | ret, rtc->periodic_irq); |
| 572 | goto err_unmap; |
| 573 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 574 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 575 | ret = devm_request_irq(&pdev->dev, rtc->carry_irq, |
| 576 | sh_rtc_interrupt, 0, "sh-rtc carry", rtc); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 577 | if (unlikely(ret)) { |
| 578 | dev_err(&pdev->dev, |
| 579 | "request carry IRQ failed with %d, IRQ %d\n", |
| 580 | ret, rtc->carry_irq); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 581 | goto err_unmap; |
| 582 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 583 | |
Jingoo Han | 0209aff | 2013-07-03 15:07:11 -0700 | [diff] [blame] | 584 | ret = devm_request_irq(&pdev->dev, rtc->alarm_irq, |
| 585 | sh_rtc_alarm, 0, "sh-rtc alarm", rtc); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 586 | if (unlikely(ret)) { |
| 587 | dev_err(&pdev->dev, |
| 588 | "request alarm IRQ failed with %d, IRQ %d\n", |
| 589 | ret, rtc->alarm_irq); |
Magnus Damm | 5e084a1 | 2009-02-24 22:11:03 +0900 | [diff] [blame] | 590 | goto err_unmap; |
| 591 | } |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 592 | } |
| 593 | |
Alessandro Zummo | 5c9740a | 2009-08-20 13:25:11 +0900 | [diff] [blame] | 594 | platform_set_drvdata(pdev, rtc); |
| 595 | |
Magnus Damm | 9cd88b9 | 2009-03-19 10:05:58 +0000 | [diff] [blame] | 596 | /* everything disabled by default */ |
Magnus Damm | 9cd88b9 | 2009-03-19 10:05:58 +0000 | [diff] [blame] | 597 | sh_rtc_setaie(&pdev->dev, 0); |
| 598 | sh_rtc_setcie(&pdev->dev, 0); |
Magnus Damm | edf2247 | 2009-03-19 10:10:44 +0000 | [diff] [blame] | 599 | |
Alexandre Belloni | 1097998 | 2019-03-20 12:30:09 +0100 | [diff] [blame] | 600 | rtc->rtc_dev->ops = &sh_rtc_ops; |
Alessandro Zummo | 5c9740a | 2009-08-20 13:25:11 +0900 | [diff] [blame] | 601 | rtc->rtc_dev->max_user_freq = 256; |
| 602 | |
Alexandre Belloni | beee05d | 2019-03-20 12:30:10 +0100 | [diff] [blame] | 603 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
| 604 | rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900; |
| 605 | rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_9999; |
| 606 | } else { |
| 607 | rtc->rtc_dev->range_min = mktime64(1999, 1, 1, 0, 0, 0); |
| 608 | rtc->rtc_dev->range_max = mktime64(2098, 12, 31, 23, 59, 59); |
Magnus Damm | edf2247 | 2009-03-19 10:10:44 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Alexandre Belloni | 1097998 | 2019-03-20 12:30:09 +0100 | [diff] [blame] | 611 | ret = rtc_register_device(rtc->rtc_dev); |
| 612 | if (ret) |
| 613 | goto err_unmap; |
| 614 | |
Magnus Damm | 7a8fe8e | 2009-03-19 10:14:41 +0000 | [diff] [blame] | 615 | device_init_wakeup(&pdev->dev, 1); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 616 | return 0; |
| 617 | |
Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 618 | err_unmap: |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 619 | clk_disable(rtc->clk); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 620 | |
| 621 | return ret; |
| 622 | } |
| 623 | |
Alessandro Zummo | 5c9740a | 2009-08-20 13:25:11 +0900 | [diff] [blame] | 624 | static int __exit sh_rtc_remove(struct platform_device *pdev) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 625 | { |
| 626 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 627 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 628 | sh_rtc_setaie(&pdev->dev, 0); |
Magnus Damm | 9cd88b9 | 2009-03-19 10:05:58 +0000 | [diff] [blame] | 629 | sh_rtc_setcie(&pdev->dev, 0); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 630 | |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 631 | clk_disable(rtc->clk); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 632 | |
| 633 | return 0; |
| 634 | } |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 635 | |
| 636 | static void sh_rtc_set_irq_wake(struct device *dev, int enabled) |
| 637 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 638 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 639 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 640 | irq_set_irq_wake(rtc->periodic_irq, enabled); |
Paul Mundt | 063adc7 | 2009-04-16 14:12:22 +0900 | [diff] [blame] | 641 | |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 642 | if (rtc->carry_irq > 0) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 643 | irq_set_irq_wake(rtc->carry_irq, enabled); |
| 644 | irq_set_irq_wake(rtc->alarm_irq, enabled); |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 645 | } |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Arnd Bergmann | 5d05e81 | 2017-04-19 19:52:43 +0200 | [diff] [blame] | 648 | static int __maybe_unused sh_rtc_suspend(struct device *dev) |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 649 | { |
| 650 | if (device_may_wakeup(dev)) |
| 651 | sh_rtc_set_irq_wake(dev, 1); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Arnd Bergmann | 5d05e81 | 2017-04-19 19:52:43 +0200 | [diff] [blame] | 656 | static int __maybe_unused sh_rtc_resume(struct device *dev) |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 657 | { |
| 658 | if (device_may_wakeup(dev)) |
| 659 | sh_rtc_set_irq_wake(dev, 0); |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
Jingoo Han | 0ed5054 | 2013-04-29 16:20:00 -0700 | [diff] [blame] | 664 | static SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops, sh_rtc_suspend, sh_rtc_resume); |
Magnus Damm | faa9fa8 | 2009-04-01 14:45:17 +0000 | [diff] [blame] | 665 | |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 666 | static const struct of_device_id sh_rtc_of_match[] = { |
| 667 | { .compatible = "renesas,sh-rtc", }, |
| 668 | { /* sentinel */ } |
| 669 | }; |
| 670 | MODULE_DEVICE_TABLE(of, sh_rtc_of_match); |
| 671 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 672 | static struct platform_driver sh_rtc_platform_driver = { |
| 673 | .driver = { |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 674 | .name = DRV_NAME, |
Jingoo Han | 0ed5054 | 2013-04-29 16:20:00 -0700 | [diff] [blame] | 675 | .pm = &sh_rtc_pm_ops, |
Chris Brandt | dab5aec | 2017-03-29 10:30:29 -0700 | [diff] [blame] | 676 | .of_match_table = sh_rtc_of_match, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 677 | }, |
Alessandro Zummo | 5c9740a | 2009-08-20 13:25:11 +0900 | [diff] [blame] | 678 | .remove = __exit_p(sh_rtc_remove), |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 679 | }; |
| 680 | |
Jingoo Han | deed5a9 | 2013-04-29 16:18:51 -0700 | [diff] [blame] | 681 | module_platform_driver_probe(sh_rtc_platform_driver, sh_rtc_probe); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 682 | |
| 683 | MODULE_DESCRIPTION("SuperH on-chip RTC driver"); |
Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 684 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, " |
| 685 | "Jamie Lenehan <lenehan@twibble.org>, " |
| 686 | "Angelo Castello <angelo.castello@st.com>"); |
Kuninori Morimoto | 4ff6f02 | 2018-11-08 06:43:22 +0000 | [diff] [blame] | 687 | MODULE_LICENSE("GPL v2"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 688 | MODULE_ALIAS("platform:" DRV_NAME); |