2 * Driver for Epson's RTC module RX-8025 SA/NB
4 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
6 * Copyright (C) 2005 by Digi International Inc.
9 * Modified by fengjh at rising.com.cn
10 * <lm-sensors@lm-sensors.org>
13 * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
14 * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
15 * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * version 2 as published by the Free Software Foundation.
21 #include <linux/bcd.h>
22 #include <linux/bitops.h>
23 #include <linux/i2c.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/rtc.h>
28 /* Register definitions */
29 #define RX8025_REG_SEC 0x00
30 #define RX8025_REG_MIN 0x01
31 #define RX8025_REG_HOUR 0x02
32 #define RX8025_REG_WDAY 0x03
33 #define RX8025_REG_MDAY 0x04
34 #define RX8025_REG_MONTH 0x05
35 #define RX8025_REG_YEAR 0x06
36 #define RX8025_REG_DIGOFF 0x07
37 #define RX8025_REG_ALWMIN 0x08
38 #define RX8025_REG_ALWHOUR 0x09
39 #define RX8025_REG_ALWWDAY 0x0a
40 #define RX8025_REG_ALDMIN 0x0b
41 #define RX8025_REG_ALDHOUR 0x0c
42 /* 0x0d is reserved */
43 #define RX8025_REG_CTRL1 0x0e
44 #define RX8025_REG_CTRL2 0x0f
46 #define RX8025_BIT_CTRL1_CT (7 << 0)
47 /* 1 Hz periodic level irq */
48 #define RX8025_BIT_CTRL1_CT_1HZ 4
49 #define RX8025_BIT_CTRL1_TEST BIT(3)
50 #define RX8025_BIT_CTRL1_1224 BIT(5)
51 #define RX8025_BIT_CTRL1_DALE BIT(6)
52 #define RX8025_BIT_CTRL1_WALE BIT(7)
54 #define RX8025_BIT_CTRL2_DAFG BIT(0)
55 #define RX8025_BIT_CTRL2_WAFG BIT(1)
56 #define RX8025_BIT_CTRL2_CTFG BIT(2)
57 #define RX8025_BIT_CTRL2_PON BIT(4)
58 #define RX8025_BIT_CTRL2_XST BIT(5)
59 #define RX8025_BIT_CTRL2_VDET BIT(6)
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
66 static const struct i2c_device_id rx8025_id
[] = {
70 MODULE_DEVICE_TABLE(i2c
, rx8025_id
);
73 struct i2c_client
*client
;
74 struct rtc_device
*rtc
;
78 static s32
rx8025_read_reg(const struct i2c_client
*client
, u8 number
)
80 return i2c_smbus_read_byte_data(client
, number
<< 4);
83 static int rx8025_read_regs(const struct i2c_client
*client
,
84 u8 number
, u8 length
, u8
*values
)
86 int ret
= i2c_smbus_read_i2c_block_data(client
, number
<< 4, length
,
89 return ret
< 0 ? ret
: -EIO
;
94 static s32
rx8025_write_reg(const struct i2c_client
*client
, u8 number
,
97 return i2c_smbus_write_byte_data(client
, number
<< 4, value
);
100 static s32
rx8025_write_regs(const struct i2c_client
*client
,
101 u8 number
, u8 length
, const u8
*values
)
103 return i2c_smbus_write_i2c_block_data(client
, number
<< 4,
107 static int rx8025_check_validity(struct device
*dev
)
109 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
112 ctrl2
= rx8025_read_reg(rx8025
->client
, RX8025_REG_CTRL2
);
116 if (ctrl2
& RX8025_BIT_CTRL2_VDET
)
117 dev_warn(dev
, "power voltage drop detected\n");
119 if (ctrl2
& RX8025_BIT_CTRL2_PON
) {
120 dev_warn(dev
, "power-on reset detected, date is invalid\n");
124 if (!(ctrl2
& RX8025_BIT_CTRL2_XST
)) {
125 dev_warn(dev
, "crystal stopped, date is invalid\n");
132 static int rx8025_reset_validity(struct i2c_client
*client
)
134 int ctrl2
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
139 ctrl2
&= ~(RX8025_BIT_CTRL2_PON
| RX8025_BIT_CTRL2_VDET
);
141 return rx8025_write_reg(client
, RX8025_REG_CTRL2
,
142 ctrl2
| RX8025_BIT_CTRL2_XST
);
145 static irqreturn_t
rx8025_handle_irq(int irq
, void *dev_id
)
147 struct i2c_client
*client
= dev_id
;
148 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
149 struct mutex
*lock
= &rx8025
->rtc
->ops_lock
;
153 status
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
157 if (!(status
& RX8025_BIT_CTRL2_XST
))
158 dev_warn(&client
->dev
, "Oscillation stop was detected,"
159 "you may have to readjust the clock\n");
161 if (status
& RX8025_BIT_CTRL2_CTFG
) {
163 status
&= ~RX8025_BIT_CTRL2_CTFG
;
164 rtc_update_irq(rx8025
->rtc
, 1, RTC_PF
| RTC_IRQF
);
167 if (status
& RX8025_BIT_CTRL2_DAFG
) {
169 status
&= RX8025_BIT_CTRL2_DAFG
;
170 if (rx8025_write_reg(client
, RX8025_REG_CTRL1
,
171 rx8025
->ctrl1
& ~RX8025_BIT_CTRL1_DALE
))
173 rtc_update_irq(rx8025
->rtc
, 1, RTC_AF
| RTC_IRQF
);
182 static int rx8025_get_time(struct device
*dev
, struct rtc_time
*dt
)
184 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
188 err
= rx8025_check_validity(dev
);
192 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
196 dev_dbg(dev
, "%s: read 0x%02x 0x%02x "
197 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__
,
198 date
[0], date
[1], date
[2], date
[3], date
[4],
201 dt
->tm_sec
= bcd2bin(date
[RX8025_REG_SEC
] & 0x7f);
202 dt
->tm_min
= bcd2bin(date
[RX8025_REG_MIN
] & 0x7f);
203 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
204 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x3f);
206 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x1f) % 12
207 + (date
[RX8025_REG_HOUR
] & 0x20 ? 12 : 0);
209 dt
->tm_mday
= bcd2bin(date
[RX8025_REG_MDAY
] & 0x3f);
210 dt
->tm_mon
= bcd2bin(date
[RX8025_REG_MONTH
] & 0x1f) - 1;
211 dt
->tm_year
= bcd2bin(date
[RX8025_REG_YEAR
]) + 100;
213 dev_dbg(dev
, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__
,
214 dt
->tm_sec
, dt
->tm_min
, dt
->tm_hour
,
215 dt
->tm_mday
, dt
->tm_mon
, dt
->tm_year
);
220 static int rx8025_set_time(struct device
*dev
, struct rtc_time
*dt
)
222 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
226 if ((dt
->tm_year
< 100) || (dt
->tm_year
> 199))
230 * Here the read-only bits are written as "0". I'm not sure if that
233 date
[RX8025_REG_SEC
] = bin2bcd(dt
->tm_sec
);
234 date
[RX8025_REG_MIN
] = bin2bcd(dt
->tm_min
);
235 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
236 date
[RX8025_REG_HOUR
] = bin2bcd(dt
->tm_hour
);
238 date
[RX8025_REG_HOUR
] = (dt
->tm_hour
>= 12 ? 0x20 : 0)
239 | bin2bcd((dt
->tm_hour
+ 11) % 12 + 1);
241 date
[RX8025_REG_WDAY
] = bin2bcd(dt
->tm_wday
);
242 date
[RX8025_REG_MDAY
] = bin2bcd(dt
->tm_mday
);
243 date
[RX8025_REG_MONTH
] = bin2bcd(dt
->tm_mon
+ 1);
244 date
[RX8025_REG_YEAR
] = bin2bcd(dt
->tm_year
- 100);
247 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
249 date
[0], date
[1], date
[2], date
[3], date
[4], date
[5], date
[6]);
251 ret
= rx8025_write_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
255 return rx8025_reset_validity(rx8025
->client
);
258 static int rx8025_init_client(struct i2c_client
*client
)
260 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
265 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_CTRL1
, 2, ctrl
);
269 /* Keep test bit zero ! */
270 rx8025
->ctrl1
= ctrl
[0] & ~RX8025_BIT_CTRL1_TEST
;
272 if (ctrl
[1] & (RX8025_BIT_CTRL2_DAFG
| RX8025_BIT_CTRL2_WAFG
)) {
273 dev_warn(&client
->dev
, "Alarm was detected\n");
277 if (ctrl
[1] & RX8025_BIT_CTRL2_CTFG
)
282 ctrl2
&= ~(RX8025_BIT_CTRL2_CTFG
| RX8025_BIT_CTRL2_WAFG
|
283 RX8025_BIT_CTRL2_DAFG
);
285 err
= rx8025_write_reg(client
, RX8025_REG_CTRL2
, ctrl2
);
292 static int rx8025_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
294 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
295 struct i2c_client
*client
= rx8025
->client
;
299 if (client
->irq
<= 0)
302 err
= rx8025_read_regs(client
, RX8025_REG_ALDMIN
, 2, ald
);
306 ctrl2
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
310 dev_dbg(dev
, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
311 __func__
, ald
[0], ald
[1], ctrl2
);
313 /* Hardware alarms precision is 1 minute! */
315 t
->time
.tm_min
= bcd2bin(ald
[0] & 0x7f);
316 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
317 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x3f);
319 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x1f) % 12
320 + (ald
[1] & 0x20 ? 12 : 0);
322 dev_dbg(dev
, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
324 t
->time
.tm_sec
, t
->time
.tm_min
, t
->time
.tm_hour
,
325 t
->time
.tm_mday
, t
->time
.tm_mon
, t
->time
.tm_year
);
326 t
->enabled
= !!(rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
);
327 t
->pending
= (ctrl2
& RX8025_BIT_CTRL2_DAFG
) && t
->enabled
;
332 static int rx8025_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
334 struct i2c_client
*client
= to_i2c_client(dev
);
335 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
339 if (client
->irq
<= 0)
343 * Hardware alarm precision is 1 minute!
344 * round up to nearest minute
346 if (t
->time
.tm_sec
) {
347 time64_t alarm_time
= rtc_tm_to_time64(&t
->time
);
349 alarm_time
+= 60 - t
->time
.tm_sec
;
350 rtc_time64_to_tm(alarm_time
, &t
->time
);
353 ald
[0] = bin2bcd(t
->time
.tm_min
);
354 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
355 ald
[1] = bin2bcd(t
->time
.tm_hour
);
357 ald
[1] = (t
->time
.tm_hour
>= 12 ? 0x20 : 0)
358 | bin2bcd((t
->time
.tm_hour
+ 11) % 12 + 1);
360 dev_dbg(dev
, "%s: write 0x%02x 0x%02x\n", __func__
, ald
[0], ald
[1]);
362 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
) {
363 rx8025
->ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
364 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
369 err
= rx8025_write_regs(rx8025
->client
, RX8025_REG_ALDMIN
, 2, ald
);
374 rx8025
->ctrl1
|= RX8025_BIT_CTRL1_DALE
;
375 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
384 static int rx8025_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
386 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
390 ctrl1
= rx8025
->ctrl1
;
392 ctrl1
|= RX8025_BIT_CTRL1_DALE
;
394 ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
396 if (ctrl1
!= rx8025
->ctrl1
) {
397 rx8025
->ctrl1
= ctrl1
;
398 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
406 static const struct rtc_class_ops rx8025_rtc_ops
= {
407 .read_time
= rx8025_get_time
,
408 .set_time
= rx8025_set_time
,
409 .read_alarm
= rx8025_read_alarm
,
410 .set_alarm
= rx8025_set_alarm
,
411 .alarm_irq_enable
= rx8025_alarm_irq_enable
,
415 * Clock precision adjustment support
417 * According to the RX8025 SA/NB application manual the frequency and
418 * temperature characteristics can be approximated using the following
421 * df = a * (ut - t)**2
423 * df: Frequency deviation in any temperature
424 * a : Coefficient = (-35 +-5) * 10**-9
425 * ut: Ultimate temperature in degree = +25 +-5 degree
426 * t : Any temperature in degree
428 * Note that the clock adjustment in ppb must be entered (which is
429 * the negative value of the deviation).
431 static int rx8025_get_clock_adjust(struct device
*dev
, int *adj
)
433 struct i2c_client
*client
= to_i2c_client(dev
);
436 digoff
= rx8025_read_reg(client
, RX8025_REG_DIGOFF
);
440 *adj
= digoff
>= 64 ? digoff
- 128 : digoff
;
443 *adj
*= -RX8025_ADJ_RESOLUTION
;
448 static int rx8025_set_clock_adjust(struct device
*dev
, int adj
)
450 struct i2c_client
*client
= to_i2c_client(dev
);
454 adj
/= -RX8025_ADJ_RESOLUTION
;
455 if (adj
> RX8025_ADJ_DATA_MAX
)
456 adj
= RX8025_ADJ_DATA_MAX
;
457 else if (adj
< RX8025_ADJ_DATA_MIN
)
458 adj
= RX8025_ADJ_DATA_MIN
;
465 err
= rx8025_write_reg(client
, RX8025_REG_DIGOFF
, digoff
);
469 dev_dbg(dev
, "%s: write 0x%02x\n", __func__
, digoff
);
474 static ssize_t
rx8025_sysfs_show_clock_adjust(struct device
*dev
,
475 struct device_attribute
*attr
,
480 err
= rx8025_get_clock_adjust(dev
, &adj
);
484 return sprintf(buf
, "%d\n", adj
);
487 static ssize_t
rx8025_sysfs_store_clock_adjust(struct device
*dev
,
488 struct device_attribute
*attr
,
489 const char *buf
, size_t count
)
493 if (sscanf(buf
, "%i", &adj
) != 1)
496 err
= rx8025_set_clock_adjust(dev
, adj
);
498 return err
? err
: count
;
501 static DEVICE_ATTR(clock_adjust_ppb
, S_IRUGO
| S_IWUSR
,
502 rx8025_sysfs_show_clock_adjust
,
503 rx8025_sysfs_store_clock_adjust
);
505 static int rx8025_sysfs_register(struct device
*dev
)
507 return device_create_file(dev
, &dev_attr_clock_adjust_ppb
);
510 static void rx8025_sysfs_unregister(struct device
*dev
)
512 device_remove_file(dev
, &dev_attr_clock_adjust_ppb
);
515 static int rx8025_probe(struct i2c_client
*client
,
516 const struct i2c_device_id
*id
)
518 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
519 struct rx8025_data
*rx8025
;
522 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
523 | I2C_FUNC_SMBUS_I2C_BLOCK
)) {
524 dev_err(&adapter
->dev
,
525 "doesn't support required functionality\n");
529 rx8025
= devm_kzalloc(&client
->dev
, sizeof(*rx8025
), GFP_KERNEL
);
533 rx8025
->client
= client
;
534 i2c_set_clientdata(client
, rx8025
);
536 err
= rx8025_init_client(client
);
540 rx8025
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
541 &rx8025_rtc_ops
, THIS_MODULE
);
542 if (IS_ERR(rx8025
->rtc
)) {
543 dev_err(&client
->dev
, "unable to register the class device\n");
544 return PTR_ERR(rx8025
->rtc
);
547 if (client
->irq
> 0) {
548 dev_info(&client
->dev
, "IRQ %d supplied\n", client
->irq
);
549 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
554 dev_err(&client
->dev
, "unable to request IRQ, alarms disabled\n");
559 rx8025
->rtc
->max_user_freq
= 1;
561 /* the rx8025 alarm only supports a minute accuracy */
562 rx8025
->rtc
->uie_unsupported
= 1;
564 err
= rx8025_sysfs_register(&client
->dev
);
568 static int rx8025_remove(struct i2c_client
*client
)
570 rx8025_sysfs_unregister(&client
->dev
);
574 static struct i2c_driver rx8025_driver
= {
576 .name
= "rtc-rx8025",
578 .probe
= rx8025_probe
,
579 .remove
= rx8025_remove
,
580 .id_table
= rx8025_id
,
583 module_i2c_driver(rx8025_driver
);
585 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
586 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
587 MODULE_LICENSE("GPL");