Alexandre Belloni | 86836d6 | 2019-04-30 11:28:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de> |
Paul Cercueil | d0f744c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 4 | * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net> |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 5 | * JZ4740 SoC RTC driver |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 8 | #include <linux/clk.h> |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 9 | #include <linux/io.h> |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Alexandre Belloni | 586655d | 2017-01-25 00:44:16 +0100 | [diff] [blame] | 11 | #include <linux/module.h> |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 12 | #include <linux/of_device.h> |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Alexandre Belloni | 3b2dc19 | 2019-04-30 11:28:19 +0200 | [diff] [blame] | 14 | #include <linux/pm_wakeirq.h> |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 15 | #include <linux/reboot.h> |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 16 | #include <linux/rtc.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | |
| 20 | #define JZ_REG_RTC_CTRL 0x00 |
| 21 | #define JZ_REG_RTC_SEC 0x04 |
| 22 | #define JZ_REG_RTC_SEC_ALARM 0x08 |
| 23 | #define JZ_REG_RTC_REGULATOR 0x0C |
| 24 | #define JZ_REG_RTC_HIBERNATE 0x20 |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 25 | #define JZ_REG_RTC_WAKEUP_FILTER 0x24 |
| 26 | #define JZ_REG_RTC_RESET_COUNTER 0x28 |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 27 | #define JZ_REG_RTC_SCRATCHPAD 0x34 |
| 28 | |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 29 | /* The following are present on the jz4780 */ |
| 30 | #define JZ_REG_RTC_WENR 0x3C |
| 31 | #define JZ_RTC_WENR_WEN BIT(31) |
| 32 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 33 | #define JZ_RTC_CTRL_WRDY BIT(7) |
| 34 | #define JZ_RTC_CTRL_1HZ BIT(6) |
| 35 | #define JZ_RTC_CTRL_1HZ_IRQ BIT(5) |
| 36 | #define JZ_RTC_CTRL_AF BIT(4) |
| 37 | #define JZ_RTC_CTRL_AF_IRQ BIT(3) |
| 38 | #define JZ_RTC_CTRL_AE BIT(2) |
| 39 | #define JZ_RTC_CTRL_ENABLE BIT(0) |
| 40 | |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 41 | /* Magic value to enable writes on jz4780 */ |
| 42 | #define JZ_RTC_WENR_MAGIC 0xA55A |
| 43 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 44 | #define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0 |
| 45 | #define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0 |
| 46 | |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 47 | enum jz4740_rtc_type { |
| 48 | ID_JZ4740, |
| 49 | ID_JZ4780, |
| 50 | }; |
| 51 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 52 | struct jz4740_rtc { |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 53 | void __iomem *base; |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 54 | enum jz4740_rtc_type type; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 55 | |
| 56 | struct rtc_device *rtc; |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 57 | struct clk *clk; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 58 | |
Lars-Peter Clausen | 7c6a52a | 2012-10-04 17:14:00 -0700 | [diff] [blame] | 59 | int irq; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 60 | |
| 61 | spinlock_t lock; |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 62 | |
| 63 | unsigned int min_wakeup_pin_assert_time; |
| 64 | unsigned int reset_pin_assert_time; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 67 | static struct device *dev_for_power_off; |
| 68 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 69 | static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg) |
| 70 | { |
| 71 | return readl(rtc->base + reg); |
| 72 | } |
| 73 | |
| 74 | static int jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc) |
| 75 | { |
| 76 | uint32_t ctrl; |
Mathieu Malaterre | 695e38d | 2017-09-18 21:10:13 +0200 | [diff] [blame] | 77 | int timeout = 10000; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 78 | |
| 79 | do { |
| 80 | ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL); |
| 81 | } while (!(ctrl & JZ_RTC_CTRL_WRDY) && --timeout); |
| 82 | |
| 83 | return timeout ? 0 : -EIO; |
| 84 | } |
| 85 | |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 86 | static inline int jz4780_rtc_enable_write(struct jz4740_rtc *rtc) |
| 87 | { |
| 88 | uint32_t ctrl; |
Mathieu Malaterre | 695e38d | 2017-09-18 21:10:13 +0200 | [diff] [blame] | 89 | int ret, timeout = 10000; |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 90 | |
| 91 | ret = jz4740_rtc_wait_write_ready(rtc); |
| 92 | if (ret != 0) |
| 93 | return ret; |
| 94 | |
| 95 | writel(JZ_RTC_WENR_MAGIC, rtc->base + JZ_REG_RTC_WENR); |
| 96 | |
| 97 | do { |
| 98 | ctrl = readl(rtc->base + JZ_REG_RTC_WENR); |
| 99 | } while (!(ctrl & JZ_RTC_WENR_WEN) && --timeout); |
| 100 | |
| 101 | return timeout ? 0 : -EIO; |
| 102 | } |
| 103 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 104 | static inline int jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg, |
| 105 | uint32_t val) |
| 106 | { |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 107 | int ret = 0; |
| 108 | |
| 109 | if (rtc->type >= ID_JZ4780) |
| 110 | ret = jz4780_rtc_enable_write(rtc); |
| 111 | if (ret == 0) |
| 112 | ret = jz4740_rtc_wait_write_ready(rtc); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 113 | if (ret == 0) |
| 114 | writel(val, rtc->base + reg); |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask, |
| 120 | bool set) |
| 121 | { |
| 122 | int ret; |
| 123 | unsigned long flags; |
| 124 | uint32_t ctrl; |
| 125 | |
| 126 | spin_lock_irqsave(&rtc->lock, flags); |
| 127 | |
| 128 | ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL); |
| 129 | |
| 130 | /* Don't clear interrupt flags by accident */ |
| 131 | ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF; |
| 132 | |
| 133 | if (set) |
| 134 | ctrl |= mask; |
| 135 | else |
| 136 | ctrl &= ~mask; |
| 137 | |
| 138 | ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl); |
| 139 | |
| 140 | spin_unlock_irqrestore(&rtc->lock, flags); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time) |
| 146 | { |
| 147 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
| 148 | uint32_t secs, secs2; |
| 149 | int timeout = 5; |
| 150 | |
Alexandre Belloni | 7fe8fce | 2019-04-30 11:28:20 +0200 | [diff] [blame] | 151 | if (jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD) != 0x12345678) |
| 152 | return -EINVAL; |
| 153 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 154 | /* If the seconds register is read while it is updated, it can contain a |
| 155 | * bogus value. This can be avoided by making sure that two consecutive |
| 156 | * reads have the same value. |
| 157 | */ |
| 158 | secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC); |
| 159 | secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC); |
| 160 | |
| 161 | while (secs != secs2 && --timeout) { |
| 162 | secs = secs2; |
| 163 | secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC); |
| 164 | } |
| 165 | |
| 166 | if (timeout == 0) |
| 167 | return -EIO; |
| 168 | |
Alexandre Belloni | be8dce9 | 2019-04-30 11:28:16 +0200 | [diff] [blame] | 169 | rtc_time64_to_tm(secs, time); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 170 | |
Alexandre Belloni | ab62670 | 2018-02-19 16:23:55 +0100 | [diff] [blame] | 171 | return 0; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexandre Belloni | e72746e | 2019-04-30 11:28:18 +0200 | [diff] [blame] | 174 | static int jz4740_rtc_set_time(struct device *dev, struct rtc_time *time) |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 175 | { |
| 176 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
Alexandre Belloni | 7fe8fce | 2019-04-30 11:28:20 +0200 | [diff] [blame] | 177 | int ret; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 178 | |
Alexandre Belloni | 7fe8fce | 2019-04-30 11:28:20 +0200 | [diff] [blame] | 179 | ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, rtc_tm_to_time64(time)); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 187 | { |
| 188 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
| 189 | uint32_t secs; |
| 190 | uint32_t ctrl; |
| 191 | |
| 192 | secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM); |
| 193 | |
| 194 | ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL); |
| 195 | |
| 196 | alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE); |
| 197 | alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF); |
| 198 | |
Alexandre Belloni | be8dce9 | 2019-04-30 11:28:16 +0200 | [diff] [blame] | 199 | rtc_time64_to_tm(secs, &alrm->time); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 200 | |
Alexandre Belloni | d10dcc95 | 2019-04-30 11:28:17 +0200 | [diff] [blame] | 201 | return 0; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 205 | { |
| 206 | int ret; |
| 207 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
Alexandre Belloni | be8dce9 | 2019-04-30 11:28:16 +0200 | [diff] [blame] | 208 | uint32_t secs = lower_32_bits(rtc_tm_to_time64(&alrm->time)); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 209 | |
| 210 | ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs); |
| 211 | if (!ret) |
Paul Cercueil | d0f744c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 212 | ret = jz4740_rtc_ctrl_set_bits(rtc, |
| 213 | JZ_RTC_CTRL_AE | JZ_RTC_CTRL_AF_IRQ, alrm->enabled); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 218 | static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
| 219 | { |
| 220 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
| 221 | return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable); |
| 222 | } |
| 223 | |
Julia Lawall | 34c7b3a | 2016-08-31 10:05:25 +0200 | [diff] [blame] | 224 | static const struct rtc_class_ops jz4740_rtc_ops = { |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 225 | .read_time = jz4740_rtc_read_time, |
Alexandre Belloni | e72746e | 2019-04-30 11:28:18 +0200 | [diff] [blame] | 226 | .set_time = jz4740_rtc_set_time, |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 227 | .read_alarm = jz4740_rtc_read_alarm, |
| 228 | .set_alarm = jz4740_rtc_set_alarm, |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 229 | .alarm_irq_enable = jz4740_rtc_alarm_irq_enable, |
| 230 | }; |
| 231 | |
| 232 | static irqreturn_t jz4740_rtc_irq(int irq, void *data) |
| 233 | { |
| 234 | struct jz4740_rtc *rtc = data; |
| 235 | uint32_t ctrl; |
| 236 | unsigned long events = 0; |
| 237 | |
| 238 | ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL); |
| 239 | |
| 240 | if (ctrl & JZ_RTC_CTRL_1HZ) |
| 241 | events |= (RTC_UF | RTC_IRQF); |
| 242 | |
| 243 | if (ctrl & JZ_RTC_CTRL_AF) |
| 244 | events |= (RTC_AF | RTC_IRQF); |
| 245 | |
| 246 | rtc_update_irq(rtc->rtc, 1, events); |
| 247 | |
| 248 | jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, false); |
| 249 | |
| 250 | return IRQ_HANDLED; |
| 251 | } |
| 252 | |
Alexandre Belloni | 819c217 | 2016-11-08 22:20:37 +0100 | [diff] [blame] | 253 | static void jz4740_rtc_poweroff(struct device *dev) |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 254 | { |
| 255 | struct jz4740_rtc *rtc = dev_get_drvdata(dev); |
| 256 | jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1); |
| 257 | } |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 258 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 259 | static void jz4740_rtc_power_off(void) |
| 260 | { |
| 261 | struct jz4740_rtc *rtc = dev_get_drvdata(dev_for_power_off); |
| 262 | unsigned long rtc_rate; |
| 263 | unsigned long wakeup_filter_ticks; |
| 264 | unsigned long reset_counter_ticks; |
| 265 | |
| 266 | clk_prepare_enable(rtc->clk); |
| 267 | |
| 268 | rtc_rate = clk_get_rate(rtc->clk); |
| 269 | |
| 270 | /* |
| 271 | * Set minimum wakeup pin assertion time: 100 ms. |
| 272 | * Range is 0 to 2 sec if RTC is clocked at 32 kHz. |
| 273 | */ |
| 274 | wakeup_filter_ticks = |
| 275 | (rtc->min_wakeup_pin_assert_time * rtc_rate) / 1000; |
| 276 | if (wakeup_filter_ticks < JZ_RTC_WAKEUP_FILTER_MASK) |
| 277 | wakeup_filter_ticks &= JZ_RTC_WAKEUP_FILTER_MASK; |
| 278 | else |
| 279 | wakeup_filter_ticks = JZ_RTC_WAKEUP_FILTER_MASK; |
| 280 | jz4740_rtc_reg_write(rtc, |
| 281 | JZ_REG_RTC_WAKEUP_FILTER, wakeup_filter_ticks); |
| 282 | |
| 283 | /* |
| 284 | * Set reset pin low-level assertion time after wakeup: 60 ms. |
| 285 | * Range is 0 to 125 ms if RTC is clocked at 32 kHz. |
| 286 | */ |
| 287 | reset_counter_ticks = (rtc->reset_pin_assert_time * rtc_rate) / 1000; |
| 288 | if (reset_counter_ticks < JZ_RTC_RESET_COUNTER_MASK) |
| 289 | reset_counter_ticks &= JZ_RTC_RESET_COUNTER_MASK; |
| 290 | else |
| 291 | reset_counter_ticks = JZ_RTC_RESET_COUNTER_MASK; |
| 292 | jz4740_rtc_reg_write(rtc, |
| 293 | JZ_REG_RTC_RESET_COUNTER, reset_counter_ticks); |
| 294 | |
| 295 | jz4740_rtc_poweroff(dev_for_power_off); |
Alexandre Belloni | 586655d | 2017-01-25 00:44:16 +0100 | [diff] [blame] | 296 | kernel_halt(); |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 299 | static const struct of_device_id jz4740_rtc_of_match[] = { |
| 300 | { .compatible = "ingenic,jz4740-rtc", .data = (void *)ID_JZ4740 }, |
| 301 | { .compatible = "ingenic,jz4780-rtc", .data = (void *)ID_JZ4780 }, |
| 302 | {}, |
| 303 | }; |
Alexandre Belloni | 586655d | 2017-01-25 00:44:16 +0100 | [diff] [blame] | 304 | MODULE_DEVICE_TABLE(of, jz4740_rtc_of_match); |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 305 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 306 | static int jz4740_rtc_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 307 | { |
| 308 | int ret; |
| 309 | struct jz4740_rtc *rtc; |
Jingoo Han | 3b6aa90 | 2014-04-03 14:49:50 -0700 | [diff] [blame] | 310 | struct resource *mem; |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 311 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 312 | const struct of_device_id *of_id = of_match_device( |
| 313 | jz4740_rtc_of_match, &pdev->dev); |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 314 | struct device_node *np = pdev->dev.of_node; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 315 | |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 316 | rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 317 | if (!rtc) |
| 318 | return -ENOMEM; |
| 319 | |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 320 | if (of_id) |
| 321 | rtc->type = (enum jz4740_rtc_type)of_id->data; |
| 322 | else |
| 323 | rtc->type = id->driver_data; |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 324 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 325 | rtc->irq = platform_get_irq(pdev, 0); |
Stephen Boyd | faac910 | 2019-07-30 11:15:39 -0700 | [diff] [blame] | 326 | if (rtc->irq < 0) |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 327 | return -ENOENT; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 328 | |
Jingoo Han | 3b6aa90 | 2014-04-03 14:49:50 -0700 | [diff] [blame] | 329 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 330 | rtc->base = devm_ioremap_resource(&pdev->dev, mem); |
| 331 | if (IS_ERR(rtc->base)) |
| 332 | return PTR_ERR(rtc->base); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 333 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 334 | rtc->clk = devm_clk_get(&pdev->dev, "rtc"); |
| 335 | if (IS_ERR(rtc->clk)) { |
| 336 | dev_err(&pdev->dev, "Failed to get RTC clock\n"); |
| 337 | return PTR_ERR(rtc->clk); |
| 338 | } |
| 339 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 340 | spin_lock_init(&rtc->lock); |
| 341 | |
| 342 | platform_set_drvdata(pdev, rtc); |
| 343 | |
Paul Cercueil | d0f744c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 344 | device_init_wakeup(&pdev->dev, 1); |
| 345 | |
Alexandre Belloni | 3b2dc19 | 2019-04-30 11:28:19 +0200 | [diff] [blame] | 346 | ret = dev_pm_set_wake_irq(&pdev->dev, rtc->irq); |
| 347 | if (ret) { |
| 348 | dev_err(&pdev->dev, "Failed to set wake irq: %d\n", ret); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Alexandre Belloni | a7ab6be | 2019-04-30 11:28:15 +0200 | [diff] [blame] | 352 | rtc->rtc = devm_rtc_allocate_device(&pdev->dev); |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 353 | if (IS_ERR(rtc->rtc)) { |
| 354 | ret = PTR_ERR(rtc->rtc); |
Alexandre Belloni | a7ab6be | 2019-04-30 11:28:15 +0200 | [diff] [blame] | 355 | dev_err(&pdev->dev, "Failed to allocate rtc device: %d\n", ret); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | rtc->rtc->ops = &jz4740_rtc_ops; |
| 360 | rtc->rtc->range_max = U32_MAX; |
| 361 | |
| 362 | ret = rtc_register_device(rtc->rtc); |
Alexandre Belloni | 44c638c | 2019-08-19 00:00:41 +0200 | [diff] [blame^] | 363 | if (ret) |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 364 | return ret; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 365 | |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 366 | ret = devm_request_irq(&pdev->dev, rtc->irq, jz4740_rtc_irq, 0, |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 367 | pdev->name, rtc); |
| 368 | if (ret) { |
| 369 | dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret); |
Jingoo Han | c08ac489 | 2013-07-03 15:07:06 -0700 | [diff] [blame] | 370 | return ret; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Paul Cercueil | f9eb69d | 2016-10-31 21:39:48 +0100 | [diff] [blame] | 373 | if (np && of_device_is_system_power_controller(np)) { |
| 374 | if (!pm_power_off) { |
| 375 | /* Default: 60ms */ |
| 376 | rtc->reset_pin_assert_time = 60; |
| 377 | of_property_read_u32(np, "reset-pin-assert-time-ms", |
| 378 | &rtc->reset_pin_assert_time); |
| 379 | |
| 380 | /* Default: 100ms */ |
| 381 | rtc->min_wakeup_pin_assert_time = 100; |
| 382 | of_property_read_u32(np, |
| 383 | "min-wakeup-pin-assert-time-ms", |
| 384 | &rtc->min_wakeup_pin_assert_time); |
| 385 | |
| 386 | dev_for_power_off = &pdev->dev; |
| 387 | pm_power_off = jz4740_rtc_power_off; |
| 388 | } else { |
| 389 | dev_warn(&pdev->dev, |
| 390 | "Poweroff handler already present!\n"); |
| 391 | } |
| 392 | } |
| 393 | |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 394 | return 0; |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 397 | static const struct platform_device_id jz4740_rtc_ids[] = { |
| 398 | { "jz4740-rtc", ID_JZ4740 }, |
| 399 | { "jz4780-rtc", ID_JZ4780 }, |
| 400 | {} |
| 401 | }; |
Alexandre Belloni | 586655d | 2017-01-25 00:44:16 +0100 | [diff] [blame] | 402 | MODULE_DEVICE_TABLE(platform, jz4740_rtc_ids); |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 403 | |
Axel Lin | 681d037 | 2012-01-10 15:10:55 -0800 | [diff] [blame] | 404 | static struct platform_driver jz4740_rtc_driver = { |
Paul Cercueil | d0f744c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 405 | .probe = jz4740_rtc_probe, |
Paul Cercueil | d0f744c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 406 | .driver = { |
| 407 | .name = "jz4740-rtc", |
Paul Cercueil | c05229a | 2016-10-31 21:39:47 +0100 | [diff] [blame] | 408 | .of_match_table = of_match_ptr(jz4740_rtc_of_match), |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 409 | }, |
Paul Cercueil | cd56320 | 2016-10-31 21:39:45 +0100 | [diff] [blame] | 410 | .id_table = jz4740_rtc_ids, |
Lars-Peter Clausen | 3bf0eea | 2010-06-19 18:29:50 +0000 | [diff] [blame] | 411 | }; |
| 412 | |
Alexandre Belloni | 586655d | 2017-01-25 00:44:16 +0100 | [diff] [blame] | 413 | module_platform_driver(jz4740_rtc_driver); |
| 414 | |
| 415 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 416 | MODULE_LICENSE("GPL"); |
| 417 | MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n"); |
| 418 | MODULE_ALIAS("platform:jz4740-rtc"); |