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);
377 unsigned int buf[5], ctrl2;
380 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
384 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
388 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
393 alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
394 alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
396 alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
397 alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
398 alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
399 alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
404 static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
406 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
409 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
410 PCF2127_BIT_CTRL2_AIE,
411 enable ? PCF2127_BIT_CTRL2_AIE : 0);
415 return pcf2127_wdt_active_ping(&pcf2127->wdd);
418 static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
420 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
424 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
425 PCF2127_BIT_CTRL2_AF, 0);
429 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
433 buf[0] = bin2bcd(alrm->time.tm_sec);
434 buf[1] = bin2bcd(alrm->time.tm_min);
435 buf[2] = bin2bcd(alrm->time.tm_hour);
436 buf[3] = bin2bcd(alrm->time.tm_mday);
437 buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
439 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
444 return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
448 * This function reads ctrl2 register, caller is responsible for calling
449 * pcf2127_wdt_active_ping()
451 static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts)
453 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
456 unsigned char data[25];
458 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data,
461 dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
466 "%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",
467 __func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2],
468 data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC],
469 data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR],
470 data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO],
471 data[PCF2127_REG_TS_YR]);
473 tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F);
474 tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F);
475 tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F);
476 tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F);
477 /* TS_MO register (month) value range: 1-12 */
478 tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1;
479 tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]);
481 tm.tm_year += 100; /* assume we are in 1970...2069 */
483 ret = rtc_valid_tm(&tm);
485 dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
489 *ts = rtc_tm_to_time64(&tm);
493 static void pcf2127_rtc_ts_snapshot(struct device *dev)
495 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
498 /* Let userspace read the first timestamp */
499 if (pcf2127->ts_valid)
502 ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts);
504 pcf2127->ts_valid = true;
507 static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
509 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
510 unsigned int ctrl1, ctrl2;
513 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
517 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
521 if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
524 if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
525 pcf2127_rtc_ts_snapshot(dev);
527 if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
528 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
529 ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
531 if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
532 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
533 ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
535 if (ctrl2 & PCF2127_BIT_CTRL2_AF)
536 rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
538 pcf2127_wdt_active_ping(&pcf2127->wdd);
543 static const struct rtc_class_ops pcf2127_rtc_ops = {
544 .ioctl = pcf2127_rtc_ioctl,
545 .read_time = pcf2127_rtc_read_time,
546 .set_time = pcf2127_rtc_set_time,
547 .read_alarm = pcf2127_rtc_read_alarm,
548 .set_alarm = pcf2127_rtc_set_alarm,
549 .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
552 /* sysfs interface */
554 static ssize_t timestamp0_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
558 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
561 if (pcf2127->irq_enabled) {
562 pcf2127->ts_valid = false;
564 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
565 PCF2127_BIT_CTRL1_TSF1, 0);
567 dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret);
571 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
572 PCF2127_BIT_CTRL2_TSF2, 0);
574 dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret);
578 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
586 static ssize_t timestamp0_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
589 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
590 unsigned int ctrl1, ctrl2;
594 if (pcf2127->irq_enabled) {
595 if (!pcf2127->ts_valid)
599 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
603 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
607 if (!(ctrl1 & PCF2127_BIT_CTRL1_TSF1) &&
608 !(ctrl2 & PCF2127_BIT_CTRL2_TSF2))
611 ret = pcf2127_rtc_ts_read(dev->parent, &ts);
615 ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
619 return sprintf(buf, "%llu\n", (unsigned long long)ts);
622 static DEVICE_ATTR_RW(timestamp0);
624 static struct attribute *pcf2127_attrs[] = {
625 &dev_attr_timestamp0.attr,
629 static const struct attribute_group pcf2127_attr_group = {
630 .attrs = pcf2127_attrs,
633 static int pcf2127_probe(struct device *dev, struct regmap *regmap,
634 int alarm_irq, const char *name, bool is_pcf2127)
636 struct pcf2127 *pcf2127;
640 dev_dbg(dev, "%s\n", __func__);
642 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
646 pcf2127->regmap = regmap;
648 dev_set_drvdata(dev, pcf2127);
650 pcf2127->rtc = devm_rtc_allocate_device(dev);
651 if (IS_ERR(pcf2127->rtc))
652 return PTR_ERR(pcf2127->rtc);
654 pcf2127->rtc->ops = &pcf2127_rtc_ops;
655 pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
656 pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
657 pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
658 pcf2127->rtc->uie_unsupported = 1;
659 clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
662 ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
664 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
667 dev_err(dev, "failed to request alarm irq\n");
670 pcf2127->irq_enabled = true;
673 if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
674 device_init_wakeup(dev, true);
675 set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
679 struct nvmem_config nvmem_cfg = {
681 .reg_read = pcf2127_nvmem_read,
682 .reg_write = pcf2127_nvmem_write,
686 ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
690 * The "Power-On Reset Override" facility prevents the RTC to do a reset
691 * after power on. For normal operation the PORO must be disabled.
693 regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
694 PCF2127_BIT_CTRL1_POR_OVRD);
696 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val);
700 if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
701 ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT,
702 PCF2127_BIT_CLKOUT_OTPR);
710 * Watchdog timer enabled and reset pin /RST activated when timed out.
711 * Select 1Hz clock source for watchdog timer.
712 * Note: Countdown timer disabled and not available.
713 * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD
714 * of register watchdg_tim_ctl. The bit[6] is labeled
715 * as T. Bits labeled as T must always be written with
718 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL,
719 PCF2127_BIT_WD_CTL_CD1 |
720 PCF2127_BIT_WD_CTL_CD0 |
721 PCF2127_BIT_WD_CTL_TF1 |
722 PCF2127_BIT_WD_CTL_TF0,
723 PCF2127_BIT_WD_CTL_CD1 |
724 (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
725 PCF2127_BIT_WD_CTL_TF1);
727 dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
731 pcf2127_watchdog_init(dev, pcf2127);
734 * Disable battery low/switch-over timestamp and interrupts.
735 * Clear battery interrupt flags which can block new trigger events.
736 * Note: This is the default chip behaviour but added to ensure
737 * correct tamper timestamp and interrupt function.
739 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
740 PCF2127_BIT_CTRL3_BTSE |
741 PCF2127_BIT_CTRL3_BIE |
742 PCF2127_BIT_CTRL3_BLIE, 0);
744 dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
750 * Enable timestamp function and store timestamp of first trigger
751 * event until TSF1 and TFS2 interrupt flags are cleared.
753 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL,
754 PCF2127_BIT_TS_CTRL_TSOFF |
755 PCF2127_BIT_TS_CTRL_TSM,
756 PCF2127_BIT_TS_CTRL_TSM);
758 dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n",
764 * Enable interrupt generation when TSF1 or TSF2 timestamp flags
765 * are set. Interrupt signal is an open-drain output and can be
766 * left floating if unused.
768 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
769 PCF2127_BIT_CTRL2_TSIE,
770 PCF2127_BIT_CTRL2_TSIE);
772 dev_err(dev, "%s: tamper detection config (ctrl2) failed\n",
777 ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group);
779 dev_err(dev, "%s: tamper sysfs registering failed\n",
784 return devm_rtc_register_device(pcf2127->rtc);
788 static const struct of_device_id pcf2127_of_match[] = {
789 { .compatible = "nxp,pcf2127" },
790 { .compatible = "nxp,pcf2129" },
791 { .compatible = "nxp,pca2129" },
794 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
797 #if IS_ENABLED(CONFIG_I2C)
799 static int pcf2127_i2c_write(void *context, const void *data, size_t count)
801 struct device *dev = context;
802 struct i2c_client *client = to_i2c_client(dev);
805 ret = i2c_master_send(client, data, count);
807 return ret < 0 ? ret : -EIO;
812 static int pcf2127_i2c_gather_write(void *context,
813 const void *reg, size_t reg_size,
814 const void *val, size_t val_size)
816 struct device *dev = context;
817 struct i2c_client *client = to_i2c_client(dev);
821 if (WARN_ON(reg_size != 1))
824 buf = kmalloc(val_size + 1, GFP_KERNEL);
829 memcpy(buf + 1, val, val_size);
831 ret = i2c_master_send(client, buf, val_size + 1);
835 if (ret != val_size + 1)
836 return ret < 0 ? ret : -EIO;
841 static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
842 void *val, size_t val_size)
844 struct device *dev = context;
845 struct i2c_client *client = to_i2c_client(dev);
848 if (WARN_ON(reg_size != 1))
851 ret = i2c_master_send(client, reg, 1);
853 return ret < 0 ? ret : -EIO;
855 ret = i2c_master_recv(client, val, val_size);
857 return ret < 0 ? ret : -EIO;
863 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
864 * is that the STOP condition is required between set register address and
865 * read register data when reading from registers.
867 static const struct regmap_bus pcf2127_i2c_regmap = {
868 .write = pcf2127_i2c_write,
869 .gather_write = pcf2127_i2c_gather_write,
870 .read = pcf2127_i2c_read,
873 static struct i2c_driver pcf2127_i2c_driver;
875 static int pcf2127_i2c_probe(struct i2c_client *client,
876 const struct i2c_device_id *id)
878 struct regmap *regmap;
879 static const struct regmap_config config = {
882 .max_register = 0x1d,
885 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
888 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
889 &client->dev, &config);
890 if (IS_ERR(regmap)) {
891 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
892 __func__, PTR_ERR(regmap));
893 return PTR_ERR(regmap);
896 return pcf2127_probe(&client->dev, regmap, client->irq,
897 pcf2127_i2c_driver.driver.name, id->driver_data);
900 static const struct i2c_device_id pcf2127_i2c_id[] = {
906 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
908 static struct i2c_driver pcf2127_i2c_driver = {
910 .name = "rtc-pcf2127-i2c",
911 .of_match_table = of_match_ptr(pcf2127_of_match),
913 .probe = pcf2127_i2c_probe,
914 .id_table = pcf2127_i2c_id,
917 static int pcf2127_i2c_register_driver(void)
919 return i2c_add_driver(&pcf2127_i2c_driver);
922 static void pcf2127_i2c_unregister_driver(void)
924 i2c_del_driver(&pcf2127_i2c_driver);
929 static int pcf2127_i2c_register_driver(void)
934 static void pcf2127_i2c_unregister_driver(void)
940 #if IS_ENABLED(CONFIG_SPI_MASTER)
942 static struct spi_driver pcf2127_spi_driver;
944 static int pcf2127_spi_probe(struct spi_device *spi)
946 static const struct regmap_config config = {
949 .read_flag_mask = 0xa0,
950 .write_flag_mask = 0x20,
951 .max_register = 0x1d,
953 struct regmap *regmap;
955 regmap = devm_regmap_init_spi(spi, &config);
956 if (IS_ERR(regmap)) {
957 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
958 __func__, PTR_ERR(regmap));
959 return PTR_ERR(regmap);
962 return pcf2127_probe(&spi->dev, regmap, spi->irq,
963 pcf2127_spi_driver.driver.name,
964 spi_get_device_id(spi)->driver_data);
967 static const struct spi_device_id pcf2127_spi_id[] = {
973 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
975 static struct spi_driver pcf2127_spi_driver = {
977 .name = "rtc-pcf2127-spi",
978 .of_match_table = of_match_ptr(pcf2127_of_match),
980 .probe = pcf2127_spi_probe,
981 .id_table = pcf2127_spi_id,
984 static int pcf2127_spi_register_driver(void)
986 return spi_register_driver(&pcf2127_spi_driver);
989 static void pcf2127_spi_unregister_driver(void)
991 spi_unregister_driver(&pcf2127_spi_driver);
996 static int pcf2127_spi_register_driver(void)
1001 static void pcf2127_spi_unregister_driver(void)
1007 static int __init pcf2127_init(void)
1011 ret = pcf2127_i2c_register_driver();
1013 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
1017 ret = pcf2127_spi_register_driver();
1019 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
1020 pcf2127_i2c_unregister_driver();
1025 module_init(pcf2127_init)
1027 static void __exit pcf2127_exit(void)
1029 pcf2127_spi_unregister_driver();
1030 pcf2127_i2c_unregister_driver();
1032 module_exit(pcf2127_exit)
1034 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1035 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
1036 MODULE_LICENSE("GPL v2");