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 * 13836e9ea3SFabio Estevam * Datasheet: https://www.nxp.com/docs/en/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) 488a914bacSLiam Beguin /* Alarm registers */ 498a914bacSLiam Beguin #define PCF2127_REG_ALARM_SC 0x0A 508a914bacSLiam Beguin #define PCF2127_REG_ALARM_MN 0x0B 518a914bacSLiam Beguin #define PCF2127_REG_ALARM_HR 0x0C 528a914bacSLiam Beguin #define PCF2127_REG_ALARM_DM 0x0D 538a914bacSLiam Beguin #define PCF2127_REG_ALARM_DW 0x0E 5427006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE BIT(7) 5515f57b3eSPhilipp Rosenberger /* CLKOUT control register */ 5615f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT 0x0f 5715f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR BIT(5) 580e735eaaSBruno Thomsen /* Watchdog registers */ 590e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL 0x10 600e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0 BIT(0) 610e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1 BIT(1) 620e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0 BIT(6) 630e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1 BIT(7) 640e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL 0x11 6503623b4bSBruno Thomsen /* Tamper timestamp registers */ 6603623b4bSBruno Thomsen #define PCF2127_REG_TS_CTRL 0x12 6703623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) 6803623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM BIT(7) 69bbfe3a7aSBruno Thomsen /* 70bbfe3a7aSBruno Thomsen * RAM registers 71bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 72bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 73bbfe3a7aSBruno Thomsen * PCF2129 doesn't have this feature. 74bbfe3a7aSBruno Thomsen */ 75bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 76bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 77bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 78f97cfddcSUwe Kleine-König 790e735eaaSBruno Thomsen /* Watchdog timer value constants */ 800e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP 0 810e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN 2 820e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX 255 830e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT 60 84653ebd75SAndrea Scian 852f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */ 862f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1) 872f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \ 882f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_AF | \ 892f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_WDTF | \ 902f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_TSF2) 912f861984SMian Yousaf Kaukab 9218cb6368SRenaud Cerrato struct pcf2127 { 9318cb6368SRenaud Cerrato struct rtc_device *rtc; 940e735eaaSBruno Thomsen struct watchdog_device wdd; 95907b3262SAkinobu Mita struct regmap *regmap; 962f861984SMian Yousaf Kaukab time64_t ts; 972f861984SMian Yousaf Kaukab bool ts_valid; 982f861984SMian Yousaf Kaukab bool irq_enabled; 9918cb6368SRenaud Cerrato }; 10018cb6368SRenaud Cerrato 10118cb6368SRenaud Cerrato /* 10218cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 10318cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 10418cb6368SRenaud Cerrato */ 105907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 10618cb6368SRenaud Cerrato { 107907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 10831f077c3SHugo Villeneuve unsigned char buf[7]; 109907b3262SAkinobu Mita int ret; 11018cb6368SRenaud Cerrato 1117f43020eSBruno Thomsen /* 1127f43020eSBruno Thomsen * Avoid reading CTRL2 register as it causes WD_VAL register 1137f43020eSBruno Thomsen * value to reset to 0 which means watchdog is stopped. 1147f43020eSBruno Thomsen */ 11531f077c3SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC, buf, 11631f077c3SHugo Villeneuve sizeof(buf)); 117907b3262SAkinobu Mita if (ret) { 118907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 119907b3262SAkinobu Mita return ret; 12018cb6368SRenaud Cerrato } 12118cb6368SRenaud Cerrato 122bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 12331f077c3SHugo Villeneuve if (buf[0] & PCF2127_BIT_SC_OSF) { 124653ebd75SAndrea Scian /* 125653ebd75SAndrea Scian * no need clear the flag here, 126653ebd75SAndrea Scian * it will be cleared once the new date is saved 127653ebd75SAndrea Scian */ 128907b3262SAkinobu Mita dev_warn(dev, 129653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 130653ebd75SAndrea Scian return -EINVAL; 13118cb6368SRenaud Cerrato } 13218cb6368SRenaud Cerrato 133907b3262SAkinobu Mita dev_dbg(dev, 13431f077c3SHugo Villeneuve "%s: raw data is sec=%02x, min=%02x, hr=%02x, " 13518cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 13631f077c3SHugo Villeneuve __func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); 13718cb6368SRenaud Cerrato 13831f077c3SHugo Villeneuve tm->tm_sec = bcd2bin(buf[0] & 0x7F); 13931f077c3SHugo Villeneuve tm->tm_min = bcd2bin(buf[1] & 0x7F); 14031f077c3SHugo Villeneuve tm->tm_hour = bcd2bin(buf[2] & 0x3F); /* rtc hr 0-23 */ 14131f077c3SHugo Villeneuve tm->tm_mday = bcd2bin(buf[3] & 0x3F); 14231f077c3SHugo Villeneuve tm->tm_wday = buf[4] & 0x07; 14331f077c3SHugo Villeneuve tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; /* rtc mn 1-12 */ 14431f077c3SHugo Villeneuve tm->tm_year = bcd2bin(buf[6]); 145b139bb5cSAlexandre Belloni tm->tm_year += 100; 14618cb6368SRenaud Cerrato 147907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 14818cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 14918cb6368SRenaud Cerrato __func__, 15018cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 15118cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 15218cb6368SRenaud Cerrato 15322652ba7SAlexandre Belloni return 0; 15418cb6368SRenaud Cerrato } 15518cb6368SRenaud Cerrato 156907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 15718cb6368SRenaud Cerrato { 158907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 159907b3262SAkinobu Mita unsigned char buf[7]; 16018cb6368SRenaud Cerrato int i = 0, err; 16118cb6368SRenaud Cerrato 162907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 16318cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 16418cb6368SRenaud Cerrato __func__, 16518cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 16618cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 16718cb6368SRenaud Cerrato 16818cb6368SRenaud Cerrato /* hours, minutes and seconds */ 169653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 17018cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 17118cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 17218cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 17318cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 17418cb6368SRenaud Cerrato 17518cb6368SRenaud Cerrato /* month, 1 - 12 */ 17618cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 17718cb6368SRenaud Cerrato 17818cb6368SRenaud Cerrato /* year */ 179b139bb5cSAlexandre Belloni buf[i++] = bin2bcd(tm->tm_year - 100); 18018cb6368SRenaud Cerrato 18118cb6368SRenaud Cerrato /* write register's data */ 182907b3262SAkinobu Mita err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); 183907b3262SAkinobu Mita if (err) { 184*3d740c64SHugo Villeneuve dev_dbg(dev, "%s: err=%d", __func__, err); 185907b3262SAkinobu Mita return err; 18618cb6368SRenaud Cerrato } 18718cb6368SRenaud Cerrato 18818cb6368SRenaud Cerrato return 0; 18918cb6368SRenaud Cerrato } 19018cb6368SRenaud Cerrato 19118cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 19218cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 19318cb6368SRenaud Cerrato { 194907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 1957d65cf8cSAlexandre Belloni int val, touser = 0; 196f97cfddcSUwe Kleine-König int ret; 19718cb6368SRenaud Cerrato 19818cb6368SRenaud Cerrato switch (cmd) { 19918cb6368SRenaud Cerrato case RTC_VL_READ: 2007d65cf8cSAlexandre Belloni ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); 201907b3262SAkinobu Mita if (ret) 202f97cfddcSUwe Kleine-König return ret; 20318cb6368SRenaud Cerrato 2047d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BLF) 2057d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_LOW; 2067d65cf8cSAlexandre Belloni 2077d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BF) 2087d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_SWITCH; 209f97cfddcSUwe Kleine-König 210af427311SAlexandre Belloni return put_user(touser, (unsigned int __user *)arg); 2117d65cf8cSAlexandre Belloni 2127d65cf8cSAlexandre Belloni case RTC_VL_CLR: 2137d65cf8cSAlexandre Belloni return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 2147d65cf8cSAlexandre Belloni PCF2127_BIT_CTRL3_BF, 0); 2157d65cf8cSAlexandre Belloni 21618cb6368SRenaud Cerrato default: 21718cb6368SRenaud Cerrato return -ENOIOCTLCMD; 21818cb6368SRenaud Cerrato } 21918cb6368SRenaud Cerrato } 22018cb6368SRenaud Cerrato 221d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 222d6c3029fSUwe Kleine-König void *val, size_t bytes) 223d6c3029fSUwe Kleine-König { 224d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 225d6c3029fSUwe Kleine-König int ret; 226d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 227d6c3029fSUwe Kleine-König 228bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 229d6c3029fSUwe Kleine-König offsetbuf, 2); 230d6c3029fSUwe Kleine-König if (ret) 231d6c3029fSUwe Kleine-König return ret; 232d6c3029fSUwe Kleine-König 233ba1c30bfSDan Carpenter return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 234d6c3029fSUwe Kleine-König val, bytes); 235d6c3029fSUwe Kleine-König } 236d6c3029fSUwe Kleine-König 237d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 238d6c3029fSUwe Kleine-König void *val, size_t bytes) 239d6c3029fSUwe Kleine-König { 240d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 241d6c3029fSUwe Kleine-König int ret; 242d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 243d6c3029fSUwe Kleine-König 244bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 245d6c3029fSUwe Kleine-König offsetbuf, 2); 246d6c3029fSUwe Kleine-König if (ret) 247d6c3029fSUwe Kleine-König return ret; 248d6c3029fSUwe Kleine-König 249ba1c30bfSDan Carpenter return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 250d6c3029fSUwe Kleine-König val, bytes); 251d6c3029fSUwe Kleine-König } 252d6c3029fSUwe Kleine-König 2530e735eaaSBruno Thomsen /* watchdog driver */ 2540e735eaaSBruno Thomsen 2550e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd) 2560e735eaaSBruno Thomsen { 2570e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 2580e735eaaSBruno Thomsen 2590e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout); 2600e735eaaSBruno Thomsen } 2610e735eaaSBruno Thomsen 2620e735eaaSBruno Thomsen /* 2630e735eaaSBruno Thomsen * Restart watchdog timer if feature is active. 2640e735eaaSBruno Thomsen * 2650e735eaaSBruno Thomsen * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, 2660e735eaaSBruno Thomsen * since register also contain control/status flags for other features. 2670e735eaaSBruno Thomsen * Always call this function after reading CTRL2 register. 2680e735eaaSBruno Thomsen */ 2690e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) 2700e735eaaSBruno Thomsen { 2710e735eaaSBruno Thomsen int ret = 0; 2720e735eaaSBruno Thomsen 2730e735eaaSBruno Thomsen if (watchdog_active(wdd)) { 2740e735eaaSBruno Thomsen ret = pcf2127_wdt_ping(wdd); 2750e735eaaSBruno Thomsen if (ret) 2760e735eaaSBruno Thomsen dev_err(wdd->parent, 2770e735eaaSBruno Thomsen "%s: watchdog restart failed, ret=%d\n", 2780e735eaaSBruno Thomsen __func__, ret); 2790e735eaaSBruno Thomsen } 2800e735eaaSBruno Thomsen 2810e735eaaSBruno Thomsen return ret; 2820e735eaaSBruno Thomsen } 2830e735eaaSBruno Thomsen 2840e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd) 2850e735eaaSBruno Thomsen { 2860e735eaaSBruno Thomsen return pcf2127_wdt_ping(wdd); 2870e735eaaSBruno Thomsen } 2880e735eaaSBruno Thomsen 2890e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd) 2900e735eaaSBruno Thomsen { 2910e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 2920e735eaaSBruno Thomsen 2930e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, 2940e735eaaSBruno Thomsen PCF2127_WD_VAL_STOP); 2950e735eaaSBruno Thomsen } 2960e735eaaSBruno Thomsen 2970e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, 2980e735eaaSBruno Thomsen unsigned int new_timeout) 2990e735eaaSBruno Thomsen { 3000e735eaaSBruno Thomsen dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", 3010e735eaaSBruno Thomsen new_timeout, wdd->timeout); 3020e735eaaSBruno Thomsen 3030e735eaaSBruno Thomsen wdd->timeout = new_timeout; 3040e735eaaSBruno Thomsen 3050e735eaaSBruno Thomsen return pcf2127_wdt_active_ping(wdd); 3060e735eaaSBruno Thomsen } 3070e735eaaSBruno Thomsen 3080e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = { 3090e735eaaSBruno Thomsen .identity = "NXP PCF2127/PCF2129 Watchdog", 3100e735eaaSBruno Thomsen .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, 3110e735eaaSBruno Thomsen }; 3120e735eaaSBruno Thomsen 3130e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = { 3140e735eaaSBruno Thomsen .owner = THIS_MODULE, 3150e735eaaSBruno Thomsen .start = pcf2127_wdt_start, 3160e735eaaSBruno Thomsen .stop = pcf2127_wdt_stop, 3170e735eaaSBruno Thomsen .ping = pcf2127_wdt_ping, 3180e735eaaSBruno Thomsen .set_timeout = pcf2127_wdt_set_timeout, 3190e735eaaSBruno Thomsen }; 3200e735eaaSBruno Thomsen 3215d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127) 3225d78533aSUwe Kleine-König { 3235d78533aSUwe Kleine-König u32 wdd_timeout; 3245d78533aSUwe Kleine-König int ret; 3255d78533aSUwe Kleine-König 32671ac1345SUwe Kleine-König if (!IS_ENABLED(CONFIG_WATCHDOG) || 32771ac1345SUwe Kleine-König !device_property_read_bool(dev, "reset-source")) 3285d78533aSUwe Kleine-König return 0; 3295d78533aSUwe Kleine-König 3305d78533aSUwe Kleine-König pcf2127->wdd.parent = dev; 3315d78533aSUwe Kleine-König pcf2127->wdd.info = &pcf2127_wdt_info; 3325d78533aSUwe Kleine-König pcf2127->wdd.ops = &pcf2127_watchdog_ops; 3335d78533aSUwe Kleine-König pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN; 3345d78533aSUwe Kleine-König pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX; 3355d78533aSUwe Kleine-König pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT; 3365d78533aSUwe Kleine-König pcf2127->wdd.min_hw_heartbeat_ms = 500; 3375d78533aSUwe Kleine-König pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS; 3385d78533aSUwe Kleine-König 3395d78533aSUwe Kleine-König watchdog_set_drvdata(&pcf2127->wdd, pcf2127); 3405d78533aSUwe Kleine-König 3415d78533aSUwe Kleine-König /* Test if watchdog timer is started by bootloader */ 3425d78533aSUwe Kleine-König ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout); 3435d78533aSUwe Kleine-König if (ret) 3445d78533aSUwe Kleine-König return ret; 3455d78533aSUwe Kleine-König 3465d78533aSUwe Kleine-König if (wdd_timeout) 3475d78533aSUwe Kleine-König set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); 3485d78533aSUwe Kleine-König 3495d78533aSUwe Kleine-König return devm_watchdog_register_device(dev, &pcf2127->wdd); 3505d78533aSUwe Kleine-König } 3515d78533aSUwe Kleine-König 3528a914bacSLiam Beguin /* Alarm */ 3538a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3548a914bacSLiam Beguin { 3558a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 35673ce0530SHugo Villeneuve u8 buf[5]; 35773ce0530SHugo Villeneuve unsigned int ctrl2; 3588a914bacSLiam Beguin int ret; 3598a914bacSLiam Beguin 3608a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 3618a914bacSLiam Beguin if (ret) 3628a914bacSLiam Beguin return ret; 3638a914bacSLiam Beguin 3648a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 3658a914bacSLiam Beguin if (ret) 3668a914bacSLiam Beguin return ret; 3678a914bacSLiam Beguin 3688a914bacSLiam Beguin ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf, 3698a914bacSLiam Beguin sizeof(buf)); 3708a914bacSLiam Beguin if (ret) 3718a914bacSLiam Beguin return ret; 3728a914bacSLiam Beguin 3738a914bacSLiam Beguin alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE; 3748a914bacSLiam Beguin alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF; 3758a914bacSLiam Beguin 3768a914bacSLiam Beguin alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F); 3778a914bacSLiam Beguin alrm->time.tm_min = bcd2bin(buf[1] & 0x7F); 3788a914bacSLiam Beguin alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F); 3798a914bacSLiam Beguin alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F); 3808a914bacSLiam Beguin 3818a914bacSLiam Beguin return 0; 3828a914bacSLiam Beguin } 3838a914bacSLiam Beguin 3848a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable) 3858a914bacSLiam Beguin { 3868a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 3878a914bacSLiam Beguin int ret; 3888a914bacSLiam Beguin 3898a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 3908a914bacSLiam Beguin PCF2127_BIT_CTRL2_AIE, 3918a914bacSLiam Beguin enable ? PCF2127_BIT_CTRL2_AIE : 0); 3928a914bacSLiam Beguin if (ret) 3938a914bacSLiam Beguin return ret; 3948a914bacSLiam Beguin 3958a914bacSLiam Beguin return pcf2127_wdt_active_ping(&pcf2127->wdd); 3968a914bacSLiam Beguin } 3978a914bacSLiam Beguin 3988a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3998a914bacSLiam Beguin { 4008a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4018a914bacSLiam Beguin uint8_t buf[5]; 4028a914bacSLiam Beguin int ret; 4038a914bacSLiam Beguin 4048a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 4058a914bacSLiam Beguin PCF2127_BIT_CTRL2_AF, 0); 4068a914bacSLiam Beguin if (ret) 4078a914bacSLiam Beguin return ret; 4088a914bacSLiam Beguin 4098a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 4108a914bacSLiam Beguin if (ret) 4118a914bacSLiam Beguin return ret; 4128a914bacSLiam Beguin 4138a914bacSLiam Beguin buf[0] = bin2bcd(alrm->time.tm_sec); 4148a914bacSLiam Beguin buf[1] = bin2bcd(alrm->time.tm_min); 4158a914bacSLiam Beguin buf[2] = bin2bcd(alrm->time.tm_hour); 4168a914bacSLiam Beguin buf[3] = bin2bcd(alrm->time.tm_mday); 41727006416SAlexandre Belloni buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */ 4188a914bacSLiam Beguin 4198a914bacSLiam Beguin ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf, 4208a914bacSLiam Beguin sizeof(buf)); 4218a914bacSLiam Beguin if (ret) 4228a914bacSLiam Beguin return ret; 4238a914bacSLiam Beguin 4248a914bacSLiam Beguin return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled); 4258a914bacSLiam Beguin } 4268a914bacSLiam Beguin 4272f861984SMian Yousaf Kaukab /* 4282f861984SMian Yousaf Kaukab * This function reads ctrl2 register, caller is responsible for calling 4292f861984SMian Yousaf Kaukab * pcf2127_wdt_active_ping() 4302f861984SMian Yousaf Kaukab */ 4312f861984SMian Yousaf Kaukab static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts) 4322f861984SMian Yousaf Kaukab { 4332f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4342f861984SMian Yousaf Kaukab struct rtc_time tm; 4352f861984SMian Yousaf Kaukab int ret; 436720fb4b8SHugo Villeneuve unsigned char data[7]; 4372f861984SMian Yousaf Kaukab 438720fb4b8SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_TS_CTRL, data, 4392f861984SMian Yousaf Kaukab sizeof(data)); 4402f861984SMian Yousaf Kaukab if (ret) { 4412f861984SMian Yousaf Kaukab dev_err(dev, "%s: read error ret=%d\n", __func__, ret); 4422f861984SMian Yousaf Kaukab return ret; 4432f861984SMian Yousaf Kaukab } 4442f861984SMian Yousaf Kaukab 4452f861984SMian Yousaf Kaukab dev_dbg(dev, 446720fb4b8SHugo Villeneuve "%s: raw data is ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", 447720fb4b8SHugo Villeneuve __func__, data[1], data[2], data[3], data[4], data[5], data[6]); 4482f861984SMian Yousaf Kaukab 449720fb4b8SHugo Villeneuve tm.tm_sec = bcd2bin(data[1] & 0x7F); 450720fb4b8SHugo Villeneuve tm.tm_min = bcd2bin(data[2] & 0x7F); 451720fb4b8SHugo Villeneuve tm.tm_hour = bcd2bin(data[3] & 0x3F); 452720fb4b8SHugo Villeneuve tm.tm_mday = bcd2bin(data[4] & 0x3F); 4532f861984SMian Yousaf Kaukab /* TS_MO register (month) value range: 1-12 */ 454720fb4b8SHugo Villeneuve tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1; 455720fb4b8SHugo Villeneuve tm.tm_year = bcd2bin(data[6]); 4562f861984SMian Yousaf Kaukab if (tm.tm_year < 70) 4572f861984SMian Yousaf Kaukab tm.tm_year += 100; /* assume we are in 1970...2069 */ 4582f861984SMian Yousaf Kaukab 4592f861984SMian Yousaf Kaukab ret = rtc_valid_tm(&tm); 4602f861984SMian Yousaf Kaukab if (ret) { 4612f861984SMian Yousaf Kaukab dev_err(dev, "Invalid timestamp. ret=%d\n", ret); 4622f861984SMian Yousaf Kaukab return ret; 4632f861984SMian Yousaf Kaukab } 4642f861984SMian Yousaf Kaukab 4652f861984SMian Yousaf Kaukab *ts = rtc_tm_to_time64(&tm); 4662f861984SMian Yousaf Kaukab return 0; 4672f861984SMian Yousaf Kaukab }; 4682f861984SMian Yousaf Kaukab 4692f861984SMian Yousaf Kaukab static void pcf2127_rtc_ts_snapshot(struct device *dev) 4702f861984SMian Yousaf Kaukab { 4712f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4722f861984SMian Yousaf Kaukab int ret; 4732f861984SMian Yousaf Kaukab 4742f861984SMian Yousaf Kaukab /* Let userspace read the first timestamp */ 4752f861984SMian Yousaf Kaukab if (pcf2127->ts_valid) 4762f861984SMian Yousaf Kaukab return; 4772f861984SMian Yousaf Kaukab 4782f861984SMian Yousaf Kaukab ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts); 4792f861984SMian Yousaf Kaukab if (!ret) 4802f861984SMian Yousaf Kaukab pcf2127->ts_valid = true; 4812f861984SMian Yousaf Kaukab } 4822f861984SMian Yousaf Kaukab 4838a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev) 4848a914bacSLiam Beguin { 4858a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4862f861984SMian Yousaf Kaukab unsigned int ctrl1, ctrl2; 4878a914bacSLiam Beguin int ret = 0; 4888a914bacSLiam Beguin 4892f861984SMian Yousaf Kaukab ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1); 4902f861984SMian Yousaf Kaukab if (ret) 4912f861984SMian Yousaf Kaukab return IRQ_NONE; 4922f861984SMian Yousaf Kaukab 4938a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 4948a914bacSLiam Beguin if (ret) 4958a914bacSLiam Beguin return IRQ_NONE; 4968a914bacSLiam Beguin 4972f861984SMian Yousaf Kaukab if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK)) 49827006416SAlexandre Belloni return IRQ_NONE; 49927006416SAlexandre Belloni 5002f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2) 5012f861984SMian Yousaf Kaukab pcf2127_rtc_ts_snapshot(dev); 5028a914bacSLiam Beguin 5032f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_CTRL1_IRQ_MASK) 5042f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1, 5052f861984SMian Yousaf Kaukab ctrl1 & ~PCF2127_CTRL1_IRQ_MASK); 5062f861984SMian Yousaf Kaukab 5072f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_CTRL2_IRQ_MASK) 5082f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 5092f861984SMian Yousaf Kaukab ctrl2 & ~PCF2127_CTRL2_IRQ_MASK); 5102f861984SMian Yousaf Kaukab 5112f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_BIT_CTRL2_AF) 5128a914bacSLiam Beguin rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF); 5138a914bacSLiam Beguin 51427006416SAlexandre Belloni pcf2127_wdt_active_ping(&pcf2127->wdd); 5158a914bacSLiam Beguin 5168a914bacSLiam Beguin return IRQ_HANDLED; 5178a914bacSLiam Beguin } 5188a914bacSLiam Beguin 51925cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = { 5208a914bacSLiam Beguin .ioctl = pcf2127_rtc_ioctl, 5218a914bacSLiam Beguin .read_time = pcf2127_rtc_read_time, 5228a914bacSLiam Beguin .set_time = pcf2127_rtc_set_time, 5238a914bacSLiam Beguin .read_alarm = pcf2127_rtc_read_alarm, 5248a914bacSLiam Beguin .set_alarm = pcf2127_rtc_set_alarm, 5258a914bacSLiam Beguin .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable, 5268a914bacSLiam Beguin }; 5278a914bacSLiam Beguin 52803623b4bSBruno Thomsen /* sysfs interface */ 52903623b4bSBruno Thomsen 53003623b4bSBruno Thomsen static ssize_t timestamp0_store(struct device *dev, 53103623b4bSBruno Thomsen struct device_attribute *attr, 53203623b4bSBruno Thomsen const char *buf, size_t count) 53303623b4bSBruno Thomsen { 53403623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 53503623b4bSBruno Thomsen int ret; 53603623b4bSBruno Thomsen 5372f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 5382f861984SMian Yousaf Kaukab pcf2127->ts_valid = false; 5392f861984SMian Yousaf Kaukab } else { 54003623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 54103623b4bSBruno Thomsen PCF2127_BIT_CTRL1_TSF1, 0); 54203623b4bSBruno Thomsen if (ret) { 54303623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret); 54403623b4bSBruno Thomsen return ret; 54503623b4bSBruno Thomsen } 54603623b4bSBruno Thomsen 54703623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 54803623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSF2, 0); 54903623b4bSBruno Thomsen if (ret) { 55003623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret); 55103623b4bSBruno Thomsen return ret; 55203623b4bSBruno Thomsen } 55303623b4bSBruno Thomsen 55403623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 55503623b4bSBruno Thomsen if (ret) 55603623b4bSBruno Thomsen return ret; 5572f861984SMian Yousaf Kaukab } 55803623b4bSBruno Thomsen 55903623b4bSBruno Thomsen return count; 56003623b4bSBruno Thomsen }; 56103623b4bSBruno Thomsen 56203623b4bSBruno Thomsen static ssize_t timestamp0_show(struct device *dev, 56303623b4bSBruno Thomsen struct device_attribute *attr, char *buf) 56403623b4bSBruno Thomsen { 56503623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 5662f861984SMian Yousaf Kaukab unsigned int ctrl1, ctrl2; 56703623b4bSBruno Thomsen int ret; 5682f861984SMian Yousaf Kaukab time64_t ts; 56903623b4bSBruno Thomsen 5702f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 5712f861984SMian Yousaf Kaukab if (!pcf2127->ts_valid) 5722f861984SMian Yousaf Kaukab return 0; 5732f861984SMian Yousaf Kaukab ts = pcf2127->ts; 5742f861984SMian Yousaf Kaukab } else { 5752f861984SMian Yousaf Kaukab ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1); 5762f861984SMian Yousaf Kaukab if (ret) 5772f861984SMian Yousaf Kaukab return 0; 57803623b4bSBruno Thomsen 5792f861984SMian Yousaf Kaukab ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 5802f861984SMian Yousaf Kaukab if (ret) 5812f861984SMian Yousaf Kaukab return 0; 5822f861984SMian Yousaf Kaukab 5832f861984SMian Yousaf Kaukab if (!(ctrl1 & PCF2127_BIT_CTRL1_TSF1) && 5842f861984SMian Yousaf Kaukab !(ctrl2 & PCF2127_BIT_CTRL2_TSF2)) 5852f861984SMian Yousaf Kaukab return 0; 5862f861984SMian Yousaf Kaukab 5872f861984SMian Yousaf Kaukab ret = pcf2127_rtc_ts_read(dev->parent, &ts); 5882f861984SMian Yousaf Kaukab if (ret) 5892f861984SMian Yousaf Kaukab return 0; 59003623b4bSBruno Thomsen 59103623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 59203623b4bSBruno Thomsen if (ret) 59303623b4bSBruno Thomsen return ret; 5942f861984SMian Yousaf Kaukab } 5952f861984SMian Yousaf Kaukab return sprintf(buf, "%llu\n", (unsigned long long)ts); 59603623b4bSBruno Thomsen }; 59703623b4bSBruno Thomsen 59803623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0); 59903623b4bSBruno Thomsen 60003623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = { 60103623b4bSBruno Thomsen &dev_attr_timestamp0.attr, 60203623b4bSBruno Thomsen NULL 60303623b4bSBruno Thomsen }; 60403623b4bSBruno Thomsen 60503623b4bSBruno Thomsen static const struct attribute_group pcf2127_attr_group = { 60603623b4bSBruno Thomsen .attrs = pcf2127_attrs, 60703623b4bSBruno Thomsen }; 60803623b4bSBruno Thomsen 609907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 6102843d565SBiwen Li int alarm_irq, const char *name, bool is_pcf2127) 61118cb6368SRenaud Cerrato { 61218cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 613d6c3029fSUwe Kleine-König int ret = 0; 61415f57b3eSPhilipp Rosenberger unsigned int val; 61518cb6368SRenaud Cerrato 616907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 61718cb6368SRenaud Cerrato 618907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 61918cb6368SRenaud Cerrato if (!pcf2127) 62018cb6368SRenaud Cerrato return -ENOMEM; 62118cb6368SRenaud Cerrato 622907b3262SAkinobu Mita pcf2127->regmap = regmap; 62318cb6368SRenaud Cerrato 624907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 625907b3262SAkinobu Mita 626e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 627d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 628d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 62918cb6368SRenaud Cerrato 630e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 631b139bb5cSAlexandre Belloni pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 632b139bb5cSAlexandre Belloni pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; 633b139bb5cSAlexandre Belloni pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ 634bda10273SAlexandre Belloni set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features); 635689fafd5SAlexandre Belloni clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features); 63625cbe9c8SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 637e788771cSBruno Thomsen 63835425bafSBiwen Li if (alarm_irq > 0) { 639d4785b46SHugo Villeneuve unsigned long flags; 640d4785b46SHugo Villeneuve 641d4785b46SHugo Villeneuve /* 642d4785b46SHugo Villeneuve * If flags = 0, devm_request_threaded_irq() will use IRQ flags 643d4785b46SHugo Villeneuve * obtained from device tree. 644d4785b46SHugo Villeneuve */ 645d4785b46SHugo Villeneuve if (dev_fwnode(dev)) 646d4785b46SHugo Villeneuve flags = 0; 647d4785b46SHugo Villeneuve else 648d4785b46SHugo Villeneuve flags = IRQF_TRIGGER_LOW; 649d4785b46SHugo Villeneuve 65027006416SAlexandre Belloni ret = devm_request_threaded_irq(dev, alarm_irq, NULL, 65127006416SAlexandre Belloni pcf2127_rtc_irq, 652d4785b46SHugo Villeneuve flags | IRQF_ONESHOT, 6538a914bacSLiam Beguin dev_name(dev), dev); 6548a914bacSLiam Beguin if (ret) { 6558a914bacSLiam Beguin dev_err(dev, "failed to request alarm irq\n"); 6568a914bacSLiam Beguin return ret; 6578a914bacSLiam Beguin } 6582f861984SMian Yousaf Kaukab pcf2127->irq_enabled = true; 6598a914bacSLiam Beguin } 6608a914bacSLiam Beguin 66135425bafSBiwen Li if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) { 6628a914bacSLiam Beguin device_init_wakeup(dev, true); 66325cbe9c8SAlexandre Belloni set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 6648a914bacSLiam Beguin } 6658a914bacSLiam Beguin 6662843d565SBiwen Li if (is_pcf2127) { 667d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 668d6c3029fSUwe Kleine-König .priv = pcf2127, 669d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 670d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 671d6c3029fSUwe Kleine-König .size = 512, 672d6c3029fSUwe Kleine-König }; 673d6c3029fSUwe Kleine-König 6743a905c2dSBartosz Golaszewski ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 675d6c3029fSUwe Kleine-König } 676d6c3029fSUwe Kleine-König 6770e735eaaSBruno Thomsen /* 678b9ac079aSPhilipp Rosenberger * The "Power-On Reset Override" facility prevents the RTC to do a reset 679b9ac079aSPhilipp Rosenberger * after power on. For normal operation the PORO must be disabled. 680b9ac079aSPhilipp Rosenberger */ 681b9ac079aSPhilipp Rosenberger regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 682b9ac079aSPhilipp Rosenberger PCF2127_BIT_CTRL1_POR_OVRD); 683b9ac079aSPhilipp Rosenberger 68415f57b3eSPhilipp Rosenberger ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val); 68515f57b3eSPhilipp Rosenberger if (ret < 0) 68615f57b3eSPhilipp Rosenberger return ret; 68715f57b3eSPhilipp Rosenberger 68815f57b3eSPhilipp Rosenberger if (!(val & PCF2127_BIT_CLKOUT_OTPR)) { 68915f57b3eSPhilipp Rosenberger ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT, 69015f57b3eSPhilipp Rosenberger PCF2127_BIT_CLKOUT_OTPR); 69115f57b3eSPhilipp Rosenberger if (ret < 0) 69215f57b3eSPhilipp Rosenberger return ret; 69315f57b3eSPhilipp Rosenberger 69415f57b3eSPhilipp Rosenberger msleep(100); 69515f57b3eSPhilipp Rosenberger } 69615f57b3eSPhilipp Rosenberger 697b9ac079aSPhilipp Rosenberger /* 6980e735eaaSBruno Thomsen * Watchdog timer enabled and reset pin /RST activated when timed out. 6990e735eaaSBruno Thomsen * Select 1Hz clock source for watchdog timer. 7000e735eaaSBruno Thomsen * Note: Countdown timer disabled and not available. 7012843d565SBiwen Li * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD 7022843d565SBiwen Li * of register watchdg_tim_ctl. The bit[6] is labeled 7032843d565SBiwen Li * as T. Bits labeled as T must always be written with 7042843d565SBiwen Li * logic 0. 7050e735eaaSBruno Thomsen */ 7060e735eaaSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL, 7070e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 7080e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 7090e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1 | 7100e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF0, 7110e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 7122843d565SBiwen Li (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) | 7130e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1); 7140e735eaaSBruno Thomsen if (ret) { 7150e735eaaSBruno Thomsen dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); 7160e735eaaSBruno Thomsen return ret; 7170e735eaaSBruno Thomsen } 7180e735eaaSBruno Thomsen 7195d78533aSUwe Kleine-König pcf2127_watchdog_init(dev, pcf2127); 7200e735eaaSBruno Thomsen 72103623b4bSBruno Thomsen /* 72203623b4bSBruno Thomsen * Disable battery low/switch-over timestamp and interrupts. 72303623b4bSBruno Thomsen * Clear battery interrupt flags which can block new trigger events. 72403623b4bSBruno Thomsen * Note: This is the default chip behaviour but added to ensure 72503623b4bSBruno Thomsen * correct tamper timestamp and interrupt function. 72603623b4bSBruno Thomsen */ 72703623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 72803623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BTSE | 72903623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BIE | 73003623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BLIE, 0); 73103623b4bSBruno Thomsen if (ret) { 73203623b4bSBruno Thomsen dev_err(dev, "%s: interrupt config (ctrl3) failed\n", 73303623b4bSBruno Thomsen __func__); 73403623b4bSBruno Thomsen return ret; 73503623b4bSBruno Thomsen } 73603623b4bSBruno Thomsen 73703623b4bSBruno Thomsen /* 73803623b4bSBruno Thomsen * Enable timestamp function and store timestamp of first trigger 7397b69b54aSHugo Villeneuve * event until TSF1 and TSF2 interrupt flags are cleared. 74003623b4bSBruno Thomsen */ 74103623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL, 74203623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSOFF | 74303623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM, 74403623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM); 74503623b4bSBruno Thomsen if (ret) { 74603623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n", 74703623b4bSBruno Thomsen __func__); 74803623b4bSBruno Thomsen return ret; 74903623b4bSBruno Thomsen } 75003623b4bSBruno Thomsen 75103623b4bSBruno Thomsen /* 75203623b4bSBruno Thomsen * Enable interrupt generation when TSF1 or TSF2 timestamp flags 75303623b4bSBruno Thomsen * are set. Interrupt signal is an open-drain output and can be 75403623b4bSBruno Thomsen * left floating if unused. 75503623b4bSBruno Thomsen */ 75603623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 75703623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE, 75803623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE); 75903623b4bSBruno Thomsen if (ret) { 76003623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ctrl2) failed\n", 76103623b4bSBruno Thomsen __func__); 76203623b4bSBruno Thomsen return ret; 76303623b4bSBruno Thomsen } 76403623b4bSBruno Thomsen 76503623b4bSBruno Thomsen ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group); 76603623b4bSBruno Thomsen if (ret) { 76703623b4bSBruno Thomsen dev_err(dev, "%s: tamper sysfs registering failed\n", 76803623b4bSBruno Thomsen __func__); 76903623b4bSBruno Thomsen return ret; 77003623b4bSBruno Thomsen } 77103623b4bSBruno Thomsen 772fdcfd854SBartosz Golaszewski return devm_rtc_register_device(pcf2127->rtc); 77318cb6368SRenaud Cerrato } 77418cb6368SRenaud Cerrato 77518cb6368SRenaud Cerrato #ifdef CONFIG_OF 77618cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 77718cb6368SRenaud Cerrato { .compatible = "nxp,pcf2127" }, 778cee2cc21SAkinobu Mita { .compatible = "nxp,pcf2129" }, 779985b30dbSLiam Beguin { .compatible = "nxp,pca2129" }, 78018cb6368SRenaud Cerrato {} 78118cb6368SRenaud Cerrato }; 78218cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 78318cb6368SRenaud Cerrato #endif 78418cb6368SRenaud Cerrato 7859408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 7869408ec1aSAkinobu Mita 787907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 788907b3262SAkinobu Mita { 789907b3262SAkinobu Mita struct device *dev = context; 790907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 791907b3262SAkinobu Mita int ret; 792907b3262SAkinobu Mita 793907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 794907b3262SAkinobu Mita if (ret != count) 795907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 796907b3262SAkinobu Mita 797907b3262SAkinobu Mita return 0; 798907b3262SAkinobu Mita } 799907b3262SAkinobu Mita 800907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 801907b3262SAkinobu Mita const void *reg, size_t reg_size, 802907b3262SAkinobu Mita const void *val, size_t val_size) 803907b3262SAkinobu Mita { 804907b3262SAkinobu Mita struct device *dev = context; 805907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 806907b3262SAkinobu Mita int ret; 807907b3262SAkinobu Mita void *buf; 808907b3262SAkinobu Mita 809907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 810907b3262SAkinobu Mita return -EINVAL; 811907b3262SAkinobu Mita 812907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 813907b3262SAkinobu Mita if (!buf) 814907b3262SAkinobu Mita return -ENOMEM; 815907b3262SAkinobu Mita 816907b3262SAkinobu Mita memcpy(buf, reg, 1); 817907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 818907b3262SAkinobu Mita 819907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 8209bde0afbSXulin Sun 8219bde0afbSXulin Sun kfree(buf); 8229bde0afbSXulin Sun 823907b3262SAkinobu Mita if (ret != val_size + 1) 824907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 825907b3262SAkinobu Mita 826907b3262SAkinobu Mita return 0; 827907b3262SAkinobu Mita } 828907b3262SAkinobu Mita 829907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 830907b3262SAkinobu Mita void *val, size_t val_size) 831907b3262SAkinobu Mita { 832907b3262SAkinobu Mita struct device *dev = context; 833907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 834907b3262SAkinobu Mita int ret; 835907b3262SAkinobu Mita 836907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 837907b3262SAkinobu Mita return -EINVAL; 838907b3262SAkinobu Mita 839907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 840907b3262SAkinobu Mita if (ret != 1) 841907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 842907b3262SAkinobu Mita 843907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 844907b3262SAkinobu Mita if (ret != val_size) 845907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 846907b3262SAkinobu Mita 847907b3262SAkinobu Mita return 0; 848907b3262SAkinobu Mita } 849907b3262SAkinobu Mita 850907b3262SAkinobu Mita /* 851907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 852907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 853907b3262SAkinobu Mita * read register data when reading from registers. 854907b3262SAkinobu Mita */ 855907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 856907b3262SAkinobu Mita .write = pcf2127_i2c_write, 857907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 858907b3262SAkinobu Mita .read = pcf2127_i2c_read, 85918cb6368SRenaud Cerrato }; 86018cb6368SRenaud Cerrato 861907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 862907b3262SAkinobu Mita 8635418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = { 8645418e595SUwe Kleine-König { "pcf2127", 1 }, 8655418e595SUwe Kleine-König { "pcf2129", 0 }, 8665418e595SUwe Kleine-König { "pca2129", 0 }, 8675418e595SUwe Kleine-König { } 8685418e595SUwe Kleine-König }; 8695418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 8705418e595SUwe Kleine-König 8715418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client) 872907b3262SAkinobu Mita { 8735418e595SUwe Kleine-König const struct i2c_device_id *id = i2c_match_id(pcf2127_i2c_id, client); 874907b3262SAkinobu Mita struct regmap *regmap; 875907b3262SAkinobu Mita static const struct regmap_config config = { 876907b3262SAkinobu Mita .reg_bits = 8, 877907b3262SAkinobu Mita .val_bits = 8, 878040e6dc0SAlexandre Belloni .max_register = 0x1d, 879907b3262SAkinobu Mita }; 880907b3262SAkinobu Mita 881907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 882907b3262SAkinobu Mita return -ENODEV; 883907b3262SAkinobu Mita 884907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 885907b3262SAkinobu Mita &client->dev, &config); 886907b3262SAkinobu Mita if (IS_ERR(regmap)) { 887907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 888907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 889907b3262SAkinobu Mita return PTR_ERR(regmap); 890907b3262SAkinobu Mita } 891907b3262SAkinobu Mita 89227006416SAlexandre Belloni return pcf2127_probe(&client->dev, regmap, client->irq, 893d6c3029fSUwe Kleine-König pcf2127_i2c_driver.driver.name, id->driver_data); 894907b3262SAkinobu Mita } 895907b3262SAkinobu Mita 896907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 897907b3262SAkinobu Mita .driver = { 898907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 899907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 900907b3262SAkinobu Mita }, 90131b0cecbSUwe Kleine-König .probe = pcf2127_i2c_probe, 902907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 903907b3262SAkinobu Mita }; 9049408ec1aSAkinobu Mita 9059408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 9069408ec1aSAkinobu Mita { 9079408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 9089408ec1aSAkinobu Mita } 9099408ec1aSAkinobu Mita 9109408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 9119408ec1aSAkinobu Mita { 9129408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 9139408ec1aSAkinobu Mita } 9149408ec1aSAkinobu Mita 9159408ec1aSAkinobu Mita #else 9169408ec1aSAkinobu Mita 9179408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 9189408ec1aSAkinobu Mita { 9199408ec1aSAkinobu Mita return 0; 9209408ec1aSAkinobu Mita } 9219408ec1aSAkinobu Mita 9229408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 9239408ec1aSAkinobu Mita { 9249408ec1aSAkinobu Mita } 9259408ec1aSAkinobu Mita 9269408ec1aSAkinobu Mita #endif 9279408ec1aSAkinobu Mita 9289408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 9299408ec1aSAkinobu Mita 9309408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 9319408ec1aSAkinobu Mita 9329408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 9339408ec1aSAkinobu Mita { 9349408ec1aSAkinobu Mita static const struct regmap_config config = { 9359408ec1aSAkinobu Mita .reg_bits = 8, 9369408ec1aSAkinobu Mita .val_bits = 8, 9379408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 9389408ec1aSAkinobu Mita .write_flag_mask = 0x20, 939040e6dc0SAlexandre Belloni .max_register = 0x1d, 9409408ec1aSAkinobu Mita }; 9419408ec1aSAkinobu Mita struct regmap *regmap; 9429408ec1aSAkinobu Mita 9439408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 9449408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 9459408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 9469408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 9479408ec1aSAkinobu Mita return PTR_ERR(regmap); 9489408ec1aSAkinobu Mita } 9499408ec1aSAkinobu Mita 95027006416SAlexandre Belloni return pcf2127_probe(&spi->dev, regmap, spi->irq, 95127006416SAlexandre Belloni pcf2127_spi_driver.driver.name, 952d6c3029fSUwe Kleine-König spi_get_device_id(spi)->driver_data); 9539408ec1aSAkinobu Mita } 9549408ec1aSAkinobu Mita 9559408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 956d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 957cee2cc21SAkinobu Mita { "pcf2129", 0 }, 958985b30dbSLiam Beguin { "pca2129", 0 }, 9599408ec1aSAkinobu Mita { } 9609408ec1aSAkinobu Mita }; 9619408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 9629408ec1aSAkinobu Mita 9639408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 9649408ec1aSAkinobu Mita .driver = { 9659408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 9669408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 9679408ec1aSAkinobu Mita }, 9689408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 9699408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 9709408ec1aSAkinobu Mita }; 9719408ec1aSAkinobu Mita 9729408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 9739408ec1aSAkinobu Mita { 9749408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 9759408ec1aSAkinobu Mita } 9769408ec1aSAkinobu Mita 9779408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 9789408ec1aSAkinobu Mita { 9799408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 9809408ec1aSAkinobu Mita } 9819408ec1aSAkinobu Mita 9829408ec1aSAkinobu Mita #else 9839408ec1aSAkinobu Mita 9849408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 9859408ec1aSAkinobu Mita { 9869408ec1aSAkinobu Mita return 0; 9879408ec1aSAkinobu Mita } 9889408ec1aSAkinobu Mita 9899408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 9909408ec1aSAkinobu Mita { 9919408ec1aSAkinobu Mita } 9929408ec1aSAkinobu Mita 9939408ec1aSAkinobu Mita #endif 9949408ec1aSAkinobu Mita 9959408ec1aSAkinobu Mita static int __init pcf2127_init(void) 9969408ec1aSAkinobu Mita { 9979408ec1aSAkinobu Mita int ret; 9989408ec1aSAkinobu Mita 9999408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 10009408ec1aSAkinobu Mita if (ret) { 10019408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 10029408ec1aSAkinobu Mita return ret; 10039408ec1aSAkinobu Mita } 10049408ec1aSAkinobu Mita 10059408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 10069408ec1aSAkinobu Mita if (ret) { 10079408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 10089408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 10099408ec1aSAkinobu Mita } 10109408ec1aSAkinobu Mita 10119408ec1aSAkinobu Mita return ret; 10129408ec1aSAkinobu Mita } 10139408ec1aSAkinobu Mita module_init(pcf2127_init) 10149408ec1aSAkinobu Mita 10159408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 10169408ec1aSAkinobu Mita { 10179408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 10189408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 10199408ec1aSAkinobu Mita } 10209408ec1aSAkinobu Mita module_exit(pcf2127_exit) 102118cb6368SRenaud Cerrato 102218cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 1023cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); 10244d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 1025