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/init.h>
24 #include <linux/bcd.h>
25 #include <linux/i2c.h>
26 #include <linux/list.h>
27 #include <linux/rtc.h>
29 /* Register definitions */
30 #define RX8025_REG_SEC 0x00
31 #define RX8025_REG_MIN 0x01
32 #define RX8025_REG_HOUR 0x02
33 #define RX8025_REG_WDAY 0x03
34 #define RX8025_REG_MDAY 0x04
35 #define RX8025_REG_MONTH 0x05
36 #define RX8025_REG_YEAR 0x06
37 #define RX8025_REG_DIGOFF 0x07
38 #define RX8025_REG_ALWMIN 0x08
39 #define RX8025_REG_ALWHOUR 0x09
40 #define RX8025_REG_ALWWDAY 0x0a
41 #define RX8025_REG_ALDMIN 0x0b
42 #define RX8025_REG_ALDHOUR 0x0c
43 /* 0x0d is reserved */
44 #define RX8025_REG_CTRL1 0x0e
45 #define RX8025_REG_CTRL2 0x0f
47 #define RX8025_BIT_CTRL1_CT (7 << 0)
48 /* 1 Hz periodic level irq */
49 #define RX8025_BIT_CTRL1_CT_1HZ 4
50 #define RX8025_BIT_CTRL1_TEST (1 << 3)
51 #define RX8025_BIT_CTRL1_1224 (1 << 5)
52 #define RX8025_BIT_CTRL1_DALE (1 << 6)
53 #define RX8025_BIT_CTRL1_WALE (1 << 7)
55 #define RX8025_BIT_CTRL2_DAFG (1 << 0)
56 #define RX8025_BIT_CTRL2_WAFG (1 << 1)
57 #define RX8025_BIT_CTRL2_CTFG (1 << 2)
58 #define RX8025_BIT_CTRL2_PON (1 << 4)
59 #define RX8025_BIT_CTRL2_XST (1 << 5)
60 #define RX8025_BIT_CTRL2_VDET (1 << 6)
62 /* Clock precision adjustment */
63 #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
64 #define RX8025_ADJ_DATA_MAX 62
65 #define RX8025_ADJ_DATA_MIN -62
67 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;
76 struct work_struct work;
81 static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value)
83 int ret = i2c_smbus_read_byte_data(client, (number << 4) | 0x08);
86 dev_err(&client->dev, "Unable to read register #%d\n", number);
94 static int rx8025_read_regs(struct i2c_client *client,
95 int number, u8 length, u8 *values)
97 int ret = i2c_smbus_read_i2c_block_data(client, (number << 4) | 0x08,
101 dev_err(&client->dev, "Unable to read registers #%d..#%d\n",
102 number, number + length - 1);
103 return ret < 0 ? ret : -EIO;
109 static int rx8025_write_reg(struct i2c_client *client, int number, u8 value)
111 int ret = i2c_smbus_write_byte_data(client, number << 4, value);
114 dev_err(&client->dev, "Unable to write register #%d\n",
120 static int rx8025_write_regs(struct i2c_client *client,
121 int number, u8 length, u8 *values)
123 int ret = i2c_smbus_write_i2c_block_data(client, (number << 4) | 0x08,
127 dev_err(&client->dev, "Unable to write registers #%d..#%d\n",
128 number, number + length - 1);
133 static irqreturn_t rx8025_irq(int irq, void *dev_id)
135 struct i2c_client *client = dev_id;
136 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
138 disable_irq_nosync(irq);
139 schedule_work(&rx8025->work);
143 static void rx8025_work(struct work_struct *work)
145 struct rx8025_data *rx8025 = container_of(work, struct rx8025_data,
147 struct i2c_client *client = rx8025->client;
148 struct mutex *lock = &rx8025->rtc->ops_lock;
153 if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status))
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;
164 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
168 if (status & RX8025_BIT_CTRL2_DAFG) {
170 status &= RX8025_BIT_CTRL2_DAFG;
171 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
172 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
175 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
179 /* acknowledge IRQ */
180 rx8025_write_reg(client, RX8025_REG_CTRL2,
181 status | RX8025_BIT_CTRL2_XST);
184 if (!rx8025->exiting)
185 enable_irq(client->irq);
190 static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
192 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
196 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
200 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
201 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
202 date[0], date[1], date[2], date[3], date[4],
205 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
206 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
207 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
208 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
210 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
211 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
213 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
214 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
215 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]);
217 if (dt->tm_year < 70)
220 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
221 dt->tm_sec, dt->tm_min, dt->tm_hour,
222 dt->tm_mday, dt->tm_mon, dt->tm_year);
224 return rtc_valid_tm(dt);
227 static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
229 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
233 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
234 * year. Next time this is wrong is 2100, which will not be a leap
239 * Here the read-only bits are written as "0". I'm not sure if that
242 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
243 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
244 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
245 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
247 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
248 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
250 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
251 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
252 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
253 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year % 100);
256 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
258 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
260 return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
263 static int rx8025_init_client(struct i2c_client *client, int *need_reset)
265 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
270 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
274 /* Keep test bit zero ! */
275 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
277 if (ctrl[1] & RX8025_BIT_CTRL2_PON) {
278 dev_warn(&client->dev, "power-on reset was detected, "
279 "you may have to readjust the clock\n");
283 if (ctrl[1] & RX8025_BIT_CTRL2_VDET) {
284 dev_warn(&client->dev, "a power voltage drop was detected, "
285 "you may have to readjust the clock\n");
289 if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) {
290 dev_warn(&client->dev, "Oscillation stop was detected,"
291 "you may have to readjust the clock\n");
295 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
296 dev_warn(&client->dev, "Alarm was detected\n");
300 if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
303 if (*need_reset || need_clear) {
305 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
306 RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
307 RX8025_BIT_CTRL2_DAFG);
308 ctrl2 |= RX8025_BIT_CTRL2_XST;
310 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
317 static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
319 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
320 struct i2c_client *client = rx8025->client;
324 if (client->irq <= 0)
327 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
331 err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2);
335 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
336 __func__, ald[0], ald[1], ctrl2);
338 /* Hardware alarms precision is 1 minute! */
340 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
341 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
342 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
344 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
345 + (ald[1] & 0x20 ? 12 : 0);
347 t->time.tm_wday = -1;
348 t->time.tm_mday = -1;
350 t->time.tm_year = -1;
352 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
354 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
355 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
356 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
357 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
362 static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
369 if (client->irq <= 0)
372 /* Hardware alarm precision is 1 minute! */
373 ald[0] = bin2bcd(t->time.tm_min);
374 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
375 ald[1] = bin2bcd(t->time.tm_hour);
377 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
378 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
380 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
382 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
383 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
384 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
389 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
394 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
395 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
404 static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
406 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
410 ctrl1 = rx8025->ctrl1;
412 ctrl1 |= RX8025_BIT_CTRL1_DALE;
414 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
416 if (ctrl1 != rx8025->ctrl1) {
417 rx8025->ctrl1 = ctrl1;
418 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
426 static int rx8025_irq_set_state(struct device *dev, int enabled)
428 struct i2c_client *client = to_i2c_client(dev);
429 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
433 if (client->irq <= 0)
436 ctrl1 = rx8025->ctrl1 & ~RX8025_BIT_CTRL1_CT;
438 ctrl1 |= RX8025_BIT_CTRL1_CT_1HZ;
439 if (ctrl1 != rx8025->ctrl1) {
440 rx8025->ctrl1 = ctrl1;
441 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
450 static struct rtc_class_ops rx8025_rtc_ops = {
451 .read_time = rx8025_get_time,
452 .set_time = rx8025_set_time,
453 .read_alarm = rx8025_read_alarm,
454 .set_alarm = rx8025_set_alarm,
455 .alarm_irq_enable = rx8025_alarm_irq_enable,
456 .irq_set_state = rx8025_irq_set_state,
460 * Clock precision adjustment support
462 * According to the RX8025 SA/NB application manual the frequency and
463 * temperature charateristics can be approximated using the following
466 * df = a * (ut - t)**2
468 * df: Frequency deviation in any temperature
469 * a : Coefficient = (-35 +-5) * 10**-9
470 * ut: Ultimate temperature in degree = +25 +-5 degree
471 * t : Any temperature in degree
473 * Note that the clock adjustment in ppb must be entered (which is
474 * the negative value of the deviation).
476 static int rx8025_get_clock_adjust(struct device *dev, int *adj)
478 struct i2c_client *client = to_i2c_client(dev);
482 err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff);
486 *adj = digoff >= 64 ? digoff - 128 : digoff;
489 *adj *= -RX8025_ADJ_RESOLUTION;
494 static int rx8025_set_clock_adjust(struct device *dev, int adj)
496 struct i2c_client *client = to_i2c_client(dev);
500 adj /= -RX8025_ADJ_RESOLUTION;
501 if (adj > RX8025_ADJ_DATA_MAX)
502 adj = RX8025_ADJ_DATA_MAX;
503 else if (adj < RX8025_ADJ_DATA_MIN)
504 adj = RX8025_ADJ_DATA_MIN;
511 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
515 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
520 static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
521 struct device_attribute *attr,
526 err = rx8025_get_clock_adjust(dev, &adj);
530 return sprintf(buf, "%d\n", adj);
533 static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
539 if (sscanf(buf, "%i", &adj) != 1)
542 err = rx8025_set_clock_adjust(dev, adj);
544 return err ? err : count;
547 static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
548 rx8025_sysfs_show_clock_adjust,
549 rx8025_sysfs_store_clock_adjust);
551 static int rx8025_sysfs_register(struct device *dev)
553 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
556 static void rx8025_sysfs_unregister(struct device *dev)
558 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
561 static int __devinit rx8025_probe(struct i2c_client *client,
562 const struct i2c_device_id *id)
564 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
565 struct rx8025_data *rx8025;
566 int err, need_reset = 0;
568 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
569 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
570 dev_err(&adapter->dev,
571 "doesn't support required functionality\n");
576 rx8025 = kzalloc(sizeof(*rx8025), GFP_KERNEL);
578 dev_err(&adapter->dev, "failed to alloc memory\n");
583 rx8025->client = client;
584 i2c_set_clientdata(client, rx8025);
585 INIT_WORK(&rx8025->work, rx8025_work);
587 err = rx8025_init_client(client, &need_reset);
593 dev_info(&client->dev,
594 "bad conditions detected, resetting date\n");
595 rtc_time_to_tm(0, &tm); /* 1970/1/1 */
596 rx8025_set_time(&client->dev, &tm);
599 rx8025->rtc = rtc_device_register(client->name, &client->dev,
600 &rx8025_rtc_ops, THIS_MODULE);
601 if (IS_ERR(rx8025->rtc)) {
602 err = PTR_ERR(rx8025->rtc);
603 dev_err(&client->dev, "unable to register the class device\n");
607 if (client->irq > 0) {
608 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
609 err = request_irq(client->irq, rx8025_irq,
610 0, "rx8025", client);
612 dev_err(&client->dev, "unable to request IRQ\n");
617 rx8025->rtc->irq_freq = 1;
618 rx8025->rtc->max_user_freq = 1;
620 err = rx8025_sysfs_register(&client->dev);
628 free_irq(client->irq, client);
631 rtc_device_unregister(rx8025->rtc);
634 i2c_set_clientdata(client, NULL);
638 dev_err(&adapter->dev, "probing for rx8025 failed\n");
642 static int __devexit rx8025_remove(struct i2c_client *client)
644 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
645 struct mutex *lock = &rx8025->rtc->ops_lock;
647 if (client->irq > 0) {
652 free_irq(client->irq, client);
653 flush_scheduled_work();
656 rx8025_sysfs_unregister(&client->dev);
657 rtc_device_unregister(rx8025->rtc);
658 i2c_set_clientdata(client, NULL);
663 static struct i2c_driver rx8025_driver = {
665 .name = "rtc-rx8025",
666 .owner = THIS_MODULE,
668 .probe = rx8025_probe,
669 .remove = __devexit_p(rx8025_remove),
670 .id_table = rx8025_id,
673 static int __init rx8025_init(void)
675 return i2c_add_driver(&rx8025_driver);
678 static void __exit rx8025_exit(void)
680 i2c_del_driver(&rx8025_driver);
683 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
684 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
685 MODULE_LICENSE("GPL");
687 module_init(rx8025_init);
688 module_exit(rx8025_exit);