1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2013 Jonas Jensen
7 * Jonas Jensen <jonas.jensen@gmail.com>
10 * Moxa Technology Co., Ltd. <www.moxa.com>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/rtc.h>
18 #include <linux/platform_device.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/gpio/consumer.h>
23 #define GPIO_RTC_RESERVED 0x0C
24 #define GPIO_RTC_DATA_SET 0x10
25 #define GPIO_RTC_DATA_CLEAR 0x14
26 #define GPIO_RTC_PIN_PULL_ENABLE 0x18
27 #define GPIO_RTC_PIN_PULL_TYPE 0x1C
28 #define GPIO_RTC_INT_ENABLE 0x20
29 #define GPIO_RTC_INT_RAW_STATE 0x24
30 #define GPIO_RTC_INT_MASKED_STATE 0x28
31 #define GPIO_RTC_INT_MASK 0x2C
32 #define GPIO_RTC_INT_CLEAR 0x30
33 #define GPIO_RTC_INT_TRIGGER 0x34
34 #define GPIO_RTC_INT_BOTH 0x38
35 #define GPIO_RTC_INT_RISE_NEG 0x3C
36 #define GPIO_RTC_BOUNCE_ENABLE 0x40
37 #define GPIO_RTC_BOUNCE_PRE_SCALE 0x44
38 #define GPIO_RTC_PROTECT_W 0x8E
39 #define GPIO_RTC_PROTECT_R 0x8F
40 #define GPIO_RTC_YEAR_W 0x8C
41 #define GPIO_RTC_YEAR_R 0x8D
42 #define GPIO_RTC_DAY_W 0x8A
43 #define GPIO_RTC_DAY_R 0x8B
44 #define GPIO_RTC_MONTH_W 0x88
45 #define GPIO_RTC_MONTH_R 0x89
46 #define GPIO_RTC_DATE_W 0x86
47 #define GPIO_RTC_DATE_R 0x87
48 #define GPIO_RTC_HOURS_W 0x84
49 #define GPIO_RTC_HOURS_R 0x85
50 #define GPIO_RTC_MINUTES_W 0x82
51 #define GPIO_RTC_MINUTES_R 0x83
52 #define GPIO_RTC_SECONDS_W 0x80
53 #define GPIO_RTC_SECONDS_R 0x81
54 #define GPIO_RTC_DELAY_TIME 8
57 struct rtc_device
*rtc
;
59 struct gpio_desc
*gpio_data
;
60 struct gpio_desc
*gpio_sclk
;
61 struct gpio_desc
*gpio_reset
;
64 static int day_of_year
[12] = { 0, 31, 59, 90, 120, 151, 181,
65 212, 243, 273, 304, 334 };
67 static void moxart_rtc_write_byte(struct device
*dev
, u8 data
)
69 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
72 for (i
= 0; i
< 8; i
++, data
>>= 1) {
73 gpiod_set_value(moxart_rtc
->gpio_sclk
, 0);
74 gpiod_set_value(moxart_rtc
->gpio_data
, ((data
& 1) == 1));
75 udelay(GPIO_RTC_DELAY_TIME
);
76 gpiod_set_value(moxart_rtc
->gpio_sclk
, 1);
77 udelay(GPIO_RTC_DELAY_TIME
);
81 static u8
moxart_rtc_read_byte(struct device
*dev
)
83 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
87 for (i
= 0; i
< 8; i
++) {
88 gpiod_set_value(moxart_rtc
->gpio_sclk
, 0);
89 udelay(GPIO_RTC_DELAY_TIME
);
90 gpiod_set_value(moxart_rtc
->gpio_sclk
, 1);
91 udelay(GPIO_RTC_DELAY_TIME
);
92 if (gpiod_get_value(moxart_rtc
->gpio_data
))
94 udelay(GPIO_RTC_DELAY_TIME
);
99 static u8
moxart_rtc_read_register(struct device
*dev
, u8 cmd
)
101 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
105 local_irq_save(flags
);
107 gpiod_direction_output(moxart_rtc
->gpio_data
, 0);
108 gpiod_set_value(moxart_rtc
->gpio_reset
, 1);
109 udelay(GPIO_RTC_DELAY_TIME
);
110 moxart_rtc_write_byte(dev
, cmd
);
111 gpiod_direction_input(moxart_rtc
->gpio_data
);
112 udelay(GPIO_RTC_DELAY_TIME
);
113 data
= moxart_rtc_read_byte(dev
);
114 gpiod_set_value(moxart_rtc
->gpio_sclk
, 0);
115 gpiod_set_value(moxart_rtc
->gpio_reset
, 0);
116 udelay(GPIO_RTC_DELAY_TIME
);
118 local_irq_restore(flags
);
123 static void moxart_rtc_write_register(struct device
*dev
, u8 cmd
, u8 data
)
125 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
128 local_irq_save(flags
);
130 gpiod_direction_output(moxart_rtc
->gpio_data
, 0);
131 gpiod_set_value(moxart_rtc
->gpio_reset
, 1);
132 udelay(GPIO_RTC_DELAY_TIME
);
133 moxart_rtc_write_byte(dev
, cmd
);
134 moxart_rtc_write_byte(dev
, data
);
135 gpiod_set_value(moxart_rtc
->gpio_sclk
, 0);
136 gpiod_set_value(moxart_rtc
->gpio_reset
, 0);
137 udelay(GPIO_RTC_DELAY_TIME
);
139 local_irq_restore(flags
);
142 static int moxart_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
144 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
146 spin_lock_irq(&moxart_rtc
->rtc_lock
);
148 moxart_rtc_write_register(dev
, GPIO_RTC_PROTECT_W
, 0);
149 moxart_rtc_write_register(dev
, GPIO_RTC_YEAR_W
,
150 (((tm
->tm_year
- 100) / 10) << 4) |
151 ((tm
->tm_year
- 100) % 10));
153 moxart_rtc_write_register(dev
, GPIO_RTC_MONTH_W
,
154 (((tm
->tm_mon
+ 1) / 10) << 4) |
155 ((tm
->tm_mon
+ 1) % 10));
157 moxart_rtc_write_register(dev
, GPIO_RTC_DATE_W
,
158 ((tm
->tm_mday
/ 10) << 4) |
161 moxart_rtc_write_register(dev
, GPIO_RTC_HOURS_W
,
162 ((tm
->tm_hour
/ 10) << 4) |
165 moxart_rtc_write_register(dev
, GPIO_RTC_MINUTES_W
,
166 ((tm
->tm_min
/ 10) << 4) |
169 moxart_rtc_write_register(dev
, GPIO_RTC_SECONDS_W
,
170 ((tm
->tm_sec
/ 10) << 4) |
173 moxart_rtc_write_register(dev
, GPIO_RTC_PROTECT_W
, 0x80);
175 spin_unlock_irq(&moxart_rtc
->rtc_lock
);
177 dev_dbg(dev
, "%s: success tm_year=%d tm_mon=%d\n"
178 "tm_mday=%d tm_hour=%d tm_min=%d tm_sec=%d\n",
179 __func__
, tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
180 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
185 static int moxart_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
187 struct moxart_rtc
*moxart_rtc
= dev_get_drvdata(dev
);
190 spin_lock_irq(&moxart_rtc
->rtc_lock
);
192 v
= moxart_rtc_read_register(dev
, GPIO_RTC_SECONDS_R
);
193 tm
->tm_sec
= (((v
& 0x70) >> 4) * 10) + (v
& 0x0F);
195 v
= moxart_rtc_read_register(dev
, GPIO_RTC_MINUTES_R
);
196 tm
->tm_min
= (((v
& 0x70) >> 4) * 10) + (v
& 0x0F);
198 v
= moxart_rtc_read_register(dev
, GPIO_RTC_HOURS_R
);
199 if (v
& 0x80) { /* 12-hour mode */
200 tm
->tm_hour
= (((v
& 0x10) >> 4) * 10) + (v
& 0x0F);
201 if (v
& 0x20) { /* PM mode */
203 if (tm
->tm_hour
>= 24)
206 } else { /* 24-hour mode */
207 tm
->tm_hour
= (((v
& 0x30) >> 4) * 10) + (v
& 0x0F);
210 v
= moxart_rtc_read_register(dev
, GPIO_RTC_DATE_R
);
211 tm
->tm_mday
= (((v
& 0x30) >> 4) * 10) + (v
& 0x0F);
213 v
= moxart_rtc_read_register(dev
, GPIO_RTC_MONTH_R
);
214 tm
->tm_mon
= (((v
& 0x10) >> 4) * 10) + (v
& 0x0F);
217 v
= moxart_rtc_read_register(dev
, GPIO_RTC_YEAR_R
);
218 tm
->tm_year
= (((v
& 0xF0) >> 4) * 10) + (v
& 0x0F);
220 if (tm
->tm_year
<= 69)
223 v
= moxart_rtc_read_register(dev
, GPIO_RTC_DAY_R
);
224 tm
->tm_wday
= (v
& 0x0f) - 1;
225 tm
->tm_yday
= day_of_year
[tm
->tm_mon
];
226 tm
->tm_yday
+= (tm
->tm_mday
- 1);
227 if (tm
->tm_mon
>= 2) {
228 if (!(tm
->tm_year
% 4) && (tm
->tm_year
% 100))
234 spin_unlock_irq(&moxart_rtc
->rtc_lock
);
239 static const struct rtc_class_ops moxart_rtc_ops
= {
240 .read_time
= moxart_rtc_read_time
,
241 .set_time
= moxart_rtc_set_time
,
244 static int moxart_rtc_probe(struct platform_device
*pdev
)
246 struct moxart_rtc
*moxart_rtc
;
249 moxart_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*moxart_rtc
), GFP_KERNEL
);
253 moxart_rtc
->gpio_data
= devm_gpiod_get(&pdev
->dev
, "rtc-data",
255 ret
= PTR_ERR_OR_ZERO(moxart_rtc
->gpio_data
);
257 dev_err(&pdev
->dev
, "can't get rtc data gpio: %d\n", ret
);
261 moxart_rtc
->gpio_sclk
= devm_gpiod_get(&pdev
->dev
, "rtc-sclk",
263 ret
= PTR_ERR_OR_ZERO(moxart_rtc
->gpio_sclk
);
265 dev_err(&pdev
->dev
, "can't get rtc sclk gpio: %d\n", ret
);
269 moxart_rtc
->gpio_reset
= devm_gpiod_get(&pdev
->dev
, "rtc-reset",
271 ret
= PTR_ERR_OR_ZERO(moxart_rtc
->gpio_reset
);
273 dev_err(&pdev
->dev
, "can't get rtc reset gpio: %d\n", ret
);
277 spin_lock_init(&moxart_rtc
->rtc_lock
);
278 platform_set_drvdata(pdev
, moxart_rtc
);
280 moxart_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
283 if (IS_ERR(moxart_rtc
->rtc
)) {
284 dev_err(&pdev
->dev
, "devm_rtc_device_register failed\n");
285 return PTR_ERR(moxart_rtc
->rtc
);
291 static const struct of_device_id moxart_rtc_match
[] = {
292 { .compatible
= "moxa,moxart-rtc" },
295 MODULE_DEVICE_TABLE(of
, moxart_rtc_match
);
297 static struct platform_driver moxart_rtc_driver
= {
298 .probe
= moxart_rtc_probe
,
300 .name
= "moxart-rtc",
301 .of_match_table
= moxart_rtc_match
,
304 module_platform_driver(moxart_rtc_driver
);
306 MODULE_DESCRIPTION("MOXART RTC driver");
307 MODULE_LICENSE("GPL");
308 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");