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 * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors>
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
[] = {
71 MODULE_DEVICE_TABLE(i2c
, rx8025_id
);
74 struct i2c_client
*client
;
75 struct rtc_device
*rtc
;
79 static s32
rx8025_read_reg(const struct i2c_client
*client
, u8 number
)
81 return i2c_smbus_read_byte_data(client
, number
<< 4);
84 static int rx8025_read_regs(const struct i2c_client
*client
,
85 u8 number
, u8 length
, u8
*values
)
87 int ret
= i2c_smbus_read_i2c_block_data(client
, number
<< 4, length
,
90 return ret
< 0 ? ret
: -EIO
;
95 static s32
rx8025_write_reg(const struct i2c_client
*client
, u8 number
,
98 return i2c_smbus_write_byte_data(client
, number
<< 4, value
);
101 static s32
rx8025_write_regs(const struct i2c_client
*client
,
102 u8 number
, u8 length
, const u8
*values
)
104 return i2c_smbus_write_i2c_block_data(client
, number
<< 4,
108 static int rx8025_check_validity(struct device
*dev
)
110 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
113 ctrl2
= rx8025_read_reg(rx8025
->client
, RX8025_REG_CTRL2
);
117 if (ctrl2
& RX8025_BIT_CTRL2_VDET
)
118 dev_warn(dev
, "power voltage drop detected\n");
120 if (ctrl2
& RX8025_BIT_CTRL2_PON
) {
121 dev_warn(dev
, "power-on reset detected, date is invalid\n");
125 if (!(ctrl2
& RX8025_BIT_CTRL2_XST
)) {
126 dev_warn(dev
, "crystal stopped, date is invalid\n");
133 static int rx8025_reset_validity(struct i2c_client
*client
)
135 int ctrl2
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
140 ctrl2
&= ~(RX8025_BIT_CTRL2_PON
| RX8025_BIT_CTRL2_VDET
);
142 return rx8025_write_reg(client
, RX8025_REG_CTRL2
,
143 ctrl2
| RX8025_BIT_CTRL2_XST
);
146 static irqreturn_t
rx8025_handle_irq(int irq
, void *dev_id
)
148 struct i2c_client
*client
= dev_id
;
149 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
152 status
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
156 if (!(status
& RX8025_BIT_CTRL2_XST
))
157 dev_warn(&client
->dev
, "Oscillation stop was detected,"
158 "you may have to readjust the clock\n");
160 if (status
& RX8025_BIT_CTRL2_CTFG
) {
162 status
&= ~RX8025_BIT_CTRL2_CTFG
;
163 rtc_update_irq(rx8025
->rtc
, 1, RTC_PF
| RTC_IRQF
);
166 if (status
& RX8025_BIT_CTRL2_DAFG
) {
168 status
&= RX8025_BIT_CTRL2_DAFG
;
169 if (rx8025_write_reg(client
, RX8025_REG_CTRL1
,
170 rx8025
->ctrl1
& ~RX8025_BIT_CTRL1_DALE
))
172 rtc_update_irq(rx8025
->rtc
, 1, RTC_AF
| RTC_IRQF
);
179 static int rx8025_get_time(struct device
*dev
, struct rtc_time
*dt
)
181 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
185 err
= rx8025_check_validity(dev
);
189 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
193 dev_dbg(dev
, "%s: read 0x%02x 0x%02x "
194 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__
,
195 date
[0], date
[1], date
[2], date
[3], date
[4],
198 dt
->tm_sec
= bcd2bin(date
[RX8025_REG_SEC
] & 0x7f);
199 dt
->tm_min
= bcd2bin(date
[RX8025_REG_MIN
] & 0x7f);
200 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
201 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x3f);
203 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x1f) % 12
204 + (date
[RX8025_REG_HOUR
] & 0x20 ? 12 : 0);
206 dt
->tm_mday
= bcd2bin(date
[RX8025_REG_MDAY
] & 0x3f);
207 dt
->tm_mon
= bcd2bin(date
[RX8025_REG_MONTH
] & 0x1f) - 1;
208 dt
->tm_year
= bcd2bin(date
[RX8025_REG_YEAR
]) + 100;
210 dev_dbg(dev
, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__
,
211 dt
->tm_sec
, dt
->tm_min
, dt
->tm_hour
,
212 dt
->tm_mday
, dt
->tm_mon
, dt
->tm_year
);
214 return rtc_valid_tm(dt
);
217 static int rx8025_set_time(struct device
*dev
, struct rtc_time
*dt
)
219 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
223 if ((dt
->tm_year
< 100) || (dt
->tm_year
> 199))
227 * Here the read-only bits are written as "0". I'm not sure if that
230 date
[RX8025_REG_SEC
] = bin2bcd(dt
->tm_sec
);
231 date
[RX8025_REG_MIN
] = bin2bcd(dt
->tm_min
);
232 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
233 date
[RX8025_REG_HOUR
] = bin2bcd(dt
->tm_hour
);
235 date
[RX8025_REG_HOUR
] = (dt
->tm_hour
>= 12 ? 0x20 : 0)
236 | bin2bcd((dt
->tm_hour
+ 11) % 12 + 1);
238 date
[RX8025_REG_WDAY
] = bin2bcd(dt
->tm_wday
);
239 date
[RX8025_REG_MDAY
] = bin2bcd(dt
->tm_mday
);
240 date
[RX8025_REG_MONTH
] = bin2bcd(dt
->tm_mon
+ 1);
241 date
[RX8025_REG_YEAR
] = bin2bcd(dt
->tm_year
- 100);
244 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
246 date
[0], date
[1], date
[2], date
[3], date
[4], date
[5], date
[6]);
248 ret
= rx8025_write_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
252 return rx8025_reset_validity(rx8025
->client
);
255 static int rx8025_init_client(struct i2c_client
*client
)
257 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
262 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_CTRL1
, 2, ctrl
);
266 /* Keep test bit zero ! */
267 rx8025
->ctrl1
= ctrl
[0] & ~RX8025_BIT_CTRL1_TEST
;
269 if (ctrl
[1] & (RX8025_BIT_CTRL2_DAFG
| RX8025_BIT_CTRL2_WAFG
)) {
270 dev_warn(&client
->dev
, "Alarm was detected\n");
274 if (ctrl
[1] & RX8025_BIT_CTRL2_CTFG
)
279 ctrl2
&= ~(RX8025_BIT_CTRL2_CTFG
| RX8025_BIT_CTRL2_WAFG
|
280 RX8025_BIT_CTRL2_DAFG
);
282 err
= rx8025_write_reg(client
, RX8025_REG_CTRL2
, ctrl2
);
289 static int rx8025_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
291 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
292 struct i2c_client
*client
= rx8025
->client
;
296 if (client
->irq
<= 0)
299 err
= rx8025_read_regs(client
, RX8025_REG_ALDMIN
, 2, ald
);
303 ctrl2
= rx8025_read_reg(client
, RX8025_REG_CTRL2
);
307 dev_dbg(dev
, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
308 __func__
, ald
[0], ald
[1], ctrl2
);
310 /* Hardware alarms precision is 1 minute! */
312 t
->time
.tm_min
= bcd2bin(ald
[0] & 0x7f);
313 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
314 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x3f);
316 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x1f) % 12
317 + (ald
[1] & 0x20 ? 12 : 0);
319 t
->time
.tm_wday
= -1;
320 t
->time
.tm_mday
= -1;
322 t
->time
.tm_year
= -1;
324 dev_dbg(dev
, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
326 t
->time
.tm_sec
, t
->time
.tm_min
, t
->time
.tm_hour
,
327 t
->time
.tm_mday
, t
->time
.tm_mon
, t
->time
.tm_year
);
328 t
->enabled
= !!(rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
);
329 t
->pending
= (ctrl2
& RX8025_BIT_CTRL2_DAFG
) && t
->enabled
;
334 static int rx8025_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
336 struct i2c_client
*client
= to_i2c_client(dev
);
337 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
341 if (client
->irq
<= 0)
344 /* Hardware alarm precision is 1 minute! */
345 ald
[0] = bin2bcd(t
->time
.tm_min
);
346 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
347 ald
[1] = bin2bcd(t
->time
.tm_hour
);
349 ald
[1] = (t
->time
.tm_hour
>= 12 ? 0x20 : 0)
350 | bin2bcd((t
->time
.tm_hour
+ 11) % 12 + 1);
352 dev_dbg(dev
, "%s: write 0x%02x 0x%02x\n", __func__
, ald
[0], ald
[1]);
354 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
) {
355 rx8025
->ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
356 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
361 err
= rx8025_write_regs(rx8025
->client
, RX8025_REG_ALDMIN
, 2, ald
);
366 rx8025
->ctrl1
|= RX8025_BIT_CTRL1_DALE
;
367 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
376 static int rx8025_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
378 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
382 ctrl1
= rx8025
->ctrl1
;
384 ctrl1
|= RX8025_BIT_CTRL1_DALE
;
386 ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
388 if (ctrl1
!= rx8025
->ctrl1
) {
389 rx8025
->ctrl1
= ctrl1
;
390 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
398 static struct rtc_class_ops rx8025_rtc_ops
= {
399 .read_time
= rx8025_get_time
,
400 .set_time
= rx8025_set_time
,
401 .read_alarm
= rx8025_read_alarm
,
402 .set_alarm
= rx8025_set_alarm
,
403 .alarm_irq_enable
= rx8025_alarm_irq_enable
,
407 * Clock precision adjustment support
409 * According to the RX8025 SA/NB application manual the frequency and
410 * temperature characteristics can be approximated using the following
413 * df = a * (ut - t)**2
415 * df: Frequency deviation in any temperature
416 * a : Coefficient = (-35 +-5) * 10**-9
417 * ut: Ultimate temperature in degree = +25 +-5 degree
418 * t : Any temperature in degree
420 * Note that the clock adjustment in ppb must be entered (which is
421 * the negative value of the deviation).
423 static int rx8025_get_clock_adjust(struct device
*dev
, int *adj
)
425 struct i2c_client
*client
= to_i2c_client(dev
);
428 digoff
= rx8025_read_reg(client
, RX8025_REG_DIGOFF
);
432 *adj
= digoff
>= 64 ? digoff
- 128 : digoff
;
435 *adj
*= -RX8025_ADJ_RESOLUTION
;
440 static int rx8025_set_clock_adjust(struct device
*dev
, int adj
)
442 struct i2c_client
*client
= to_i2c_client(dev
);
446 adj
/= -RX8025_ADJ_RESOLUTION
;
447 if (adj
> RX8025_ADJ_DATA_MAX
)
448 adj
= RX8025_ADJ_DATA_MAX
;
449 else if (adj
< RX8025_ADJ_DATA_MIN
)
450 adj
= RX8025_ADJ_DATA_MIN
;
457 err
= rx8025_write_reg(client
, RX8025_REG_DIGOFF
, digoff
);
461 dev_dbg(dev
, "%s: write 0x%02x\n", __func__
, digoff
);
466 static ssize_t
rx8025_sysfs_show_clock_adjust(struct device
*dev
,
467 struct device_attribute
*attr
,
472 err
= rx8025_get_clock_adjust(dev
, &adj
);
476 return sprintf(buf
, "%d\n", adj
);
479 static ssize_t
rx8025_sysfs_store_clock_adjust(struct device
*dev
,
480 struct device_attribute
*attr
,
481 const char *buf
, size_t count
)
485 if (sscanf(buf
, "%i", &adj
) != 1)
488 err
= rx8025_set_clock_adjust(dev
, adj
);
490 return err
? err
: count
;
493 static DEVICE_ATTR(clock_adjust_ppb
, S_IRUGO
| S_IWUSR
,
494 rx8025_sysfs_show_clock_adjust
,
495 rx8025_sysfs_store_clock_adjust
);
497 static int rx8025_sysfs_register(struct device
*dev
)
499 return device_create_file(dev
, &dev_attr_clock_adjust_ppb
);
502 static void rx8025_sysfs_unregister(struct device
*dev
)
504 device_remove_file(dev
, &dev_attr_clock_adjust_ppb
);
507 static int rx8025_probe(struct i2c_client
*client
,
508 const struct i2c_device_id
*id
)
510 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
511 struct rx8025_data
*rx8025
;
514 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
515 | I2C_FUNC_SMBUS_I2C_BLOCK
)) {
516 dev_err(&adapter
->dev
,
517 "doesn't support required functionality\n");
521 rx8025
= devm_kzalloc(&client
->dev
, sizeof(*rx8025
), GFP_KERNEL
);
525 rx8025
->client
= client
;
526 i2c_set_clientdata(client
, rx8025
);
528 err
= rx8025_init_client(client
);
532 rx8025
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
533 &rx8025_rtc_ops
, THIS_MODULE
);
534 if (IS_ERR(rx8025
->rtc
)) {
535 dev_err(&client
->dev
, "unable to register the class device\n");
536 return PTR_ERR(rx8025
->rtc
);
539 if (client
->irq
> 0) {
540 dev_info(&client
->dev
, "IRQ %d supplied\n", client
->irq
);
541 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
542 rx8025_handle_irq
, 0, "rx8025",
545 dev_err(&client
->dev
, "unable to request IRQ, alarms disabled\n");
550 rx8025
->rtc
->max_user_freq
= 1;
552 err
= rx8025_sysfs_register(&client
->dev
);
556 static int rx8025_remove(struct i2c_client
*client
)
558 rx8025_sysfs_unregister(&client
->dev
);
562 static struct i2c_driver rx8025_driver
= {
564 .name
= "rtc-rx8025",
566 .probe
= rx8025_probe
,
567 .remove
= rx8025_remove
,
568 .id_table
= rx8025_id
,
571 module_i2c_driver(rx8025_driver
);
573 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
574 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
575 MODULE_LICENSE("GPL");