blob: b45ee2cb2c0449cbb9e5a12524864bee6491665a [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07002/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07003 */
Josh Cartwright5a418552014-04-03 14:50:13 -07004#include <linux/of.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07005#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/rtc.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -07008#include <linux/platform_device.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07009#include <linux/pm.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070010#include <linux/regmap.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070011#include <linux/slab.h>
12#include <linux/spinlock.h>
13
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070014/* 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 Ghayal9a9a54a2011-07-25 17:13:33 -070022#define PM8xxx_RTC_ALARM_CLEAR BIT(0)
23
24#define NUM_8_BIT_RTC_REGS 0x4
25
26/**
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070027 * 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 */
36struct 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 Ghayal9a9a54a2011-07-25 17:13:33 -070047 * struct pm8xxx_rtc - rtc driver internal structure
48 * @rtc: rtc device for this driver.
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070049 * @regmap: regmap used to access RTC registers
Josh Cartwright5a418552014-04-03 14:50:13 -070050 * @allow_set_time: indicates whether writing to the RTC is allowed
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070051 * @rtc_alarm_irq: rtc alarm irq number.
Alexandre Belloni863d7b12019-11-22 11:22:10 +010052 * @regs: rtc registers description.
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070053 * @rtc_dev: device structure.
54 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
55 */
56struct pm8xxx_rtc {
57 struct rtc_device *rtc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070058 struct regmap *regmap;
Josh Cartwright5a418552014-04-03 14:50:13 -070059 bool allow_set_time;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070060 int rtc_alarm_irq;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070061 const struct pm8xxx_rtc_regs *regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070062 struct device *rtc_dev;
63 spinlock_t ctrl_reg_lock;
64};
65
66/*
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070067 * Steps to write the RTC registers.
68 * 1. Disable alarm if enabled.
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053069 * 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 Ghayal9a9a54a2011-07-25 17:13:33 -070074 */
75static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
76{
77 int rc, i;
78 unsigned long secs, irq_flags;
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053079 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0;
80 unsigned int ctrl_reg, rtc_ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070081 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070082 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070083
Josh Cartwright5a418552014-04-03 14:50:13 -070084 if (!rtc_dd->allow_set_time)
85 return -EACCES;
86
Alexandre Belloni4c470b22020-03-06 08:37:57 +010087 secs = rtc_tm_to_time64(tm);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070088
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053089 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
90
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070091 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
92 value[i] = secs & 0xFF;
93 secs >>= 8;
94 }
95
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070096 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070097
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053098 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070099 if (rc)
100 goto rtc_rw_fail;
101
102 if (ctrl_reg & regs->alarm_en) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700103 alarm_enabled = 1;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700104 ctrl_reg &= ~regs->alarm_en;
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530105 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 Cartwright5d7dc4c2014-04-03 14:50:11 -0700121 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700122 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700123 goto rtc_rw_fail;
124 }
Josh Cartwright5bed8112014-04-03 14:50:10 -0700125 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700126
127 /* Write 0 to Byte[0] */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700128 rc = regmap_write(rtc_dd->regmap, regs->write, 0);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700129 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700130 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 Varbanovc8d523a2014-10-29 14:50:33 -0700135 rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700136 &value[1], sizeof(value) - 1);
137 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700138 dev_err(dev, "Write to RTC write data register failed\n");
139 goto rtc_rw_fail;
140 }
141
142 /* Write Byte[0] */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700143 rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700144 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700145 dev_err(dev, "Write to RTC write data register failed\n");
146 goto rtc_rw_fail;
147 }
148
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530149 /* 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 Cartwright5d7dc4c2014-04-03 14:50:11 -0700153 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700154 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700155 goto rtc_rw_fail;
156 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700157 }
158
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530159 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 Ghayal9a9a54a2011-07-25 17:13:33 -0700168rtc_rw_fail:
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700169 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700170
171 return rc;
172}
173
174static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
175{
176 int rc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700177 u8 value[NUM_8_BIT_RTC_REGS];
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700178 unsigned long secs;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700179 unsigned int reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700180 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700181 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700182
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700183 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700184 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700185 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 Varbanovc8d523a2014-10-29 14:50:33 -0700193 rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700194 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 Varbanovc8d523a2014-10-29 14:50:33 -0700200 rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700201 value, sizeof(value));
202 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700203 dev_err(dev, "RTC read data register failed\n");
204 return rc;
205 }
206 }
207
Colin Ian Kinge4228082019-02-06 10:31:02 +0000208 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
209 ((unsigned long)value[3] << 24);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700210
Alexandre Belloni4c470b22020-03-06 08:37:57 +0100211 rtc_time64_to_tm(secs, tm);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700212
Andy Shevchenko4f5ef6e2018-12-04 23:23:20 +0200213 dev_dbg(dev, "secs = %lu, h:m:s == %ptRt, y-m-d = %ptRdr\n", secs, tm, tm);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700214
215 return 0;
216}
217
218static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
219{
220 int rc, i;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700221 u8 value[NUM_8_BIT_RTC_REGS];
222 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700223 unsigned long secs, irq_flags;
224 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700225 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700226
Alexandre Belloni4c470b22020-03-06 08:37:57 +0100227 secs = rtc_tm_to_time64(&alarm->time);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700228
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 Varbanovc8d523a2014-10-29 14:50:33 -0700236 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700237 sizeof(value));
238 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700239 dev_err(dev, "Write to RTC ALARM register failed\n");
240 goto rtc_rw_fail;
241 }
242
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700243 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
244 if (rc)
245 goto rtc_rw_fail;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700246
247 if (alarm->enabled)
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700248 ctrl_reg |= regs->alarm_en;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700249 else
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700250 ctrl_reg &= ~regs->alarm_en;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700251
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700252 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700253 if (rc) {
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700254 dev_err(dev, "Write to RTC alarm control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700255 goto rtc_rw_fail;
256 }
257
Andy Shevchenko4f5ef6e2018-12-04 23:23:20 +0200258 dev_dbg(dev, "Alarm Set for h:m:s=%ptRt, y-m-d=%ptRdr\n",
259 &alarm->time, &alarm->time);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700260rtc_rw_fail:
261 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
262 return rc;
263}
264
265static 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 Varbanovc8d523a2014-10-29 14:50:33 -0700271 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700272
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700273 rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700274 sizeof(value));
275 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700276 dev_err(dev, "RTC alarm time read failed\n");
277 return rc;
278 }
279
Colin Ian Kinge4228082019-02-06 10:31:02 +0000280 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
281 ((unsigned long)value[3] << 24);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700282
Alexandre Belloni4c470b22020-03-06 08:37:57 +0100283 rtc_time64_to_tm(secs, &alarm->time);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700284
Andy Shevchenko4f5ef6e2018-12-04 23:23:20 +0200285 dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n",
286 &alarm->time, &alarm->time);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700287
288 return 0;
289}
290
291static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
292{
293 int rc;
294 unsigned long irq_flags;
295 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700296 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
297 unsigned int ctrl_reg;
韩科才34ce2972020-03-21 19:50:17 +0800298 u8 value[NUM_8_BIT_RTC_REGS] = {0};
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700299
300 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700301
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700302 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
303 if (rc)
304 goto rtc_rw_fail;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700305
306 if (enable)
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700307 ctrl_reg |= regs->alarm_en;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700308 else
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700309 ctrl_reg &= ~regs->alarm_en;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700310
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700311 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700312 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700313 dev_err(dev, "Write to RTC control register failed\n");
314 goto rtc_rw_fail;
315 }
316
韩科才34ce2972020-03-21 19:50:17 +0800317 /* Clear Alarm register */
318 if (!enable) {
319 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
320 sizeof(value));
321 if (rc) {
322 dev_err(dev, "Clear RTC ALARM register failed\n");
323 goto rtc_rw_fail;
324 }
325 }
326
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700327rtc_rw_fail:
328 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
329 return rc;
330}
331
Josh Cartwright5a418552014-04-03 14:50:13 -0700332static const struct rtc_class_ops pm8xxx_rtc_ops = {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700333 .read_time = pm8xxx_rtc_read_time,
Josh Cartwright5a418552014-04-03 14:50:13 -0700334 .set_time = pm8xxx_rtc_set_time,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700335 .set_alarm = pm8xxx_rtc_set_alarm,
336 .read_alarm = pm8xxx_rtc_read_alarm,
337 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
338};
339
340static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
341{
342 struct pm8xxx_rtc *rtc_dd = dev_id;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700343 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700344 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700345 int rc;
346 unsigned long irq_flags;
347
348 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
349
350 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
351
352 /* Clear the alarm enable bit */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700353 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
354 if (rc) {
355 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
356 goto rtc_alarm_handled;
357 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700358
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700359 ctrl_reg &= ~regs->alarm_en;
360
361 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700362 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700363 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700364 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700365 "Write to alarm control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700366 goto rtc_alarm_handled;
367 }
368
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700369 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
370
371 /* Clear RTC alarm register */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700372 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700373 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700374 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700375 "RTC Alarm control2 register read failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700376 goto rtc_alarm_handled;
377 }
378
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700379 ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
380 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700381 if (rc)
Josh Cartwright5bed8112014-04-03 14:50:10 -0700382 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700383 "Write to RTC Alarm control2 register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700384
385rtc_alarm_handled:
386 return IRQ_HANDLED;
387}
388
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700389static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
390{
391 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
392 unsigned int ctrl_reg;
393 int rc;
394
395 /* Check if the RTC is on, else turn it on */
396 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
397 if (rc)
398 return rc;
399
400 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
401 ctrl_reg |= PM8xxx_RTC_ENABLE;
402 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
403 if (rc)
404 return rc;
405 }
406
407 return 0;
408}
409
410static const struct pm8xxx_rtc_regs pm8921_regs = {
411 .ctrl = 0x11d,
412 .write = 0x11f,
413 .read = 0x123,
414 .alarm_rw = 0x127,
415 .alarm_ctrl = 0x11d,
416 .alarm_ctrl2 = 0x11e,
417 .alarm_en = BIT(1),
418};
419
420static const struct pm8xxx_rtc_regs pm8058_regs = {
421 .ctrl = 0x1e8,
422 .write = 0x1ea,
423 .read = 0x1ee,
424 .alarm_rw = 0x1f2,
425 .alarm_ctrl = 0x1e8,
426 .alarm_ctrl2 = 0x1e9,
427 .alarm_en = BIT(1),
428};
429
430static const struct pm8xxx_rtc_regs pm8941_regs = {
431 .ctrl = 0x6046,
432 .write = 0x6040,
433 .read = 0x6048,
434 .alarm_rw = 0x6140,
435 .alarm_ctrl = 0x6146,
436 .alarm_ctrl2 = 0x6148,
437 .alarm_en = BIT(7),
438};
439
Josh Cartwright5a418552014-04-03 14:50:13 -0700440/*
441 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
442 */
443static const struct of_device_id pm8xxx_id_table[] = {
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700444 { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
Neil Armstrong08655bc2016-08-11 15:16:44 +0200445 { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs },
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700446 { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
447 { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
Josh Cartwright5a418552014-04-03 14:50:13 -0700448 { },
449};
450MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
451
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800452static int pm8xxx_rtc_probe(struct platform_device *pdev)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700453{
454 int rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700455 struct pm8xxx_rtc *rtc_dd;
Josh Cartwright5a418552014-04-03 14:50:13 -0700456 const struct of_device_id *match;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700457
Josh Cartwright5a418552014-04-03 14:50:13 -0700458 match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
459 if (!match)
460 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700461
Jingoo Hanc4172992013-07-03 15:07:09 -0700462 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
Jingoo Han49ae4252014-04-03 14:49:43 -0700463 if (rtc_dd == NULL)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700464 return -ENOMEM;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700465
466 /* Initialise spinlock to protect RTC control register */
467 spin_lock_init(&rtc_dd->ctrl_reg_lock);
468
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700469 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
470 if (!rtc_dd->regmap) {
471 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
472 return -ENXIO;
473 }
474
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700475 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700476 if (rtc_dd->rtc_alarm_irq < 0)
Jingoo Hanc4172992013-07-03 15:07:09 -0700477 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700478
Josh Cartwright5a418552014-04-03 14:50:13 -0700479 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
480 "allow-set-time");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700481
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700482 rtc_dd->regs = match->data;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700483 rtc_dd->rtc_dev = &pdev->dev;
484
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700485 rc = pm8xxx_rtc_enable(rtc_dd);
486 if (rc)
Jingoo Hanc4172992013-07-03 15:07:09 -0700487 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700488
489 platform_set_drvdata(pdev, rtc_dd);
490
Josh Cartwrightfda99092014-04-03 14:50:14 -0700491 device_init_wakeup(&pdev->dev, 1);
492
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700493 /* Register the RTC device */
Alexandre Bellonid5d55b72020-03-06 08:37:55 +0100494 rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
495 if (IS_ERR(rtc_dd->rtc))
Jingoo Hanc4172992013-07-03 15:07:09 -0700496 return PTR_ERR(rtc_dd->rtc);
Alexandre Bellonid5d55b72020-03-06 08:37:55 +0100497
498 rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
Alexandre Belloni3cfe5262020-03-06 08:37:56 +0100499 rtc_dd->rtc->range_max = U32_MAX;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700500
501 /* Request the alarm IRQ */
Josh Cartwrightbffcbc02014-04-03 14:50:12 -0700502 rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
503 pm8xxx_alarm_trigger,
504 IRQF_TRIGGER_RISING,
505 "pm8xxx_rtc_alarm", rtc_dd);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700506 if (rc < 0) {
507 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
Jingoo Hanc4172992013-07-03 15:07:09 -0700508 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700509 }
510
Alexandre Bellonid5d55b72020-03-06 08:37:55 +0100511 return rtc_register_device(rtc_dd->rtc);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700512}
513
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700514#ifdef CONFIG_PM_SLEEP
515static int pm8xxx_rtc_resume(struct device *dev)
516{
517 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
518
519 if (device_may_wakeup(dev))
520 disable_irq_wake(rtc_dd->rtc_alarm_irq);
521
522 return 0;
523}
524
525static int pm8xxx_rtc_suspend(struct device *dev)
526{
527 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
528
529 if (device_may_wakeup(dev))
530 enable_irq_wake(rtc_dd->rtc_alarm_irq);
531
532 return 0;
533}
534#endif
535
Josh Cartwright5bed8112014-04-03 14:50:10 -0700536static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops,
537 pm8xxx_rtc_suspend,
538 pm8xxx_rtc_resume);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700539
540static struct platform_driver pm8xxx_rtc_driver = {
541 .probe = pm8xxx_rtc_probe,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700542 .driver = {
Josh Cartwright5a418552014-04-03 14:50:13 -0700543 .name = "rtc-pm8xxx",
Josh Cartwright5a418552014-04-03 14:50:13 -0700544 .pm = &pm8xxx_rtc_pm_ops,
545 .of_match_table = pm8xxx_id_table,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700546 },
547};
548
Axel Lin0c4eae62012-01-10 15:10:48 -0800549module_platform_driver(pm8xxx_rtc_driver);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700550
551MODULE_ALIAS("platform:rtc-pm8xxx");
552MODULE_DESCRIPTION("PMIC8xxx RTC driver");
553MODULE_LICENSE("GPL v2");
554MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");