xref: /linux/drivers/rtc/rtc-meson-vrtc.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 BayLibre, SAS
4  * Author: Neil Armstrong <narmstrong@baylibre.com>
5  * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6  */
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/rtc.h>
10 #include <linux/io.h>
11 #include <linux/of.h>
12 #include <linux/time64.h>
13 
14 struct meson_vrtc_data {
15 	void __iomem *io_alarm;
16 	struct rtc_device *rtc;
17 	unsigned long alarm_time;
18 	bool enabled;
19 };
20 
21 static int meson_vrtc_read_time(struct device *dev, struct rtc_time *tm)
22 {
23 	struct timespec64 time;
24 
25 	dev_dbg(dev, "%s\n", __func__);
26 	ktime_get_real_ts64(&time);
27 	rtc_time64_to_tm(time.tv_sec, tm);
28 
29 	return 0;
30 }
31 
32 static void meson_vrtc_set_wakeup_time(struct meson_vrtc_data *vrtc,
33 				       unsigned long time)
34 {
35 	writel_relaxed(time, vrtc->io_alarm);
36 }
37 
38 static int meson_vrtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
39 {
40 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
41 
42 	dev_dbg(dev, "%s: alarm->enabled=%d\n", __func__, alarm->enabled);
43 	if (alarm->enabled)
44 		vrtc->alarm_time = rtc_tm_to_time64(&alarm->time);
45 	else
46 		vrtc->alarm_time = 0;
47 
48 	return 0;
49 }
50 
51 static int meson_vrtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
52 {
53 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
54 
55 	vrtc->enabled = enabled;
56 	return 0;
57 }
58 
59 static const struct rtc_class_ops meson_vrtc_ops = {
60 	.read_time = meson_vrtc_read_time,
61 	.set_alarm = meson_vrtc_set_alarm,
62 	.alarm_irq_enable = meson_vrtc_alarm_irq_enable,
63 };
64 
65 static int meson_vrtc_probe(struct platform_device *pdev)
66 {
67 	struct meson_vrtc_data *vrtc;
68 
69 	vrtc = devm_kzalloc(&pdev->dev, sizeof(*vrtc), GFP_KERNEL);
70 	if (!vrtc)
71 		return -ENOMEM;
72 
73 	vrtc->io_alarm = devm_platform_ioremap_resource(pdev, 0);
74 	if (IS_ERR(vrtc->io_alarm))
75 		return PTR_ERR(vrtc->io_alarm);
76 
77 	device_init_wakeup(&pdev->dev, 1);
78 
79 	platform_set_drvdata(pdev, vrtc);
80 
81 	vrtc->rtc = devm_rtc_allocate_device(&pdev->dev);
82 	if (IS_ERR(vrtc->rtc))
83 		return PTR_ERR(vrtc->rtc);
84 
85 	vrtc->rtc->ops = &meson_vrtc_ops;
86 	return devm_rtc_register_device(vrtc->rtc);
87 }
88 
89 static int __maybe_unused meson_vrtc_suspend(struct device *dev)
90 {
91 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
92 
93 	dev_dbg(dev, "%s\n", __func__);
94 	if (vrtc->alarm_time) {
95 		unsigned long local_time;
96 		long alarm_secs;
97 		struct timespec64 time;
98 
99 		ktime_get_real_ts64(&time);
100 		local_time = time.tv_sec;
101 
102 		dev_dbg(dev, "alarm_time = %lus, local_time=%lus\n",
103 			vrtc->alarm_time, local_time);
104 		alarm_secs = vrtc->alarm_time - local_time;
105 		if (alarm_secs > 0) {
106 			meson_vrtc_set_wakeup_time(vrtc, alarm_secs);
107 			dev_dbg(dev, "system will wakeup in %lds.\n",
108 				alarm_secs);
109 		} else {
110 			dev_err(dev, "alarm time already passed: %lds.\n",
111 				alarm_secs);
112 		}
113 	}
114 
115 	return 0;
116 }
117 
118 static int __maybe_unused meson_vrtc_resume(struct device *dev)
119 {
120 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
121 
122 	dev_dbg(dev, "%s\n", __func__);
123 
124 	vrtc->alarm_time = 0;
125 	meson_vrtc_set_wakeup_time(vrtc, 0);
126 	return 0;
127 }
128 
129 static SIMPLE_DEV_PM_OPS(meson_vrtc_pm_ops,
130 			 meson_vrtc_suspend, meson_vrtc_resume);
131 
132 static const struct of_device_id meson_vrtc_dt_match[] = {
133 	{ .compatible = "amlogic,meson-vrtc"},
134 	{},
135 };
136 MODULE_DEVICE_TABLE(of, meson_vrtc_dt_match);
137 
138 static struct platform_driver meson_vrtc_driver = {
139 	.probe = meson_vrtc_probe,
140 	.driver = {
141 		.name = "meson-vrtc",
142 		.of_match_table = meson_vrtc_dt_match,
143 		.pm = &meson_vrtc_pm_ops,
144 	},
145 };
146 
147 module_platform_driver(meson_vrtc_driver);
148 
149 MODULE_DESCRIPTION("Amlogic Virtual Wakeup RTC Timer driver");
150 MODULE_LICENSE("GPL");
151