1 // SPDX-License-Identifier: GPL-2.0-only
3 * An I2C and SPI driver for the NXP PCF2127/29 RTC
4 * Copyright 2013 Til-Technologies
6 * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
8 * Watchdog and tamper functions
9 * Author: Bruno Thomsen <bruno.thomsen@gmail.com>
11 * based on the other drivers in this same directory.
13 * Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf
16 #include <linux/i2c.h>
17 #include <linux/spi/spi.h>
18 #include <linux/bcd.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
25 #include <linux/watchdog.h>
27 /* Control register 1 */
28 #define PCF2127_REG_CTRL1 0x00
29 #define PCF2127_BIT_CTRL1_POR_OVRD BIT(3)
30 #define PCF2127_BIT_CTRL1_TSF1 BIT(4)
31 /* Control register 2 */
32 #define PCF2127_REG_CTRL2 0x01
33 #define PCF2127_BIT_CTRL2_AIE BIT(1)
34 #define PCF2127_BIT_CTRL2_TSIE BIT(2)
35 #define PCF2127_BIT_CTRL2_AF BIT(4)
36 #define PCF2127_BIT_CTRL2_TSF2 BIT(5)
37 #define PCF2127_BIT_CTRL2_WDTF BIT(6)
38 /* Control register 3 */
39 #define PCF2127_REG_CTRL3 0x02
40 #define PCF2127_BIT_CTRL3_BLIE BIT(0)
41 #define PCF2127_BIT_CTRL3_BIE BIT(1)
42 #define PCF2127_BIT_CTRL3_BLF BIT(2)
43 #define PCF2127_BIT_CTRL3_BF BIT(3)
44 #define PCF2127_BIT_CTRL3_BTSE BIT(4)
45 /* Time and date registers */
46 #define PCF2127_REG_SC 0x03
47 #define PCF2127_BIT_SC_OSF BIT(7)
48 #define PCF2127_REG_MN 0x04
49 #define PCF2127_REG_HR 0x05
50 #define PCF2127_REG_DM 0x06
51 #define PCF2127_REG_DW 0x07
52 #define PCF2127_REG_MO 0x08
53 #define PCF2127_REG_YR 0x09
55 #define PCF2127_REG_ALARM_SC 0x0A
56 #define PCF2127_REG_ALARM_MN 0x0B
57 #define PCF2127_REG_ALARM_HR 0x0C
58 #define PCF2127_REG_ALARM_DM 0x0D
59 #define PCF2127_REG_ALARM_DW 0x0E
60 #define PCF2127_BIT_ALARM_AE BIT(7)
61 /* CLKOUT control register */
62 #define PCF2127_REG_CLKOUT 0x0f
63 #define PCF2127_BIT_CLKOUT_OTPR BIT(5)
64 /* Watchdog registers */
65 #define PCF2127_REG_WD_CTL 0x10
66 #define PCF2127_BIT_WD_CTL_TF0 BIT(0)
67 #define PCF2127_BIT_WD_CTL_TF1 BIT(1)
68 #define PCF2127_BIT_WD_CTL_CD0 BIT(6)
69 #define PCF2127_BIT_WD_CTL_CD1 BIT(7)
70 #define PCF2127_REG_WD_VAL 0x11
71 /* Tamper timestamp registers */
72 #define PCF2127_REG_TS_CTRL 0x12
73 #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6)
74 #define PCF2127_BIT_TS_CTRL_TSM BIT(7)
75 #define PCF2127_REG_TS_SC 0x13
76 #define PCF2127_REG_TS_MN 0x14
77 #define PCF2127_REG_TS_HR 0x15
78 #define PCF2127_REG_TS_DM 0x16
79 #define PCF2127_REG_TS_MO 0x17
80 #define PCF2127_REG_TS_YR 0x18
83 * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
84 * battery backed and can survive a power outage.
85 * PCF2129 doesn't have this feature.
87 #define PCF2127_REG_RAM_ADDR_MSB 0x1A
88 #define PCF2127_REG_RAM_WRT_CMD 0x1C
89 #define PCF2127_REG_RAM_RD_CMD 0x1D
91 /* Watchdog timer value constants */
92 #define PCF2127_WD_VAL_STOP 0
93 #define PCF2127_WD_VAL_MIN 2
94 #define PCF2127_WD_VAL_MAX 255
95 #define PCF2127_WD_VAL_DEFAULT 60
97 /* Mask for currently enabled interrupts */
98 #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1)
99 #define PCF2127_CTRL2_IRQ_MASK ( \
100 PCF2127_BIT_CTRL2_AF | \
101 PCF2127_BIT_CTRL2_WDTF | \
102 PCF2127_BIT_CTRL2_TSF2)
105 struct rtc_device *rtc;
106 struct watchdog_device wdd;
107 struct regmap *regmap;
114 * In the routines that deal directly with the pcf2127 hardware, we use
115 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
117 static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
119 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
120 unsigned char buf[10];
124 * Avoid reading CTRL2 register as it causes WD_VAL register
125 * value to reset to 0 which means watchdog is stopped.
127 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3,
128 (buf + PCF2127_REG_CTRL3),
129 ARRAY_SIZE(buf) - PCF2127_REG_CTRL3);
131 dev_err(dev, "%s: read error\n", __func__);
135 if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF)
137 "low voltage detected, check/replace RTC battery.\n");
139 /* Clock integrity is not guaranteed when OSF flag is set. */
140 if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) {
142 * no need clear the flag here,
143 * it will be cleared once the new date is saved
146 "oscillator stop detected, date/time is not reliable\n");
151 "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, "
152 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
153 __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC],
154 buf[PCF2127_REG_MN], buf[PCF2127_REG_HR],
155 buf[PCF2127_REG_DM], buf[PCF2127_REG_DW],
156 buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]);
158 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
159 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
160 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
161 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
162 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
163 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
164 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
167 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
168 "mday=%d, mon=%d, year=%d, wday=%d\n",
170 tm->tm_sec, tm->tm_min, tm->tm_hour,
171 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
176 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
178 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
179 unsigned char buf[7];
182 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
183 "mday=%d, mon=%d, year=%d, wday=%d\n",
185 tm->tm_sec, tm->tm_min, tm->tm_hour,
186 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
188 /* hours, minutes and seconds */
189 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
190 buf[i++] = bin2bcd(tm->tm_min);
191 buf[i++] = bin2bcd(tm->tm_hour);
192 buf[i++] = bin2bcd(tm->tm_mday);
193 buf[i++] = tm->tm_wday & 0x07;
196 buf[i++] = bin2bcd(tm->tm_mon + 1);
199 buf[i++] = bin2bcd(tm->tm_year - 100);
201 /* write register's data */
202 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
205 "%s: err=%d", __func__, err);
212 static int pcf2127_rtc_ioctl(struct device *dev,
213 unsigned int cmd, unsigned long arg)
215 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
221 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
225 if (val & PCF2127_BIT_CTRL3_BLF)
226 touser |= RTC_VL_BACKUP_LOW;
228 if (val & PCF2127_BIT_CTRL3_BF)
229 touser |= RTC_VL_BACKUP_SWITCH;
231 return put_user(touser, (unsigned int __user *)arg);
234 return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
235 PCF2127_BIT_CTRL3_BF, 0);
242 static int pcf2127_nvmem_read(void *priv, unsigned int offset,
243 void *val, size_t bytes)
245 struct pcf2127 *pcf2127 = priv;
247 unsigned char offsetbuf[] = { offset >> 8, offset };
249 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
254 return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
258 static int pcf2127_nvmem_write(void *priv, unsigned int offset,
259 void *val, size_t bytes)
261 struct pcf2127 *pcf2127 = priv;
263 unsigned char offsetbuf[] = { offset >> 8, offset };
265 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
270 return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
274 /* watchdog driver */
276 static int pcf2127_wdt_ping(struct watchdog_device *wdd)
278 struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
280 return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout);
284 * Restart watchdog timer if feature is active.
286 * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
287 * since register also contain control/status flags for other features.
288 * Always call this function after reading CTRL2 register.
290 static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
294 if (watchdog_active(wdd)) {
295 ret = pcf2127_wdt_ping(wdd);
298 "%s: watchdog restart failed, ret=%d\n",
305 static int pcf2127_wdt_start(struct watchdog_device *wdd)
307 return pcf2127_wdt_ping(wdd);
310 static int pcf2127_wdt_stop(struct watchdog_device *wdd)
312 struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
314 return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL,
315 PCF2127_WD_VAL_STOP);
318 static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
319 unsigned int new_timeout)
321 dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
322 new_timeout, wdd->timeout);
324 wdd->timeout = new_timeout;
326 return pcf2127_wdt_active_ping(wdd);
329 static const struct watchdog_info pcf2127_wdt_info = {
330 .identity = "NXP PCF2127/PCF2129 Watchdog",
331 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
334 static const struct watchdog_ops pcf2127_watchdog_ops = {
335 .owner = THIS_MODULE,
336 .start = pcf2127_wdt_start,
337 .stop = pcf2127_wdt_stop,
338 .ping = pcf2127_wdt_ping,
339 .set_timeout = pcf2127_wdt_set_timeout,
342 static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
347 if (!IS_ENABLED(CONFIG_WATCHDOG) ||
348 !device_property_read_bool(dev, "reset-source"))
351 pcf2127->wdd.parent = dev;
352 pcf2127->wdd.info = &pcf2127_wdt_info;
353 pcf2127->wdd.ops = &pcf2127_watchdog_ops;
354 pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN;
355 pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX;
356 pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT;
357 pcf2127->wdd.min_hw_heartbeat_ms = 500;
358 pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
360 watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
362 /* Test if watchdog timer is started by bootloader */
363 ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout);
368 set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status);
370 return devm_watchdog_register_device(dev, &pcf2127->wdd);
374 static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
376 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
381 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
385 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
389 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
394 alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
395 alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
397 alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
398 alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
399 alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
400 alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
405 static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
407 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
410 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
411 PCF2127_BIT_CTRL2_AIE,
412 enable ? PCF2127_BIT_CTRL2_AIE : 0);
416 return pcf2127_wdt_active_ping(&pcf2127->wdd);
419 static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
421 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
425 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
426 PCF2127_BIT_CTRL2_AF, 0);
430 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
434 buf[0] = bin2bcd(alrm->time.tm_sec);
435 buf[1] = bin2bcd(alrm->time.tm_min);
436 buf[2] = bin2bcd(alrm->time.tm_hour);
437 buf[3] = bin2bcd(alrm->time.tm_mday);
438 buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
440 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
445 return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
449 * This function reads ctrl2 register, caller is responsible for calling
450 * pcf2127_wdt_active_ping()
452 static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts)
454 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
457 unsigned char data[25];
459 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data,
462 dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
467 "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n",
468 __func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2],
469 data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC],
470 data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR],
471 data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO],
472 data[PCF2127_REG_TS_YR]);
474 tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F);
475 tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F);
476 tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F);
477 tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F);
478 /* TS_MO register (month) value range: 1-12 */
479 tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1;
480 tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]);
482 tm.tm_year += 100; /* assume we are in 1970...2069 */
484 ret = rtc_valid_tm(&tm);
486 dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
490 *ts = rtc_tm_to_time64(&tm);
494 static void pcf2127_rtc_ts_snapshot(struct device *dev)
496 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
499 /* Let userspace read the first timestamp */
500 if (pcf2127->ts_valid)
503 ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts);
505 pcf2127->ts_valid = true;
508 static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
510 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
511 unsigned int ctrl1, ctrl2;
514 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
518 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
522 if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
525 if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
526 pcf2127_rtc_ts_snapshot(dev);
528 if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
529 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
530 ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
532 if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
533 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
534 ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
536 if (ctrl2 & PCF2127_BIT_CTRL2_AF)
537 rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
539 pcf2127_wdt_active_ping(&pcf2127->wdd);
544 static const struct rtc_class_ops pcf2127_rtc_ops = {
545 .ioctl = pcf2127_rtc_ioctl,
546 .read_time = pcf2127_rtc_read_time,
547 .set_time = pcf2127_rtc_set_time,
548 .read_alarm = pcf2127_rtc_read_alarm,
549 .set_alarm = pcf2127_rtc_set_alarm,
550 .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
553 /* sysfs interface */
555 static ssize_t timestamp0_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
562 if (pcf2127->irq_enabled) {
563 pcf2127->ts_valid = false;
565 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
566 PCF2127_BIT_CTRL1_TSF1, 0);
568 dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret);
572 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
573 PCF2127_BIT_CTRL2_TSF2, 0);
575 dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret);
579 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
587 static ssize_t timestamp0_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
591 unsigned int ctrl1, ctrl2;
595 if (pcf2127->irq_enabled) {
596 if (!pcf2127->ts_valid)
600 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
604 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
608 if (!(ctrl1 & PCF2127_BIT_CTRL1_TSF1) &&
609 !(ctrl2 & PCF2127_BIT_CTRL2_TSF2))
612 ret = pcf2127_rtc_ts_read(dev->parent, &ts);
616 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
620 return sprintf(buf, "%llu\n", (unsigned long long)ts);
623 static DEVICE_ATTR_RW(timestamp0);
625 static struct attribute *pcf2127_attrs[] = {
626 &dev_attr_timestamp0.attr,
630 static const struct attribute_group pcf2127_attr_group = {
631 .attrs = pcf2127_attrs,
634 static int pcf2127_probe(struct device *dev, struct regmap *regmap,
635 int alarm_irq, const char *name, bool is_pcf2127)
637 struct pcf2127 *pcf2127;
641 dev_dbg(dev, "%s\n", __func__);
643 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
647 pcf2127->regmap = regmap;
649 dev_set_drvdata(dev, pcf2127);
651 pcf2127->rtc = devm_rtc_allocate_device(dev);
652 if (IS_ERR(pcf2127->rtc))
653 return PTR_ERR(pcf2127->rtc);
655 pcf2127->rtc->ops = &pcf2127_rtc_ops;
656 pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
657 pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
658 pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
659 set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features);
660 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features);
661 clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
667 * If flags = 0, devm_request_threaded_irq() will use IRQ flags
668 * obtained from device tree.
673 flags = IRQF_TRIGGER_LOW;
675 ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
677 flags | IRQF_ONESHOT,
680 dev_err(dev, "failed to request alarm irq\n");
683 pcf2127->irq_enabled = true;
686 if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
687 device_init_wakeup(dev, true);
688 set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
692 struct nvmem_config nvmem_cfg = {
694 .reg_read = pcf2127_nvmem_read,
695 .reg_write = pcf2127_nvmem_write,
699 ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
703 * The "Power-On Reset Override" facility prevents the RTC to do a reset
704 * after power on. For normal operation the PORO must be disabled.
706 regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
707 PCF2127_BIT_CTRL1_POR_OVRD);
709 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val);
713 if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
714 ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT,
715 PCF2127_BIT_CLKOUT_OTPR);
723 * Watchdog timer enabled and reset pin /RST activated when timed out.
724 * Select 1Hz clock source for watchdog timer.
725 * Note: Countdown timer disabled and not available.
726 * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD
727 * of register watchdg_tim_ctl. The bit[6] is labeled
728 * as T. Bits labeled as T must always be written with
731 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL,
732 PCF2127_BIT_WD_CTL_CD1 |
733 PCF2127_BIT_WD_CTL_CD0 |
734 PCF2127_BIT_WD_CTL_TF1 |
735 PCF2127_BIT_WD_CTL_TF0,
736 PCF2127_BIT_WD_CTL_CD1 |
737 (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
738 PCF2127_BIT_WD_CTL_TF1);
740 dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
744 pcf2127_watchdog_init(dev, pcf2127);
747 * Disable battery low/switch-over timestamp and interrupts.
748 * Clear battery interrupt flags which can block new trigger events.
749 * Note: This is the default chip behaviour but added to ensure
750 * correct tamper timestamp and interrupt function.
752 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
753 PCF2127_BIT_CTRL3_BTSE |
754 PCF2127_BIT_CTRL3_BIE |
755 PCF2127_BIT_CTRL3_BLIE, 0);
757 dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
763 * Enable timestamp function and store timestamp of first trigger
764 * event until TSF1 and TSF2 interrupt flags are cleared.
766 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL,
767 PCF2127_BIT_TS_CTRL_TSOFF |
768 PCF2127_BIT_TS_CTRL_TSM,
769 PCF2127_BIT_TS_CTRL_TSM);
771 dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n",
777 * Enable interrupt generation when TSF1 or TSF2 timestamp flags
778 * are set. Interrupt signal is an open-drain output and can be
779 * left floating if unused.
781 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
782 PCF2127_BIT_CTRL2_TSIE,
783 PCF2127_BIT_CTRL2_TSIE);
785 dev_err(dev, "%s: tamper detection config (ctrl2) failed\n",
790 ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group);
792 dev_err(dev, "%s: tamper sysfs registering failed\n",
797 return devm_rtc_register_device(pcf2127->rtc);
801 static const struct of_device_id pcf2127_of_match[] = {
802 { .compatible = "nxp,pcf2127" },
803 { .compatible = "nxp,pcf2129" },
804 { .compatible = "nxp,pca2129" },
807 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
810 #if IS_ENABLED(CONFIG_I2C)
812 static int pcf2127_i2c_write(void *context, const void *data, size_t count)
814 struct device *dev = context;
815 struct i2c_client *client = to_i2c_client(dev);
818 ret = i2c_master_send(client, data, count);
820 return ret < 0 ? ret : -EIO;
825 static int pcf2127_i2c_gather_write(void *context,
826 const void *reg, size_t reg_size,
827 const void *val, size_t val_size)
829 struct device *dev = context;
830 struct i2c_client *client = to_i2c_client(dev);
834 if (WARN_ON(reg_size != 1))
837 buf = kmalloc(val_size + 1, GFP_KERNEL);
842 memcpy(buf + 1, val, val_size);
844 ret = i2c_master_send(client, buf, val_size + 1);
848 if (ret != val_size + 1)
849 return ret < 0 ? ret : -EIO;
854 static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
855 void *val, size_t val_size)
857 struct device *dev = context;
858 struct i2c_client *client = to_i2c_client(dev);
861 if (WARN_ON(reg_size != 1))
864 ret = i2c_master_send(client, reg, 1);
866 return ret < 0 ? ret : -EIO;
868 ret = i2c_master_recv(client, val, val_size);
870 return ret < 0 ? ret : -EIO;
876 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
877 * is that the STOP condition is required between set register address and
878 * read register data when reading from registers.
880 static const struct regmap_bus pcf2127_i2c_regmap = {
881 .write = pcf2127_i2c_write,
882 .gather_write = pcf2127_i2c_gather_write,
883 .read = pcf2127_i2c_read,
886 static struct i2c_driver pcf2127_i2c_driver;
888 static int pcf2127_i2c_probe(struct i2c_client *client,
889 const struct i2c_device_id *id)
891 struct regmap *regmap;
892 static const struct regmap_config config = {
895 .max_register = 0x1d,
898 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
901 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
902 &client->dev, &config);
903 if (IS_ERR(regmap)) {
904 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
905 __func__, PTR_ERR(regmap));
906 return PTR_ERR(regmap);
909 return pcf2127_probe(&client->dev, regmap, client->irq,
910 pcf2127_i2c_driver.driver.name, id->driver_data);
913 static const struct i2c_device_id pcf2127_i2c_id[] = {
919 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
921 static struct i2c_driver pcf2127_i2c_driver = {
923 .name = "rtc-pcf2127-i2c",
924 .of_match_table = of_match_ptr(pcf2127_of_match),
926 .probe = pcf2127_i2c_probe,
927 .id_table = pcf2127_i2c_id,
930 static int pcf2127_i2c_register_driver(void)
932 return i2c_add_driver(&pcf2127_i2c_driver);
935 static void pcf2127_i2c_unregister_driver(void)
937 i2c_del_driver(&pcf2127_i2c_driver);
942 static int pcf2127_i2c_register_driver(void)
947 static void pcf2127_i2c_unregister_driver(void)
953 #if IS_ENABLED(CONFIG_SPI_MASTER)
955 static struct spi_driver pcf2127_spi_driver;
957 static int pcf2127_spi_probe(struct spi_device *spi)
959 static const struct regmap_config config = {
962 .read_flag_mask = 0xa0,
963 .write_flag_mask = 0x20,
964 .max_register = 0x1d,
966 struct regmap *regmap;
968 regmap = devm_regmap_init_spi(spi, &config);
969 if (IS_ERR(regmap)) {
970 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
971 __func__, PTR_ERR(regmap));
972 return PTR_ERR(regmap);
975 return pcf2127_probe(&spi->dev, regmap, spi->irq,
976 pcf2127_spi_driver.driver.name,
977 spi_get_device_id(spi)->driver_data);
980 static const struct spi_device_id pcf2127_spi_id[] = {
986 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
988 static struct spi_driver pcf2127_spi_driver = {
990 .name = "rtc-pcf2127-spi",
991 .of_match_table = of_match_ptr(pcf2127_of_match),
993 .probe = pcf2127_spi_probe,
994 .id_table = pcf2127_spi_id,
997 static int pcf2127_spi_register_driver(void)
999 return spi_register_driver(&pcf2127_spi_driver);
1002 static void pcf2127_spi_unregister_driver(void)
1004 spi_unregister_driver(&pcf2127_spi_driver);
1009 static int pcf2127_spi_register_driver(void)
1014 static void pcf2127_spi_unregister_driver(void)
1020 static int __init pcf2127_init(void)
1024 ret = pcf2127_i2c_register_driver();
1026 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
1030 ret = pcf2127_spi_register_driver();
1032 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
1033 pcf2127_i2c_unregister_driver();
1038 module_init(pcf2127_init)
1040 static void __exit pcf2127_exit(void)
1042 pcf2127_spi_unregister_driver();
1043 pcf2127_i2c_unregister_driver();
1045 module_exit(pcf2127_exit)
1047 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1048 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
1049 MODULE_LICENSE("GPL v2");