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/kstrtox.h>
23 #include <linux/module.h>
24 #include <linux/rtc.h>
26 /* Register definitions */
27 #define RX8025_REG_SEC 0x00
28 #define RX8025_REG_MIN 0x01
29 #define RX8025_REG_HOUR 0x02
30 #define RX8025_REG_WDAY 0x03
31 #define RX8025_REG_MDAY 0x04
32 #define RX8025_REG_MONTH 0x05
33 #define RX8025_REG_YEAR 0x06
34 #define RX8025_REG_DIGOFF 0x07
35 #define RX8025_REG_ALWMIN 0x08
36 #define RX8025_REG_ALWHOUR 0x09
37 #define RX8025_REG_ALWWDAY 0x0a
38 #define RX8025_REG_ALDMIN 0x0b
39 #define RX8025_REG_ALDHOUR 0x0c
40 /* 0x0d is reserved */
41 #define RX8025_REG_CTRL1 0x0e
42 #define RX8025_REG_CTRL2 0x0f
44 #define RX8025_BIT_CTRL1_CT (7 << 0)
45 /* 1 Hz periodic level irq */
46 #define RX8025_BIT_CTRL1_CT_1HZ 4
47 #define RX8025_BIT_CTRL1_TEST BIT(3)
48 #define RX8025_BIT_CTRL1_1224 BIT(5)
49 #define RX8025_BIT_CTRL1_DALE BIT(6)
50 #define RX8025_BIT_CTRL1_WALE BIT(7)
52 #define RX8025_BIT_CTRL2_DAFG BIT(0)
53 #define RX8025_BIT_CTRL2_WAFG BIT(1)
54 #define RX8025_BIT_CTRL2_CTFG BIT(2)
55 #define RX8025_BIT_CTRL2_PON BIT(4)
56 #define RX8025_BIT_CTRL2_XST BIT(5)
57 #define RX8025_BIT_CTRL2_VDET BIT(6)
59 #define RX8035_BIT_HOUR_1224 BIT(7)
61 /* Clock precision adjustment */
62 #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
63 #define RX8025_ADJ_DATA_MAX 62
64 #define RX8025_ADJ_DATA_MIN -62
73 static const struct i2c_device_id rx8025_id[] = {
74 { "rx8025", model_rx_8025 },
75 { "rx8035", model_rx_8035 },
78 MODULE_DEVICE_TABLE(i2c, rx8025_id);
81 struct rtc_device *rtc;
87 static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
89 return i2c_smbus_read_byte_data(client, number << 4);
92 static int rx8025_read_regs(const struct i2c_client *client,
93 u8 number, u8 length, u8 *values)
95 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
98 return ret < 0 ? ret : -EIO;
103 static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
106 return i2c_smbus_write_byte_data(client, number << 4, value);
109 static s32 rx8025_write_regs(const struct i2c_client *client,
110 u8 number, u8 length, const u8 *values)
112 return i2c_smbus_write_i2c_block_data(client, number << 4,
116 static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
118 int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
119 /* XSTP bit has different polarity on RX-8025 vs RX-8035.
120 * RX-8025: 0 == oscillator stopped
121 * RX-8035: 1 == oscillator stopped
124 if (model == model_rx_8025)
130 static int rx8025_check_validity(struct device *dev)
132 struct i2c_client *client = to_i2c_client(dev);
133 struct rx8025_data *drvdata = dev_get_drvdata(dev);
137 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
141 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
142 dev_warn(dev, "power voltage drop detected\n");
144 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
145 dev_warn(dev, "power-on reset detected, date is invalid\n");
149 xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
151 dev_warn(dev, "crystal stopped, date is invalid\n");
158 static int rx8025_reset_validity(struct i2c_client *client)
160 struct rx8025_data *drvdata = i2c_get_clientdata(client);
161 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
166 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
168 if (drvdata->model == model_rx_8025)
169 ctrl2 |= RX8025_BIT_CTRL2_XST;
171 ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
173 return rx8025_write_reg(client, RX8025_REG_CTRL2,
177 static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
179 struct i2c_client *client = dev_id;
180 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
183 rtc_lock(rx8025->rtc);
184 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
188 xstp = rx8025_is_osc_stopped(rx8025->model, status);
190 dev_warn(&client->dev, "Oscillation stop was detected,"
191 "you may have to readjust the clock\n");
193 if (status & RX8025_BIT_CTRL2_CTFG) {
195 status &= ~RX8025_BIT_CTRL2_CTFG;
196 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
199 if (status & RX8025_BIT_CTRL2_DAFG) {
201 status &= RX8025_BIT_CTRL2_DAFG;
202 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
203 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
205 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
209 rtc_unlock(rx8025->rtc);
214 static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
216 struct i2c_client *client = to_i2c_client(dev);
217 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
221 err = rx8025_check_validity(dev);
225 err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
229 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
231 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
232 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
234 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
236 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
237 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
239 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
240 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
241 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
243 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
248 static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
250 struct i2c_client *client = to_i2c_client(dev);
251 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
256 * Here the read-only bits are written as "0". I'm not sure if that
259 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
260 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
262 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
264 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
265 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
267 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
268 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
269 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
270 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
272 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
274 ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
278 return rx8025_reset_validity(client);
281 static int rx8025_init_client(struct i2c_client *client)
283 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
289 err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
293 /* Keep test bit zero ! */
294 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
296 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
297 dev_warn(&client->dev, "Alarm was detected\n");
301 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
306 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
307 RX8025_BIT_CTRL2_DAFG);
309 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
312 if (rx8025->model == model_rx_8035) {
313 /* In RX-8035, 12/24 flag is in the hour register */
314 hour_reg = rx8025_read_reg(client, RX8025_REG_HOUR);
317 rx8025->is_24 = (hour_reg & RX8035_BIT_HOUR_1224);
319 rx8025->is_24 = (ctrl[1] & RX8025_BIT_CTRL1_1224);
326 static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
328 struct i2c_client *client = to_i2c_client(dev);
329 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
333 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
337 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
341 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
342 __func__, ald[0], ald[1], ctrl2);
344 /* Hardware alarms precision is 1 minute! */
346 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
348 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
350 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
351 + (ald[1] & 0x20 ? 12 : 0);
353 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
354 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
355 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
360 static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
362 struct i2c_client *client = to_i2c_client(dev);
363 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
367 ald[0] = bin2bcd(t->time.tm_min);
369 ald[1] = bin2bcd(t->time.tm_hour);
371 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
372 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
374 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
376 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
377 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
378 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
383 err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
388 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
389 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
398 static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
400 struct i2c_client *client = to_i2c_client(dev);
401 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
405 ctrl1 = rx8025->ctrl1;
407 ctrl1 |= RX8025_BIT_CTRL1_DALE;
409 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
411 if (ctrl1 != rx8025->ctrl1) {
412 rx8025->ctrl1 = ctrl1;
413 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
422 * According to the RX8025 SA/NB application manual the frequency and
423 * temperature characteristics can be approximated using the following
426 * df = a * (ut - t)**2
428 * df: Frequency deviation in any temperature
429 * a : Coefficient = (-35 +-5) * 10**-9
430 * ut: Ultimate temperature in degree = +25 +-5 degree
431 * t : Any temperature in degree
433 static int rx8025_read_offset(struct device *dev, long *offset)
435 struct i2c_client *client = to_i2c_client(dev);
438 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
442 *offset = digoff >= 64 ? digoff - 128 : digoff;
445 *offset *= RX8025_ADJ_RESOLUTION;
450 static int rx8025_set_offset(struct device *dev, long offset)
452 struct i2c_client *client = to_i2c_client(dev);
455 offset /= RX8025_ADJ_RESOLUTION;
456 if (offset > RX8025_ADJ_DATA_MAX)
457 offset = RX8025_ADJ_DATA_MAX;
458 else if (offset < RX8025_ADJ_DATA_MIN)
459 offset = RX8025_ADJ_DATA_MIN;
466 return rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
469 static const struct rtc_class_ops rx8025_rtc_ops = {
470 .read_time = rx8025_get_time,
471 .set_time = rx8025_set_time,
472 .read_alarm = rx8025_read_alarm,
473 .set_alarm = rx8025_set_alarm,
474 .alarm_irq_enable = rx8025_alarm_irq_enable,
475 .read_offset = rx8025_read_offset,
476 .set_offset = rx8025_set_offset,
479 static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
480 struct device_attribute *attr,
486 dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
487 err = rx8025_read_offset(dev, &adj);
491 return sprintf(buf, "%ld\n", -adj);
494 static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
501 dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
502 if (kstrtol(buf, 10, &adj) != 0)
505 err = rx8025_set_offset(dev, -adj);
507 return err ? err : count;
510 static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
511 rx8025_sysfs_show_clock_adjust,
512 rx8025_sysfs_store_clock_adjust);
514 static struct attribute *rx8025_attrs[] = {
515 &dev_attr_clock_adjust_ppb.attr,
519 static const struct attribute_group rx8025_attr_group = {
520 .attrs = rx8025_attrs,
523 static int rx8025_probe(struct i2c_client *client)
525 const struct i2c_device_id *id = i2c_match_id(rx8025_id, client);
526 struct i2c_adapter *adapter = client->adapter;
527 struct rx8025_data *rx8025;
530 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
531 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
532 dev_err(&adapter->dev,
533 "doesn't support required functionality\n");
537 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
541 i2c_set_clientdata(client, rx8025);
544 rx8025->model = id->driver_data;
546 err = rx8025_init_client(client);
550 rx8025->rtc = devm_rtc_allocate_device(&client->dev);
551 if (IS_ERR(rx8025->rtc))
552 return PTR_ERR(rx8025->rtc);
554 rx8025->rtc->ops = &rx8025_rtc_ops;
555 rx8025->rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
556 rx8025->rtc->range_max = RTC_TIMESTAMP_END_2099;
558 if (client->irq > 0) {
559 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
560 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
565 clear_bit(RTC_FEATURE_ALARM, rx8025->rtc->features);
568 rx8025->rtc->max_user_freq = 1;
570 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rx8025->rtc->features);
571 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rx8025->rtc->features);
573 err = rtc_add_group(rx8025->rtc, &rx8025_attr_group);
577 return devm_rtc_register_device(rx8025->rtc);
580 static struct i2c_driver rx8025_driver = {
582 .name = "rtc-rx8025",
584 .probe = rx8025_probe,
585 .id_table = rx8025_id,
588 module_i2c_driver(rx8025_driver);
590 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
591 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
592 MODULE_LICENSE("GPL");