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#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/platform_device.h>
37#include <linux/init.h>
38#include <linux/rtc.h>
39#include <linux/spi/spi.h>
40#include <linux/bcd.h>
41
42#define RX4581_REG_SC 0x00
43#define RX4581_REG_MN 0x01
44#define RX4581_REG_HR 0x02
45#define RX4581_REG_DW 0x03
46#define RX4581_REG_DM 0x04
47#define RX4581_REG_MO 0x05
48#define RX4581_REG_YR 0x06
49#define RX4581_REG_RAM 0x07
50#define RX4581_REG_AMN 0x08
51#define RX4581_REG_AHR 0x09
52#define RX4581_REG_ADM 0x0A
53#define RX4581_REG_ADW 0x0A
54#define RX4581_REG_TMR0 0x0B
55#define RX4581_REG_TMR1 0x0C
56#define RX4581_REG_EXT 0x0D
57#define RX4581_REG_FLAG 0x0E
58#define RX4581_REG_CTRL 0x0F
59
60
61
62#define RX4581_FLAG_UF 0x20
63#define RX4581_FLAG_TF 0x10
64#define RX4581_FLAG_AF 0x08
65#define RX4581_FLAG_VLF 0x02
66
67
68#define RX4581_CTRL_UIE 0x20
69#define RX4581_CTRL_TIE 0x10
70#define RX4581_CTRL_AIE 0x08
71#define RX4581_CTRL_STOP 0x02
72#define RX4581_CTRL_RESET 0x01
73
74static int rx4581_set_reg(struct device *dev, unsigned char address,
75 unsigned char data)
76{
77 struct spi_device *spi = to_spi_device(dev);
78 unsigned char buf[2];
79
80
81 buf[0] = address & 0x0f;
82 buf[1] = data;
83
84 return spi_write_then_read(spi, buf, 2, NULL, 0);
85}
86
87static int rx4581_get_reg(struct device *dev, unsigned char address,
88 unsigned char *data)
89{
90 struct spi_device *spi = to_spi_device(dev);
91
92
93 *data = address | 0x80;
94
95 return spi_write_then_read(spi, data, 1, data, 1);
96}
97
98
99
100
101
102static int rx4581_get_datetime(struct device *dev, struct rtc_time *tm)
103{
104 struct spi_device *spi = to_spi_device(dev);
105 unsigned char date[7];
106 unsigned char data;
107 int err;
108
109
110
111
112
113
114 err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
115 if (err != 0) {
116 dev_err(dev, "Unable to read device flags\n");
117 return -EIO;
118 }
119
120 do {
121
122 if (data & RX4581_FLAG_UF) {
123 err = rx4581_set_reg(dev,
124 RX4581_REG_FLAG, (data & ~RX4581_FLAG_UF));
125 if (err != 0) {
126 dev_err(dev, "Unable to write device "
127 "flags\n");
128 return -EIO;
129 }
130 }
131
132
133 date[0] = 0x80;
134 err = spi_write_then_read(spi, date, 1, date, 7);
135 if (err < 0) {
136 dev_err(dev, "Unable to read date\n");
137 return -EIO;
138 }
139
140
141 err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
142 if (err != 0) {
143 dev_err(dev, "Unable to read device flags\n");
144 return -EIO;
145 }
146 } while (data & RX4581_FLAG_UF);
147
148 if (data & RX4581_FLAG_VLF)
149 dev_info(dev,
150 "low voltage detected, date/time is not reliable.\n");
151
152 dev_dbg(dev,
153 "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
154 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
155 __func__,
156 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
157
158 tm->tm_sec = bcd2bin(date[RX4581_REG_SC] & 0x7F);
159 tm->tm_min = bcd2bin(date[RX4581_REG_MN] & 0x7F);
160 tm->tm_hour = bcd2bin(date[RX4581_REG_HR] & 0x3F);
161 tm->tm_wday = ilog2(date[RX4581_REG_DW] & 0x7F);
162 tm->tm_mday = bcd2bin(date[RX4581_REG_DM] & 0x3F);
163 tm->tm_mon = bcd2bin(date[RX4581_REG_MO] & 0x1F) - 1;
164 tm->tm_year = bcd2bin(date[RX4581_REG_YR]);
165 if (tm->tm_year < 70)
166 tm->tm_year += 100;
167
168
169 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
170 "mday=%d, mon=%d, year=%d, wday=%d\n",
171 __func__,
172 tm->tm_sec, tm->tm_min, tm->tm_hour,
173 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
174
175 err = rtc_valid_tm(tm);
176 if (err < 0)
177 dev_err(dev, "retrieved date/time is not valid.\n");
178
179 return err;
180}
181
182static int rx4581_set_datetime(struct device *dev, struct rtc_time *tm)
183{
184 struct spi_device *spi = to_spi_device(dev);
185 int err;
186 unsigned char buf[8], data;
187
188 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
189 "mday=%d, mon=%d, year=%d, wday=%d\n",
190 __func__,
191 tm->tm_sec, tm->tm_min, tm->tm_hour,
192 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
193
194 buf[0] = 0x00;
195
196 buf[RX4581_REG_SC+1] = bin2bcd(tm->tm_sec);
197 buf[RX4581_REG_MN+1] = bin2bcd(tm->tm_min);
198 buf[RX4581_REG_HR+1] = bin2bcd(tm->tm_hour);
199
200 buf[RX4581_REG_DM+1] = bin2bcd(tm->tm_mday);
201
202
203 buf[RX4581_REG_MO+1] = bin2bcd(tm->tm_mon + 1);
204
205
206 buf[RX4581_REG_YR+1] = bin2bcd(tm->tm_year % 100);
207 buf[RX4581_REG_DW+1] = (0x1 << tm->tm_wday);
208
209
210 err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
211 if (err != 0) {
212 dev_err(dev, "Unable to read control register\n");
213 return -EIO;
214 }
215
216 err = rx4581_set_reg(dev, RX4581_REG_CTRL,
217 (data | RX4581_CTRL_STOP));
218 if (err != 0) {
219 dev_err(dev, "Unable to write control register\n");
220 return -EIO;
221 }
222
223
224 err = spi_write_then_read(spi, buf, 8, NULL, 0);
225 if (err != 0) {
226 dev_err(dev, "Unable to write to date registers\n");
227 return -EIO;
228 }
229
230
231 err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
232 if (err != 0) {
233 dev_err(dev, "Unable to read flag register\n");
234 return -EIO;
235 }
236
237 err = rx4581_set_reg(dev, RX4581_REG_FLAG,
238 (data & ~(RX4581_FLAG_VLF)));
239 if (err != 0) {
240 dev_err(dev, "Unable to write flag register\n");
241 return -EIO;
242 }
243
244
245 err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
246 if (err != 0) {
247 dev_err(dev, "Unable to read control register\n");
248 return -EIO;
249 }
250
251 err = rx4581_set_reg(dev, RX4581_REG_CTRL,
252 (data & ~(RX4581_CTRL_STOP)));
253 if (err != 0) {
254 dev_err(dev, "Unable to write control register\n");
255 return -EIO;
256 }
257
258 return 0;
259}
260
261static const struct rtc_class_ops rx4581_rtc_ops = {
262 .read_time = rx4581_get_datetime,
263 .set_time = rx4581_set_datetime,
264};
265
266static int rx4581_probe(struct spi_device *spi)
267{
268 struct rtc_device *rtc;
269 unsigned char tmp;
270 int res;
271
272 res = rx4581_get_reg(&spi->dev, RX4581_REG_SC, &tmp);
273 if (res != 0)
274 return res;
275
276 rtc = devm_rtc_device_register(&spi->dev, "rx4581",
277 &rx4581_rtc_ops, THIS_MODULE);
278 if (IS_ERR(rtc))
279 return PTR_ERR(rtc);
280
281 spi_set_drvdata(spi, rtc);
282 return 0;
283}
284
285static const struct spi_device_id rx4581_id[] = {
286 { "rx4581", 0 },
287 { }
288};
289MODULE_DEVICE_TABLE(spi, rx4581_id);
290
291static struct spi_driver rx4581_driver = {
292 .driver = {
293 .name = "rtc-rx4581",
294 },
295 .probe = rx4581_probe,
296 .id_table = rx4581_id,
297};
298
299module_spi_driver(rx4581_driver);
300
301MODULE_DESCRIPTION("rx4581 spi RTC driver");
302MODULE_AUTHOR("Torben Hohn");
303MODULE_LICENSE("GPL");
304MODULE_ALIAS("spi:rtc-rx4581");
305