Get rid of 'remove_new' relic from platform driver struct
[linux.git] / arch / um / drivers / rtc_kern.c
blob134a58f93c859d658dc2d9798037cac5de65b914
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020 Intel Corporation
4 * Author: Johannes Berg <johannes@sipsolutions.net>
5 */
6 #include <linux/platform_device.h>
7 #include <linux/time-internal.h>
8 #include <linux/suspend.h>
9 #include <linux/err.h>
10 #include <linux/rtc.h>
11 #include <kern_util.h>
12 #include <irq_kern.h>
13 #include <os.h>
14 #include "rtc.h"
16 static time64_t uml_rtc_alarm_time;
17 static bool uml_rtc_alarm_enabled;
18 static struct rtc_device *uml_rtc;
19 static int uml_rtc_irq_fd, uml_rtc_irq;
21 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
23 static void uml_rtc_time_travel_alarm(struct time_travel_event *ev)
25 uml_rtc_send_timetravel_alarm();
28 static struct time_travel_event uml_rtc_alarm_event = {
29 .fn = uml_rtc_time_travel_alarm,
31 #endif
33 static int uml_rtc_read_time(struct device *dev, struct rtc_time *tm)
35 struct timespec64 ts;
37 /* Use this to get correct time in time-travel mode */
38 read_persistent_clock64(&ts);
39 rtc_time64_to_tm(timespec64_to_ktime(ts) / NSEC_PER_SEC, tm);
41 return 0;
44 static int uml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
46 rtc_time64_to_tm(uml_rtc_alarm_time, &alrm->time);
47 alrm->enabled = uml_rtc_alarm_enabled;
49 return 0;
52 static int uml_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
54 unsigned long long secs;
56 if (!enable && !uml_rtc_alarm_enabled)
57 return 0;
59 uml_rtc_alarm_enabled = enable;
61 secs = uml_rtc_alarm_time - ktime_get_real_seconds();
63 if (time_travel_mode == TT_MODE_OFF) {
64 if (!enable) {
65 uml_rtc_disable_alarm();
66 return 0;
69 /* enable or update */
70 return uml_rtc_enable_alarm(secs);
71 } else {
72 time_travel_del_event(&uml_rtc_alarm_event);
74 if (enable)
75 time_travel_add_event_rel(&uml_rtc_alarm_event,
76 secs * NSEC_PER_SEC);
79 return 0;
82 static int uml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
84 uml_rtc_alarm_irq_enable(dev, 0);
85 uml_rtc_alarm_time = rtc_tm_to_time64(&alrm->time);
86 uml_rtc_alarm_irq_enable(dev, alrm->enabled);
88 return 0;
91 static const struct rtc_class_ops uml_rtc_ops = {
92 .read_time = uml_rtc_read_time,
93 .read_alarm = uml_rtc_read_alarm,
94 .alarm_irq_enable = uml_rtc_alarm_irq_enable,
95 .set_alarm = uml_rtc_set_alarm,
98 static irqreturn_t uml_rtc_interrupt(int irq, void *data)
100 unsigned long long c = 0;
102 /* alarm triggered, it's now off */
103 uml_rtc_alarm_enabled = false;
105 os_read_file(uml_rtc_irq_fd, &c, sizeof(c));
106 WARN_ON(c == 0);
108 pm_system_wakeup();
109 rtc_update_irq(uml_rtc, 1, RTC_IRQF | RTC_AF);
111 return IRQ_HANDLED;
114 static int uml_rtc_setup(void)
116 int err;
118 err = uml_rtc_start(time_travel_mode != TT_MODE_OFF);
119 if (WARN(err < 0, "err = %d\n", err))
120 return err;
122 uml_rtc_irq_fd = err;
124 err = um_request_irq(UM_IRQ_ALLOC, uml_rtc_irq_fd, IRQ_READ,
125 uml_rtc_interrupt, 0, "rtc", NULL);
126 if (err < 0) {
127 uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
128 return err;
131 irq_set_irq_wake(err, 1);
133 uml_rtc_irq = err;
134 return 0;
137 static void uml_rtc_cleanup(void)
139 um_free_irq(uml_rtc_irq, NULL);
140 uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
143 static int uml_rtc_probe(struct platform_device *pdev)
145 int err;
147 err = uml_rtc_setup();
148 if (err)
149 return err;
151 uml_rtc = devm_rtc_allocate_device(&pdev->dev);
152 if (IS_ERR(uml_rtc)) {
153 err = PTR_ERR(uml_rtc);
154 goto cleanup;
157 uml_rtc->ops = &uml_rtc_ops;
159 device_init_wakeup(&pdev->dev, 1);
161 err = devm_rtc_register_device(uml_rtc);
162 if (err)
163 goto cleanup;
165 return 0;
166 cleanup:
167 uml_rtc_cleanup();
168 return err;
171 static void uml_rtc_remove(struct platform_device *pdev)
173 device_init_wakeup(&pdev->dev, 0);
174 uml_rtc_cleanup();
177 static struct platform_driver uml_rtc_driver = {
178 .probe = uml_rtc_probe,
179 .remove = uml_rtc_remove,
180 .driver = {
181 .name = "uml-rtc",
185 static int __init uml_rtc_init(void)
187 struct platform_device *pdev;
188 int err;
190 err = platform_driver_register(&uml_rtc_driver);
191 if (err)
192 return err;
194 pdev = platform_device_alloc("uml-rtc", 0);
195 if (!pdev) {
196 err = -ENOMEM;
197 goto unregister;
200 err = platform_device_add(pdev);
201 if (err)
202 goto unregister;
203 return 0;
205 unregister:
206 platform_device_put(pdev);
207 platform_driver_unregister(&uml_rtc_driver);
208 return err;
210 device_initcall(uml_rtc_init);