hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd
 */
 
#include <linux/module.h>
#include <linux/rtc.h>
#include <linux/time.h>
#include <linux/of_platform.h>
 
static struct timespec begtime;
static unsigned long time;
 
static unsigned long timespec_to_ulong(struct timespec *ts)
{
   return ts->tv_nsec < NSEC_PER_SEC / 2 ? ts->tv_sec : ts->tv_sec + 1;
}
 
static void get_uptime(struct timespec *ts)
{
   getrawmonotonic(ts);
}
 
static int fake_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
   struct timespec now, diff;
 
   get_uptime(&now);
   diff = timespec_sub(now, begtime);
 
   rtc_time_to_tm(time + timespec_to_ulong(&diff), tm);
 
   return rtc_valid_tm(tm);
}
 
static int fake_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
   get_uptime(&begtime);
   rtc_tm_to_time(tm, &time);
 
   return 0;
}
 
static int fake_rtc_alarm_irq_enable(struct device *dev,
                   unsigned int enabled)
{
   return 0;
}
 
static int fake_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
{
   return 0;
}
 
static int fake_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
{
   return 0;
}
 
static const struct rtc_class_ops fake_rtc_ops = {
   .read_time    = fake_rtc_read_time,
   .set_time    = fake_rtc_set_time,
   .alarm_irq_enable    = fake_rtc_alarm_irq_enable,
   .read_alarm        = fake_rtc_read_alarm,
   .set_alarm        = fake_rtc_set_alarm,
};
 
static int fake_rtc_probe(struct platform_device *pdev)
{
   struct rtc_device *fake_rtc;
   struct rtc_time tm = {
       .tm_wday = 1,
       .tm_year = 118,
       .tm_mon = 0,
       .tm_mday = 1,
       .tm_hour = 12,
       .tm_min = 0,
       .tm_sec = 0,
   };
 
   get_uptime(&begtime);
   fake_rtc_set_time(&pdev->dev, &tm);
 
   device_init_wakeup(&pdev->dev, 1);
 
   fake_rtc = devm_rtc_device_register(&pdev->dev,
               pdev->name, &fake_rtc_ops, THIS_MODULE);
   if (IS_ERR(fake_rtc))
       return PTR_ERR(fake_rtc);
 
   dev_info(&pdev->dev, "loaded; begtime is %lu, time is %lu\n",
        timespec_to_ulong(&begtime), time);
 
   return 0;
}
 
static const struct of_device_id rtc_dt_ids[] = {
   { .compatible = "rtc-fake" },
   {},
};
 
struct platform_driver fake_rtc_driver = {
   .driver        = {
       .name    = "rtc-fake",
       .owner    = THIS_MODULE,
       .of_match_table = of_match_ptr(rtc_dt_ids),
   },
   .probe        = fake_rtc_probe,
};
 
module_platform_driver(fake_rtc_driver);
 
MODULE_AUTHOR("jesse.huang@rock-chips.com");
MODULE_DESCRIPTION("FAKE RTC driver");
MODULE_LICENSE("GPL");