forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrtc-msc313e.c
executable file
·332 lines (273 loc) · 8.36 KB
/
rtc-msc313e.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Real Time Clock driver for msb252x
*
* (C) 2011 Heyn lu, Mstar
* (C) 2019 Daniel Palmer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/rtc.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/ctype.h>
#include <asm/io.h>
#include <linux/of_irq.h>
#define DRIVER_NAME "msc313e-rtc"
#define REG_RTC_CTRL 0x00
#define SOFT_RSTZ_BIT BIT(0)
#define CNT_EN_BIT BIT(1)
#define WRAP_EN_BIT BIT(2)
#define LOAD_EN_BIT BIT(3)
#define READ_EN_BIT BIT(4)
#define INT_MASK_BIT BIT(5)
#define INT_FORCE_BIT BIT(6)
#define INT_CLEAR_BIT BIT(7)
#define REG_RTC_FREQ_CW_L 0x04
#define REG_RTC_FREQ_CW_H 0x08
#define REG_RTC_LOAD_VAL_L 0x0C
#define REG_RTC_LOAD_VAL_H 0x10
#define REG_RTC_MATCH_VAL_L 0x14
#define REG_RTC_MATCH_VAL_H 0x18
#define REG_RTC_CNT_VAL_L 0x20
#define REG_RTC_CNT_VAL_H 0x24
struct ms_rtc_info {
struct platform_device *pdev;
struct rtc_device *rtc_dev;
void __iomem *rtc_base;
};
int auto_wakeup_delay_seconds = 0;
static ssize_t auto_wakeup_timer_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t n)
{
if(NULL!=buf)
{
size_t len;
const char *str = buf;
while (*str && !isspace(*str)) str++;
len = str - buf;
if(len)
{
auto_wakeup_delay_seconds = simple_strtoul(buf, NULL, 10);
//printk("\nauto_wakeup_delay_seconds=%d\n", auto_wakeup_delay_seconds);
return n;
}
return -EINVAL;
}
return -EINVAL;
}
static ssize_t auto_wakeup_timer_show(struct device *dev, struct device_attribute *attr, char *buf)
{
char *str = buf;
char *end = buf + PAGE_SIZE;
str += scnprintf(str, end - str, "%d\n", auto_wakeup_delay_seconds);
return (str - buf);
}
DEVICE_ATTR(auto_wakeup_timer, 0644, auto_wakeup_timer_show, auto_wakeup_timer_store);
static int ms_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
struct ms_rtc_info *info = dev_get_drvdata(dev);
unsigned long seconds;
seconds = readw(info->rtc_base + REG_RTC_MATCH_VAL_L) | (readw(info->rtc_base + REG_RTC_MATCH_VAL_H) << 16);
rtc_time_to_tm(seconds, &alarm->time);
if( !(readw(info->rtc_base + REG_RTC_CTRL) & INT_MASK_BIT) )
alarm->enabled = 1;
return 0;
}
static int ms_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
struct ms_rtc_info *info = dev_get_drvdata(dev);
unsigned long seconds;
u16 reg;
rtc_tm_to_time(&alarm->time, &seconds);
writew((seconds & 0xFFFF), info->rtc_base + REG_RTC_MATCH_VAL_L);
writew((seconds>>16) & 0xFFFF, info->rtc_base + REG_RTC_MATCH_VAL_H);
reg = readw(info->rtc_base + REG_RTC_CTRL);
if(alarm->enabled)
{
writew(reg & ~(INT_MASK_BIT), info->rtc_base + REG_RTC_CTRL);
}
else
{
writew(reg | INT_MASK_BIT, info->rtc_base + REG_RTC_CTRL);
}
return 0;
}
static int ms_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
struct ms_rtc_info *info = dev_get_drvdata(dev);
unsigned long seconds;
u16 reg;
reg = readw(info->rtc_base + REG_RTC_CTRL);
writew(reg | READ_EN_BIT, info->rtc_base + REG_RTC_CTRL);
while(readw(info->rtc_base + REG_RTC_CTRL) & READ_EN_BIT); //wait for HW latch done
seconds = readw(info->rtc_base + REG_RTC_CNT_VAL_L) | (readw(info->rtc_base + REG_RTC_CNT_VAL_H) << 16);
rtc_time_to_tm(seconds, tm);
return rtc_valid_tm(tm);
}
static int ms_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
struct ms_rtc_info *info = dev_get_drvdata(dev);
unsigned long seconds;
u16 reg;
rtc_tm_to_time(tm, &seconds);
writew(seconds & 0xFFFF, info->rtc_base + REG_RTC_LOAD_VAL_L);
writew((seconds >> 16) & 0xFFFF, info->rtc_base + REG_RTC_LOAD_VAL_H);
reg = readw(info->rtc_base + REG_RTC_CTRL);
writew(reg | LOAD_EN_BIT, info->rtc_base + REG_RTC_CTRL);
/* need to check carefully if we want to clear REG_RTC_LOAD_VAL_H for customer*/
while(readw(info->rtc_base + REG_RTC_CTRL) & LOAD_EN_BIT);
writew(0, info->rtc_base + REG_RTC_LOAD_VAL_H);
return 0;
}
static const struct rtc_class_ops ms_rtc_ops = {
.read_time = ms_rtc_read_time,
.set_time = ms_rtc_set_time,
.read_alarm = ms_rtc_read_alarm,
.set_alarm = ms_rtc_set_alarm,
};
static irqreturn_t ms_rtc_interrupt(s32 irq, void *dev_id)
{
struct ms_rtc_info *info = dev_get_drvdata(dev_id);
u16 reg;
reg = readw(info->rtc_base + REG_RTC_CTRL);
reg |= INT_CLEAR_BIT;
writew(reg, info->rtc_base + REG_RTC_CTRL);
return IRQ_HANDLED;
}
#ifdef CONFIG_PM
static s32 ms_rtc_suspend(struct platform_device *pdev, pm_message_t state)
{
if(auto_wakeup_delay_seconds)
{
struct rtc_time tm;
struct rtc_wkalrm alarm;
unsigned long seconds;
ms_rtc_read_time(&pdev->dev, &tm);
rtc_tm_to_time(&tm, &seconds);
seconds += auto_wakeup_delay_seconds;
rtc_time_to_tm(seconds, &alarm.time);
alarm.enabled=1;
ms_rtc_set_alarm(&pdev->dev, &alarm);
}
return 0;
}
static s32 ms_rtc_resume(struct platform_device *pdev)
{
return 0;
}
#endif
static int ms_rtc_remove(struct platform_device *pdev)
{
struct clk *clk = of_clk_get(pdev->dev.of_node, 0);
if(IS_ERR(clk))
{
return PTR_ERR(clk);
}
clk_disable_unprepare(clk);
return 0;
}
static int ms_rtc_probe(struct platform_device *pdev)
{
struct ms_rtc_info *info;
struct resource *res;
struct clk *clk;
dev_t dev;
int ret = 0, irq;
u16 reg;
u32 rate;
info = devm_kzalloc(&pdev->dev, sizeof(struct ms_rtc_info), GFP_KERNEL);
if (!info)
return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
{
return -ENODEV;
}
info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(info->rtc_base))
return PTR_ERR(info->rtc_base);
info->pdev = pdev;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!res)
{
return -ENODEV;
}
irq = of_irq_get(pdev->dev.of_node, 0);
if(irq == 0)
{
return -ENODEV;
}
ret = devm_request_irq(&pdev->dev, irq, ms_rtc_interrupt, IRQF_SHARED,
dev_name(&pdev->dev), &pdev->dev);
if (ret)
return ret;
platform_set_drvdata(pdev, info);
info->rtc_dev = devm_rtc_device_register(&pdev->dev,
dev_name(&pdev->dev), &ms_rtc_ops,
THIS_MODULE);
if (IS_ERR(info->rtc_dev)) {
return PTR_ERR(info->rtc_dev);
}
//Note: is it needed?
//device_set_wakeup_capable(&pdev->dev, 1);
//device_wakeup_enable(&pdev->dev);
//init rtc
//1. release reset
reg = readw(info->rtc_base + REG_RTC_CTRL);
if( !(reg & SOFT_RSTZ_BIT) )
{
reg |= SOFT_RSTZ_BIT;
writew(reg, info->rtc_base + REG_RTC_CTRL);
}
//2. set frequency
clk = of_clk_get(pdev->dev.of_node, 0);
if(IS_ERR(clk))
{
return PTR_ERR(clk);
}
/* Try to determine the frequency from the device tree */
if (of_property_read_u32(pdev->dev.of_node, "clock-frequency", &rate))
{
rate = clk_get_rate(clk);
}
else
{
clk_set_rate(clk, rate);
}
clk_prepare_enable(clk);
writew(rate & 0xFFFF, info->rtc_base + REG_RTC_FREQ_CW_L);
writew((rate >>16) & 0xFFFF, info->rtc_base + REG_RTC_FREQ_CW_H);
//3. enable counter
reg |= CNT_EN_BIT;
writew(reg, info->rtc_base + REG_RTC_CTRL);
if (0 != (ret = alloc_chrdev_region(&dev, 0, 1, DRIVER_NAME)))
return ret;
return ret;
}
static const struct of_device_id ms_rtc_of_match_table[] = {
{ .compatible = "mstar,msc313e-rtc" },
{}
};
MODULE_DEVICE_TABLE(of, ms_rtc_of_match_table);
static struct platform_driver ms_rtc_driver = {
.remove = ms_rtc_remove,
.probe = ms_rtc_probe,
#ifdef CONFIG_PM
.suspend = ms_rtc_suspend,
.resume = ms_rtc_resume,
#endif
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = ms_rtc_of_match_table,
},
};
module_platform_driver(ms_rtc_driver);
MODULE_AUTHOR("MStar Semiconductor, Inc.");
MODULE_DESCRIPTION("MStar RTC Driver");
MODULE_LICENSE("GPL v2");