blob: dae527f8c58e48cbce93cc87645c76f53a1733bb [file] [log] [blame]
Qiao Zhou2985c292012-07-09 14:37:34 +08001/*
2 * Real Time Clock driver for Marvell 88PM80x PMIC
3 *
4 * Copyright (c) 2012 Marvell International Ltd.
5 * Wenzeng Chen<wzch@marvell.com>
6 * Qiao Zhou <zhouqiao@marvell.com>
7 *
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file "COPYING" in the main directory of this
10 * archive for more details.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/regmap.h>
26#include <linux/mfd/core.h>
27#include <linux/mfd/88pm80x.h>
28#include <linux/rtc.h>
29
30#define PM800_RTC_COUNTER1 (0xD1)
31#define PM800_RTC_COUNTER2 (0xD2)
32#define PM800_RTC_COUNTER3 (0xD3)
33#define PM800_RTC_COUNTER4 (0xD4)
34#define PM800_RTC_EXPIRE1_1 (0xD5)
35#define PM800_RTC_EXPIRE1_2 (0xD6)
36#define PM800_RTC_EXPIRE1_3 (0xD7)
37#define PM800_RTC_EXPIRE1_4 (0xD8)
38#define PM800_RTC_TRIM1 (0xD9)
39#define PM800_RTC_TRIM2 (0xDA)
40#define PM800_RTC_TRIM3 (0xDB)
41#define PM800_RTC_TRIM4 (0xDC)
42#define PM800_RTC_EXPIRE2_1 (0xDD)
43#define PM800_RTC_EXPIRE2_2 (0xDE)
44#define PM800_RTC_EXPIRE2_3 (0xDF)
45#define PM800_RTC_EXPIRE2_4 (0xE0)
46
47#define PM800_POWER_DOWN_LOG1 (0xE5)
48#define PM800_POWER_DOWN_LOG2 (0xE6)
49
50struct pm80x_rtc_info {
51 struct pm80x_chip *chip;
52 struct regmap *map;
53 struct rtc_device *rtc_dev;
54 struct device *dev;
Qiao Zhou2985c292012-07-09 14:37:34 +080055
56 int irq;
Qiao Zhou2985c292012-07-09 14:37:34 +080057};
58
59static irqreturn_t rtc_update_handler(int irq, void *data)
60{
61 struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
62 int mask;
63
64 mask = PM800_ALARM | PM800_ALARM_WAKEUP;
65 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
66 mask);
67 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
68 return IRQ_HANDLED;
69}
70
71static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
72{
73 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
74
75 if (enabled)
76 regmap_update_bits(info->map, PM800_RTC_CONTROL,
77 PM800_ALARM1_EN, PM800_ALARM1_EN);
78 else
79 regmap_update_bits(info->map, PM800_RTC_CONTROL,
80 PM800_ALARM1_EN, 0);
81 return 0;
82}
83
84/*
85 * Calculate the next alarm time given the requested alarm time mask
86 * and the current time.
87 */
88static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
89 struct rtc_time *alrm)
90{
91 unsigned long next_time;
92 unsigned long now_time;
93
94 next->tm_year = now->tm_year;
95 next->tm_mon = now->tm_mon;
96 next->tm_mday = now->tm_mday;
97 next->tm_hour = alrm->tm_hour;
98 next->tm_min = alrm->tm_min;
99 next->tm_sec = alrm->tm_sec;
100
101 rtc_tm_to_time(now, &now_time);
102 rtc_tm_to_time(next, &next_time);
103
104 if (next_time < now_time) {
105 /* Advance one day */
106 next_time += 60 * 60 * 24;
107 rtc_time_to_tm(next_time, next);
108 }
109}
110
111static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
112{
113 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
114 unsigned char buf[4];
115 unsigned long ticks, base, data;
116 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
117 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
118 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
119
120 /* load 32-bit read-only counter */
121 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
122 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
123 ticks = base + data;
124 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
125 base, data, ticks);
126 rtc_time_to_tm(ticks, tm);
127 return 0;
128}
129
130static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
131{
132 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
133 unsigned char buf[4];
134 unsigned long ticks, base, data;
Alexandre Bellonidf15ee12018-03-10 14:29:28 +0100135 if (tm->tm_year > 206) {
Qiao Zhou2985c292012-07-09 14:37:34 +0800136 dev_dbg(info->dev,
Alexandre Bellonidf15ee12018-03-10 14:29:28 +0100137 "Set time %d out of range. Please set time between 1970 to 2106.\n",
Qiao Zhou2985c292012-07-09 14:37:34 +0800138 1900 + tm->tm_year);
139 return -EINVAL;
140 }
141 rtc_tm_to_time(tm, &ticks);
142
143 /* load 32-bit read-only counter */
144 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
145 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
146 base = ticks - data;
147 dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
148 base, data, ticks);
149 buf[0] = base & 0xFF;
150 buf[1] = (base >> 8) & 0xFF;
151 buf[2] = (base >> 16) & 0xFF;
152 buf[3] = (base >> 24) & 0xFF;
153 regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
154
155 return 0;
156}
157
158static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
159{
160 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
161 unsigned char buf[4];
162 unsigned long ticks, base, data;
163 int ret;
164
165 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
166 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
167 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
168
169 regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
170 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
171 ticks = base + data;
172 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
173 base, data, ticks);
174
175 rtc_time_to_tm(ticks, &alrm->time);
176 regmap_read(info->map, PM800_RTC_CONTROL, &ret);
177 alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
178 alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
179 return 0;
180}
181
182static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
183{
184 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
185 struct rtc_time now_tm, alarm_tm;
186 unsigned long ticks, base, data;
187 unsigned char buf[4];
188 int mask;
189
190 regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
191
192 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
193 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
194 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
195
196 /* load 32-bit read-only counter */
197 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
198 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
199 ticks = base + data;
200 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
201 base, data, ticks);
202
203 rtc_time_to_tm(ticks, &now_tm);
204 dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
205 rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
206 /* get new ticks for alarm in 24 hours */
207 rtc_tm_to_time(&alarm_tm, &ticks);
208 dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
209 data = ticks - base;
210
211 buf[0] = data & 0xff;
212 buf[1] = (data >> 8) & 0xff;
213 buf[2] = (data >> 16) & 0xff;
214 buf[3] = (data >> 24) & 0xff;
215 regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
216 if (alrm->enabled) {
217 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
218 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
219 } else {
220 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
221 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
222 PM800_ALARM | PM800_ALARM_WAKEUP);
223 }
224 return 0;
225}
226
227static const struct rtc_class_ops pm80x_rtc_ops = {
228 .read_time = pm80x_rtc_read_time,
229 .set_time = pm80x_rtc_set_time,
230 .read_alarm = pm80x_rtc_read_alarm,
231 .set_alarm = pm80x_rtc_set_alarm,
232 .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
233};
234
Jingoo Han569b05a2013-04-29 16:20:08 -0700235#ifdef CONFIG_PM_SLEEP
Qiao Zhou2985c292012-07-09 14:37:34 +0800236static int pm80x_rtc_suspend(struct device *dev)
237{
238 return pm80x_dev_suspend(dev);
239}
240
241static int pm80x_rtc_resume(struct device *dev)
242{
243 return pm80x_dev_resume(dev);
244}
245#endif
246
247static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
248
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800249static int pm80x_rtc_probe(struct platform_device *pdev)
Qiao Zhou2985c292012-07-09 14:37:34 +0800250{
251 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Vaibhav Hiremath4ab82102015-07-09 12:25:51 +0530252 struct pm80x_rtc_pdata *pdata = dev_get_platdata(&pdev->dev);
Qiao Zhou2985c292012-07-09 14:37:34 +0800253 struct pm80x_rtc_info *info;
Vaibhav Hiremath4ab82102015-07-09 12:25:51 +0530254 struct device_node *node = pdev->dev.of_node;
Qiao Zhou2985c292012-07-09 14:37:34 +0800255 int ret;
256
Vaibhav Hiremath4ab82102015-07-09 12:25:51 +0530257 if (!pdata && !node) {
258 dev_err(&pdev->dev,
259 "pm80x-rtc requires platform data or of_node\n");
260 return -EINVAL;
261 }
262
263 if (!pdata) {
264 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
265 if (!pdata) {
266 dev_err(&pdev->dev, "failed to allocate memory\n");
267 return -ENOMEM;
268 }
269 }
Qiao Zhou2985c292012-07-09 14:37:34 +0800270
271 info =
272 devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
273 if (!info)
274 return -ENOMEM;
275 info->irq = platform_get_irq(pdev, 0);
276 if (info->irq < 0) {
277 dev_err(&pdev->dev, "No IRQ resource!\n");
278 ret = -EINVAL;
279 goto out;
280 }
281
282 info->chip = chip;
283 info->map = chip->regmap;
284 if (!info->map) {
285 dev_err(&pdev->dev, "no regmap!\n");
286 ret = -EINVAL;
287 goto out;
288 }
289
290 info->dev = &pdev->dev;
291 dev_set_drvdata(&pdev->dev, info);
292
Alexandre Belloni661eb892018-05-17 22:18:58 +0200293 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
294 if (IS_ERR(info->rtc_dev))
295 return PTR_ERR(info->rtc_dev);
296
Qiao Zhou2985c292012-07-09 14:37:34 +0800297 ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
298 IRQF_ONESHOT, "rtc", info);
299 if (ret < 0) {
300 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
301 info->irq, ret);
302 goto out;
303 }
304
Alexandre Belloni661eb892018-05-17 22:18:58 +0200305 info->rtc_dev->ops = &pm80x_rtc_ops;
306
307 ret = rtc_register_device(info->rtc_dev);
308 if (ret) {
Qiao Zhou2985c292012-07-09 14:37:34 +0800309 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
310 goto out_rtc;
311 }
312 /*
313 * enable internal XO instead of internal 3.25MHz clock since it can
314 * free running in PMIC power-down state.
315 */
316 regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
317 PM800_RTC1_USE_XO);
318
Vaibhav Hiremath4ab82102015-07-09 12:25:51 +0530319 /* remember whether this power up is caused by PMIC RTC or not */
320 info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
Qiao Zhou2985c292012-07-09 14:37:34 +0800321
322 device_init_wakeup(&pdev->dev, 1);
323
324 return 0;
325out_rtc:
326 pm80x_free_irq(chip, info->irq, info);
327out:
Qiao Zhou2985c292012-07-09 14:37:34 +0800328 return ret;
329}
330
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800331static int pm80x_rtc_remove(struct platform_device *pdev)
Qiao Zhou2985c292012-07-09 14:37:34 +0800332{
333 struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
Qiao Zhou2985c292012-07-09 14:37:34 +0800334 pm80x_free_irq(info->chip, info->irq, info);
Qiao Zhou2985c292012-07-09 14:37:34 +0800335 return 0;
336}
337
338static struct platform_driver pm80x_rtc_driver = {
339 .driver = {
340 .name = "88pm80x-rtc",
Qiao Zhou2985c292012-07-09 14:37:34 +0800341 .pm = &pm80x_rtc_pm_ops,
342 },
343 .probe = pm80x_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800344 .remove = pm80x_rtc_remove,
Qiao Zhou2985c292012-07-09 14:37:34 +0800345};
346
347module_platform_driver(pm80x_rtc_driver);
348
349MODULE_LICENSE("GPL");
350MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
351MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
352MODULE_ALIAS("platform:88pm80x-rtc");