blob: 29358a04592581c537d4d30fed775ecb81222175 [file] [log] [blame]
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
Josh Cartwright5a418552014-04-03 14:50:13 -070012#include <linux/of.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/rtc.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070016#include <linux/platform_device.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070017#include <linux/pm.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070018#include <linux/regmap.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070019#include <linux/slab.h>
20#include <linux/spinlock.h>
21
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070022/* RTC Register offsets from RTC CTRL REG */
23#define PM8XXX_ALARM_CTRL_OFFSET 0x01
24#define PM8XXX_RTC_WRITE_OFFSET 0x02
25#define PM8XXX_RTC_READ_OFFSET 0x06
26#define PM8XXX_ALARM_RW_OFFSET 0x0A
27
28/* RTC_CTRL register bit fields */
29#define PM8xxx_RTC_ENABLE BIT(7)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070030#define PM8xxx_RTC_ALARM_CLEAR BIT(0)
31
32#define NUM_8_BIT_RTC_REGS 0x4
33
34/**
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070035 * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
36 * @ctrl: base address of control register
37 * @write: base address of write register
38 * @read: base address of read register
39 * @alarm_ctrl: base address of alarm control register
40 * @alarm_ctrl2: base address of alarm control2 register
41 * @alarm_rw: base address of alarm read-write register
42 * @alarm_en: alarm enable mask
43 */
44struct pm8xxx_rtc_regs {
45 unsigned int ctrl;
46 unsigned int write;
47 unsigned int read;
48 unsigned int alarm_ctrl;
49 unsigned int alarm_ctrl2;
50 unsigned int alarm_rw;
51 unsigned int alarm_en;
52};
53
54/**
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070055 * struct pm8xxx_rtc - rtc driver internal structure
56 * @rtc: rtc device for this driver.
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070057 * @regmap: regmap used to access RTC registers
Josh Cartwright5a418552014-04-03 14:50:13 -070058 * @allow_set_time: indicates whether writing to the RTC is allowed
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070059 * @rtc_alarm_irq: rtc alarm irq number.
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070060 * @ctrl_reg: rtc control register.
61 * @rtc_dev: device structure.
62 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
63 */
64struct pm8xxx_rtc {
65 struct rtc_device *rtc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070066 struct regmap *regmap;
Josh Cartwright5a418552014-04-03 14:50:13 -070067 bool allow_set_time;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070068 int rtc_alarm_irq;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070069 const struct pm8xxx_rtc_regs *regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070070 struct device *rtc_dev;
71 spinlock_t ctrl_reg_lock;
72};
73
74/*
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070075 * Steps to write the RTC registers.
76 * 1. Disable alarm if enabled.
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053077 * 2. Disable rtc if enabled.
78 * 3. Write 0x00 to LSB.
79 * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
80 * 5. Enable rtc if disabled in step 2.
81 * 6. Enable alarm if disabled in step 1.
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070082 */
83static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
84{
85 int rc, i;
86 unsigned long secs, irq_flags;
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053087 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0;
88 unsigned int ctrl_reg, rtc_ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070089 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -070090 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070091
Josh Cartwright5a418552014-04-03 14:50:13 -070092 if (!rtc_dd->allow_set_time)
93 return -EACCES;
94
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070095 rtc_tm_to_time(tm, &secs);
96
Mohit Aggarwal83220bf2018-03-05 14:35:58 +053097 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
98
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070099 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
100 value[i] = secs & 0xFF;
101 secs >>= 8;
102 }
103
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700104 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700105
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530106 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700107 if (rc)
108 goto rtc_rw_fail;
109
110 if (ctrl_reg & regs->alarm_en) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700111 alarm_enabled = 1;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700112 ctrl_reg &= ~regs->alarm_en;
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530113 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
114 if (rc) {
115 dev_err(dev, "Write to RTC Alarm control register failed\n");
116 goto rtc_rw_fail;
117 }
118 }
119
120 /* Disable RTC H/w before writing on RTC register */
121 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg);
122 if (rc)
123 goto rtc_rw_fail;
124
125 if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) {
126 rtc_disabled = 1;
127 rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE;
128 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700129 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700130 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700131 goto rtc_rw_fail;
132 }
Josh Cartwright5bed8112014-04-03 14:50:10 -0700133 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700134
135 /* Write 0 to Byte[0] */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700136 rc = regmap_write(rtc_dd->regmap, regs->write, 0);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700137 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[1], Byte[2], Byte[3] */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700143 rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700144 &value[1], sizeof(value) - 1);
145 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700146 dev_err(dev, "Write to RTC write data register failed\n");
147 goto rtc_rw_fail;
148 }
149
150 /* Write Byte[0] */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700151 rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700152 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700153 dev_err(dev, "Write to RTC write data register failed\n");
154 goto rtc_rw_fail;
155 }
156
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530157 /* Enable RTC H/w after writing on RTC register */
158 if (rtc_disabled) {
159 rtc_ctrl_reg |= PM8xxx_RTC_ENABLE;
160 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700161 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700162 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700163 goto rtc_rw_fail;
164 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700165 }
166
Mohit Aggarwal83220bf2018-03-05 14:35:58 +0530167 if (alarm_enabled) {
168 ctrl_reg |= regs->alarm_en;
169 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
170 if (rc) {
171 dev_err(dev, "Write to RTC Alarm control register failed\n");
172 goto rtc_rw_fail;
173 }
174 }
175
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700176rtc_rw_fail:
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700177 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700178
179 return rc;
180}
181
182static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
183{
184 int rc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700185 u8 value[NUM_8_BIT_RTC_REGS];
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700186 unsigned long secs;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700187 unsigned int reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700188 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700189 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700190
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700191 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700192 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700193 dev_err(dev, "RTC read data register failed\n");
194 return rc;
195 }
196
197 /*
198 * Read the LSB again and check if there has been a carry over.
199 * If there is, redo the read operation.
200 */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700201 rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700202 if (rc < 0) {
203 dev_err(dev, "RTC read data register failed\n");
204 return rc;
205 }
206
207 if (unlikely(reg < value[0])) {
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700208 rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700209 value, sizeof(value));
210 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700211 dev_err(dev, "RTC read data register failed\n");
212 return rc;
213 }
214 }
215
216 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
217
218 rtc_time_to_tm(secs, tm);
219
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700220 dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700221 secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
222 tm->tm_mday, tm->tm_mon, tm->tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700223
224 return 0;
225}
226
227static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
228{
229 int rc, i;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700230 u8 value[NUM_8_BIT_RTC_REGS];
231 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700232 unsigned long secs, irq_flags;
233 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700234 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700235
236 rtc_tm_to_time(&alarm->time, &secs);
237
238 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
239 value[i] = secs & 0xFF;
240 secs >>= 8;
241 }
242
243 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
244
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700245 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700246 sizeof(value));
247 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700248 dev_err(dev, "Write to RTC ALARM register failed\n");
249 goto rtc_rw_fail;
250 }
251
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700252 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
253 if (rc)
254 goto rtc_rw_fail;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700255
256 if (alarm->enabled)
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700257 ctrl_reg |= regs->alarm_en;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700258 else
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700259 ctrl_reg &= ~regs->alarm_en;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700260
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700261 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700262 if (rc) {
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700263 dev_err(dev, "Write to RTC alarm control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700264 goto rtc_rw_fail;
265 }
266
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700267 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700268 alarm->time.tm_hour, alarm->time.tm_min,
269 alarm->time.tm_sec, alarm->time.tm_mday,
270 alarm->time.tm_mon, alarm->time.tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700271rtc_rw_fail:
272 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
273 return rc;
274}
275
276static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
277{
278 int rc;
279 u8 value[NUM_8_BIT_RTC_REGS];
280 unsigned long secs;
281 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700282 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700283
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700284 rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700285 sizeof(value));
286 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700287 dev_err(dev, "RTC alarm time read failed\n");
288 return rc;
289 }
290
291 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
292
293 rtc_time_to_tm(secs, &alarm->time);
294
295 rc = rtc_valid_tm(&alarm->time);
296 if (rc < 0) {
297 dev_err(dev, "Invalid alarm time read from RTC\n");
298 return rc;
299 }
300
301 dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700302 alarm->time.tm_hour, alarm->time.tm_min,
303 alarm->time.tm_sec, alarm->time.tm_mday,
304 alarm->time.tm_mon, alarm->time.tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700305
306 return 0;
307}
308
309static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
310{
311 int rc;
312 unsigned long irq_flags;
313 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700314 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
315 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700316
317 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700318
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700319 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
320 if (rc)
321 goto rtc_rw_fail;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700322
323 if (enable)
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700324 ctrl_reg |= regs->alarm_en;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700325 else
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700326 ctrl_reg &= ~regs->alarm_en;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700327
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700328 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700329 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700330 dev_err(dev, "Write to RTC control register failed\n");
331 goto rtc_rw_fail;
332 }
333
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700334rtc_rw_fail:
335 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
336 return rc;
337}
338
Josh Cartwright5a418552014-04-03 14:50:13 -0700339static const struct rtc_class_ops pm8xxx_rtc_ops = {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700340 .read_time = pm8xxx_rtc_read_time,
Josh Cartwright5a418552014-04-03 14:50:13 -0700341 .set_time = pm8xxx_rtc_set_time,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700342 .set_alarm = pm8xxx_rtc_set_alarm,
343 .read_alarm = pm8xxx_rtc_read_alarm,
344 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
345};
346
347static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
348{
349 struct pm8xxx_rtc *rtc_dd = dev_id;
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700350 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700351 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700352 int rc;
353 unsigned long irq_flags;
354
355 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
356
357 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
358
359 /* Clear the alarm enable bit */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700360 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
361 if (rc) {
362 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
363 goto rtc_alarm_handled;
364 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700365
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700366 ctrl_reg &= ~regs->alarm_en;
367
368 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700369 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700370 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700371 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700372 "Write to alarm control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700373 goto rtc_alarm_handled;
374 }
375
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700376 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
377
378 /* Clear RTC alarm register */
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700379 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700380 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700381 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700382 "RTC Alarm control2 register read failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700383 goto rtc_alarm_handled;
384 }
385
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700386 ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
387 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700388 if (rc)
Josh Cartwright5bed8112014-04-03 14:50:10 -0700389 dev_err(rtc_dd->rtc_dev,
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700390 "Write to RTC Alarm control2 register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700391
392rtc_alarm_handled:
393 return IRQ_HANDLED;
394}
395
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700396static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
397{
398 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
399 unsigned int ctrl_reg;
400 int rc;
401
402 /* Check if the RTC is on, else turn it on */
403 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
404 if (rc)
405 return rc;
406
407 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
408 ctrl_reg |= PM8xxx_RTC_ENABLE;
409 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
410 if (rc)
411 return rc;
412 }
413
414 return 0;
415}
416
417static const struct pm8xxx_rtc_regs pm8921_regs = {
418 .ctrl = 0x11d,
419 .write = 0x11f,
420 .read = 0x123,
421 .alarm_rw = 0x127,
422 .alarm_ctrl = 0x11d,
423 .alarm_ctrl2 = 0x11e,
424 .alarm_en = BIT(1),
425};
426
427static const struct pm8xxx_rtc_regs pm8058_regs = {
428 .ctrl = 0x1e8,
429 .write = 0x1ea,
430 .read = 0x1ee,
431 .alarm_rw = 0x1f2,
432 .alarm_ctrl = 0x1e8,
433 .alarm_ctrl2 = 0x1e9,
434 .alarm_en = BIT(1),
435};
436
437static const struct pm8xxx_rtc_regs pm8941_regs = {
438 .ctrl = 0x6046,
439 .write = 0x6040,
440 .read = 0x6048,
441 .alarm_rw = 0x6140,
442 .alarm_ctrl = 0x6146,
443 .alarm_ctrl2 = 0x6148,
444 .alarm_en = BIT(7),
445};
446
Josh Cartwright5a418552014-04-03 14:50:13 -0700447/*
448 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
449 */
450static const struct of_device_id pm8xxx_id_table[] = {
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700451 { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
Neil Armstrong08655bc2016-08-11 15:16:44 +0200452 { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs },
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700453 { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
454 { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
Josh Cartwright5a418552014-04-03 14:50:13 -0700455 { },
456};
457MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
458
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800459static int pm8xxx_rtc_probe(struct platform_device *pdev)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700460{
461 int rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700462 struct pm8xxx_rtc *rtc_dd;
Josh Cartwright5a418552014-04-03 14:50:13 -0700463 const struct of_device_id *match;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700464
Josh Cartwright5a418552014-04-03 14:50:13 -0700465 match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
466 if (!match)
467 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700468
Jingoo Hanc4172992013-07-03 15:07:09 -0700469 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
Jingoo Han49ae4252014-04-03 14:49:43 -0700470 if (rtc_dd == NULL)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700471 return -ENOMEM;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700472
473 /* Initialise spinlock to protect RTC control register */
474 spin_lock_init(&rtc_dd->ctrl_reg_lock);
475
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700476 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
477 if (!rtc_dd->regmap) {
478 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
479 return -ENXIO;
480 }
481
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700482 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
483 if (rtc_dd->rtc_alarm_irq < 0) {
484 dev_err(&pdev->dev, "Alarm IRQ resource absent!\n");
Jingoo Hanc4172992013-07-03 15:07:09 -0700485 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700486 }
487
Josh Cartwright5a418552014-04-03 14:50:13 -0700488 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
489 "allow-set-time");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700490
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700491 rtc_dd->regs = match->data;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700492 rtc_dd->rtc_dev = &pdev->dev;
493
Stanimir Varbanovc8d523a2014-10-29 14:50:33 -0700494 rc = pm8xxx_rtc_enable(rtc_dd);
495 if (rc)
Jingoo Hanc4172992013-07-03 15:07:09 -0700496 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700497
498 platform_set_drvdata(pdev, rtc_dd);
499
Josh Cartwrightfda99092014-04-03 14:50:14 -0700500 device_init_wakeup(&pdev->dev, 1);
501
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700502 /* Register the RTC device */
Jingoo Hanc4172992013-07-03 15:07:09 -0700503 rtc_dd->rtc = devm_rtc_device_register(&pdev->dev, "pm8xxx_rtc",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700504 &pm8xxx_rtc_ops, THIS_MODULE);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700505 if (IS_ERR(rtc_dd->rtc)) {
506 dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700507 __func__, PTR_ERR(rtc_dd->rtc));
Jingoo Hanc4172992013-07-03 15:07:09 -0700508 return PTR_ERR(rtc_dd->rtc);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700509 }
510
511 /* Request the alarm IRQ */
Josh Cartwrightbffcbc02014-04-03 14:50:12 -0700512 rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
513 pm8xxx_alarm_trigger,
514 IRQF_TRIGGER_RISING,
515 "pm8xxx_rtc_alarm", rtc_dd);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700516 if (rc < 0) {
517 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
Jingoo Hanc4172992013-07-03 15:07:09 -0700518 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700519 }
520
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700521 dev_dbg(&pdev->dev, "Probe success !!\n");
522
523 return 0;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700524}
525
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700526#ifdef CONFIG_PM_SLEEP
527static int pm8xxx_rtc_resume(struct device *dev)
528{
529 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
530
531 if (device_may_wakeup(dev))
532 disable_irq_wake(rtc_dd->rtc_alarm_irq);
533
534 return 0;
535}
536
537static int pm8xxx_rtc_suspend(struct device *dev)
538{
539 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
540
541 if (device_may_wakeup(dev))
542 enable_irq_wake(rtc_dd->rtc_alarm_irq);
543
544 return 0;
545}
546#endif
547
Josh Cartwright5bed8112014-04-03 14:50:10 -0700548static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops,
549 pm8xxx_rtc_suspend,
550 pm8xxx_rtc_resume);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700551
552static struct platform_driver pm8xxx_rtc_driver = {
553 .probe = pm8xxx_rtc_probe,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700554 .driver = {
Josh Cartwright5a418552014-04-03 14:50:13 -0700555 .name = "rtc-pm8xxx",
Josh Cartwright5a418552014-04-03 14:50:13 -0700556 .pm = &pm8xxx_rtc_pm_ops,
557 .of_match_table = pm8xxx_id_table,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700558 },
559};
560
Axel Lin0c4eae62012-01-10 15:10:48 -0800561module_platform_driver(pm8xxx_rtc_driver);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700562
563MODULE_ALIAS("platform:rtc-pm8xxx");
564MODULE_DESCRIPTION("PMIC8xxx RTC driver");
565MODULE_LICENSE("GPL v2");
566MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");