1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 218cb6368SRenaud Cerrato /* 3cee2cc21SAkinobu Mita * An I2C and SPI driver for the NXP PCF2127/29 RTC 418cb6368SRenaud Cerrato * Copyright 2013 Til-Technologies 518cb6368SRenaud Cerrato * 618cb6368SRenaud Cerrato * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> 718cb6368SRenaud Cerrato * 80e735eaaSBruno Thomsen * Watchdog and tamper functions 90e735eaaSBruno Thomsen * Author: Bruno Thomsen <bruno.thomsen@gmail.com> 100e735eaaSBruno Thomsen * 1118cb6368SRenaud Cerrato * based on the other drivers in this same directory. 1218cb6368SRenaud Cerrato * 13cee2cc21SAkinobu Mita * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf 1418cb6368SRenaud Cerrato */ 1518cb6368SRenaud Cerrato 1618cb6368SRenaud Cerrato #include <linux/i2c.h> 179408ec1aSAkinobu Mita #include <linux/spi/spi.h> 1818cb6368SRenaud Cerrato #include <linux/bcd.h> 1918cb6368SRenaud Cerrato #include <linux/rtc.h> 2018cb6368SRenaud Cerrato #include <linux/slab.h> 2118cb6368SRenaud Cerrato #include <linux/module.h> 2218cb6368SRenaud Cerrato #include <linux/of.h> 238a914bacSLiam Beguin #include <linux/of_irq.h> 24907b3262SAkinobu Mita #include <linux/regmap.h> 250e735eaaSBruno Thomsen #include <linux/watchdog.h> 2618cb6368SRenaud Cerrato 27bbfe3a7aSBruno Thomsen /* Control register 1 */ 28bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1 0x00 29b9ac079aSPhilipp Rosenberger #define PCF2127_BIT_CTRL1_POR_OVRD BIT(3) 3003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL1_TSF1 BIT(4) 31bbfe3a7aSBruno Thomsen /* Control register 2 */ 32bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2 0x01 338a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AIE BIT(1) 3403623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE BIT(2) 358a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AF BIT(4) 3603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2 BIT(5) 3727006416SAlexandre Belloni #define PCF2127_BIT_CTRL2_WDTF BIT(6) 38bbfe3a7aSBruno Thomsen /* Control register 3 */ 39bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3 0x02 4003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE BIT(0) 4103623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE BIT(1) 42bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF BIT(2) 4303623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF BIT(3) 4403623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE BIT(4) 45bbfe3a7aSBruno Thomsen /* Time and date registers */ 46bbfe3a7aSBruno Thomsen #define PCF2127_REG_SC 0x03 47bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF BIT(7) 48bbfe3a7aSBruno Thomsen #define PCF2127_REG_MN 0x04 49bbfe3a7aSBruno Thomsen #define PCF2127_REG_HR 0x05 50bbfe3a7aSBruno Thomsen #define PCF2127_REG_DM 0x06 51bbfe3a7aSBruno Thomsen #define PCF2127_REG_DW 0x07 52bbfe3a7aSBruno Thomsen #define PCF2127_REG_MO 0x08 53bbfe3a7aSBruno Thomsen #define PCF2127_REG_YR 0x09 548a914bacSLiam Beguin /* Alarm registers */ 558a914bacSLiam Beguin #define PCF2127_REG_ALARM_SC 0x0A 568a914bacSLiam Beguin #define PCF2127_REG_ALARM_MN 0x0B 578a914bacSLiam Beguin #define PCF2127_REG_ALARM_HR 0x0C 588a914bacSLiam Beguin #define PCF2127_REG_ALARM_DM 0x0D 598a914bacSLiam Beguin #define PCF2127_REG_ALARM_DW 0x0E 6027006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE BIT(7) 6115f57b3eSPhilipp Rosenberger /* CLKOUT control register */ 6215f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT 0x0f 6315f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR BIT(5) 640e735eaaSBruno Thomsen /* Watchdog registers */ 650e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL 0x10 660e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0 BIT(0) 670e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1 BIT(1) 680e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0 BIT(6) 690e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1 BIT(7) 700e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL 0x11 7103623b4bSBruno Thomsen /* Tamper timestamp registers */ 7203623b4bSBruno Thomsen #define PCF2127_REG_TS_CTRL 0x12 7303623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) 7403623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM BIT(7) 7503623b4bSBruno Thomsen #define PCF2127_REG_TS_SC 0x13 7603623b4bSBruno Thomsen #define PCF2127_REG_TS_MN 0x14 7703623b4bSBruno Thomsen #define PCF2127_REG_TS_HR 0x15 7803623b4bSBruno Thomsen #define PCF2127_REG_TS_DM 0x16 7903623b4bSBruno Thomsen #define PCF2127_REG_TS_MO 0x17 8003623b4bSBruno Thomsen #define PCF2127_REG_TS_YR 0x18 81bbfe3a7aSBruno Thomsen /* 82bbfe3a7aSBruno Thomsen * RAM registers 83bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 84bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 85bbfe3a7aSBruno Thomsen * PCF2129 doesn't have this feature. 86bbfe3a7aSBruno Thomsen */ 87bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 88bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 89bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 90f97cfddcSUwe Kleine-König 910e735eaaSBruno Thomsen /* Watchdog timer value constants */ 920e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP 0 930e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN 2 940e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX 255 950e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT 60 96653ebd75SAndrea Scian 9718cb6368SRenaud Cerrato struct pcf2127 { 9818cb6368SRenaud Cerrato struct rtc_device *rtc; 990e735eaaSBruno Thomsen struct watchdog_device wdd; 100907b3262SAkinobu Mita struct regmap *regmap; 10118cb6368SRenaud Cerrato }; 10218cb6368SRenaud Cerrato 10318cb6368SRenaud Cerrato /* 10418cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 10518cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 10618cb6368SRenaud Cerrato */ 107907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 10818cb6368SRenaud Cerrato { 109907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 110907b3262SAkinobu Mita unsigned char buf[10]; 111907b3262SAkinobu Mita int ret; 11218cb6368SRenaud Cerrato 1137f43020eSBruno Thomsen /* 1147f43020eSBruno Thomsen * Avoid reading CTRL2 register as it causes WD_VAL register 1157f43020eSBruno Thomsen * value to reset to 0 which means watchdog is stopped. 1167f43020eSBruno Thomsen */ 1177f43020eSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3, 1187f43020eSBruno Thomsen (buf + PCF2127_REG_CTRL3), 1197f43020eSBruno Thomsen ARRAY_SIZE(buf) - PCF2127_REG_CTRL3); 120907b3262SAkinobu Mita if (ret) { 121907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 122907b3262SAkinobu Mita return ret; 12318cb6368SRenaud Cerrato } 12418cb6368SRenaud Cerrato 125bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF) 126907b3262SAkinobu Mita dev_info(dev, 127653ebd75SAndrea Scian "low voltage detected, check/replace RTC battery.\n"); 128653ebd75SAndrea Scian 129bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 130bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) { 131653ebd75SAndrea Scian /* 132653ebd75SAndrea Scian * no need clear the flag here, 133653ebd75SAndrea Scian * it will be cleared once the new date is saved 134653ebd75SAndrea Scian */ 135907b3262SAkinobu Mita dev_warn(dev, 136653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 137653ebd75SAndrea Scian return -EINVAL; 13818cb6368SRenaud Cerrato } 13918cb6368SRenaud Cerrato 140907b3262SAkinobu Mita dev_dbg(dev, 1417f43020eSBruno Thomsen "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, " 14218cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 1437f43020eSBruno Thomsen __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC], 1447f43020eSBruno Thomsen buf[PCF2127_REG_MN], buf[PCF2127_REG_HR], 1457f43020eSBruno Thomsen buf[PCF2127_REG_DM], buf[PCF2127_REG_DW], 1467f43020eSBruno Thomsen buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]); 14718cb6368SRenaud Cerrato 14818cb6368SRenaud Cerrato tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); 14918cb6368SRenaud Cerrato tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); 15018cb6368SRenaud Cerrato tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ 15118cb6368SRenaud Cerrato tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); 15218cb6368SRenaud Cerrato tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; 15318cb6368SRenaud Cerrato tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 15418cb6368SRenaud Cerrato tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); 155b139bb5cSAlexandre Belloni tm->tm_year += 100; 15618cb6368SRenaud Cerrato 157907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 15818cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 15918cb6368SRenaud Cerrato __func__, 16018cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 16118cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 16218cb6368SRenaud Cerrato 16322652ba7SAlexandre Belloni return 0; 16418cb6368SRenaud Cerrato } 16518cb6368SRenaud Cerrato 166907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 16718cb6368SRenaud Cerrato { 168907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 169907b3262SAkinobu Mita unsigned char buf[7]; 17018cb6368SRenaud Cerrato int i = 0, err; 17118cb6368SRenaud Cerrato 172907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 17318cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 17418cb6368SRenaud Cerrato __func__, 17518cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 17618cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 17718cb6368SRenaud Cerrato 17818cb6368SRenaud Cerrato /* hours, minutes and seconds */ 179653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 18018cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 18118cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 18218cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 18318cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 18418cb6368SRenaud Cerrato 18518cb6368SRenaud Cerrato /* month, 1 - 12 */ 18618cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 18718cb6368SRenaud Cerrato 18818cb6368SRenaud Cerrato /* year */ 189b139bb5cSAlexandre Belloni buf[i++] = bin2bcd(tm->tm_year - 100); 19018cb6368SRenaud Cerrato 19118cb6368SRenaud Cerrato /* write register's data */ 192907b3262SAkinobu Mita err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); 193907b3262SAkinobu Mita if (err) { 194907b3262SAkinobu Mita dev_err(dev, 19518cb6368SRenaud Cerrato "%s: err=%d", __func__, err); 196907b3262SAkinobu Mita return err; 19718cb6368SRenaud Cerrato } 19818cb6368SRenaud Cerrato 19918cb6368SRenaud Cerrato return 0; 20018cb6368SRenaud Cerrato } 20118cb6368SRenaud Cerrato 20218cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 20318cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 20418cb6368SRenaud Cerrato { 205907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 2067d65cf8cSAlexandre Belloni int val, touser = 0; 207f97cfddcSUwe Kleine-König int ret; 20818cb6368SRenaud Cerrato 20918cb6368SRenaud Cerrato switch (cmd) { 21018cb6368SRenaud Cerrato case RTC_VL_READ: 2117d65cf8cSAlexandre Belloni ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); 212907b3262SAkinobu Mita if (ret) 213f97cfddcSUwe Kleine-König return ret; 21418cb6368SRenaud Cerrato 2157d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BLF) 2167d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_LOW; 2177d65cf8cSAlexandre Belloni 2187d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BF) 2197d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_SWITCH; 220f97cfddcSUwe Kleine-König 221af427311SAlexandre Belloni return put_user(touser, (unsigned int __user *)arg); 2227d65cf8cSAlexandre Belloni 2237d65cf8cSAlexandre Belloni case RTC_VL_CLR: 2247d65cf8cSAlexandre Belloni return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 2257d65cf8cSAlexandre Belloni PCF2127_BIT_CTRL3_BF, 0); 2267d65cf8cSAlexandre Belloni 22718cb6368SRenaud Cerrato default: 22818cb6368SRenaud Cerrato return -ENOIOCTLCMD; 22918cb6368SRenaud Cerrato } 23018cb6368SRenaud Cerrato } 23118cb6368SRenaud Cerrato 232d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 233d6c3029fSUwe Kleine-König void *val, size_t bytes) 234d6c3029fSUwe Kleine-König { 235d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 236d6c3029fSUwe Kleine-König int ret; 237d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 238d6c3029fSUwe Kleine-König 239bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 240d6c3029fSUwe Kleine-König offsetbuf, 2); 241d6c3029fSUwe Kleine-König if (ret) 242d6c3029fSUwe Kleine-König return ret; 243d6c3029fSUwe Kleine-König 244ba1c30bfSDan Carpenter return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 245d6c3029fSUwe Kleine-König val, bytes); 246d6c3029fSUwe Kleine-König } 247d6c3029fSUwe Kleine-König 248d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 249d6c3029fSUwe Kleine-König void *val, size_t bytes) 250d6c3029fSUwe Kleine-König { 251d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 252d6c3029fSUwe Kleine-König int ret; 253d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 254d6c3029fSUwe Kleine-König 255bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 256d6c3029fSUwe Kleine-König offsetbuf, 2); 257d6c3029fSUwe Kleine-König if (ret) 258d6c3029fSUwe Kleine-König return ret; 259d6c3029fSUwe Kleine-König 260ba1c30bfSDan Carpenter return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 261d6c3029fSUwe Kleine-König val, bytes); 262d6c3029fSUwe Kleine-König } 263d6c3029fSUwe Kleine-König 2640e735eaaSBruno Thomsen /* watchdog driver */ 2650e735eaaSBruno Thomsen 2660e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd) 2670e735eaaSBruno Thomsen { 2680e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 2690e735eaaSBruno Thomsen 2700e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout); 2710e735eaaSBruno Thomsen } 2720e735eaaSBruno Thomsen 2730e735eaaSBruno Thomsen /* 2740e735eaaSBruno Thomsen * Restart watchdog timer if feature is active. 2750e735eaaSBruno Thomsen * 2760e735eaaSBruno Thomsen * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, 2770e735eaaSBruno Thomsen * since register also contain control/status flags for other features. 2780e735eaaSBruno Thomsen * Always call this function after reading CTRL2 register. 2790e735eaaSBruno Thomsen */ 2800e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) 2810e735eaaSBruno Thomsen { 2820e735eaaSBruno Thomsen int ret = 0; 2830e735eaaSBruno Thomsen 2840e735eaaSBruno Thomsen if (watchdog_active(wdd)) { 2850e735eaaSBruno Thomsen ret = pcf2127_wdt_ping(wdd); 2860e735eaaSBruno Thomsen if (ret) 2870e735eaaSBruno Thomsen dev_err(wdd->parent, 2880e735eaaSBruno Thomsen "%s: watchdog restart failed, ret=%d\n", 2890e735eaaSBruno Thomsen __func__, ret); 2900e735eaaSBruno Thomsen } 2910e735eaaSBruno Thomsen 2920e735eaaSBruno Thomsen return ret; 2930e735eaaSBruno Thomsen } 2940e735eaaSBruno Thomsen 2950e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd) 2960e735eaaSBruno Thomsen { 2970e735eaaSBruno Thomsen return pcf2127_wdt_ping(wdd); 2980e735eaaSBruno Thomsen } 2990e735eaaSBruno Thomsen 3000e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd) 3010e735eaaSBruno Thomsen { 3020e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 3030e735eaaSBruno Thomsen 3040e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, 3050e735eaaSBruno Thomsen PCF2127_WD_VAL_STOP); 3060e735eaaSBruno Thomsen } 3070e735eaaSBruno Thomsen 3080e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, 3090e735eaaSBruno Thomsen unsigned int new_timeout) 3100e735eaaSBruno Thomsen { 3110e735eaaSBruno Thomsen dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", 3120e735eaaSBruno Thomsen new_timeout, wdd->timeout); 3130e735eaaSBruno Thomsen 3140e735eaaSBruno Thomsen wdd->timeout = new_timeout; 3150e735eaaSBruno Thomsen 3160e735eaaSBruno Thomsen return pcf2127_wdt_active_ping(wdd); 3170e735eaaSBruno Thomsen } 3180e735eaaSBruno Thomsen 3190e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = { 3200e735eaaSBruno Thomsen .identity = "NXP PCF2127/PCF2129 Watchdog", 3210e735eaaSBruno Thomsen .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, 3220e735eaaSBruno Thomsen }; 3230e735eaaSBruno Thomsen 3240e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = { 3250e735eaaSBruno Thomsen .owner = THIS_MODULE, 3260e735eaaSBruno Thomsen .start = pcf2127_wdt_start, 3270e735eaaSBruno Thomsen .stop = pcf2127_wdt_stop, 3280e735eaaSBruno Thomsen .ping = pcf2127_wdt_ping, 3290e735eaaSBruno Thomsen .set_timeout = pcf2127_wdt_set_timeout, 3300e735eaaSBruno Thomsen }; 3310e735eaaSBruno Thomsen 3325d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127) 3335d78533aSUwe Kleine-König { 3345d78533aSUwe Kleine-König u32 wdd_timeout; 3355d78533aSUwe Kleine-König int ret; 3365d78533aSUwe Kleine-König 33771ac1345SUwe Kleine-König if (!IS_ENABLED(CONFIG_WATCHDOG) || 33871ac1345SUwe Kleine-König !device_property_read_bool(dev, "reset-source")) 3395d78533aSUwe Kleine-König return 0; 3405d78533aSUwe Kleine-König 3415d78533aSUwe Kleine-König pcf2127->wdd.parent = dev; 3425d78533aSUwe Kleine-König pcf2127->wdd.info = &pcf2127_wdt_info; 3435d78533aSUwe Kleine-König pcf2127->wdd.ops = &pcf2127_watchdog_ops; 3445d78533aSUwe Kleine-König pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN; 3455d78533aSUwe Kleine-König pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX; 3465d78533aSUwe Kleine-König pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT; 3475d78533aSUwe Kleine-König pcf2127->wdd.min_hw_heartbeat_ms = 500; 3485d78533aSUwe Kleine-König pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS; 3495d78533aSUwe Kleine-König 3505d78533aSUwe Kleine-König watchdog_set_drvdata(&pcf2127->wdd, pcf2127); 3515d78533aSUwe Kleine-König 3525d78533aSUwe Kleine-König /* Test if watchdog timer is started by bootloader */ 3535d78533aSUwe Kleine-König ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout); 3545d78533aSUwe Kleine-König if (ret) 3555d78533aSUwe Kleine-König return ret; 3565d78533aSUwe Kleine-König 3575d78533aSUwe Kleine-König if (wdd_timeout) 3585d78533aSUwe Kleine-König set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); 3595d78533aSUwe Kleine-König 3605d78533aSUwe Kleine-König return devm_watchdog_register_device(dev, &pcf2127->wdd); 3615d78533aSUwe Kleine-König } 3625d78533aSUwe Kleine-König 3638a914bacSLiam Beguin /* Alarm */ 3648a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3658a914bacSLiam Beguin { 3668a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 3678a914bacSLiam Beguin unsigned int buf[5], ctrl2; 3688a914bacSLiam Beguin int ret; 3698a914bacSLiam Beguin 3708a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 3718a914bacSLiam Beguin if (ret) 3728a914bacSLiam Beguin return ret; 3738a914bacSLiam Beguin 3748a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 3758a914bacSLiam Beguin if (ret) 3768a914bacSLiam Beguin return ret; 3778a914bacSLiam Beguin 3788a914bacSLiam Beguin ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf, 3798a914bacSLiam Beguin sizeof(buf)); 3808a914bacSLiam Beguin if (ret) 3818a914bacSLiam Beguin return ret; 3828a914bacSLiam Beguin 3838a914bacSLiam Beguin alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE; 3848a914bacSLiam Beguin alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF; 3858a914bacSLiam Beguin 3868a914bacSLiam Beguin alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F); 3878a914bacSLiam Beguin alrm->time.tm_min = bcd2bin(buf[1] & 0x7F); 3888a914bacSLiam Beguin alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F); 3898a914bacSLiam Beguin alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F); 3908a914bacSLiam Beguin 3918a914bacSLiam Beguin return 0; 3928a914bacSLiam Beguin } 3938a914bacSLiam Beguin 3948a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable) 3958a914bacSLiam Beguin { 3968a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 3978a914bacSLiam Beguin int ret; 3988a914bacSLiam Beguin 3998a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 4008a914bacSLiam Beguin PCF2127_BIT_CTRL2_AIE, 4018a914bacSLiam Beguin enable ? PCF2127_BIT_CTRL2_AIE : 0); 4028a914bacSLiam Beguin if (ret) 4038a914bacSLiam Beguin return ret; 4048a914bacSLiam Beguin 4058a914bacSLiam Beguin return pcf2127_wdt_active_ping(&pcf2127->wdd); 4068a914bacSLiam Beguin } 4078a914bacSLiam Beguin 4088a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 4098a914bacSLiam Beguin { 4108a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4118a914bacSLiam Beguin uint8_t buf[5]; 4128a914bacSLiam Beguin int ret; 4138a914bacSLiam Beguin 4148a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 4158a914bacSLiam Beguin PCF2127_BIT_CTRL2_AF, 0); 4168a914bacSLiam Beguin if (ret) 4178a914bacSLiam Beguin return ret; 4188a914bacSLiam Beguin 4198a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 4208a914bacSLiam Beguin if (ret) 4218a914bacSLiam Beguin return ret; 4228a914bacSLiam Beguin 4238a914bacSLiam Beguin buf[0] = bin2bcd(alrm->time.tm_sec); 4248a914bacSLiam Beguin buf[1] = bin2bcd(alrm->time.tm_min); 4258a914bacSLiam Beguin buf[2] = bin2bcd(alrm->time.tm_hour); 4268a914bacSLiam Beguin buf[3] = bin2bcd(alrm->time.tm_mday); 42727006416SAlexandre Belloni buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */ 4288a914bacSLiam Beguin 4298a914bacSLiam Beguin ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf, 4308a914bacSLiam Beguin sizeof(buf)); 4318a914bacSLiam Beguin if (ret) 4328a914bacSLiam Beguin return ret; 4338a914bacSLiam Beguin 4348a914bacSLiam Beguin return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled); 4358a914bacSLiam Beguin } 4368a914bacSLiam Beguin 4378a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev) 4388a914bacSLiam Beguin { 4398a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4408a914bacSLiam Beguin unsigned int ctrl2 = 0; 4418a914bacSLiam Beguin int ret = 0; 4428a914bacSLiam Beguin 4438a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 4448a914bacSLiam Beguin if (ret) 4458a914bacSLiam Beguin return IRQ_NONE; 4468a914bacSLiam Beguin 44727006416SAlexandre Belloni if (!(ctrl2 & PCF2127_BIT_CTRL2_AF)) 44827006416SAlexandre Belloni return IRQ_NONE; 44927006416SAlexandre Belloni 4508a914bacSLiam Beguin regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 45127006416SAlexandre Belloni ctrl2 & ~(PCF2127_BIT_CTRL2_AF | PCF2127_BIT_CTRL2_WDTF)); 4528a914bacSLiam Beguin 4538a914bacSLiam Beguin rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF); 4548a914bacSLiam Beguin 45527006416SAlexandre Belloni pcf2127_wdt_active_ping(&pcf2127->wdd); 4568a914bacSLiam Beguin 4578a914bacSLiam Beguin return IRQ_HANDLED; 4588a914bacSLiam Beguin } 4598a914bacSLiam Beguin 46025cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = { 4618a914bacSLiam Beguin .ioctl = pcf2127_rtc_ioctl, 4628a914bacSLiam Beguin .read_time = pcf2127_rtc_read_time, 4638a914bacSLiam Beguin .set_time = pcf2127_rtc_set_time, 4648a914bacSLiam Beguin .read_alarm = pcf2127_rtc_read_alarm, 4658a914bacSLiam Beguin .set_alarm = pcf2127_rtc_set_alarm, 4668a914bacSLiam Beguin .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable, 4678a914bacSLiam Beguin }; 4688a914bacSLiam Beguin 46903623b4bSBruno Thomsen /* sysfs interface */ 47003623b4bSBruno Thomsen 47103623b4bSBruno Thomsen static ssize_t timestamp0_store(struct device *dev, 47203623b4bSBruno Thomsen struct device_attribute *attr, 47303623b4bSBruno Thomsen const char *buf, size_t count) 47403623b4bSBruno Thomsen { 47503623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 47603623b4bSBruno Thomsen int ret; 47703623b4bSBruno Thomsen 47803623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 47903623b4bSBruno Thomsen PCF2127_BIT_CTRL1_TSF1, 0); 48003623b4bSBruno Thomsen if (ret) { 48103623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret); 48203623b4bSBruno Thomsen return ret; 48303623b4bSBruno Thomsen } 48403623b4bSBruno Thomsen 48503623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 48603623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSF2, 0); 48703623b4bSBruno Thomsen if (ret) { 48803623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret); 48903623b4bSBruno Thomsen return ret; 49003623b4bSBruno Thomsen } 49103623b4bSBruno Thomsen 49203623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 49303623b4bSBruno Thomsen if (ret) 49403623b4bSBruno Thomsen return ret; 49503623b4bSBruno Thomsen 49603623b4bSBruno Thomsen return count; 49703623b4bSBruno Thomsen }; 49803623b4bSBruno Thomsen 49903623b4bSBruno Thomsen static ssize_t timestamp0_show(struct device *dev, 50003623b4bSBruno Thomsen struct device_attribute *attr, char *buf) 50103623b4bSBruno Thomsen { 50203623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 50303623b4bSBruno Thomsen struct rtc_time tm; 50403623b4bSBruno Thomsen int ret; 50503623b4bSBruno Thomsen unsigned char data[25]; 50603623b4bSBruno Thomsen 50703623b4bSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data, 50803623b4bSBruno Thomsen sizeof(data)); 50903623b4bSBruno Thomsen if (ret) { 51003623b4bSBruno Thomsen dev_err(dev, "%s: read error ret=%d\n", __func__, ret); 51103623b4bSBruno Thomsen return ret; 51203623b4bSBruno Thomsen } 51303623b4bSBruno Thomsen 51403623b4bSBruno Thomsen dev_dbg(dev, 51503623b4bSBruno Thomsen "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, ts_sc=%02x, " 51603623b4bSBruno Thomsen "ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", 51703623b4bSBruno Thomsen __func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2], 51803623b4bSBruno Thomsen data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC], 51903623b4bSBruno Thomsen data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR], 52003623b4bSBruno Thomsen data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO], 52103623b4bSBruno Thomsen data[PCF2127_REG_TS_YR]); 52203623b4bSBruno Thomsen 52303623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 52403623b4bSBruno Thomsen if (ret) 52503623b4bSBruno Thomsen return ret; 52603623b4bSBruno Thomsen 52703623b4bSBruno Thomsen if (!(data[PCF2127_REG_CTRL1] & PCF2127_BIT_CTRL1_TSF1) && 52803623b4bSBruno Thomsen !(data[PCF2127_REG_CTRL2] & PCF2127_BIT_CTRL2_TSF2)) 52903623b4bSBruno Thomsen return 0; 53003623b4bSBruno Thomsen 53103623b4bSBruno Thomsen tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F); 53203623b4bSBruno Thomsen tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F); 53303623b4bSBruno Thomsen tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F); 53403623b4bSBruno Thomsen tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F); 53503623b4bSBruno Thomsen /* TS_MO register (month) value range: 1-12 */ 53603623b4bSBruno Thomsen tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1; 53703623b4bSBruno Thomsen tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]); 53803623b4bSBruno Thomsen if (tm.tm_year < 70) 53903623b4bSBruno Thomsen tm.tm_year += 100; /* assume we are in 1970...2069 */ 54003623b4bSBruno Thomsen 54103623b4bSBruno Thomsen ret = rtc_valid_tm(&tm); 54203623b4bSBruno Thomsen if (ret) 54303623b4bSBruno Thomsen return ret; 54403623b4bSBruno Thomsen 54503623b4bSBruno Thomsen return sprintf(buf, "%llu\n", 54603623b4bSBruno Thomsen (unsigned long long)rtc_tm_to_time64(&tm)); 54703623b4bSBruno Thomsen }; 54803623b4bSBruno Thomsen 54903623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0); 55003623b4bSBruno Thomsen 55103623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = { 55203623b4bSBruno Thomsen &dev_attr_timestamp0.attr, 55303623b4bSBruno Thomsen NULL 55403623b4bSBruno Thomsen }; 55503623b4bSBruno Thomsen 55603623b4bSBruno Thomsen static const struct attribute_group pcf2127_attr_group = { 55703623b4bSBruno Thomsen .attrs = pcf2127_attrs, 55803623b4bSBruno Thomsen }; 55903623b4bSBruno Thomsen 560907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 561*2843d565SBiwen Li int alarm_irq, const char *name, bool is_pcf2127) 56218cb6368SRenaud Cerrato { 56318cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 564d6c3029fSUwe Kleine-König int ret = 0; 56515f57b3eSPhilipp Rosenberger unsigned int val; 56618cb6368SRenaud Cerrato 567907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 56818cb6368SRenaud Cerrato 569907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 57018cb6368SRenaud Cerrato if (!pcf2127) 57118cb6368SRenaud Cerrato return -ENOMEM; 57218cb6368SRenaud Cerrato 573907b3262SAkinobu Mita pcf2127->regmap = regmap; 57418cb6368SRenaud Cerrato 575907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 576907b3262SAkinobu Mita 577e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 578d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 579d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 58018cb6368SRenaud Cerrato 581e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 582b139bb5cSAlexandre Belloni pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 583b139bb5cSAlexandre Belloni pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; 584b139bb5cSAlexandre Belloni pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ 58527006416SAlexandre Belloni pcf2127->rtc->uie_unsupported = 1; 58625cbe9c8SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 587e788771cSBruno Thomsen 58835425bafSBiwen Li if (alarm_irq > 0) { 58927006416SAlexandre Belloni ret = devm_request_threaded_irq(dev, alarm_irq, NULL, 59027006416SAlexandre Belloni pcf2127_rtc_irq, 5918a914bacSLiam Beguin IRQF_TRIGGER_LOW | IRQF_ONESHOT, 5928a914bacSLiam Beguin dev_name(dev), dev); 5938a914bacSLiam Beguin if (ret) { 5948a914bacSLiam Beguin dev_err(dev, "failed to request alarm irq\n"); 5958a914bacSLiam Beguin return ret; 5968a914bacSLiam Beguin } 5978a914bacSLiam Beguin } 5988a914bacSLiam Beguin 59935425bafSBiwen Li if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) { 6008a914bacSLiam Beguin device_init_wakeup(dev, true); 60125cbe9c8SAlexandre Belloni set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 6028a914bacSLiam Beguin } 6038a914bacSLiam Beguin 604*2843d565SBiwen Li if (is_pcf2127) { 605d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 606d6c3029fSUwe Kleine-König .priv = pcf2127, 607d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 608d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 609d6c3029fSUwe Kleine-König .size = 512, 610d6c3029fSUwe Kleine-König }; 611d6c3029fSUwe Kleine-König 6123a905c2dSBartosz Golaszewski ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 613d6c3029fSUwe Kleine-König } 614d6c3029fSUwe Kleine-König 6150e735eaaSBruno Thomsen /* 616b9ac079aSPhilipp Rosenberger * The "Power-On Reset Override" facility prevents the RTC to do a reset 617b9ac079aSPhilipp Rosenberger * after power on. For normal operation the PORO must be disabled. 618b9ac079aSPhilipp Rosenberger */ 619b9ac079aSPhilipp Rosenberger regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 620b9ac079aSPhilipp Rosenberger PCF2127_BIT_CTRL1_POR_OVRD); 621b9ac079aSPhilipp Rosenberger 62215f57b3eSPhilipp Rosenberger ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val); 62315f57b3eSPhilipp Rosenberger if (ret < 0) 62415f57b3eSPhilipp Rosenberger return ret; 62515f57b3eSPhilipp Rosenberger 62615f57b3eSPhilipp Rosenberger if (!(val & PCF2127_BIT_CLKOUT_OTPR)) { 62715f57b3eSPhilipp Rosenberger ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT, 62815f57b3eSPhilipp Rosenberger PCF2127_BIT_CLKOUT_OTPR); 62915f57b3eSPhilipp Rosenberger if (ret < 0) 63015f57b3eSPhilipp Rosenberger return ret; 63115f57b3eSPhilipp Rosenberger 63215f57b3eSPhilipp Rosenberger msleep(100); 63315f57b3eSPhilipp Rosenberger } 63415f57b3eSPhilipp Rosenberger 635b9ac079aSPhilipp Rosenberger /* 6360e735eaaSBruno Thomsen * Watchdog timer enabled and reset pin /RST activated when timed out. 6370e735eaaSBruno Thomsen * Select 1Hz clock source for watchdog timer. 6380e735eaaSBruno Thomsen * Note: Countdown timer disabled and not available. 639*2843d565SBiwen Li * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD 640*2843d565SBiwen Li * of register watchdg_tim_ctl. The bit[6] is labeled 641*2843d565SBiwen Li * as T. Bits labeled as T must always be written with 642*2843d565SBiwen Li * logic 0. 6430e735eaaSBruno Thomsen */ 6440e735eaaSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL, 6450e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 6460e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 6470e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1 | 6480e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF0, 6490e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 650*2843d565SBiwen Li (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) | 6510e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1); 6520e735eaaSBruno Thomsen if (ret) { 6530e735eaaSBruno Thomsen dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); 6540e735eaaSBruno Thomsen return ret; 6550e735eaaSBruno Thomsen } 6560e735eaaSBruno Thomsen 6575d78533aSUwe Kleine-König pcf2127_watchdog_init(dev, pcf2127); 6580e735eaaSBruno Thomsen 65903623b4bSBruno Thomsen /* 66003623b4bSBruno Thomsen * Disable battery low/switch-over timestamp and interrupts. 66103623b4bSBruno Thomsen * Clear battery interrupt flags which can block new trigger events. 66203623b4bSBruno Thomsen * Note: This is the default chip behaviour but added to ensure 66303623b4bSBruno Thomsen * correct tamper timestamp and interrupt function. 66403623b4bSBruno Thomsen */ 66503623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 66603623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BTSE | 66703623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BIE | 66803623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BLIE, 0); 66903623b4bSBruno Thomsen if (ret) { 67003623b4bSBruno Thomsen dev_err(dev, "%s: interrupt config (ctrl3) failed\n", 67103623b4bSBruno Thomsen __func__); 67203623b4bSBruno Thomsen return ret; 67303623b4bSBruno Thomsen } 67403623b4bSBruno Thomsen 67503623b4bSBruno Thomsen /* 67603623b4bSBruno Thomsen * Enable timestamp function and store timestamp of first trigger 67703623b4bSBruno Thomsen * event until TSF1 and TFS2 interrupt flags are cleared. 67803623b4bSBruno Thomsen */ 67903623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL, 68003623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSOFF | 68103623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM, 68203623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM); 68303623b4bSBruno Thomsen if (ret) { 68403623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n", 68503623b4bSBruno Thomsen __func__); 68603623b4bSBruno Thomsen return ret; 68703623b4bSBruno Thomsen } 68803623b4bSBruno Thomsen 68903623b4bSBruno Thomsen /* 69003623b4bSBruno Thomsen * Enable interrupt generation when TSF1 or TSF2 timestamp flags 69103623b4bSBruno Thomsen * are set. Interrupt signal is an open-drain output and can be 69203623b4bSBruno Thomsen * left floating if unused. 69303623b4bSBruno Thomsen */ 69403623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 69503623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE, 69603623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE); 69703623b4bSBruno Thomsen if (ret) { 69803623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ctrl2) failed\n", 69903623b4bSBruno Thomsen __func__); 70003623b4bSBruno Thomsen return ret; 70103623b4bSBruno Thomsen } 70203623b4bSBruno Thomsen 70303623b4bSBruno Thomsen ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group); 70403623b4bSBruno Thomsen if (ret) { 70503623b4bSBruno Thomsen dev_err(dev, "%s: tamper sysfs registering failed\n", 70603623b4bSBruno Thomsen __func__); 70703623b4bSBruno Thomsen return ret; 70803623b4bSBruno Thomsen } 70903623b4bSBruno Thomsen 710fdcfd854SBartosz Golaszewski return devm_rtc_register_device(pcf2127->rtc); 71118cb6368SRenaud Cerrato } 71218cb6368SRenaud Cerrato 71318cb6368SRenaud Cerrato #ifdef CONFIG_OF 71418cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 71518cb6368SRenaud Cerrato { .compatible = "nxp,pcf2127" }, 716cee2cc21SAkinobu Mita { .compatible = "nxp,pcf2129" }, 717985b30dbSLiam Beguin { .compatible = "nxp,pca2129" }, 71818cb6368SRenaud Cerrato {} 71918cb6368SRenaud Cerrato }; 72018cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 72118cb6368SRenaud Cerrato #endif 72218cb6368SRenaud Cerrato 7239408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 7249408ec1aSAkinobu Mita 725907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 726907b3262SAkinobu Mita { 727907b3262SAkinobu Mita struct device *dev = context; 728907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 729907b3262SAkinobu Mita int ret; 730907b3262SAkinobu Mita 731907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 732907b3262SAkinobu Mita if (ret != count) 733907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 734907b3262SAkinobu Mita 735907b3262SAkinobu Mita return 0; 736907b3262SAkinobu Mita } 737907b3262SAkinobu Mita 738907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 739907b3262SAkinobu Mita const void *reg, size_t reg_size, 740907b3262SAkinobu Mita const void *val, size_t val_size) 741907b3262SAkinobu Mita { 742907b3262SAkinobu Mita struct device *dev = context; 743907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 744907b3262SAkinobu Mita int ret; 745907b3262SAkinobu Mita void *buf; 746907b3262SAkinobu Mita 747907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 748907b3262SAkinobu Mita return -EINVAL; 749907b3262SAkinobu Mita 750907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 751907b3262SAkinobu Mita if (!buf) 752907b3262SAkinobu Mita return -ENOMEM; 753907b3262SAkinobu Mita 754907b3262SAkinobu Mita memcpy(buf, reg, 1); 755907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 756907b3262SAkinobu Mita 757907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 7589bde0afbSXulin Sun 7599bde0afbSXulin Sun kfree(buf); 7609bde0afbSXulin Sun 761907b3262SAkinobu Mita if (ret != val_size + 1) 762907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 763907b3262SAkinobu Mita 764907b3262SAkinobu Mita return 0; 765907b3262SAkinobu Mita } 766907b3262SAkinobu Mita 767907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 768907b3262SAkinobu Mita void *val, size_t val_size) 769907b3262SAkinobu Mita { 770907b3262SAkinobu Mita struct device *dev = context; 771907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 772907b3262SAkinobu Mita int ret; 773907b3262SAkinobu Mita 774907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 775907b3262SAkinobu Mita return -EINVAL; 776907b3262SAkinobu Mita 777907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 778907b3262SAkinobu Mita if (ret != 1) 779907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 780907b3262SAkinobu Mita 781907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 782907b3262SAkinobu Mita if (ret != val_size) 783907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 784907b3262SAkinobu Mita 785907b3262SAkinobu Mita return 0; 786907b3262SAkinobu Mita } 787907b3262SAkinobu Mita 788907b3262SAkinobu Mita /* 789907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 790907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 791907b3262SAkinobu Mita * read register data when reading from registers. 792907b3262SAkinobu Mita */ 793907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 794907b3262SAkinobu Mita .write = pcf2127_i2c_write, 795907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 796907b3262SAkinobu Mita .read = pcf2127_i2c_read, 79718cb6368SRenaud Cerrato }; 79818cb6368SRenaud Cerrato 799907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 800907b3262SAkinobu Mita 801907b3262SAkinobu Mita static int pcf2127_i2c_probe(struct i2c_client *client, 802907b3262SAkinobu Mita const struct i2c_device_id *id) 803907b3262SAkinobu Mita { 804907b3262SAkinobu Mita struct regmap *regmap; 805907b3262SAkinobu Mita static const struct regmap_config config = { 806907b3262SAkinobu Mita .reg_bits = 8, 807907b3262SAkinobu Mita .val_bits = 8, 808040e6dc0SAlexandre Belloni .max_register = 0x1d, 809907b3262SAkinobu Mita }; 810907b3262SAkinobu Mita 811907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 812907b3262SAkinobu Mita return -ENODEV; 813907b3262SAkinobu Mita 814907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 815907b3262SAkinobu Mita &client->dev, &config); 816907b3262SAkinobu Mita if (IS_ERR(regmap)) { 817907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 818907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 819907b3262SAkinobu Mita return PTR_ERR(regmap); 820907b3262SAkinobu Mita } 821907b3262SAkinobu Mita 82227006416SAlexandre Belloni return pcf2127_probe(&client->dev, regmap, client->irq, 823d6c3029fSUwe Kleine-König pcf2127_i2c_driver.driver.name, id->driver_data); 824907b3262SAkinobu Mita } 825907b3262SAkinobu Mita 826907b3262SAkinobu Mita static const struct i2c_device_id pcf2127_i2c_id[] = { 827d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 828cee2cc21SAkinobu Mita { "pcf2129", 0 }, 829985b30dbSLiam Beguin { "pca2129", 0 }, 830907b3262SAkinobu Mita { } 831907b3262SAkinobu Mita }; 832907b3262SAkinobu Mita MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 833907b3262SAkinobu Mita 834907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 835907b3262SAkinobu Mita .driver = { 836907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 837907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 838907b3262SAkinobu Mita }, 839907b3262SAkinobu Mita .probe = pcf2127_i2c_probe, 840907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 841907b3262SAkinobu Mita }; 8429408ec1aSAkinobu Mita 8439408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 8449408ec1aSAkinobu Mita { 8459408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 8469408ec1aSAkinobu Mita } 8479408ec1aSAkinobu Mita 8489408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 8499408ec1aSAkinobu Mita { 8509408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 8519408ec1aSAkinobu Mita } 8529408ec1aSAkinobu Mita 8539408ec1aSAkinobu Mita #else 8549408ec1aSAkinobu Mita 8559408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 8569408ec1aSAkinobu Mita { 8579408ec1aSAkinobu Mita return 0; 8589408ec1aSAkinobu Mita } 8599408ec1aSAkinobu Mita 8609408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 8619408ec1aSAkinobu Mita { 8629408ec1aSAkinobu Mita } 8639408ec1aSAkinobu Mita 8649408ec1aSAkinobu Mita #endif 8659408ec1aSAkinobu Mita 8669408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 8679408ec1aSAkinobu Mita 8689408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 8699408ec1aSAkinobu Mita 8709408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 8719408ec1aSAkinobu Mita { 8729408ec1aSAkinobu Mita static const struct regmap_config config = { 8739408ec1aSAkinobu Mita .reg_bits = 8, 8749408ec1aSAkinobu Mita .val_bits = 8, 8759408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 8769408ec1aSAkinobu Mita .write_flag_mask = 0x20, 877040e6dc0SAlexandre Belloni .max_register = 0x1d, 8789408ec1aSAkinobu Mita }; 8799408ec1aSAkinobu Mita struct regmap *regmap; 8809408ec1aSAkinobu Mita 8819408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 8829408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 8839408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 8849408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 8859408ec1aSAkinobu Mita return PTR_ERR(regmap); 8869408ec1aSAkinobu Mita } 8879408ec1aSAkinobu Mita 88827006416SAlexandre Belloni return pcf2127_probe(&spi->dev, regmap, spi->irq, 88927006416SAlexandre Belloni pcf2127_spi_driver.driver.name, 890d6c3029fSUwe Kleine-König spi_get_device_id(spi)->driver_data); 8919408ec1aSAkinobu Mita } 8929408ec1aSAkinobu Mita 8939408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 894d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 895cee2cc21SAkinobu Mita { "pcf2129", 0 }, 896985b30dbSLiam Beguin { "pca2129", 0 }, 8979408ec1aSAkinobu Mita { } 8989408ec1aSAkinobu Mita }; 8999408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 9009408ec1aSAkinobu Mita 9019408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 9029408ec1aSAkinobu Mita .driver = { 9039408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 9049408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 9059408ec1aSAkinobu Mita }, 9069408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 9079408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 9089408ec1aSAkinobu Mita }; 9099408ec1aSAkinobu Mita 9109408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 9119408ec1aSAkinobu Mita { 9129408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 9139408ec1aSAkinobu Mita } 9149408ec1aSAkinobu Mita 9159408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 9169408ec1aSAkinobu Mita { 9179408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 9189408ec1aSAkinobu Mita } 9199408ec1aSAkinobu Mita 9209408ec1aSAkinobu Mita #else 9219408ec1aSAkinobu Mita 9229408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 9239408ec1aSAkinobu Mita { 9249408ec1aSAkinobu Mita return 0; 9259408ec1aSAkinobu Mita } 9269408ec1aSAkinobu Mita 9279408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 9289408ec1aSAkinobu Mita { 9299408ec1aSAkinobu Mita } 9309408ec1aSAkinobu Mita 9319408ec1aSAkinobu Mita #endif 9329408ec1aSAkinobu Mita 9339408ec1aSAkinobu Mita static int __init pcf2127_init(void) 9349408ec1aSAkinobu Mita { 9359408ec1aSAkinobu Mita int ret; 9369408ec1aSAkinobu Mita 9379408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 9389408ec1aSAkinobu Mita if (ret) { 9399408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 9409408ec1aSAkinobu Mita return ret; 9419408ec1aSAkinobu Mita } 9429408ec1aSAkinobu Mita 9439408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 9449408ec1aSAkinobu Mita if (ret) { 9459408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 9469408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 9479408ec1aSAkinobu Mita } 9489408ec1aSAkinobu Mita 9499408ec1aSAkinobu Mita return ret; 9509408ec1aSAkinobu Mita } 9519408ec1aSAkinobu Mita module_init(pcf2127_init) 9529408ec1aSAkinobu Mita 9539408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 9549408ec1aSAkinobu Mita { 9559408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 9569408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 9579408ec1aSAkinobu Mita } 9589408ec1aSAkinobu Mita module_exit(pcf2127_exit) 95918cb6368SRenaud Cerrato 96018cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 961cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); 9624d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 963