21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/list.h>
28 #include <linux/rtc.h>
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
45 #define RX8025_REG_CTRL1 0x0e
46 #define RX8025_REG_CTRL2 0x0f
48 #define RX8025_BIT_CTRL1_CT (7 << 0)
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)
64 #define RX8025_ADJ_RESOLUTION 3050
65 #define RX8025_ADJ_DATA_MAX 62
66 #define RX8025_ADJ_DATA_MIN -62
87 dev_err(&client->
dev,
"Unable to read register #%d\n", number);
102 dev_err(&client->
dev,
"Unable to read registers #%d..#%d\n",
103 number, number + length - 1);
104 return ret < 0 ? ret : -
EIO;
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)
128 dev_err(&client->
dev,
"Unable to write registers #%d..#%d\n",
129 number, number + length - 1);
137 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
158 dev_warn(&client->
dev,
"Oscillation stop was detected,"
159 "you may have to readjust the clock\n");
163 status &= ~RX8025_BIT_CTRL2_CTFG;
182 status | RX8025_BIT_CTRL2_XST);
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],
221 dev_dbg(dev,
"%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
228 static int rx8025_set_time(
struct device *dev,
struct rtc_time *dt)
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]);
264 static int rx8025_init_client(
struct i2c_client *client,
int *need_reset)
266 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
279 dev_warn(&client->
dev,
"power-on reset was detected, "
280 "you may have to readjust the clock\n");
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");
301 if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
304 if (*need_reset || need_clear) {
306 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
325 if (client->
irq <= 0)
336 dev_dbg(dev,
"%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
337 __func__, ald[0], ald[1], ctrl2);
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",
370 if (client->
irq <= 0)
378 ald[1] = (t->
time.tm_hour >= 12 ? 0x20 : 0)
381 dev_dbg(dev,
"%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
405 static int rx8025_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
411 ctrl1 = rx8025->
ctrl1;
417 if (ctrl1 != rx8025->
ctrl1) {
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,
452 static int rx8025_get_clock_adjust(
struct device *dev,
int *adj)
462 *adj = digoff >= 64 ? digoff - 128 : digoff;
470 static int rx8025_set_clock_adjust(
struct device *dev,
int adj)
491 dev_dbg(dev,
"%s: write 0x%02x\n", __func__, digoff);
496 static ssize_t rx8025_sysfs_show_clock_adjust(
struct device *dev,
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,
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;
524 rx8025_sysfs_show_clock_adjust,
525 rx8025_sysfs_store_clock_adjust);
527 static int rx8025_sysfs_register(
struct device *dev)
532 static void rx8025_sysfs_unregister(
struct device *dev)
542 int err, need_reset = 0;
547 "doesn't support required functionality\n");
552 rx8025 = kzalloc(
sizeof(*rx8025),
GFP_KERNEL);
554 dev_err(&adapter->
dev,
"failed to alloc memory\n");
560 i2c_set_clientdata(client, rx8025);
563 err = rx8025_init_client(client, &need_reset);
570 "bad conditions detected, resetting date\n");
572 rx8025_set_time(&client->
dev, &
tm);
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) {
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);
613 dev_err(&adapter->
dev,
"probing for rx8025 failed\n");
619 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
620 struct mutex *lock = &rx8025->
rtc->ops_lock;
622 if (client->
irq > 0) {
631 rx8025_sysfs_unregister(&client->
dev);
639 .name =
"rtc-rx8025",
642 .probe = rx8025_probe,
644 .id_table = rx8025_id,