summaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-ssd202d.c
blob: ed64932600965f85cdb585638dc35f40c3807a4e (plain)
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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Real time clocks driver for MStar/SigmaStar SSD202D SoCs.
 *
 * (C) 2021 Daniel Palmer
 * (C) 2023 Romain Perier
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/regmap.h>
#include <linux/pm.h>

#define REG_CTRL	0x0
#define REG_CTRL1	0x4
#define REG_ISO_CTRL	0xc
#define REG_WRDATA_L	0x10
#define REG_WRDATA_H	0x14
#define REG_ISOACK	0x20
#define REG_RDDATA_L	0x24
#define REG_RDDATA_H	0x28
#define REG_RDCNT_L	0x30
#define REG_RDCNT_H	0x34
#define REG_CNT_TRIG	0x38
#define REG_PWRCTRL	0x3c
#define REG_RTC_TEST	0x54

#define CNT_RD_TRIG_BIT BIT(0)
#define CNT_RD_BIT BIT(0)
#define BASE_WR_BIT BIT(1)
#define BASE_RD_BIT BIT(2)
#define CNT_RST_BIT BIT(3)
#define ISO_CTRL_ACK_MASK BIT(3)
#define ISO_CTRL_ACK_SHIFT 3
#define SW0_WR_BIT BIT(5)
#define SW1_WR_BIT BIT(6)
#define SW0_RD_BIT BIT(7)
#define SW1_RD_BIT BIT(8)

#define ISO_CTRL_MASK GENMASK(2, 0)

struct ssd202d_rtc {
	struct rtc_device *rtc_dev;
	void __iomem *base;
};

static u8 read_iso_en(void __iomem *base)
{
	return readb(base + REG_RTC_TEST) & 0x1;
}

static u8 read_iso_ctrl_ack(void __iomem *base)
{
	return (readb(base + REG_ISOACK) & ISO_CTRL_ACK_MASK) >> ISO_CTRL_ACK_SHIFT;
}

static int ssd202d_rtc_isoctrl(struct ssd202d_rtc *priv)
{
	static const unsigned int sequence[] = { 0x0, 0x1, 0x3, 0x7, 0x5, 0x1, 0x0 };
	unsigned int val;
	struct device *dev = &priv->rtc_dev->dev;
	int i, ret;

	/*
	 * This gates iso_en by writing a special sequence of bytes to iso_ctrl
	 * and ensuring that it has been correctly applied by reading iso_ctrl_ack
	 */
	for (i = 0; i < ARRAY_SIZE(sequence); i++) {
		writeb(sequence[i] & ISO_CTRL_MASK, priv->base +  REG_ISO_CTRL);

		ret = read_poll_timeout(read_iso_ctrl_ack, val, val == (i % 2), 100,
					20 * 100, true, priv->base);
		if (ret) {
			dev_dbg(dev, "Timeout waiting for ack byte %i (%x) of sequence\n", i,
				sequence[i]);
			return ret;
		}
	}

	/*
	 * At this point iso_en should be raised for 1ms
	 */
	ret = read_poll_timeout(read_iso_en, val, val, 100, 22 * 100, true, priv->base);
	if (ret)
		dev_dbg(dev, "Timeout waiting for iso_en\n");
	mdelay(2);
	return 0;
}

static void ssd202d_rtc_read_reg(struct ssd202d_rtc *priv, unsigned int reg,
				 unsigned int field, unsigned int *base)
{
	unsigned int l, h;
	u16 val;

	/* Ask for the content of an RTC value into RDDATA by gating iso_en,
	 * then iso_en is gated and the content of RDDATA can be read
	 */
	val = readw(priv->base + reg);
	writew(val | field, priv->base + reg);
	ssd202d_rtc_isoctrl(priv);
	writew(val & ~field, priv->base + reg);

	l = readw(priv->base + REG_RDDATA_L);
	h = readw(priv->base + REG_RDDATA_H);

	*base = (h << 16) | l;
}

static void ssd202d_rtc_write_reg(struct ssd202d_rtc *priv, unsigned int reg,
				  unsigned int field, u32 base)
{
	u16 val;

	/* Set the content of an RTC value from WRDATA by gating iso_en */
	val = readw(priv->base + reg);
	writew(val | field, priv->base + reg);
	writew(base, priv->base + REG_WRDATA_L);
	writew(base >> 16, priv->base + REG_WRDATA_H);
	ssd202d_rtc_isoctrl(priv);
	writew(val & ~field, priv->base + reg);
}

static int ssd202d_rtc_read_counter(struct ssd202d_rtc *priv, unsigned int *counter)
{
	unsigned int l, h;
	u16 val;

	val = readw(priv->base + REG_CTRL1);
	writew(val | CNT_RD_BIT, priv->base + REG_CTRL1);
	ssd202d_rtc_isoctrl(priv);
	writew(val & ~CNT_RD_BIT, priv->base + REG_CTRL1);

	val = readw(priv->base + REG_CTRL1);
	writew(val | CNT_RD_TRIG_BIT, priv->base + REG_CNT_TRIG);
	writew(val & ~CNT_RD_TRIG_BIT, priv->base + REG_CNT_TRIG);

	l = readw(priv->base + REG_RDCNT_L);
	h = readw(priv->base + REG_RDCNT_H);

	*counter = (h << 16) | l;

	return 0;
}

static int ssd202d_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct ssd202d_rtc *priv = dev_get_drvdata(dev);
	unsigned int sw0, base, counter;
	u32 seconds;
	int ret;

	/* Check that RTC is enabled by SW */
	ssd202d_rtc_read_reg(priv, REG_CTRL, SW0_RD_BIT, &sw0);
	if (sw0 != 1)
		return -EINVAL;

	/* Get RTC base value from RDDATA */
	ssd202d_rtc_read_reg(priv, REG_CTRL, BASE_RD_BIT, &base);
	/* Get RTC counter value from RDDATA */
	ret = ssd202d_rtc_read_counter(priv, &counter);
	if (ret)
		return ret;

	seconds = base + counter;

	rtc_time64_to_tm(seconds, tm);

	return 0;
}

static int ssd202d_rtc_reset_counter(struct ssd202d_rtc *priv)
{
	u16 val;

	val = readw(priv->base + REG_CTRL);
	writew(val | CNT_RST_BIT, priv->base + REG_CTRL);
	ssd202d_rtc_isoctrl(priv);
	writew(val & ~CNT_RST_BIT, priv->base + REG_CTRL);
	ssd202d_rtc_isoctrl(priv);

	return 0;
}

static int ssd202d_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct ssd202d_rtc *priv = dev_get_drvdata(dev);
	unsigned long seconds = rtc_tm_to_time64(tm);

	ssd202d_rtc_write_reg(priv, REG_CTRL, BASE_WR_BIT, seconds);
	ssd202d_rtc_reset_counter(priv);
	ssd202d_rtc_write_reg(priv, REG_CTRL, SW0_WR_BIT, 1);

	return 0;
}

static const struct rtc_class_ops ssd202d_rtc_ops = {
	.read_time = ssd202d_rtc_read_time,
	.set_time = ssd202d_rtc_set_time,
};

static int ssd202d_rtc_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct ssd202d_rtc *priv;

	priv = devm_kzalloc(&pdev->dev, sizeof(struct ssd202d_rtc), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(priv->base))
		return PTR_ERR(priv->base);

	priv->rtc_dev = devm_rtc_allocate_device(dev);
	if (IS_ERR(priv->rtc_dev))
		return PTR_ERR(priv->rtc_dev);

	priv->rtc_dev->ops = &ssd202d_rtc_ops;
	priv->rtc_dev->range_max = U32_MAX;

	platform_set_drvdata(pdev, priv);

	return devm_rtc_register_device(priv->rtc_dev);
}

static const struct of_device_id ssd202d_rtc_of_match_table[] = {
	{ .compatible = "mstar,ssd202d-rtc" },
	{ }
};
MODULE_DEVICE_TABLE(of, ssd202d_rtc_of_match_table);

static struct platform_driver ssd202d_rtc_driver = {
	.probe = ssd202d_rtc_probe,
	.driver = {
		.name = "ssd202d-rtc",
		.of_match_table = ssd202d_rtc_of_match_table,
	},
};
module_platform_driver(ssd202d_rtc_driver);

MODULE_AUTHOR("Daniel Palmer <daniel@thingy.jp>");
MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
MODULE_DESCRIPTION("MStar SSD202D RTC Driver");
MODULE_LICENSE("GPL");