4 * Copyright (C) 2013 Jonas Jensen
6 * Jonas Jensen <jonas.jensen@gmail.com>
9 * Moxa Technology Co., Ltd. <www.moxa.com>
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/rtc.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/gpio.h>
23 #include <linux/of_gpio.h>
25 #define GPIO_RTC_RESERVED 0x0C
26 #define GPIO_RTC_DATA_SET 0x10
27 #define GPIO_RTC_DATA_CLEAR 0x14
28 #define GPIO_RTC_PIN_PULL_ENABLE 0x18
29 #define GPIO_RTC_PIN_PULL_TYPE 0x1C
30 #define GPIO_RTC_INT_ENABLE 0x20
31 #define GPIO_RTC_INT_RAW_STATE 0x24
32 #define GPIO_RTC_INT_MASKED_STATE 0x28
33 #define GPIO_RTC_INT_MASK 0x2C
34 #define GPIO_RTC_INT_CLEAR 0x30
35 #define GPIO_RTC_INT_TRIGGER 0x34
36 #define GPIO_RTC_INT_BOTH 0x38
37 #define GPIO_RTC_INT_RISE_NEG 0x3C
38 #define GPIO_RTC_BOUNCE_ENABLE 0x40
39 #define GPIO_RTC_BOUNCE_PRE_SCALE 0x44
40 #define GPIO_RTC_PROTECT_W 0x8E
41 #define GPIO_RTC_PROTECT_R 0x8F
42 #define GPIO_RTC_YEAR_W 0x8C
43 #define GPIO_RTC_YEAR_R 0x8D
44 #define GPIO_RTC_DAY_W 0x8A
45 #define GPIO_RTC_DAY_R 0x8B
46 #define GPIO_RTC_MONTH_W 0x88
47 #define GPIO_RTC_MONTH_R 0x89
48 #define GPIO_RTC_DATE_W 0x86
49 #define GPIO_RTC_DATE_R 0x87
50 #define GPIO_RTC_HOURS_W 0x84
51 #define GPIO_RTC_HOURS_R 0x85
52 #define GPIO_RTC_MINUTES_W 0x82
53 #define GPIO_RTC_MINUTES_R 0x83
54 #define GPIO_RTC_SECONDS_W 0x80
55 #define GPIO_RTC_SECONDS_R 0x81
56 #define GPIO_RTC_DELAY_TIME 8
59 struct rtc_device *rtc;
61 int gpio_data, gpio_sclk, 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 gpio_set_value(moxart_rtc->gpio_sclk, 0);
74 gpio_set_value(moxart_rtc->gpio_data, ((data & 1) == 1));
75 udelay(GPIO_RTC_DELAY_TIME);
76 gpio_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 gpio_set_value(moxart_rtc->gpio_sclk, 0);
89 udelay(GPIO_RTC_DELAY_TIME);
90 gpio_set_value(moxart_rtc->gpio_sclk, 1);
91 udelay(GPIO_RTC_DELAY_TIME);
92 if (gpio_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 gpio_direction_output(moxart_rtc->gpio_data, 0);
108 gpio_set_value(moxart_rtc->gpio_reset, 1);
109 udelay(GPIO_RTC_DELAY_TIME);
110 moxart_rtc_write_byte(dev, cmd);
111 gpio_direction_input(moxart_rtc->gpio_data);
112 udelay(GPIO_RTC_DELAY_TIME);
113 data = moxart_rtc_read_byte(dev);
114 gpio_set_value(moxart_rtc->gpio_sclk, 0);
115 gpio_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 gpio_direction_output(moxart_rtc->gpio_data, 0);
131 gpio_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 gpio_set_value(moxart_rtc->gpio_sclk, 0);
136 gpio_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 = of_get_named_gpio(pdev->dev.of_node,
255 if (!gpio_is_valid(moxart_rtc->gpio_data)) {
256 dev_err(&pdev->dev, "invalid gpio (data): %d\n",
257 moxart_rtc->gpio_data);
258 return moxart_rtc->gpio_data;
261 moxart_rtc->gpio_sclk = of_get_named_gpio(pdev->dev.of_node,
263 if (!gpio_is_valid(moxart_rtc->gpio_sclk)) {
264 dev_err(&pdev->dev, "invalid gpio (sclk): %d\n",
265 moxart_rtc->gpio_sclk);
266 return moxart_rtc->gpio_sclk;
269 moxart_rtc->gpio_reset = of_get_named_gpio(pdev->dev.of_node,
270 "gpio-rtc-reset", 0);
271 if (!gpio_is_valid(moxart_rtc->gpio_reset)) {
272 dev_err(&pdev->dev, "invalid gpio (reset): %d\n",
273 moxart_rtc->gpio_reset);
274 return moxart_rtc->gpio_reset;
277 spin_lock_init(&moxart_rtc->rtc_lock);
278 platform_set_drvdata(pdev, moxart_rtc);
280 ret = devm_gpio_request(&pdev->dev, moxart_rtc->gpio_data, "rtc_data");
282 dev_err(&pdev->dev, "can't get rtc_data gpio\n");
286 ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_sclk,
287 GPIOF_DIR_OUT, "rtc_sclk");
289 dev_err(&pdev->dev, "can't get rtc_sclk gpio\n");
293 ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_reset,
294 GPIOF_DIR_OUT, "rtc_reset");
296 dev_err(&pdev->dev, "can't get rtc_reset gpio\n");
300 moxart_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
303 if (IS_ERR(moxart_rtc->rtc)) {
304 dev_err(&pdev->dev, "devm_rtc_device_register failed\n");
305 return PTR_ERR(moxart_rtc->rtc);
311 static const struct of_device_id moxart_rtc_match[] = {
312 { .compatible = "moxa,moxart-rtc" },
315 MODULE_DEVICE_TABLE(of, moxart_rtc_match);
317 static struct platform_driver moxart_rtc_driver = {
318 .probe = moxart_rtc_probe,
320 .name = "moxart-rtc",
321 .of_match_table = moxart_rtc_match,
324 module_platform_driver(moxart_rtc_driver);
326 MODULE_DESCRIPTION("MOXART RTC driver");
327 MODULE_LICENSE("GPL");
328 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");