Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 3 | */ |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 4 | #include <linux/of.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/rtc.h> |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 8 | #include <linux/platform_device.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 9 | #include <linux/pm.h> |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 10 | #include <linux/regmap.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 14 | /* RTC Register offsets from RTC CTRL REG */ |
| 15 | #define PM8XXX_ALARM_CTRL_OFFSET 0x01 |
| 16 | #define PM8XXX_RTC_WRITE_OFFSET 0x02 |
| 17 | #define PM8XXX_RTC_READ_OFFSET 0x06 |
| 18 | #define PM8XXX_ALARM_RW_OFFSET 0x0A |
| 19 | |
| 20 | /* RTC_CTRL register bit fields */ |
| 21 | #define PM8xxx_RTC_ENABLE BIT(7) |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 22 | #define PM8xxx_RTC_ALARM_CLEAR BIT(0) |
| 23 | |
| 24 | #define NUM_8_BIT_RTC_REGS 0x4 |
| 25 | |
| 26 | /** |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 27 | * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions |
| 28 | * @ctrl: base address of control register |
| 29 | * @write: base address of write register |
| 30 | * @read: base address of read register |
| 31 | * @alarm_ctrl: base address of alarm control register |
| 32 | * @alarm_ctrl2: base address of alarm control2 register |
| 33 | * @alarm_rw: base address of alarm read-write register |
| 34 | * @alarm_en: alarm enable mask |
| 35 | */ |
| 36 | struct pm8xxx_rtc_regs { |
| 37 | unsigned int ctrl; |
| 38 | unsigned int write; |
| 39 | unsigned int read; |
| 40 | unsigned int alarm_ctrl; |
| 41 | unsigned int alarm_ctrl2; |
| 42 | unsigned int alarm_rw; |
| 43 | unsigned int alarm_en; |
| 44 | }; |
| 45 | |
| 46 | /** |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 47 | * struct pm8xxx_rtc - rtc driver internal structure |
| 48 | * @rtc: rtc device for this driver. |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 49 | * @regmap: regmap used to access RTC registers |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 50 | * @allow_set_time: indicates whether writing to the RTC is allowed |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 51 | * @rtc_alarm_irq: rtc alarm irq number. |
Alexandre Belloni | 863d7b1 | 2019-11-22 11:22:10 +0100 | [diff] [blame] | 52 | * @regs: rtc registers description. |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 53 | * @rtc_dev: device structure. |
| 54 | * @ctrl_reg_lock: spinlock protecting access to ctrl_reg. |
| 55 | */ |
| 56 | struct pm8xxx_rtc { |
| 57 | struct rtc_device *rtc; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 58 | struct regmap *regmap; |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 59 | bool allow_set_time; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 60 | int rtc_alarm_irq; |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 61 | const struct pm8xxx_rtc_regs *regs; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 62 | struct device *rtc_dev; |
| 63 | spinlock_t ctrl_reg_lock; |
| 64 | }; |
| 65 | |
| 66 | /* |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 67 | * Steps to write the RTC registers. |
| 68 | * 1. Disable alarm if enabled. |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 69 | * 2. Disable rtc if enabled. |
| 70 | * 3. Write 0x00 to LSB. |
| 71 | * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0]. |
| 72 | * 5. Enable rtc if disabled in step 2. |
| 73 | * 6. Enable alarm if disabled in step 1. |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 74 | */ |
| 75 | static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 76 | { |
| 77 | int rc, i; |
| 78 | unsigned long secs, irq_flags; |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 79 | u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0; |
| 80 | unsigned int ctrl_reg, rtc_ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 81 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 82 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 83 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 84 | if (!rtc_dd->allow_set_time) |
| 85 | return -EACCES; |
| 86 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 87 | rtc_tm_to_time(tm, &secs); |
| 88 | |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 89 | dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); |
| 90 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 91 | for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) { |
| 92 | value[i] = secs & 0xFF; |
| 93 | secs >>= 8; |
| 94 | } |
| 95 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 96 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 97 | |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 98 | rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 99 | if (rc) |
| 100 | goto rtc_rw_fail; |
| 101 | |
| 102 | if (ctrl_reg & regs->alarm_en) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 103 | alarm_enabled = 1; |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 104 | ctrl_reg &= ~regs->alarm_en; |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 105 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); |
| 106 | if (rc) { |
| 107 | dev_err(dev, "Write to RTC Alarm control register failed\n"); |
| 108 | goto rtc_rw_fail; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* Disable RTC H/w before writing on RTC register */ |
| 113 | rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg); |
| 114 | if (rc) |
| 115 | goto rtc_rw_fail; |
| 116 | |
| 117 | if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) { |
| 118 | rtc_disabled = 1; |
| 119 | rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE; |
| 120 | rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 121 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 122 | dev_err(dev, "Write to RTC control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 123 | goto rtc_rw_fail; |
| 124 | } |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 125 | } |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 126 | |
| 127 | /* Write 0 to Byte[0] */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 128 | rc = regmap_write(rtc_dd->regmap, regs->write, 0); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 129 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 130 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 131 | goto rtc_rw_fail; |
| 132 | } |
| 133 | |
| 134 | /* Write Byte[1], Byte[2], Byte[3] */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 135 | rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1, |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 136 | &value[1], sizeof(value) - 1); |
| 137 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 138 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 139 | goto rtc_rw_fail; |
| 140 | } |
| 141 | |
| 142 | /* Write Byte[0] */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 143 | rc = regmap_write(rtc_dd->regmap, regs->write, value[0]); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 144 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 145 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 146 | goto rtc_rw_fail; |
| 147 | } |
| 148 | |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 149 | /* Enable RTC H/w after writing on RTC register */ |
| 150 | if (rtc_disabled) { |
| 151 | rtc_ctrl_reg |= PM8xxx_RTC_ENABLE; |
| 152 | rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 153 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 154 | dev_err(dev, "Write to RTC control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 155 | goto rtc_rw_fail; |
| 156 | } |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Mohit Aggarwal | 83220bf | 2018-03-05 14:35:58 +0530 | [diff] [blame] | 159 | if (alarm_enabled) { |
| 160 | ctrl_reg |= regs->alarm_en; |
| 161 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); |
| 162 | if (rc) { |
| 163 | dev_err(dev, "Write to RTC Alarm control register failed\n"); |
| 164 | goto rtc_rw_fail; |
| 165 | } |
| 166 | } |
| 167 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 168 | rtc_rw_fail: |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 169 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 170 | |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 175 | { |
| 176 | int rc; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 177 | u8 value[NUM_8_BIT_RTC_REGS]; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 178 | unsigned long secs; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 179 | unsigned int reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 180 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 181 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 182 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 183 | rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value)); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 184 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 185 | dev_err(dev, "RTC read data register failed\n"); |
| 186 | return rc; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Read the LSB again and check if there has been a carry over. |
| 191 | * If there is, redo the read operation. |
| 192 | */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 193 | rc = regmap_read(rtc_dd->regmap, regs->read, ®); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 194 | if (rc < 0) { |
| 195 | dev_err(dev, "RTC read data register failed\n"); |
| 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | if (unlikely(reg < value[0])) { |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 200 | rc = regmap_bulk_read(rtc_dd->regmap, regs->read, |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 201 | value, sizeof(value)); |
| 202 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 203 | dev_err(dev, "RTC read data register failed\n"); |
| 204 | return rc; |
| 205 | } |
| 206 | } |
| 207 | |
Colin Ian King | e422808 | 2019-02-06 10:31:02 +0000 | [diff] [blame] | 208 | secs = value[0] | (value[1] << 8) | (value[2] << 16) | |
| 209 | ((unsigned long)value[3] << 24); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 210 | |
| 211 | rtc_time_to_tm(secs, tm); |
| 212 | |
Andy Shevchenko | 4f5ef6e | 2018-12-04 23:23:20 +0200 | [diff] [blame] | 213 | dev_dbg(dev, "secs = %lu, h:m:s == %ptRt, y-m-d = %ptRdr\n", secs, tm, tm); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 219 | { |
| 220 | int rc, i; |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 221 | u8 value[NUM_8_BIT_RTC_REGS]; |
| 222 | unsigned int ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 223 | unsigned long secs, irq_flags; |
| 224 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 225 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 226 | |
| 227 | rtc_tm_to_time(&alarm->time, &secs); |
| 228 | |
| 229 | for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) { |
| 230 | value[i] = secs & 0xFF; |
| 231 | secs >>= 8; |
| 232 | } |
| 233 | |
| 234 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 235 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 236 | rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value, |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 237 | sizeof(value)); |
| 238 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 239 | dev_err(dev, "Write to RTC ALARM register failed\n"); |
| 240 | goto rtc_rw_fail; |
| 241 | } |
| 242 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 243 | rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg); |
| 244 | if (rc) |
| 245 | goto rtc_rw_fail; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 246 | |
| 247 | if (alarm->enabled) |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 248 | ctrl_reg |= regs->alarm_en; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 249 | else |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 250 | ctrl_reg &= ~regs->alarm_en; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 251 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 252 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 253 | if (rc) { |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 254 | dev_err(dev, "Write to RTC alarm control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 255 | goto rtc_rw_fail; |
| 256 | } |
| 257 | |
Andy Shevchenko | 4f5ef6e | 2018-12-04 23:23:20 +0200 | [diff] [blame] | 258 | dev_dbg(dev, "Alarm Set for h:m:s=%ptRt, y-m-d=%ptRdr\n", |
| 259 | &alarm->time, &alarm->time); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 260 | rtc_rw_fail: |
| 261 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 266 | { |
| 267 | int rc; |
| 268 | u8 value[NUM_8_BIT_RTC_REGS]; |
| 269 | unsigned long secs; |
| 270 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 271 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 272 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 273 | rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value, |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 274 | sizeof(value)); |
| 275 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 276 | dev_err(dev, "RTC alarm time read failed\n"); |
| 277 | return rc; |
| 278 | } |
| 279 | |
Colin Ian King | e422808 | 2019-02-06 10:31:02 +0000 | [diff] [blame] | 280 | secs = value[0] | (value[1] << 8) | (value[2] << 16) | |
| 281 | ((unsigned long)value[3] << 24); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 282 | |
| 283 | rtc_time_to_tm(secs, &alarm->time); |
| 284 | |
| 285 | rc = rtc_valid_tm(&alarm->time); |
| 286 | if (rc < 0) { |
| 287 | dev_err(dev, "Invalid alarm time read from RTC\n"); |
| 288 | return rc; |
| 289 | } |
| 290 | |
Andy Shevchenko | 4f5ef6e | 2018-12-04 23:23:20 +0200 | [diff] [blame] | 291 | dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n", |
| 292 | &alarm->time, &alarm->time); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
| 298 | { |
| 299 | int rc; |
| 300 | unsigned long irq_flags; |
| 301 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 302 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
| 303 | unsigned int ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 304 | |
| 305 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 306 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 307 | rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg); |
| 308 | if (rc) |
| 309 | goto rtc_rw_fail; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 310 | |
| 311 | if (enable) |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 312 | ctrl_reg |= regs->alarm_en; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 313 | else |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 314 | ctrl_reg &= ~regs->alarm_en; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 315 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 316 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 317 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 318 | dev_err(dev, "Write to RTC control register failed\n"); |
| 319 | goto rtc_rw_fail; |
| 320 | } |
| 321 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 322 | rtc_rw_fail: |
| 323 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 324 | return rc; |
| 325 | } |
| 326 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 327 | static const struct rtc_class_ops pm8xxx_rtc_ops = { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 328 | .read_time = pm8xxx_rtc_read_time, |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 329 | .set_time = pm8xxx_rtc_set_time, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 330 | .set_alarm = pm8xxx_rtc_set_alarm, |
| 331 | .read_alarm = pm8xxx_rtc_read_alarm, |
| 332 | .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable, |
| 333 | }; |
| 334 | |
| 335 | static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id) |
| 336 | { |
| 337 | struct pm8xxx_rtc *rtc_dd = dev_id; |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 338 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 339 | unsigned int ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 340 | int rc; |
| 341 | unsigned long irq_flags; |
| 342 | |
| 343 | rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF); |
| 344 | |
| 345 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 346 | |
| 347 | /* Clear the alarm enable bit */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 348 | rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg); |
| 349 | if (rc) { |
| 350 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 351 | goto rtc_alarm_handled; |
| 352 | } |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 353 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 354 | ctrl_reg &= ~regs->alarm_en; |
| 355 | |
| 356 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 357 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 358 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 359 | dev_err(rtc_dd->rtc_dev, |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 360 | "Write to alarm control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 361 | goto rtc_alarm_handled; |
| 362 | } |
| 363 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 364 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 365 | |
| 366 | /* Clear RTC alarm register */ |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 367 | rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 368 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 369 | dev_err(rtc_dd->rtc_dev, |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 370 | "RTC Alarm control2 register read failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 371 | goto rtc_alarm_handled; |
| 372 | } |
| 373 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 374 | ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR; |
| 375 | rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg); |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 376 | if (rc) |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 377 | dev_err(rtc_dd->rtc_dev, |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 378 | "Write to RTC Alarm control2 register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 379 | |
| 380 | rtc_alarm_handled: |
| 381 | return IRQ_HANDLED; |
| 382 | } |
| 383 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 384 | static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd) |
| 385 | { |
| 386 | const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; |
| 387 | unsigned int ctrl_reg; |
| 388 | int rc; |
| 389 | |
| 390 | /* Check if the RTC is on, else turn it on */ |
| 391 | rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg); |
| 392 | if (rc) |
| 393 | return rc; |
| 394 | |
| 395 | if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) { |
| 396 | ctrl_reg |= PM8xxx_RTC_ENABLE; |
| 397 | rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg); |
| 398 | if (rc) |
| 399 | return rc; |
| 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static const struct pm8xxx_rtc_regs pm8921_regs = { |
| 406 | .ctrl = 0x11d, |
| 407 | .write = 0x11f, |
| 408 | .read = 0x123, |
| 409 | .alarm_rw = 0x127, |
| 410 | .alarm_ctrl = 0x11d, |
| 411 | .alarm_ctrl2 = 0x11e, |
| 412 | .alarm_en = BIT(1), |
| 413 | }; |
| 414 | |
| 415 | static const struct pm8xxx_rtc_regs pm8058_regs = { |
| 416 | .ctrl = 0x1e8, |
| 417 | .write = 0x1ea, |
| 418 | .read = 0x1ee, |
| 419 | .alarm_rw = 0x1f2, |
| 420 | .alarm_ctrl = 0x1e8, |
| 421 | .alarm_ctrl2 = 0x1e9, |
| 422 | .alarm_en = BIT(1), |
| 423 | }; |
| 424 | |
| 425 | static const struct pm8xxx_rtc_regs pm8941_regs = { |
| 426 | .ctrl = 0x6046, |
| 427 | .write = 0x6040, |
| 428 | .read = 0x6048, |
| 429 | .alarm_rw = 0x6140, |
| 430 | .alarm_ctrl = 0x6146, |
| 431 | .alarm_ctrl2 = 0x6148, |
| 432 | .alarm_en = BIT(7), |
| 433 | }; |
| 434 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 435 | /* |
| 436 | * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out |
| 437 | */ |
| 438 | static const struct of_device_id pm8xxx_id_table[] = { |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 439 | { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs }, |
Neil Armstrong | 08655bc | 2016-08-11 15:16:44 +0200 | [diff] [blame] | 440 | { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs }, |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 441 | { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs }, |
| 442 | { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs }, |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 443 | { }, |
| 444 | }; |
| 445 | MODULE_DEVICE_TABLE(of, pm8xxx_id_table); |
| 446 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 447 | static int pm8xxx_rtc_probe(struct platform_device *pdev) |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 448 | { |
| 449 | int rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 450 | struct pm8xxx_rtc *rtc_dd; |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 451 | const struct of_device_id *match; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 452 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 453 | match = of_match_node(pm8xxx_id_table, pdev->dev.of_node); |
| 454 | if (!match) |
| 455 | return -ENXIO; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 456 | |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 457 | rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL); |
Jingoo Han | 49ae425 | 2014-04-03 14:49:43 -0700 | [diff] [blame] | 458 | if (rtc_dd == NULL) |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 459 | return -ENOMEM; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 460 | |
| 461 | /* Initialise spinlock to protect RTC control register */ |
| 462 | spin_lock_init(&rtc_dd->ctrl_reg_lock); |
| 463 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 464 | rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 465 | if (!rtc_dd->regmap) { |
| 466 | dev_err(&pdev->dev, "Parent regmap unavailable.\n"); |
| 467 | return -ENXIO; |
| 468 | } |
| 469 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 470 | rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0); |
Stephen Boyd | faac910 | 2019-07-30 11:15:39 -0700 | [diff] [blame] | 471 | if (rtc_dd->rtc_alarm_irq < 0) |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 472 | return -ENXIO; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 473 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 474 | rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node, |
| 475 | "allow-set-time"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 476 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 477 | rtc_dd->regs = match->data; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 478 | rtc_dd->rtc_dev = &pdev->dev; |
| 479 | |
Stanimir Varbanov | c8d523a | 2014-10-29 14:50:33 -0700 | [diff] [blame] | 480 | rc = pm8xxx_rtc_enable(rtc_dd); |
| 481 | if (rc) |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 482 | return rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 483 | |
| 484 | platform_set_drvdata(pdev, rtc_dd); |
| 485 | |
Josh Cartwright | fda9909 | 2014-04-03 14:50:14 -0700 | [diff] [blame] | 486 | device_init_wakeup(&pdev->dev, 1); |
| 487 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 488 | /* Register the RTC device */ |
Alexandre Belloni | d5d55b7 | 2020-03-06 08:37:55 +0100 | [diff] [blame] | 489 | rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev); |
| 490 | if (IS_ERR(rtc_dd->rtc)) |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 491 | return PTR_ERR(rtc_dd->rtc); |
Alexandre Belloni | d5d55b7 | 2020-03-06 08:37:55 +0100 | [diff] [blame] | 492 | |
| 493 | rtc_dd->rtc->ops = &pm8xxx_rtc_ops; |
Alexandre Belloni | 3cfe526 | 2020-03-06 08:37:56 +0100 | [diff] [blame^] | 494 | rtc_dd->rtc->range_max = U32_MAX; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 495 | |
| 496 | /* Request the alarm IRQ */ |
Josh Cartwright | bffcbc0 | 2014-04-03 14:50:12 -0700 | [diff] [blame] | 497 | rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq, |
| 498 | pm8xxx_alarm_trigger, |
| 499 | IRQF_TRIGGER_RISING, |
| 500 | "pm8xxx_rtc_alarm", rtc_dd); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 501 | if (rc < 0) { |
| 502 | dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 503 | return rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Alexandre Belloni | d5d55b7 | 2020-03-06 08:37:55 +0100 | [diff] [blame] | 506 | return rtc_register_device(rtc_dd->rtc); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 509 | #ifdef CONFIG_PM_SLEEP |
| 510 | static int pm8xxx_rtc_resume(struct device *dev) |
| 511 | { |
| 512 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 513 | |
| 514 | if (device_may_wakeup(dev)) |
| 515 | disable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int pm8xxx_rtc_suspend(struct device *dev) |
| 521 | { |
| 522 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 523 | |
| 524 | if (device_may_wakeup(dev)) |
| 525 | enable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | #endif |
| 530 | |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 531 | static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops, |
| 532 | pm8xxx_rtc_suspend, |
| 533 | pm8xxx_rtc_resume); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 534 | |
| 535 | static struct platform_driver pm8xxx_rtc_driver = { |
| 536 | .probe = pm8xxx_rtc_probe, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 537 | .driver = { |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 538 | .name = "rtc-pm8xxx", |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 539 | .pm = &pm8xxx_rtc_pm_ops, |
| 540 | .of_match_table = pm8xxx_id_table, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 541 | }, |
| 542 | }; |
| 543 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 544 | module_platform_driver(pm8xxx_rtc_driver); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 545 | |
| 546 | MODULE_ALIAS("platform:rtc-pm8xxx"); |
| 547 | MODULE_DESCRIPTION("PMIC8xxx RTC driver"); |
| 548 | MODULE_LICENSE("GPL v2"); |
| 549 | MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>"); |