2 * A driver for the I2C members of the Abracon AB x8xx RTC family,
3 * and compatible: AB 1805 and AB 0805
5 * Copyright 2014-2015 Macq S.A.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/bcd.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/watchdog.h>
22 #define ABX8XX_REG_HTH 0x00
23 #define ABX8XX_REG_SC 0x01
24 #define ABX8XX_REG_MN 0x02
25 #define ABX8XX_REG_HR 0x03
26 #define ABX8XX_REG_DA 0x04
27 #define ABX8XX_REG_MO 0x05
28 #define ABX8XX_REG_YR 0x06
29 #define ABX8XX_REG_WD 0x07
31 #define ABX8XX_REG_AHTH 0x08
32 #define ABX8XX_REG_ASC 0x09
33 #define ABX8XX_REG_AMN 0x0a
34 #define ABX8XX_REG_AHR 0x0b
35 #define ABX8XX_REG_ADA 0x0c
36 #define ABX8XX_REG_AMO 0x0d
37 #define ABX8XX_REG_AWD 0x0e
39 #define ABX8XX_REG_STATUS 0x0f
40 #define ABX8XX_STATUS_AF BIT(2)
41 #define ABX8XX_STATUS_WDT BIT(6)
43 #define ABX8XX_REG_CTRL1 0x10
44 #define ABX8XX_CTRL_WRITE BIT(0)
45 #define ABX8XX_CTRL_ARST BIT(2)
46 #define ABX8XX_CTRL_12_24 BIT(6)
48 #define ABX8XX_REG_IRQ 0x12
49 #define ABX8XX_IRQ_AIE BIT(2)
50 #define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
52 #define ABX8XX_REG_CD_TIMER_CTL 0x18
54 #define ABX8XX_REG_OSC 0x1c
55 #define ABX8XX_OSC_FOS BIT(3)
56 #define ABX8XX_OSC_BOS BIT(4)
57 #define ABX8XX_OSC_ACAL_512 BIT(5)
58 #define ABX8XX_OSC_ACAL_1024 BIT(6)
60 #define ABX8XX_OSC_OSEL BIT(7)
62 #define ABX8XX_REG_OSS 0x1d
63 #define ABX8XX_OSS_OF BIT(1)
64 #define ABX8XX_OSS_OMODE BIT(4)
66 #define ABX8XX_REG_WDT 0x1b
67 #define ABX8XX_WDT_WDS BIT(7)
68 #define ABX8XX_WDT_BMB_MASK 0x7c
69 #define ABX8XX_WDT_BMB_SHIFT 2
70 #define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT)
71 #define ABX8XX_WDT_WRB_MASK 0x03
72 #define ABX8XX_WDT_WRB_1HZ 0x02
74 #define ABX8XX_REG_CFG_KEY 0x1f
75 #define ABX8XX_CFG_KEY_OSC 0xa1
76 #define ABX8XX_CFG_KEY_MISC 0x9d
78 #define ABX8XX_REG_ID0 0x28
80 #define ABX8XX_REG_TRICKLE 0x20
81 #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
82 #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
83 #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
85 static u8 trickle_resistors[] = {0, 3, 6, 11};
87 enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
88 AB1801, AB1803, AB1804, AB1805, ABX80X};
96 static struct abx80x_cap abx80x_caps[] = {
97 [AB0801] = {.pn = 0x0801},
98 [AB0803] = {.pn = 0x0803},
99 [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true},
100 [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true},
101 [AB1801] = {.pn = 0x1801},
102 [AB1803] = {.pn = 0x1803},
103 [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true},
104 [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
109 struct rtc_device *rtc;
110 struct i2c_client *client;
111 struct watchdog_device wdog;
114 static int abx80x_is_rc_mode(struct i2c_client *client)
118 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
120 dev_err(&client->dev,
121 "Failed to read autocalibration attribute\n");
125 return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
128 static int abx80x_enable_trickle_charger(struct i2c_client *client,
134 * Write the configuration key register to enable access to the Trickle
137 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
138 ABX8XX_CFG_KEY_MISC);
140 dev_err(&client->dev, "Unable to write configuration key\n");
144 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
145 ABX8XX_TRICKLE_CHARGE_ENABLE |
148 dev_err(&client->dev, "Unable to write trickle register\n");
155 static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
157 struct i2c_client *client = to_i2c_client(dev);
158 unsigned char buf[8];
159 int err, flags, rc_mode = 0;
161 /* Read the Oscillator Failure only in XT mode */
162 rc_mode = abx80x_is_rc_mode(client);
167 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
171 if (flags & ABX8XX_OSS_OF) {
172 dev_err(dev, "Oscillator failure, data is invalid.\n");
177 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
180 dev_err(&client->dev, "Unable to read date\n");
184 tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
185 tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
186 tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
187 tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
188 tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
189 tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
190 tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
195 static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
197 struct i2c_client *client = to_i2c_client(dev);
198 unsigned char buf[8];
201 if (tm->tm_year < 100)
204 buf[ABX8XX_REG_HTH] = 0;
205 buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
206 buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
207 buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
208 buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
209 buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
210 buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
211 buf[ABX8XX_REG_WD] = tm->tm_wday;
213 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
216 dev_err(&client->dev, "Unable to write to date registers\n");
220 /* Clear the OF bit of Oscillator Status Register */
221 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
225 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
226 flags & ~ABX8XX_OSS_OF);
228 dev_err(&client->dev, "Unable to write oscillator status register\n");
235 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
237 struct i2c_client *client = dev_id;
238 struct abx80x_priv *priv = i2c_get_clientdata(client);
239 struct rtc_device *rtc = priv->rtc;
242 status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
246 if (status & ABX8XX_STATUS_AF)
247 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
250 * It is unclear if we'll get an interrupt before the external
253 if (status & ABX8XX_STATUS_WDT)
254 dev_alert(&client->dev, "watchdog timeout interrupt.\n");
256 i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
261 static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
263 struct i2c_client *client = to_i2c_client(dev);
264 unsigned char buf[7];
268 if (client->irq <= 0)
271 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
276 irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
280 t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
281 t->time.tm_min = bcd2bin(buf[1] & 0x7F);
282 t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
283 t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
284 t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
285 t->time.tm_wday = buf[5] & 0x7;
287 t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
288 t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
293 static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
295 struct i2c_client *client = to_i2c_client(dev);
299 if (client->irq <= 0)
303 alarm[1] = bin2bcd(t->time.tm_sec);
304 alarm[2] = bin2bcd(t->time.tm_min);
305 alarm[3] = bin2bcd(t->time.tm_hour);
306 alarm[4] = bin2bcd(t->time.tm_mday);
307 alarm[5] = bin2bcd(t->time.tm_mon + 1);
309 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
310 sizeof(alarm), alarm);
312 dev_err(&client->dev, "Unable to write alarm registers\n");
317 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
327 static int abx80x_rtc_set_autocalibration(struct device *dev,
330 struct i2c_client *client = to_i2c_client(dev);
331 int retval, flags = 0;
333 if ((autocalibration != 0) && (autocalibration != 1024) &&
334 (autocalibration != 512)) {
335 dev_err(dev, "autocalibration value outside permitted range\n");
339 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
343 if (autocalibration == 0) {
344 flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
345 } else if (autocalibration == 1024) {
346 /* 1024 autocalibration is 0x10 */
347 flags |= ABX8XX_OSC_ACAL_1024;
348 flags &= ~(ABX8XX_OSC_ACAL_512);
350 /* 512 autocalibration is 0x11 */
351 flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
354 /* Unlock write access to Oscillator Control Register */
355 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
358 dev_err(dev, "Failed to write CONFIG_KEY register\n");
362 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
367 static int abx80x_rtc_get_autocalibration(struct device *dev)
369 struct i2c_client *client = to_i2c_client(dev);
370 int flags = 0, autocalibration;
372 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
376 if (flags & ABX8XX_OSC_ACAL_512)
377 autocalibration = 512;
378 else if (flags & ABX8XX_OSC_ACAL_1024)
379 autocalibration = 1024;
383 return autocalibration;
386 static ssize_t autocalibration_store(struct device *dev,
387 struct device_attribute *attr,
388 const char *buf, size_t count)
391 unsigned long autocalibration = 0;
393 retval = kstrtoul(buf, 10, &autocalibration);
395 dev_err(dev, "Failed to store RTC autocalibration attribute\n");
399 retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
401 return retval ? retval : count;
404 static ssize_t autocalibration_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 int autocalibration = 0;
409 autocalibration = abx80x_rtc_get_autocalibration(dev);
410 if (autocalibration < 0) {
411 dev_err(dev, "Failed to read RTC autocalibration\n");
413 return autocalibration;
416 return sprintf(buf, "%d\n", autocalibration);
419 static DEVICE_ATTR_RW(autocalibration);
421 static ssize_t oscillator_store(struct device *dev,
422 struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct i2c_client *client = to_i2c_client(dev);
426 int retval, flags, rc_mode = 0;
428 if (strncmp(buf, "rc", 2) == 0) {
430 } else if (strncmp(buf, "xtal", 4) == 0) {
433 dev_err(dev, "Oscillator selection value outside permitted ones\n");
437 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
442 flags &= ~(ABX8XX_OSC_OSEL);
444 flags |= (ABX8XX_OSC_OSEL);
446 /* Unlock write access on Oscillator Control register */
447 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
450 dev_err(dev, "Failed to write CONFIG_KEY register\n");
454 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
456 dev_err(dev, "Failed to write Oscillator Control register\n");
460 return retval ? retval : count;
463 static ssize_t oscillator_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
467 struct i2c_client *client = to_i2c_client(dev);
469 rc_mode = abx80x_is_rc_mode(client);
472 dev_err(dev, "Failed to read RTC oscillator selection\n");
478 return sprintf(buf, "rc\n");
480 return sprintf(buf, "xtal\n");
483 static DEVICE_ATTR_RW(oscillator);
485 static struct attribute *rtc_calib_attrs[] = {
486 &dev_attr_autocalibration.attr,
487 &dev_attr_oscillator.attr,
491 static const struct attribute_group rtc_calib_attr_group = {
492 .attrs = rtc_calib_attrs,
495 static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
497 struct i2c_client *client = to_i2c_client(dev);
501 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
505 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
510 static const struct rtc_class_ops abx80x_rtc_ops = {
511 .read_time = abx80x_rtc_read_time,
512 .set_time = abx80x_rtc_set_time,
513 .read_alarm = abx80x_read_alarm,
514 .set_alarm = abx80x_set_alarm,
515 .alarm_irq_enable = abx80x_alarm_irq_enable,
518 static int abx80x_dt_trickle_cfg(struct device_node *np)
525 ret = of_property_read_string(np, "abracon,tc-diode", &diode);
529 if (!strcmp(diode, "standard"))
530 trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
531 else if (!strcmp(diode, "schottky"))
532 trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
536 ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
540 for (i = 0; i < sizeof(trickle_resistors); i++)
541 if (trickle_resistors[i] == tmp)
544 if (i == sizeof(trickle_resistors))
547 return (trickle_cfg | i);
550 static void rtc_calib_remove_sysfs_group(void *_dev)
552 struct device *dev = _dev;
554 sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
557 #ifdef CONFIG_WATCHDOG
559 static inline u8 timeout_bits(unsigned int timeout)
561 return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) |
565 static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog,
566 unsigned int timeout)
568 struct abx80x_priv *priv = watchdog_get_drvdata(wdog);
569 u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout);
572 * Writing any timeout to the WDT register resets the watchdog timer.
573 * Writing 0 disables it.
575 return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val);
578 static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
579 unsigned int new_timeout)
583 if (watchdog_hw_running(wdog))
584 err = __abx80x_wdog_set_timeout(wdog, new_timeout);
587 wdog->timeout = new_timeout;
592 static int abx80x_wdog_ping(struct watchdog_device *wdog)
594 return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
597 static int abx80x_wdog_start(struct watchdog_device *wdog)
599 return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
602 static int abx80x_wdog_stop(struct watchdog_device *wdog)
604 return __abx80x_wdog_set_timeout(wdog, 0);
607 static const struct watchdog_info abx80x_wdog_info = {
608 .identity = "abx80x watchdog",
609 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
612 static const struct watchdog_ops abx80x_wdog_ops = {
613 .owner = THIS_MODULE,
614 .start = abx80x_wdog_start,
615 .stop = abx80x_wdog_stop,
616 .ping = abx80x_wdog_ping,
617 .set_timeout = abx80x_wdog_set_timeout,
620 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
622 priv->wdog.parent = &priv->client->dev;
623 priv->wdog.ops = &abx80x_wdog_ops;
624 priv->wdog.info = &abx80x_wdog_info;
625 priv->wdog.min_timeout = 1;
626 priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME;
627 priv->wdog.timeout = ABX8XX_WDT_MAX_TIME;
629 watchdog_set_drvdata(&priv->wdog, priv);
631 return devm_watchdog_register_device(&priv->client->dev, &priv->wdog);
634 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
640 static int abx80x_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
643 struct device_node *np = client->dev.of_node;
644 struct abx80x_priv *priv;
645 int i, data, err, trickle_cfg = -EINVAL;
647 unsigned int part = id->driver_data;
648 unsigned int partnumber;
649 unsigned int majrev, minrev;
654 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
657 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
660 dev_err(&client->dev, "Unable to read partnumber\n");
664 partnumber = (buf[0] << 8) | buf[1];
665 majrev = buf[2] >> 3;
666 minrev = buf[2] & 0x7;
667 lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
668 uid = ((buf[4] & 0x7f) << 8) | buf[5];
669 wafer = (buf[6] & 0x7c) >> 2;
670 dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
671 partnumber, majrev, minrev, lot, wafer, uid);
673 data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
675 dev_err(&client->dev, "Unable to read control register\n");
679 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
680 ((data & ~(ABX8XX_CTRL_12_24 |
684 dev_err(&client->dev, "Unable to write control register\n");
688 /* part autodetection */
689 if (part == ABX80X) {
690 for (i = 0; abx80x_caps[i].pn; i++)
691 if (partnumber == abx80x_caps[i].pn)
693 if (abx80x_caps[i].pn == 0) {
694 dev_err(&client->dev, "Unknown part: %04x\n",
701 if (partnumber != abx80x_caps[part].pn) {
702 dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
703 partnumber, abx80x_caps[part].pn);
707 if (np && abx80x_caps[part].has_tc)
708 trickle_cfg = abx80x_dt_trickle_cfg(np);
710 if (trickle_cfg > 0) {
711 dev_info(&client->dev, "Enabling trickle charger: %02x\n",
713 abx80x_enable_trickle_charger(client, trickle_cfg);
716 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
721 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
725 priv->rtc = devm_rtc_allocate_device(&client->dev);
726 if (IS_ERR(priv->rtc))
727 return PTR_ERR(priv->rtc);
729 priv->rtc->ops = &abx80x_rtc_ops;
730 priv->client = client;
732 i2c_set_clientdata(client, priv);
734 if (abx80x_caps[part].has_wdog) {
735 err = abx80x_setup_watchdog(priv);
740 if (client->irq > 0) {
741 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
742 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
744 IRQF_SHARED | IRQF_ONESHOT,
748 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
753 /* Export sysfs entries */
754 err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
756 dev_err(&client->dev, "Failed to create sysfs group: %d\n",
761 err = devm_add_action_or_reset(&client->dev,
762 rtc_calib_remove_sysfs_group,
765 dev_err(&client->dev,
766 "Failed to add sysfs cleanup action: %d\n",
771 err = rtc_register_device(priv->rtc);
776 static int abx80x_remove(struct i2c_client *client)
781 static const struct i2c_device_id abx80x_id[] = {
782 { "abx80x", ABX80X },
783 { "ab0801", AB0801 },
784 { "ab0803", AB0803 },
785 { "ab0804", AB0804 },
786 { "ab0805", AB0805 },
787 { "ab1801", AB1801 },
788 { "ab1803", AB1803 },
789 { "ab1804", AB1804 },
790 { "ab1805", AB1805 },
791 { "rv1805", AB1805 },
794 MODULE_DEVICE_TABLE(i2c, abx80x_id);
796 static struct i2c_driver abx80x_driver = {
798 .name = "rtc-abx80x",
800 .probe = abx80x_probe,
801 .remove = abx80x_remove,
802 .id_table = abx80x_id,
805 module_i2c_driver(abx80x_driver);
809 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
810 MODULE_LICENSE("GPL v2");