Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / rtc / rtc-rx8010.c
blob8340ab47a05976e123f6ea643876dd7def9fc5c1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for the Epson RTC module RX-8010 SJ
5 * Copyright(C) Timesys Corporation 2015
6 * Copyright(C) General Electric Company 2015
7 */
9 #include <linux/bcd.h>
10 #include <linux/bitops.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/rtc.h>
17 #define RX8010_SEC 0x10
18 #define RX8010_MIN 0x11
19 #define RX8010_HOUR 0x12
20 #define RX8010_WDAY 0x13
21 #define RX8010_MDAY 0x14
22 #define RX8010_MONTH 0x15
23 #define RX8010_YEAR 0x16
24 #define RX8010_RESV17 0x17
25 #define RX8010_ALMIN 0x18
26 #define RX8010_ALHOUR 0x19
27 #define RX8010_ALWDAY 0x1A
28 #define RX8010_TCOUNT0 0x1B
29 #define RX8010_TCOUNT1 0x1C
30 #define RX8010_EXT 0x1D
31 #define RX8010_FLAG 0x1E
32 #define RX8010_CTRL 0x1F
33 /* 0x20 to 0x2F are user registers */
34 #define RX8010_RESV30 0x30
35 #define RX8010_RESV31 0x31
36 #define RX8010_IRQ 0x32
38 #define RX8010_EXT_WADA BIT(3)
40 #define RX8010_FLAG_VLF BIT(1)
41 #define RX8010_FLAG_AF BIT(3)
42 #define RX8010_FLAG_TF BIT(4)
43 #define RX8010_FLAG_UF BIT(5)
45 #define RX8010_CTRL_AIE BIT(3)
46 #define RX8010_CTRL_UIE BIT(5)
47 #define RX8010_CTRL_STOP BIT(6)
48 #define RX8010_CTRL_TEST BIT(7)
50 #define RX8010_ALARM_AE BIT(7)
52 static const struct i2c_device_id rx8010_id[] = {
53 { "rx8010", 0 },
54 { }
56 MODULE_DEVICE_TABLE(i2c, rx8010_id);
58 static const struct of_device_id rx8010_of_match[] = {
59 { .compatible = "epson,rx8010" },
60 { }
62 MODULE_DEVICE_TABLE(of, rx8010_of_match);
64 struct rx8010_data {
65 struct regmap *regs;
66 struct rtc_device *rtc;
67 u8 ctrlreg;
70 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
72 struct i2c_client *client = dev_id;
73 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
74 int flagreg, err;
76 mutex_lock(&rx8010->rtc->ops_lock);
78 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
79 if (err) {
80 mutex_unlock(&rx8010->rtc->ops_lock);
81 return IRQ_NONE;
84 if (flagreg & RX8010_FLAG_VLF)
85 dev_warn(&client->dev, "Frequency stop detected\n");
87 if (flagreg & RX8010_FLAG_TF) {
88 flagreg &= ~RX8010_FLAG_TF;
89 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
92 if (flagreg & RX8010_FLAG_AF) {
93 flagreg &= ~RX8010_FLAG_AF;
94 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
97 if (flagreg & RX8010_FLAG_UF) {
98 flagreg &= ~RX8010_FLAG_UF;
99 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
102 err = regmap_write(rx8010->regs, RX8010_FLAG, flagreg);
103 mutex_unlock(&rx8010->rtc->ops_lock);
104 return err ? IRQ_NONE : IRQ_HANDLED;
107 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
109 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
110 u8 date[RX8010_YEAR - RX8010_SEC + 1];
111 int flagreg, err;
113 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
114 if (err)
115 return err;
117 if (flagreg & RX8010_FLAG_VLF) {
118 dev_warn(dev, "Frequency stop detected\n");
119 return -EINVAL;
122 err = regmap_bulk_read(rx8010->regs, RX8010_SEC, date, sizeof(date));
123 if (err)
124 return err;
126 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
127 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
128 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
129 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
130 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
131 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
132 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
134 return 0;
137 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
139 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
140 u8 date[RX8010_YEAR - RX8010_SEC + 1];
141 int err;
143 /* set STOP bit before changing clock/calendar */
144 err = regmap_set_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
145 if (err)
146 return err;
148 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
149 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
150 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
151 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
152 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
153 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
154 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
156 err = regmap_bulk_write(rx8010->regs, RX8010_SEC, date, sizeof(date));
157 if (err)
158 return err;
160 /* clear STOP bit after changing clock/calendar */
161 err = regmap_clear_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
162 if (err)
163 return err;
165 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_VLF);
166 if (err)
167 return err;
169 return 0;
172 static int rx8010_init(struct device *dev)
174 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
175 u8 ctrl[2];
176 int need_clear = 0, err;
178 /* Initialize reserved registers as specified in datasheet */
179 err = regmap_write(rx8010->regs, RX8010_RESV17, 0xD8);
180 if (err)
181 return err;
183 err = regmap_write(rx8010->regs, RX8010_RESV30, 0x00);
184 if (err)
185 return err;
187 err = regmap_write(rx8010->regs, RX8010_RESV31, 0x08);
188 if (err)
189 return err;
191 err = regmap_write(rx8010->regs, RX8010_IRQ, 0x00);
192 if (err)
193 return err;
195 err = regmap_bulk_read(rx8010->regs, RX8010_FLAG, ctrl, 2);
196 if (err)
197 return err;
199 if (ctrl[0] & RX8010_FLAG_VLF)
200 dev_warn(dev, "Frequency stop was detected\n");
202 if (ctrl[0] & RX8010_FLAG_AF) {
203 dev_warn(dev, "Alarm was detected\n");
204 need_clear = 1;
207 if (ctrl[0] & RX8010_FLAG_TF)
208 need_clear = 1;
210 if (ctrl[0] & RX8010_FLAG_UF)
211 need_clear = 1;
213 if (need_clear) {
214 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
215 err = regmap_write(rx8010->regs, RX8010_FLAG, ctrl[0]);
216 if (err)
217 return err;
220 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
222 return 0;
225 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
227 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
228 u8 alarmvals[3];
229 int flagreg, err;
231 err = regmap_bulk_read(rx8010->regs, RX8010_ALMIN, alarmvals, 3);
232 if (err)
233 return err;
235 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
236 if (err)
237 return err;
239 t->time.tm_sec = 0;
240 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
241 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
243 if (!(alarmvals[2] & RX8010_ALARM_AE))
244 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
246 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
247 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
249 return 0;
252 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
254 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
255 u8 alarmvals[3];
256 int err;
258 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
259 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
260 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
261 if (err)
262 return err;
265 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
266 if (err)
267 return err;
269 alarmvals[0] = bin2bcd(t->time.tm_min);
270 alarmvals[1] = bin2bcd(t->time.tm_hour);
271 alarmvals[2] = bin2bcd(t->time.tm_mday);
273 err = regmap_bulk_write(rx8010->regs, RX8010_ALMIN, alarmvals, 2);
274 if (err)
275 return err;
277 err = regmap_clear_bits(rx8010->regs, RX8010_EXT, RX8010_EXT_WADA);
278 if (err)
279 return err;
281 if (alarmvals[2] == 0)
282 alarmvals[2] |= RX8010_ALARM_AE;
284 err = regmap_write(rx8010->regs, RX8010_ALWDAY, alarmvals[2]);
285 if (err)
286 return err;
288 if (t->enabled) {
289 if (rx8010->rtc->uie_rtctimer.enabled)
290 rx8010->ctrlreg |= RX8010_CTRL_UIE;
291 if (rx8010->rtc->aie_timer.enabled)
292 rx8010->ctrlreg |=
293 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
295 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
296 if (err)
297 return err;
300 return 0;
303 static int rx8010_alarm_irq_enable(struct device *dev,
304 unsigned int enabled)
306 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
307 int err;
308 u8 ctrl;
310 ctrl = rx8010->ctrlreg;
312 if (enabled) {
313 if (rx8010->rtc->uie_rtctimer.enabled)
314 ctrl |= RX8010_CTRL_UIE;
315 if (rx8010->rtc->aie_timer.enabled)
316 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
317 } else {
318 if (!rx8010->rtc->uie_rtctimer.enabled)
319 ctrl &= ~RX8010_CTRL_UIE;
320 if (!rx8010->rtc->aie_timer.enabled)
321 ctrl &= ~RX8010_CTRL_AIE;
324 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
325 if (err)
326 return err;
328 if (ctrl != rx8010->ctrlreg) {
329 rx8010->ctrlreg = ctrl;
330 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
331 if (err)
332 return err;
335 return 0;
338 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
340 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
341 int tmp, flagreg, err;
343 switch (cmd) {
344 case RTC_VL_READ:
345 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
346 if (err)
347 return err;
349 tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0;
350 return put_user(tmp, (unsigned int __user *)arg);
352 default:
353 return -ENOIOCTLCMD;
357 static const struct rtc_class_ops rx8010_rtc_ops_default = {
358 .read_time = rx8010_get_time,
359 .set_time = rx8010_set_time,
360 .ioctl = rx8010_ioctl,
363 static const struct rtc_class_ops rx8010_rtc_ops_alarm = {
364 .read_time = rx8010_get_time,
365 .set_time = rx8010_set_time,
366 .ioctl = rx8010_ioctl,
367 .read_alarm = rx8010_read_alarm,
368 .set_alarm = rx8010_set_alarm,
369 .alarm_irq_enable = rx8010_alarm_irq_enable,
372 static const struct regmap_config rx8010_regmap_config = {
373 .name = "rx8010-rtc",
374 .reg_bits = 8,
375 .val_bits = 8,
378 static int rx8010_probe(struct i2c_client *client)
380 struct device *dev = &client->dev;
381 struct rx8010_data *rx8010;
382 int err = 0;
384 rx8010 = devm_kzalloc(dev, sizeof(*rx8010), GFP_KERNEL);
385 if (!rx8010)
386 return -ENOMEM;
388 i2c_set_clientdata(client, rx8010);
390 rx8010->regs = devm_regmap_init_i2c(client, &rx8010_regmap_config);
391 if (IS_ERR(rx8010->regs))
392 return PTR_ERR(rx8010->regs);
394 err = rx8010_init(dev);
395 if (err)
396 return err;
398 rx8010->rtc = devm_rtc_allocate_device(dev);
399 if (IS_ERR(rx8010->rtc))
400 return PTR_ERR(rx8010->rtc);
402 if (client->irq > 0) {
403 dev_info(dev, "IRQ %d supplied\n", client->irq);
404 err = devm_request_threaded_irq(dev, client->irq, NULL,
405 rx8010_irq_1_handler,
406 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
407 "rx8010", client);
408 if (err) {
409 dev_err(dev, "unable to request IRQ\n");
410 return err;
413 rx8010->rtc->ops = &rx8010_rtc_ops_alarm;
414 } else {
415 rx8010->rtc->ops = &rx8010_rtc_ops_default;
418 rx8010->rtc->max_user_freq = 1;
419 rx8010->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
420 rx8010->rtc->range_max = RTC_TIMESTAMP_END_2099;
422 return devm_rtc_register_device(rx8010->rtc);
425 static struct i2c_driver rx8010_driver = {
426 .driver = {
427 .name = "rtc-rx8010",
428 .of_match_table = of_match_ptr(rx8010_of_match),
430 .probe_new = rx8010_probe,
431 .id_table = rx8010_id,
434 module_i2c_driver(rx8010_driver);
436 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
437 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
438 MODULE_LICENSE("GPL v2");