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/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/bcd.h>
26 #include <linux/i2c.h>
27 #include <linux/list.h>
28 #include <linux/rtc.h>
30 /* Register definitions */
31 #define RX8025_REG_SEC 0x00
32 #define RX8025_REG_MIN 0x01
33 #define RX8025_REG_HOUR 0x02
34 #define RX8025_REG_WDAY 0x03
35 #define RX8025_REG_MDAY 0x04
36 #define RX8025_REG_MONTH 0x05
37 #define RX8025_REG_YEAR 0x06
38 #define RX8025_REG_DIGOFF 0x07
39 #define RX8025_REG_ALWMIN 0x08
40 #define RX8025_REG_ALWHOUR 0x09
41 #define RX8025_REG_ALWWDAY 0x0a
42 #define RX8025_REG_ALDMIN 0x0b
43 #define RX8025_REG_ALDHOUR 0x0c
44 /* 0x0d is reserved */
45 #define RX8025_REG_CTRL1 0x0e
46 #define RX8025_REG_CTRL2 0x0f
48 #define RX8025_BIT_CTRL1_CT (7 << 0)
49 /* 1 Hz periodic level irq */
50 #define RX8025_BIT_CTRL1_CT_1HZ 4
51 #define RX8025_BIT_CTRL1_TEST (1 << 3)
52 #define RX8025_BIT_CTRL1_1224 (1 << 5)
53 #define RX8025_BIT_CTRL1_DALE (1 << 6)
54 #define RX8025_BIT_CTRL1_WALE (1 << 7)
56 #define RX8025_BIT_CTRL2_DAFG (1 << 0)
57 #define RX8025_BIT_CTRL2_WAFG (1 << 1)
58 #define RX8025_BIT_CTRL2_CTFG (1 << 2)
59 #define RX8025_BIT_CTRL2_PON (1 << 4)
60 #define RX8025_BIT_CTRL2_XST (1 << 5)
61 #define RX8025_BIT_CTRL2_VDET (1 << 6)
63 /* Clock precision adjustment */
64 #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
65 #define RX8025_ADJ_DATA_MAX 62
66 #define RX8025_ADJ_DATA_MIN -62
68 static const struct i2c_device_id rx8025_id
[] = {
72 MODULE_DEVICE_TABLE(i2c
, rx8025_id
);
75 struct i2c_client
*client
;
76 struct rtc_device
*rtc
;
77 struct work_struct work
;
82 static int rx8025_read_reg(struct i2c_client
*client
, int number
, u8
*value
)
84 int ret
= i2c_smbus_read_byte_data(client
, (number
<< 4) | 0x08);
87 dev_err(&client
->dev
, "Unable to read register #%d\n", number
);
95 static int rx8025_read_regs(struct i2c_client
*client
,
96 int number
, u8 length
, u8
*values
)
98 int ret
= i2c_smbus_read_i2c_block_data(client
, (number
<< 4) | 0x08,
102 dev_err(&client
->dev
, "Unable to read registers #%d..#%d\n",
103 number
, number
+ length
- 1);
104 return ret
< 0 ? ret
: -EIO
;
110 static int rx8025_write_reg(struct i2c_client
*client
, int number
, u8 value
)
112 int ret
= i2c_smbus_write_byte_data(client
, number
<< 4, value
);
115 dev_err(&client
->dev
, "Unable to write register #%d\n",
121 static int rx8025_write_regs(struct i2c_client
*client
,
122 int number
, u8 length
, u8
*values
)
124 int ret
= i2c_smbus_write_i2c_block_data(client
, (number
<< 4) | 0x08,
128 dev_err(&client
->dev
, "Unable to write registers #%d..#%d\n",
129 number
, number
+ length
- 1);
134 static irqreturn_t
rx8025_irq(int irq
, void *dev_id
)
136 struct i2c_client
*client
= dev_id
;
137 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
139 disable_irq_nosync(irq
);
140 schedule_work(&rx8025
->work
);
144 static void rx8025_work(struct work_struct
*work
)
146 struct rx8025_data
*rx8025
= container_of(work
, struct rx8025_data
,
148 struct i2c_client
*client
= rx8025
->client
;
149 struct mutex
*lock
= &rx8025
->rtc
->ops_lock
;
154 if (rx8025_read_reg(client
, RX8025_REG_CTRL2
, &status
))
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
;
165 rtc_update_irq(rx8025
->rtc
, 1, RTC_PF
| RTC_IRQF
);
169 if (status
& RX8025_BIT_CTRL2_DAFG
) {
171 status
&= RX8025_BIT_CTRL2_DAFG
;
172 if (rx8025_write_reg(client
, RX8025_REG_CTRL1
,
173 rx8025
->ctrl1
& ~RX8025_BIT_CTRL1_DALE
))
176 rtc_update_irq(rx8025
->rtc
, 1, RTC_AF
| RTC_IRQF
);
180 /* acknowledge IRQ */
181 rx8025_write_reg(client
, RX8025_REG_CTRL2
,
182 status
| RX8025_BIT_CTRL2_XST
);
185 if (!rx8025
->exiting
)
186 enable_irq(client
->irq
);
191 static int rx8025_get_time(struct device
*dev
, struct rtc_time
*dt
)
193 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
197 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
201 dev_dbg(dev
, "%s: read 0x%02x 0x%02x "
202 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__
,
203 date
[0], date
[1], date
[2], date
[3], date
[4],
206 dt
->tm_sec
= bcd2bin(date
[RX8025_REG_SEC
] & 0x7f);
207 dt
->tm_min
= bcd2bin(date
[RX8025_REG_MIN
] & 0x7f);
208 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
209 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x3f);
211 dt
->tm_hour
= bcd2bin(date
[RX8025_REG_HOUR
] & 0x1f) % 12
212 + (date
[RX8025_REG_HOUR
] & 0x20 ? 12 : 0);
214 dt
->tm_mday
= bcd2bin(date
[RX8025_REG_MDAY
] & 0x3f);
215 dt
->tm_mon
= bcd2bin(date
[RX8025_REG_MONTH
] & 0x1f) - 1;
216 dt
->tm_year
= bcd2bin(date
[RX8025_REG_YEAR
]);
218 if (dt
->tm_year
< 70)
221 dev_dbg(dev
, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__
,
222 dt
->tm_sec
, dt
->tm_min
, dt
->tm_hour
,
223 dt
->tm_mday
, dt
->tm_mon
, dt
->tm_year
);
225 return rtc_valid_tm(dt
);
228 static int rx8025_set_time(struct device
*dev
, struct rtc_time
*dt
)
230 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
234 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
235 * year. Next time this is wrong is 2100, which will not be a leap
240 * Here the read-only bits are written as "0". I'm not sure if that
243 date
[RX8025_REG_SEC
] = bin2bcd(dt
->tm_sec
);
244 date
[RX8025_REG_MIN
] = bin2bcd(dt
->tm_min
);
245 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
246 date
[RX8025_REG_HOUR
] = bin2bcd(dt
->tm_hour
);
248 date
[RX8025_REG_HOUR
] = (dt
->tm_hour
>= 12 ? 0x20 : 0)
249 | bin2bcd((dt
->tm_hour
+ 11) % 12 + 1);
251 date
[RX8025_REG_WDAY
] = bin2bcd(dt
->tm_wday
);
252 date
[RX8025_REG_MDAY
] = bin2bcd(dt
->tm_mday
);
253 date
[RX8025_REG_MONTH
] = bin2bcd(dt
->tm_mon
+ 1);
254 date
[RX8025_REG_YEAR
] = bin2bcd(dt
->tm_year
% 100);
257 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
259 date
[0], date
[1], date
[2], date
[3], date
[4], date
[5], date
[6]);
261 return rx8025_write_regs(rx8025
->client
, RX8025_REG_SEC
, 7, date
);
264 static int rx8025_init_client(struct i2c_client
*client
, int *need_reset
)
266 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
271 err
= rx8025_read_regs(rx8025
->client
, RX8025_REG_CTRL1
, 2, ctrl
);
275 /* Keep test bit zero ! */
276 rx8025
->ctrl1
= ctrl
[0] & ~RX8025_BIT_CTRL1_TEST
;
278 if (ctrl
[1] & RX8025_BIT_CTRL2_PON
) {
279 dev_warn(&client
->dev
, "power-on reset was detected, "
280 "you may have to readjust the clock\n");
284 if (ctrl
[1] & RX8025_BIT_CTRL2_VDET
) {
285 dev_warn(&client
->dev
, "a power voltage drop was detected, "
286 "you may have to readjust the clock\n");
290 if (!(ctrl
[1] & RX8025_BIT_CTRL2_XST
)) {
291 dev_warn(&client
->dev
, "Oscillation stop was detected,"
292 "you may have to readjust the clock\n");
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
))
304 if (*need_reset
|| need_clear
) {
306 ctrl2
&= ~(RX8025_BIT_CTRL2_PON
| RX8025_BIT_CTRL2_VDET
|
307 RX8025_BIT_CTRL2_CTFG
| RX8025_BIT_CTRL2_WAFG
|
308 RX8025_BIT_CTRL2_DAFG
);
309 ctrl2
|= RX8025_BIT_CTRL2_XST
;
311 err
= rx8025_write_reg(client
, RX8025_REG_CTRL2
, ctrl2
);
318 static int rx8025_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
320 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
321 struct i2c_client
*client
= rx8025
->client
;
325 if (client
->irq
<= 0)
328 err
= rx8025_read_regs(client
, RX8025_REG_ALDMIN
, 2, ald
);
332 err
= rx8025_read_reg(client
, RX8025_REG_CTRL2
, &ctrl2
);
336 dev_dbg(dev
, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
337 __func__
, ald
[0], ald
[1], ctrl2
);
339 /* Hardware alarms precision is 1 minute! */
341 t
->time
.tm_min
= bcd2bin(ald
[0] & 0x7f);
342 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
343 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x3f);
345 t
->time
.tm_hour
= bcd2bin(ald
[1] & 0x1f) % 12
346 + (ald
[1] & 0x20 ? 12 : 0);
348 t
->time
.tm_wday
= -1;
349 t
->time
.tm_mday
= -1;
351 t
->time
.tm_year
= -1;
353 dev_dbg(dev
, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
355 t
->time
.tm_sec
, t
->time
.tm_min
, t
->time
.tm_hour
,
356 t
->time
.tm_mday
, t
->time
.tm_mon
, t
->time
.tm_year
);
357 t
->enabled
= !!(rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
);
358 t
->pending
= (ctrl2
& RX8025_BIT_CTRL2_DAFG
) && t
->enabled
;
363 static int rx8025_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
365 struct i2c_client
*client
= to_i2c_client(dev
);
366 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
370 if (client
->irq
<= 0)
373 /* Hardware alarm precision is 1 minute! */
374 ald
[0] = bin2bcd(t
->time
.tm_min
);
375 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_1224
)
376 ald
[1] = bin2bcd(t
->time
.tm_hour
);
378 ald
[1] = (t
->time
.tm_hour
>= 12 ? 0x20 : 0)
379 | bin2bcd((t
->time
.tm_hour
+ 11) % 12 + 1);
381 dev_dbg(dev
, "%s: write 0x%02x 0x%02x\n", __func__
, ald
[0], ald
[1]);
383 if (rx8025
->ctrl1
& RX8025_BIT_CTRL1_DALE
) {
384 rx8025
->ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
385 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
390 err
= rx8025_write_regs(rx8025
->client
, RX8025_REG_ALDMIN
, 2, ald
);
395 rx8025
->ctrl1
|= RX8025_BIT_CTRL1_DALE
;
396 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
405 static int rx8025_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
407 struct rx8025_data
*rx8025
= dev_get_drvdata(dev
);
411 ctrl1
= rx8025
->ctrl1
;
413 ctrl1
|= RX8025_BIT_CTRL1_DALE
;
415 ctrl1
&= ~RX8025_BIT_CTRL1_DALE
;
417 if (ctrl1
!= rx8025
->ctrl1
) {
418 rx8025
->ctrl1
= ctrl1
;
419 err
= rx8025_write_reg(rx8025
->client
, RX8025_REG_CTRL1
,
427 static struct rtc_class_ops rx8025_rtc_ops
= {
428 .read_time
= rx8025_get_time
,
429 .set_time
= rx8025_set_time
,
430 .read_alarm
= rx8025_read_alarm
,
431 .set_alarm
= rx8025_set_alarm
,
432 .alarm_irq_enable
= rx8025_alarm_irq_enable
,
436 * Clock precision adjustment support
438 * According to the RX8025 SA/NB application manual the frequency and
439 * temperature characteristics can be approximated using the following
442 * df = a * (ut - t)**2
444 * df: Frequency deviation in any temperature
445 * a : Coefficient = (-35 +-5) * 10**-9
446 * ut: Ultimate temperature in degree = +25 +-5 degree
447 * t : Any temperature in degree
449 * Note that the clock adjustment in ppb must be entered (which is
450 * the negative value of the deviation).
452 static int rx8025_get_clock_adjust(struct device
*dev
, int *adj
)
454 struct i2c_client
*client
= to_i2c_client(dev
);
458 err
= rx8025_read_reg(client
, RX8025_REG_DIGOFF
, &digoff
);
462 *adj
= digoff
>= 64 ? digoff
- 128 : digoff
;
465 *adj
*= -RX8025_ADJ_RESOLUTION
;
470 static int rx8025_set_clock_adjust(struct device
*dev
, int adj
)
472 struct i2c_client
*client
= to_i2c_client(dev
);
476 adj
/= -RX8025_ADJ_RESOLUTION
;
477 if (adj
> RX8025_ADJ_DATA_MAX
)
478 adj
= RX8025_ADJ_DATA_MAX
;
479 else if (adj
< RX8025_ADJ_DATA_MIN
)
480 adj
= RX8025_ADJ_DATA_MIN
;
487 err
= rx8025_write_reg(client
, RX8025_REG_DIGOFF
, digoff
);
491 dev_dbg(dev
, "%s: write 0x%02x\n", __func__
, digoff
);
496 static ssize_t
rx8025_sysfs_show_clock_adjust(struct device
*dev
,
497 struct device_attribute
*attr
,
502 err
= rx8025_get_clock_adjust(dev
, &adj
);
506 return sprintf(buf
, "%d\n", adj
);
509 static ssize_t
rx8025_sysfs_store_clock_adjust(struct device
*dev
,
510 struct device_attribute
*attr
,
511 const char *buf
, size_t count
)
515 if (sscanf(buf
, "%i", &adj
) != 1)
518 err
= rx8025_set_clock_adjust(dev
, adj
);
520 return err
? err
: count
;
523 static DEVICE_ATTR(clock_adjust_ppb
, S_IRUGO
| S_IWUSR
,
524 rx8025_sysfs_show_clock_adjust
,
525 rx8025_sysfs_store_clock_adjust
);
527 static int rx8025_sysfs_register(struct device
*dev
)
529 return device_create_file(dev
, &dev_attr_clock_adjust_ppb
);
532 static void rx8025_sysfs_unregister(struct device
*dev
)
534 device_remove_file(dev
, &dev_attr_clock_adjust_ppb
);
537 static int rx8025_probe(struct i2c_client
*client
,
538 const struct i2c_device_id
*id
)
540 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
541 struct rx8025_data
*rx8025
;
542 int err
, need_reset
= 0;
544 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
545 | I2C_FUNC_SMBUS_I2C_BLOCK
)) {
546 dev_err(&adapter
->dev
,
547 "doesn't support required functionality\n");
552 rx8025
= devm_kzalloc(&client
->dev
, sizeof(*rx8025
), GFP_KERNEL
);
554 dev_err(&adapter
->dev
, "failed to alloc memory\n");
559 rx8025
->client
= client
;
560 i2c_set_clientdata(client
, rx8025
);
561 INIT_WORK(&rx8025
->work
, rx8025_work
);
563 err
= rx8025_init_client(client
, &need_reset
);
569 dev_info(&client
->dev
,
570 "bad conditions detected, resetting date\n");
571 rtc_time_to_tm(0, &tm
); /* 1970/1/1 */
572 rx8025_set_time(&client
->dev
, &tm
);
575 rx8025
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
576 &rx8025_rtc_ops
, THIS_MODULE
);
577 if (IS_ERR(rx8025
->rtc
)) {
578 err
= PTR_ERR(rx8025
->rtc
);
579 dev_err(&client
->dev
, "unable to register the class device\n");
583 if (client
->irq
> 0) {
584 dev_info(&client
->dev
, "IRQ %d supplied\n", client
->irq
);
585 err
= request_irq(client
->irq
, rx8025_irq
,
586 0, "rx8025", client
);
588 dev_err(&client
->dev
, "unable to request IRQ\n");
593 rx8025
->rtc
->irq_freq
= 1;
594 rx8025
->rtc
->max_user_freq
= 1;
596 err
= rx8025_sysfs_register(&client
->dev
);
604 free_irq(client
->irq
, client
);
607 dev_err(&adapter
->dev
, "probing for rx8025 failed\n");
611 static int rx8025_remove(struct i2c_client
*client
)
613 struct rx8025_data
*rx8025
= i2c_get_clientdata(client
);
614 struct mutex
*lock
= &rx8025
->rtc
->ops_lock
;
616 if (client
->irq
> 0) {
621 free_irq(client
->irq
, client
);
622 cancel_work_sync(&rx8025
->work
);
625 rx8025_sysfs_unregister(&client
->dev
);
629 static struct i2c_driver rx8025_driver
= {
631 .name
= "rtc-rx8025",
632 .owner
= THIS_MODULE
,
634 .probe
= rx8025_probe
,
635 .remove
= rx8025_remove
,
636 .id_table
= rx8025_id
,
639 module_i2c_driver(rx8025_driver
);
641 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
642 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
643 MODULE_LICENSE("GPL");