summaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-moxart.c
blob: 6b24ac9e1cfa846bb7f8375c04654142039dbc35 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * MOXA ART RTC driver.
 *
 * Copyright (C) 2013 Jonas Jensen
 *
 * Jonas Jensen <jonas.jensen@gmail.com>
 *
 * Based on code from
 * Moxa Technology Co., Ltd. <www.moxa.com>
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/rtc.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>

#define GPIO_RTC_RESERVED			0x0C
#define GPIO_RTC_DATA_SET			0x10
#define GPIO_RTC_DATA_CLEAR			0x14
#define GPIO_RTC_PIN_PULL_ENABLE		0x18
#define GPIO_RTC_PIN_PULL_TYPE			0x1C
#define GPIO_RTC_INT_ENABLE			0x20
#define GPIO_RTC_INT_RAW_STATE			0x24
#define GPIO_RTC_INT_MASKED_STATE		0x28
#define GPIO_RTC_INT_MASK			0x2C
#define GPIO_RTC_INT_CLEAR			0x30
#define GPIO_RTC_INT_TRIGGER			0x34
#define GPIO_RTC_INT_BOTH			0x38
#define GPIO_RTC_INT_RISE_NEG			0x3C
#define GPIO_RTC_BOUNCE_ENABLE			0x40
#define GPIO_RTC_BOUNCE_PRE_SCALE		0x44
#define GPIO_RTC_PROTECT_W			0x8E
#define GPIO_RTC_PROTECT_R			0x8F
#define GPIO_RTC_YEAR_W				0x8C
#define GPIO_RTC_YEAR_R				0x8D
#define GPIO_RTC_DAY_W				0x8A
#define GPIO_RTC_DAY_R				0x8B
#define GPIO_RTC_MONTH_W			0x88
#define GPIO_RTC_MONTH_R			0x89
#define GPIO_RTC_DATE_W				0x86
#define GPIO_RTC_DATE_R				0x87
#define GPIO_RTC_HOURS_W			0x84
#define GPIO_RTC_HOURS_R			0x85
#define GPIO_RTC_MINUTES_W			0x82
#define GPIO_RTC_MINUTES_R			0x83
#define GPIO_RTC_SECONDS_W			0x80
#define GPIO_RTC_SECONDS_R			0x81
#define GPIO_RTC_DELAY_TIME			8

struct moxart_rtc {
	struct rtc_device *rtc;
	spinlock_t rtc_lock;
	int gpio_data, gpio_sclk, gpio_reset;
};

static int day_of_year[12] =	{ 0, 31, 59, 90, 120, 151, 181,
				  212, 243, 273, 304, 334 };

static void moxart_rtc_write_byte(struct device *dev, u8 data)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
	int i;

	for (i = 0; i < 8; i++, data >>= 1) {
		gpio_set_value(moxart_rtc->gpio_sclk, 0);
		gpio_set_value(moxart_rtc->gpio_data, ((data & 1) == 1));
		udelay(GPIO_RTC_DELAY_TIME);
		gpio_set_value(moxart_rtc->gpio_sclk, 1);
		udelay(GPIO_RTC_DELAY_TIME);
	}
}

static u8 moxart_rtc_read_byte(struct device *dev)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
	int i;
	u8 data = 0;

	for (i = 0; i < 8; i++) {
		gpio_set_value(moxart_rtc->gpio_sclk, 0);
		udelay(GPIO_RTC_DELAY_TIME);
		gpio_set_value(moxart_rtc->gpio_sclk, 1);
		udelay(GPIO_RTC_DELAY_TIME);
		if (gpio_get_value(moxart_rtc->gpio_data))
			data |= (1 << i);
		udelay(GPIO_RTC_DELAY_TIME);
	}
	return data;
}

static u8 moxart_rtc_read_register(struct device *dev, u8 cmd)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
	u8 data;
	unsigned long flags;

	local_irq_save(flags);

	gpio_direction_output(moxart_rtc->gpio_data, 0);
	gpio_set_value(moxart_rtc->gpio_reset, 1);
	udelay(GPIO_RTC_DELAY_TIME);
	moxart_rtc_write_byte(dev, cmd);
	gpio_direction_input(moxart_rtc->gpio_data);
	udelay(GPIO_RTC_DELAY_TIME);
	data = moxart_rtc_read_byte(dev);
	gpio_set_value(moxart_rtc->gpio_sclk, 0);
	gpio_set_value(moxart_rtc->gpio_reset, 0);
	udelay(GPIO_RTC_DELAY_TIME);

	local_irq_restore(flags);

	return data;
}

static void moxart_rtc_write_register(struct device *dev, u8 cmd, u8 data)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
	unsigned long flags;

	local_irq_save(flags);

	gpio_direction_output(moxart_rtc->gpio_data, 0);
	gpio_set_value(moxart_rtc->gpio_reset, 1);
	udelay(GPIO_RTC_DELAY_TIME);
	moxart_rtc_write_byte(dev, cmd);
	moxart_rtc_write_byte(dev, data);
	gpio_set_value(moxart_rtc->gpio_sclk, 0);
	gpio_set_value(moxart_rtc->gpio_reset, 0);
	udelay(GPIO_RTC_DELAY_TIME);

	local_irq_restore(flags);
}

static int moxart_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);

	spin_lock_irq(&moxart_rtc->rtc_lock);

	moxart_rtc_write_register(dev, GPIO_RTC_PROTECT_W, 0);
	moxart_rtc_write_register(dev, GPIO_RTC_YEAR_W,
				  (((tm->tm_year - 100) / 10) << 4) |
				  ((tm->tm_year - 100) % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_MONTH_W,
				  (((tm->tm_mon + 1) / 10) << 4) |
				  ((tm->tm_mon + 1) % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_DATE_W,
				  ((tm->tm_mday / 10) << 4) |
				  (tm->tm_mday % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_HOURS_W,
				  ((tm->tm_hour / 10) << 4) |
				  (tm->tm_hour % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_MINUTES_W,
				  ((tm->tm_min / 10) << 4) |
				  (tm->tm_min % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_SECONDS_W,
				  ((tm->tm_sec / 10) << 4) |
				  (tm->tm_sec % 10));

	moxart_rtc_write_register(dev, GPIO_RTC_PROTECT_W, 0x80);

	spin_unlock_irq(&moxart_rtc->rtc_lock);

	dev_dbg(dev, "%s: success tm_year=%d tm_mon=%d\n"
		"tm_mday=%d tm_hour=%d tm_min=%d tm_sec=%d\n",
		__func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
		tm->tm_hour, tm->tm_min, tm->tm_sec);

	return 0;
}

static int moxart_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
	unsigned char v;

	spin_lock_irq(&moxart_rtc->rtc_lock);

	v = moxart_rtc_read_register(dev, GPIO_RTC_SECONDS_R);
	tm->tm_sec = (((v & 0x70) >> 4) * 10) + (v & 0x0F);

	v = moxart_rtc_read_register(dev, GPIO_RTC_MINUTES_R);
	tm->tm_min = (((v & 0x70) >> 4) * 10) + (v & 0x0F);

	v = moxart_rtc_read_register(dev, GPIO_RTC_HOURS_R);
	if (v & 0x80) { /* 12-hour mode */
		tm->tm_hour = (((v & 0x10) >> 4) * 10) + (v & 0x0F);
		if (v & 0x20) { /* PM mode */
			tm->tm_hour += 12;
			if (tm->tm_hour >= 24)
				tm->tm_hour = 0;
		}
	} else { /* 24-hour mode */
		tm->tm_hour = (((v & 0x30) >> 4) * 10) + (v & 0x0F);
	}

	v = moxart_rtc_read_register(dev, GPIO_RTC_DATE_R);
	tm->tm_mday = (((v & 0x30) >> 4) * 10) + (v & 0x0F);

	v = moxart_rtc_read_register(dev, GPIO_RTC_MONTH_R);
	tm->tm_mon = (((v & 0x10) >> 4) * 10) + (v & 0x0F);
	tm->tm_mon--;

	v = moxart_rtc_read_register(dev, GPIO_RTC_YEAR_R);
	tm->tm_year = (((v & 0xF0) >> 4) * 10) + (v & 0x0F);
	tm->tm_year += 100;
	if (tm->tm_year <= 69)
		tm->tm_year += 100;

	v = moxart_rtc_read_register(dev, GPIO_RTC_DAY_R);
	tm->tm_wday = (v & 0x0f) - 1;
	tm->tm_yday = day_of_year[tm->tm_mon];
	tm->tm_yday += (tm->tm_mday - 1);
	if (tm->tm_mon >= 2) {
		if (!(tm->tm_year % 4) && (tm->tm_year % 100))
			tm->tm_yday++;
	}

	tm->tm_isdst = 0;

	spin_unlock_irq(&moxart_rtc->rtc_lock);

	return 0;
}

static const struct rtc_class_ops moxart_rtc_ops = {
	.read_time	= moxart_rtc_read_time,
	.set_time	= moxart_rtc_set_time,
};

static int moxart_rtc_probe(struct platform_device *pdev)
{
	struct moxart_rtc *moxart_rtc;
	int ret = 0;

	moxart_rtc = devm_kzalloc(&pdev->dev, sizeof(*moxart_rtc), GFP_KERNEL);
	if (!moxart_rtc)
		return -ENOMEM;

	moxart_rtc->gpio_data = of_get_named_gpio(pdev->dev.of_node,
						  "gpio-rtc-data", 0);
	if (!gpio_is_valid(moxart_rtc->gpio_data)) {
		dev_err(&pdev->dev, "invalid gpio (data): %d\n",
			moxart_rtc->gpio_data);
		return moxart_rtc->gpio_data;
	}

	moxart_rtc->gpio_sclk = of_get_named_gpio(pdev->dev.of_node,
						  "gpio-rtc-sclk", 0);
	if (!gpio_is_valid(moxart_rtc->gpio_sclk)) {
		dev_err(&pdev->dev, "invalid gpio (sclk): %d\n",
			moxart_rtc->gpio_sclk);
		return moxart_rtc->gpio_sclk;
	}

	moxart_rtc->gpio_reset = of_get_named_gpio(pdev->dev.of_node,
						   "gpio-rtc-reset", 0);
	if (!gpio_is_valid(moxart_rtc->gpio_reset)) {
		dev_err(&pdev->dev, "invalid gpio (reset): %d\n",
			moxart_rtc->gpio_reset);
		return moxart_rtc->gpio_reset;
	}

	spin_lock_init(&moxart_rtc->rtc_lock);
	platform_set_drvdata(pdev, moxart_rtc);

	ret = devm_gpio_request(&pdev->dev, moxart_rtc->gpio_data, "rtc_data");
	if (ret) {
		dev_err(&pdev->dev, "can't get rtc_data gpio\n");
		return ret;
	}

	ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_sclk,
				    GPIOF_DIR_OUT, "rtc_sclk");
	if (ret) {
		dev_err(&pdev->dev, "can't get rtc_sclk gpio\n");
		return ret;
	}

	ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_reset,
				    GPIOF_DIR_OUT, "rtc_reset");
	if (ret) {
		dev_err(&pdev->dev, "can't get rtc_reset gpio\n");
		return ret;
	}

	moxart_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
						   &moxart_rtc_ops,
						   THIS_MODULE);
	if (IS_ERR(moxart_rtc->rtc)) {
		dev_err(&pdev->dev, "devm_rtc_device_register failed\n");
		return PTR_ERR(moxart_rtc->rtc);
	}

	return 0;
}

static const struct of_device_id moxart_rtc_match[] = {
	{ .compatible = "moxa,moxart-rtc" },
	{ },
};
MODULE_DEVICE_TABLE(of, moxart_rtc_match);

static struct platform_driver moxart_rtc_driver = {
	.probe	= moxart_rtc_probe,
	.driver	= {
		.name		= "moxart-rtc",
		.of_match_table	= moxart_rtc_match,
	},
};
module_platform_driver(moxart_rtc_driver);

MODULE_DESCRIPTION("MOXART RTC driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");