1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Epson's RTC module RX-8025 SA/NB
5 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
7 * Copyright (C) 2005 by Digi International Inc.
10 * Modified by fengjh at rising.com.cn
11 * <lm-sensors@lm-sensors.org>
14 * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
15 * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
16 * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
18 #include <linux/bcd.h>
19 #include <linux/bitops.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/rtc.h>
25 /* Register definitions */
26 #define RX8025_REG_SEC 0x00
27 #define RX8025_REG_MIN 0x01
28 #define RX8025_REG_HOUR 0x02
29 #define RX8025_REG_WDAY 0x03
30 #define RX8025_REG_MDAY 0x04
31 #define RX8025_REG_MONTH 0x05
32 #define RX8025_REG_YEAR 0x06
33 #define RX8025_REG_DIGOFF 0x07
34 #define RX8025_REG_ALWMIN 0x08
35 #define RX8025_REG_ALWHOUR 0x09
36 #define RX8025_REG_ALWWDAY 0x0a
37 #define RX8025_REG_ALDMIN 0x0b
38 #define RX8025_REG_ALDHOUR 0x0c
39 /* 0x0d is reserved */
40 #define RX8025_REG_CTRL1 0x0e
41 #define RX8025_REG_CTRL2 0x0f
43 #define RX8025_BIT_CTRL1_CT (7 << 0)
44 /* 1 Hz periodic level irq */
45 #define RX8025_BIT_CTRL1_CT_1HZ 4
46 #define RX8025_BIT_CTRL1_TEST BIT(3)
47 #define RX8025_BIT_CTRL1_1224 BIT(5)
48 #define RX8025_BIT_CTRL1_DALE BIT(6)
49 #define RX8025_BIT_CTRL1_WALE BIT(7)
51 #define RX8025_BIT_CTRL2_DAFG BIT(0)
52 #define RX8025_BIT_CTRL2_WAFG BIT(1)
53 #define RX8025_BIT_CTRL2_CTFG BIT(2)
54 #define RX8025_BIT_CTRL2_PON BIT(4)
55 #define RX8025_BIT_CTRL2_XST BIT(5)
56 #define RX8025_BIT_CTRL2_VDET BIT(6)
58 /* Clock precision adjustment */
59 #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
60 #define RX8025_ADJ_DATA_MAX 62
61 #define RX8025_ADJ_DATA_MIN -62
70 static const struct i2c_device_id rx8025_id[] = {
71 { "rx8025", model_rx_8025 },
72 { "rx8035", model_rx_8035 },
75 MODULE_DEVICE_TABLE(i2c, rx8025_id);
78 struct rtc_device *rtc;
83 static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
85 return i2c_smbus_read_byte_data(client, number << 4);
88 static int rx8025_read_regs(const struct i2c_client *client,
89 u8 number, u8 length, u8 *values)
91 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
94 return ret < 0 ? ret : -EIO;
99 static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
102 return i2c_smbus_write_byte_data(client, number << 4, value);
105 static s32 rx8025_write_regs(const struct i2c_client *client,
106 u8 number, u8 length, const u8 *values)
108 return i2c_smbus_write_i2c_block_data(client, number << 4,
112 static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
114 int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
115 /* XSTP bit has different polarity on RX-8025 vs RX-8035.
116 * RX-8025: 0 == oscillator stopped
117 * RX-8035: 1 == oscillator stopped
120 if (model == model_rx_8025)
126 static int rx8025_check_validity(struct device *dev)
128 struct i2c_client *client = to_i2c_client(dev);
129 struct rx8025_data *drvdata = dev_get_drvdata(dev);
133 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
137 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
138 dev_warn(dev, "power voltage drop detected\n");
140 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
141 dev_warn(dev, "power-on reset detected, date is invalid\n");
145 xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
147 dev_warn(dev, "crystal stopped, date is invalid\n");
154 static int rx8025_reset_validity(struct i2c_client *client)
156 struct rx8025_data *drvdata = i2c_get_clientdata(client);
157 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
162 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
164 if (drvdata->model == model_rx_8025)
165 ctrl2 |= RX8025_BIT_CTRL2_XST;
167 ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
169 return rx8025_write_reg(client, RX8025_REG_CTRL2,
173 static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
175 struct i2c_client *client = dev_id;
176 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
179 rtc_lock(rx8025->rtc);
180 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
184 xstp = rx8025_is_osc_stopped(rx8025->model, status);
186 dev_warn(&client->dev, "Oscillation stop was detected,"
187 "you may have to readjust the clock\n");
189 if (status & RX8025_BIT_CTRL2_CTFG) {
191 status &= ~RX8025_BIT_CTRL2_CTFG;
192 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
195 if (status & RX8025_BIT_CTRL2_DAFG) {
197 status &= RX8025_BIT_CTRL2_DAFG;
198 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
199 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
201 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
205 rtc_unlock(rx8025->rtc);
210 static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
212 struct i2c_client *client = to_i2c_client(dev);
213 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
217 err = rx8025_check_validity(dev);
221 err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
225 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
227 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
228 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
229 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
230 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
232 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
233 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
235 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
236 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
237 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
239 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
244 static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
246 struct i2c_client *client = to_i2c_client(dev);
247 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
251 if ((dt->tm_year < 100) || (dt->tm_year > 199))
255 * Here the read-only bits are written as "0". I'm not sure if that
258 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
259 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
260 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
261 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
263 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
264 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
266 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
267 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
268 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
269 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
271 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
273 ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
277 return rx8025_reset_validity(client);
280 static int rx8025_init_client(struct i2c_client *client)
282 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
287 err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
291 /* Keep test bit zero ! */
292 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
294 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
295 dev_warn(&client->dev, "Alarm was detected\n");
299 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
304 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
305 RX8025_BIT_CTRL2_DAFG);
307 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
314 static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
316 struct i2c_client *client = to_i2c_client(dev);
317 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
321 if (client->irq <= 0)
324 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
328 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
332 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
333 __func__, ald[0], ald[1], ctrl2);
335 /* Hardware alarms precision is 1 minute! */
337 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
338 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
339 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
341 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
342 + (ald[1] & 0x20 ? 12 : 0);
344 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
345 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
346 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
351 static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
353 struct i2c_client *client = to_i2c_client(dev);
354 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
358 if (client->irq <= 0)
362 * Hardware alarm precision is 1 minute!
363 * round up to nearest minute
365 if (t->time.tm_sec) {
366 time64_t alarm_time = rtc_tm_to_time64(&t->time);
368 alarm_time += 60 - t->time.tm_sec;
369 rtc_time64_to_tm(alarm_time, &t->time);
372 ald[0] = bin2bcd(t->time.tm_min);
373 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
374 ald[1] = bin2bcd(t->time.tm_hour);
376 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
377 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
379 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
381 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
382 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
383 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
388 err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
393 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
394 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
403 static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
405 struct i2c_client *client = to_i2c_client(dev);
406 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
410 ctrl1 = rx8025->ctrl1;
412 ctrl1 |= RX8025_BIT_CTRL1_DALE;
414 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
416 if (ctrl1 != rx8025->ctrl1) {
417 rx8025->ctrl1 = ctrl1;
418 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
426 static const struct rtc_class_ops rx8025_rtc_ops = {
427 .read_time = rx8025_get_time,
428 .set_time = rx8025_set_time,
429 .read_alarm = rx8025_read_alarm,
430 .set_alarm = rx8025_set_alarm,
431 .alarm_irq_enable = rx8025_alarm_irq_enable,
435 * Clock precision adjustment support
437 * According to the RX8025 SA/NB application manual the frequency and
438 * temperature characteristics can be approximated using the following
441 * df = a * (ut - t)**2
443 * df: Frequency deviation in any temperature
444 * a : Coefficient = (-35 +-5) * 10**-9
445 * ut: Ultimate temperature in degree = +25 +-5 degree
446 * t : Any temperature in degree
448 * Note that the clock adjustment in ppb must be entered (which is
449 * the negative value of the deviation).
451 static int rx8025_get_clock_adjust(struct device *dev, int *adj)
453 struct i2c_client *client = to_i2c_client(dev);
456 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
460 *adj = digoff >= 64 ? digoff - 128 : digoff;
463 *adj *= -RX8025_ADJ_RESOLUTION;
468 static int rx8025_set_clock_adjust(struct device *dev, int adj)
470 struct i2c_client *client = to_i2c_client(dev);
474 adj /= -RX8025_ADJ_RESOLUTION;
475 if (adj > RX8025_ADJ_DATA_MAX)
476 adj = RX8025_ADJ_DATA_MAX;
477 else if (adj < RX8025_ADJ_DATA_MIN)
478 adj = RX8025_ADJ_DATA_MIN;
485 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
489 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
494 static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
495 struct device_attribute *attr,
500 err = rx8025_get_clock_adjust(dev, &adj);
504 return sprintf(buf, "%d\n", adj);
507 static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
513 if (sscanf(buf, "%i", &adj) != 1)
516 err = rx8025_set_clock_adjust(dev, adj);
518 return err ? err : count;
521 static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
522 rx8025_sysfs_show_clock_adjust,
523 rx8025_sysfs_store_clock_adjust);
525 static int rx8025_sysfs_register(struct device *dev)
527 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
530 static void rx8025_sysfs_unregister(struct device *dev)
532 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
535 static int rx8025_probe(struct i2c_client *client,
536 const struct i2c_device_id *id)
538 struct i2c_adapter *adapter = client->adapter;
539 struct rx8025_data *rx8025;
542 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
543 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
544 dev_err(&adapter->dev,
545 "doesn't support required functionality\n");
549 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
553 i2c_set_clientdata(client, rx8025);
556 rx8025->model = id->driver_data;
558 err = rx8025_init_client(client);
562 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
563 &rx8025_rtc_ops, THIS_MODULE);
564 if (IS_ERR(rx8025->rtc)) {
565 dev_err(&client->dev, "unable to register the class device\n");
566 return PTR_ERR(rx8025->rtc);
569 if (client->irq > 0) {
570 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
571 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
576 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
581 rx8025->rtc->max_user_freq = 1;
583 /* the rx8025 alarm only supports a minute accuracy */
584 rx8025->rtc->uie_unsupported = 1;
586 err = rx8025_sysfs_register(&client->dev);
590 static int rx8025_remove(struct i2c_client *client)
592 rx8025_sysfs_unregister(&client->dev);
596 static struct i2c_driver rx8025_driver = {
598 .name = "rtc-rx8025",
600 .probe = rx8025_probe,
601 .remove = rx8025_remove,
602 .id_table = rx8025_id,
605 module_i2c_driver(rx8025_driver);
607 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
608 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
609 MODULE_LICENSE("GPL");