1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 218cb6368SRenaud Cerrato /* 3afc505bfSHugo Villeneuve * An I2C and SPI driver for the NXP PCF2127/29/31 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 * 11afc505bfSHugo Villeneuve * PCF2131 support 12afc505bfSHugo Villeneuve * Author: Hugo Villeneuve <hvilleneuve@dimonoff.com> 13afc505bfSHugo Villeneuve * 1418cb6368SRenaud Cerrato * based on the other drivers in this same directory. 1518cb6368SRenaud Cerrato * 16afc505bfSHugo Villeneuve * Datasheets: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf 17afc505bfSHugo Villeneuve * https://www.nxp.com/docs/en/data-sheet/PCF2131DS.pdf 1818cb6368SRenaud Cerrato */ 1918cb6368SRenaud Cerrato 2018cb6368SRenaud Cerrato #include <linux/i2c.h> 219408ec1aSAkinobu Mita #include <linux/spi/spi.h> 2218cb6368SRenaud Cerrato #include <linux/bcd.h> 2318cb6368SRenaud Cerrato #include <linux/rtc.h> 2418cb6368SRenaud Cerrato #include <linux/slab.h> 2518cb6368SRenaud Cerrato #include <linux/module.h> 2618cb6368SRenaud Cerrato #include <linux/of.h> 278a914bacSLiam Beguin #include <linux/of_irq.h> 28fd28ceb4SHugo Villeneuve #include <linux/of_device.h> 29907b3262SAkinobu Mita #include <linux/regmap.h> 300e735eaaSBruno Thomsen #include <linux/watchdog.h> 3118cb6368SRenaud Cerrato 32bbfe3a7aSBruno Thomsen /* Control register 1 */ 33bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1 0x00 34b9ac079aSPhilipp Rosenberger #define PCF2127_BIT_CTRL1_POR_OVRD BIT(3) 3503623b4bSBruno Thomsen #define PCF2127_BIT_CTRL1_TSF1 BIT(4) 363d715ebaSHugo Villeneuve #define PCF2127_BIT_CTRL1_STOP BIT(5) 37bbfe3a7aSBruno Thomsen /* Control register 2 */ 38bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2 0x01 398a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AIE BIT(1) 4003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE BIT(2) 418a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AF BIT(4) 4203623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2 BIT(5) 4327006416SAlexandre Belloni #define PCF2127_BIT_CTRL2_WDTF BIT(6) 44bbfe3a7aSBruno Thomsen /* Control register 3 */ 45bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3 0x02 4603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE BIT(0) 4703623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE BIT(1) 48bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF BIT(2) 4903623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF BIT(3) 5003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE BIT(4) 51bbfe3a7aSBruno Thomsen /* Time and date registers */ 526211aceeSHugo Villeneuve #define PCF2127_REG_TIME_BASE 0x03 53bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF BIT(7) 548a914bacSLiam Beguin /* Alarm registers */ 557c6f0db4SHugo Villeneuve #define PCF2127_REG_ALARM_BASE 0x0A 5627006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE BIT(7) 5715f57b3eSPhilipp Rosenberger /* CLKOUT control register */ 5815f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT 0x0f 5915f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR BIT(5) 600e735eaaSBruno Thomsen /* Watchdog registers */ 610e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL 0x10 620e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0 BIT(0) 630e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1 BIT(1) 640e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0 BIT(6) 650e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1 BIT(7) 660e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL 0x11 67420cc9e8SHugo Villeneuve /* Tamper timestamp1 registers */ 68420cc9e8SHugo Villeneuve #define PCF2127_REG_TS1_BASE 0x12 6903623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) 7003623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM BIT(7) 71bbfe3a7aSBruno Thomsen /* 72bbfe3a7aSBruno Thomsen * RAM registers 73bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 74bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 75afc505bfSHugo Villeneuve * PCF2129/31 doesn't have this feature. 76bbfe3a7aSBruno Thomsen */ 77bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 78bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 79bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 80f97cfddcSUwe Kleine-König 810e735eaaSBruno Thomsen /* Watchdog timer value constants */ 820e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP 0 83adb9675dSHugo Villeneuve /* PCF2127/29 watchdog timer value constants */ 84adb9675dSHugo Villeneuve #define PCF2127_WD_CLOCK_HZ_X1000 1000 /* 1Hz */ 85adb9675dSHugo Villeneuve #define PCF2127_WD_MIN_HW_HEARTBEAT_MS 500 86adb9675dSHugo Villeneuve /* PCF2131 watchdog timer value constants */ 87adb9675dSHugo Villeneuve #define PCF2131_WD_CLOCK_HZ_X1000 250 /* 1/4Hz */ 88adb9675dSHugo Villeneuve #define PCF2131_WD_MIN_HW_HEARTBEAT_MS 4000 89adb9675dSHugo Villeneuve 90adb9675dSHugo Villeneuve #define PCF2127_WD_DEFAULT_TIMEOUT_S 60 91653ebd75SAndrea Scian 922f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */ 932f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1) 942f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \ 952f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_AF | \ 962f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_WDTF | \ 972f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_TSF2) 982f861984SMian Yousaf Kaukab 99afc505bfSHugo Villeneuve #define PCF2127_MAX_TS_SUPPORTED 4 100afc505bfSHugo Villeneuve 101afc505bfSHugo Villeneuve /* Control register 4 */ 102afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL4 0x03 103afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF4 BIT(4) 104afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF3 BIT(5) 105afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF2 BIT(6) 106afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF1 BIT(7) 107afc505bfSHugo Villeneuve /* Control register 5 */ 108afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL5 0x04 109afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE4 BIT(4) 110afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE3 BIT(5) 111afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE2 BIT(6) 112afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE1 BIT(7) 113afc505bfSHugo Villeneuve /* Software reset register */ 114afc505bfSHugo Villeneuve #define PCF2131_REG_SR_RESET 0x05 115afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_READ_PATTERN (BIT(2) | BIT(5)) 116afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_CPR_CMD (PCF2131_SR_RESET_READ_PATTERN | BIT(7)) 117afc505bfSHugo Villeneuve /* Time and date registers */ 118afc505bfSHugo Villeneuve #define PCF2131_REG_TIME_BASE 0x07 119afc505bfSHugo Villeneuve /* Alarm registers */ 120afc505bfSHugo Villeneuve #define PCF2131_REG_ALARM_BASE 0x0E 121afc505bfSHugo Villeneuve /* CLKOUT control register */ 122afc505bfSHugo Villeneuve #define PCF2131_REG_CLKOUT 0x13 123afc505bfSHugo Villeneuve /* Watchdog registers */ 124afc505bfSHugo Villeneuve #define PCF2131_REG_WD_CTL 0x35 125afc505bfSHugo Villeneuve #define PCF2131_REG_WD_VAL 0x36 126afc505bfSHugo Villeneuve /* Tamper timestamp1 registers */ 127afc505bfSHugo Villeneuve #define PCF2131_REG_TS1_BASE 0x14 128afc505bfSHugo Villeneuve /* Tamper timestamp2 registers */ 129afc505bfSHugo Villeneuve #define PCF2131_REG_TS2_BASE 0x1B 130afc505bfSHugo Villeneuve /* Tamper timestamp3 registers */ 131afc505bfSHugo Villeneuve #define PCF2131_REG_TS3_BASE 0x22 132afc505bfSHugo Villeneuve /* Tamper timestamp4 registers */ 133afc505bfSHugo Villeneuve #define PCF2131_REG_TS4_BASE 0x29 134afc505bfSHugo Villeneuve /* Interrupt mask registers */ 135afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK1 0x31 136afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK2 0x32 137afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK1 0x33 138afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK2 0x34 139afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BLIE BIT(0) 140afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BIE BIT(1) 141afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_AIE BIT(2) 142afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_WD_CD BIT(3) 143afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_SI BIT(4) 144afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_MI BIT(5) 145afc505bfSHugo Villeneuve #define PCF2131_CTRL2_IRQ_MASK ( \ 146afc505bfSHugo Villeneuve PCF2127_BIT_CTRL2_AF | \ 147afc505bfSHugo Villeneuve PCF2127_BIT_CTRL2_WDTF) 148afc505bfSHugo Villeneuve #define PCF2131_CTRL4_IRQ_MASK ( \ 149afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF4 | \ 150afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF3 | \ 151afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF2 | \ 152afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF1) 153420cc9e8SHugo Villeneuve 154fd28ceb4SHugo Villeneuve enum pcf21xx_type { 155fd28ceb4SHugo Villeneuve PCF2127, 156fd28ceb4SHugo Villeneuve PCF2129, 157afc505bfSHugo Villeneuve PCF2131, 158fd28ceb4SHugo Villeneuve PCF21XX_LAST_ID 159fd28ceb4SHugo Villeneuve }; 160fd28ceb4SHugo Villeneuve 161420cc9e8SHugo Villeneuve struct pcf21xx_ts_config { 162420cc9e8SHugo Villeneuve u8 reg_base; /* Base register to read timestamp values. */ 163420cc9e8SHugo Villeneuve 164420cc9e8SHugo Villeneuve /* 165420cc9e8SHugo Villeneuve * If the TS input pin is driven to GND, an interrupt can be generated 166420cc9e8SHugo Villeneuve * (supported by all variants). 167420cc9e8SHugo Villeneuve */ 168420cc9e8SHugo Villeneuve u8 gnd_detect_reg; /* Interrupt control register address. */ 169420cc9e8SHugo Villeneuve u8 gnd_detect_bit; /* Interrupt bit. */ 170420cc9e8SHugo Villeneuve 171420cc9e8SHugo Villeneuve /* 172420cc9e8SHugo Villeneuve * If the TS input pin is driven to an intermediate level between GND 173420cc9e8SHugo Villeneuve * and supply, an interrupt can be generated (optional feature depending 174420cc9e8SHugo Villeneuve * on variant). 175420cc9e8SHugo Villeneuve */ 176420cc9e8SHugo Villeneuve u8 inter_detect_reg; /* Interrupt control register address. */ 177420cc9e8SHugo Villeneuve u8 inter_detect_bit; /* Interrupt bit. */ 178420cc9e8SHugo Villeneuve 179420cc9e8SHugo Villeneuve u8 ie_reg; /* Interrupt enable control register. */ 180420cc9e8SHugo Villeneuve u8 ie_bit; /* Interrupt enable bit. */ 181420cc9e8SHugo Villeneuve }; 182420cc9e8SHugo Villeneuve 183fd28ceb4SHugo Villeneuve struct pcf21xx_config { 184fd28ceb4SHugo Villeneuve int type; /* IC variant */ 185fd28ceb4SHugo Villeneuve int max_register; 186fd28ceb4SHugo Villeneuve unsigned int has_nvmem:1; 187fd28ceb4SHugo Villeneuve unsigned int has_bit_wd_ctl_cd0:1; 188081602a1SHugo Villeneuve unsigned int wd_val_reg_readable:1; /* If watchdog value register can be read. */ 189e1849b8fSHugo Villeneuve unsigned int has_int_a_b:1; /* PCF2131 supports two interrupt outputs. */ 1906211aceeSHugo Villeneuve u8 reg_time_base; /* Time/date base register. */ 1917c6f0db4SHugo Villeneuve u8 regs_alarm_base; /* Alarm function base registers. */ 1926b57ec29SHugo Villeneuve u8 reg_wd_ctl; /* Watchdog control register. */ 1936b57ec29SHugo Villeneuve u8 reg_wd_val; /* Watchdog value register. */ 194fc16599eSHugo Villeneuve u8 reg_clkout; /* Clkout register. */ 195adb9675dSHugo Villeneuve int wdd_clock_hz_x1000; /* Watchdog clock in Hz multiplicated by 1000 */ 196adb9675dSHugo Villeneuve int wdd_min_hw_heartbeat_ms; 197420cc9e8SHugo Villeneuve unsigned int ts_count; 198420cc9e8SHugo Villeneuve struct pcf21xx_ts_config ts[PCF2127_MAX_TS_SUPPORTED]; 199420cc9e8SHugo Villeneuve struct attribute_group attribute_group; 200fd28ceb4SHugo Villeneuve }; 201fd28ceb4SHugo Villeneuve 20218cb6368SRenaud Cerrato struct pcf2127 { 20318cb6368SRenaud Cerrato struct rtc_device *rtc; 2040e735eaaSBruno Thomsen struct watchdog_device wdd; 205907b3262SAkinobu Mita struct regmap *regmap; 206fd28ceb4SHugo Villeneuve const struct pcf21xx_config *cfg; 2072f861984SMian Yousaf Kaukab bool irq_enabled; 208420cc9e8SHugo Villeneuve time64_t ts[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp values. */ 209420cc9e8SHugo Villeneuve bool ts_valid[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp valid indication. */ 21018cb6368SRenaud Cerrato }; 21118cb6368SRenaud Cerrato 21218cb6368SRenaud Cerrato /* 21318cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 21418cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 21518cb6368SRenaud Cerrato */ 216907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 21718cb6368SRenaud Cerrato { 218907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 21931f077c3SHugo Villeneuve unsigned char buf[7]; 220907b3262SAkinobu Mita int ret; 22118cb6368SRenaud Cerrato 2227f43020eSBruno Thomsen /* 2237f43020eSBruno Thomsen * Avoid reading CTRL2 register as it causes WD_VAL register 2247f43020eSBruno Thomsen * value to reset to 0 which means watchdog is stopped. 2257f43020eSBruno Thomsen */ 2266211aceeSHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->reg_time_base, 2276211aceeSHugo Villeneuve buf, sizeof(buf)); 228907b3262SAkinobu Mita if (ret) { 229907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 230907b3262SAkinobu Mita return ret; 23118cb6368SRenaud Cerrato } 23218cb6368SRenaud Cerrato 233bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 23431f077c3SHugo Villeneuve if (buf[0] & PCF2127_BIT_SC_OSF) { 235653ebd75SAndrea Scian /* 236653ebd75SAndrea Scian * no need clear the flag here, 237653ebd75SAndrea Scian * it will be cleared once the new date is saved 238653ebd75SAndrea Scian */ 239907b3262SAkinobu Mita dev_warn(dev, 240653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 241653ebd75SAndrea Scian return -EINVAL; 24218cb6368SRenaud Cerrato } 24318cb6368SRenaud Cerrato 244907b3262SAkinobu Mita dev_dbg(dev, 24531f077c3SHugo Villeneuve "%s: raw data is sec=%02x, min=%02x, hr=%02x, " 24618cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 24731f077c3SHugo Villeneuve __func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); 24818cb6368SRenaud Cerrato 24931f077c3SHugo Villeneuve tm->tm_sec = bcd2bin(buf[0] & 0x7F); 25031f077c3SHugo Villeneuve tm->tm_min = bcd2bin(buf[1] & 0x7F); 2510476b6c8SHugo Villeneuve tm->tm_hour = bcd2bin(buf[2] & 0x3F); 25231f077c3SHugo Villeneuve tm->tm_mday = bcd2bin(buf[3] & 0x3F); 25331f077c3SHugo Villeneuve tm->tm_wday = buf[4] & 0x07; 2540476b6c8SHugo Villeneuve tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; 25531f077c3SHugo Villeneuve tm->tm_year = bcd2bin(buf[6]); 256b139bb5cSAlexandre Belloni tm->tm_year += 100; 25718cb6368SRenaud Cerrato 258907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 25918cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 26018cb6368SRenaud Cerrato __func__, 26118cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 26218cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 26318cb6368SRenaud Cerrato 26422652ba7SAlexandre Belloni return 0; 26518cb6368SRenaud Cerrato } 26618cb6368SRenaud Cerrato 267907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 26818cb6368SRenaud Cerrato { 269907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 270907b3262SAkinobu Mita unsigned char buf[7]; 27118cb6368SRenaud Cerrato int i = 0, err; 27218cb6368SRenaud Cerrato 273907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 27418cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 27518cb6368SRenaud Cerrato __func__, 27618cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 27718cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 27818cb6368SRenaud Cerrato 27918cb6368SRenaud Cerrato /* hours, minutes and seconds */ 280653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 28118cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 28218cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 28318cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 28418cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 28518cb6368SRenaud Cerrato 28618cb6368SRenaud Cerrato /* month, 1 - 12 */ 28718cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 28818cb6368SRenaud Cerrato 28918cb6368SRenaud Cerrato /* year */ 290b139bb5cSAlexandre Belloni buf[i++] = bin2bcd(tm->tm_year - 100); 29118cb6368SRenaud Cerrato 2923d715ebaSHugo Villeneuve /* Write access to time registers: 2933d715ebaSHugo Villeneuve * PCF2127/29: no special action required. 2943d715ebaSHugo Villeneuve * PCF2131: requires setting the STOP and CPR bits. STOP bit needs to 2953d715ebaSHugo Villeneuve * be cleared after time registers are updated. 2963d715ebaSHugo Villeneuve */ 2973d715ebaSHugo Villeneuve if (pcf2127->cfg->type == PCF2131) { 2983d715ebaSHugo Villeneuve err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 2993d715ebaSHugo Villeneuve PCF2127_BIT_CTRL1_STOP, 3003d715ebaSHugo Villeneuve PCF2127_BIT_CTRL1_STOP); 3013d715ebaSHugo Villeneuve if (err) { 3023d715ebaSHugo Villeneuve dev_dbg(dev, "setting STOP bit failed\n"); 3033d715ebaSHugo Villeneuve return err; 3043d715ebaSHugo Villeneuve } 3053d715ebaSHugo Villeneuve 3063d715ebaSHugo Villeneuve err = regmap_write(pcf2127->regmap, PCF2131_REG_SR_RESET, 3073d715ebaSHugo Villeneuve PCF2131_SR_RESET_CPR_CMD); 3083d715ebaSHugo Villeneuve if (err) { 3093d715ebaSHugo Villeneuve dev_dbg(dev, "sending CPR cmd failed\n"); 3103d715ebaSHugo Villeneuve return err; 3113d715ebaSHugo Villeneuve } 3123d715ebaSHugo Villeneuve } 3133d715ebaSHugo Villeneuve 3143d715ebaSHugo Villeneuve /* write time register's data */ 3156211aceeSHugo Villeneuve err = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->reg_time_base, buf, i); 316907b3262SAkinobu Mita if (err) { 3173d740c64SHugo Villeneuve dev_dbg(dev, "%s: err=%d", __func__, err); 318907b3262SAkinobu Mita return err; 31918cb6368SRenaud Cerrato } 32018cb6368SRenaud Cerrato 3213d715ebaSHugo Villeneuve if (pcf2127->cfg->type == PCF2131) { 3223d715ebaSHugo Villeneuve /* Clear STOP bit (PCF2131 only) after write is completed. */ 3233d715ebaSHugo Villeneuve err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 3243d715ebaSHugo Villeneuve PCF2127_BIT_CTRL1_STOP, 0); 3253d715ebaSHugo Villeneuve if (err) { 3263d715ebaSHugo Villeneuve dev_dbg(dev, "clearing STOP bit failed\n"); 3273d715ebaSHugo Villeneuve return err; 3283d715ebaSHugo Villeneuve } 3293d715ebaSHugo Villeneuve } 3303d715ebaSHugo Villeneuve 33118cb6368SRenaud Cerrato return 0; 33218cb6368SRenaud Cerrato } 33318cb6368SRenaud Cerrato 33418cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 33518cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 33618cb6368SRenaud Cerrato { 337907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 3387d65cf8cSAlexandre Belloni int val, touser = 0; 339f97cfddcSUwe Kleine-König int ret; 34018cb6368SRenaud Cerrato 34118cb6368SRenaud Cerrato switch (cmd) { 34218cb6368SRenaud Cerrato case RTC_VL_READ: 3437d65cf8cSAlexandre Belloni ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); 344907b3262SAkinobu Mita if (ret) 345f97cfddcSUwe Kleine-König return ret; 34618cb6368SRenaud Cerrato 3477d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BLF) 3487d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_LOW; 3497d65cf8cSAlexandre Belloni 3507d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BF) 3517d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_SWITCH; 352f97cfddcSUwe Kleine-König 353af427311SAlexandre Belloni return put_user(touser, (unsigned int __user *)arg); 3547d65cf8cSAlexandre Belloni 3557d65cf8cSAlexandre Belloni case RTC_VL_CLR: 3567d65cf8cSAlexandre Belloni return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 3577d65cf8cSAlexandre Belloni PCF2127_BIT_CTRL3_BF, 0); 3587d65cf8cSAlexandre Belloni 35918cb6368SRenaud Cerrato default: 36018cb6368SRenaud Cerrato return -ENOIOCTLCMD; 36118cb6368SRenaud Cerrato } 36218cb6368SRenaud Cerrato } 36318cb6368SRenaud Cerrato 364d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 365d6c3029fSUwe Kleine-König void *val, size_t bytes) 366d6c3029fSUwe Kleine-König { 367d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 368d6c3029fSUwe Kleine-König int ret; 369d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 370d6c3029fSUwe Kleine-König 371bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 372d6c3029fSUwe Kleine-König offsetbuf, 2); 373d6c3029fSUwe Kleine-König if (ret) 374d6c3029fSUwe Kleine-König return ret; 375d6c3029fSUwe Kleine-König 376ba1c30bfSDan Carpenter return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 377d6c3029fSUwe Kleine-König val, bytes); 378d6c3029fSUwe Kleine-König } 379d6c3029fSUwe Kleine-König 380d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 381d6c3029fSUwe Kleine-König void *val, size_t bytes) 382d6c3029fSUwe Kleine-König { 383d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 384d6c3029fSUwe Kleine-König int ret; 385d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 386d6c3029fSUwe Kleine-König 387bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 388d6c3029fSUwe Kleine-König offsetbuf, 2); 389d6c3029fSUwe Kleine-König if (ret) 390d6c3029fSUwe Kleine-König return ret; 391d6c3029fSUwe Kleine-König 392ba1c30bfSDan Carpenter return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 393d6c3029fSUwe Kleine-König val, bytes); 394d6c3029fSUwe Kleine-König } 395d6c3029fSUwe Kleine-König 3960e735eaaSBruno Thomsen /* watchdog driver */ 3970e735eaaSBruno Thomsen 3980e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd) 3990e735eaaSBruno Thomsen { 400adb9675dSHugo Villeneuve int wd_val; 4010e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 4020e735eaaSBruno Thomsen 403adb9675dSHugo Villeneuve /* 404adb9675dSHugo Villeneuve * Compute counter value of WATCHDG_TIM_VAL to obtain desired period 405adb9675dSHugo Villeneuve * in seconds, depending on the source clock frequency. 406adb9675dSHugo Villeneuve */ 407adb9675dSHugo Villeneuve wd_val = ((wdd->timeout * pcf2127->cfg->wdd_clock_hz_x1000) / 1000) + 1; 408adb9675dSHugo Villeneuve 409adb9675dSHugo Villeneuve return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, wd_val); 4100e735eaaSBruno Thomsen } 4110e735eaaSBruno Thomsen 4120e735eaaSBruno Thomsen /* 4130e735eaaSBruno Thomsen * Restart watchdog timer if feature is active. 4140e735eaaSBruno Thomsen * 4150e735eaaSBruno Thomsen * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, 4160e735eaaSBruno Thomsen * since register also contain control/status flags for other features. 4170e735eaaSBruno Thomsen * Always call this function after reading CTRL2 register. 4180e735eaaSBruno Thomsen */ 4190e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) 4200e735eaaSBruno Thomsen { 4210e735eaaSBruno Thomsen int ret = 0; 4220e735eaaSBruno Thomsen 4230e735eaaSBruno Thomsen if (watchdog_active(wdd)) { 4240e735eaaSBruno Thomsen ret = pcf2127_wdt_ping(wdd); 4250e735eaaSBruno Thomsen if (ret) 4260e735eaaSBruno Thomsen dev_err(wdd->parent, 4270e735eaaSBruno Thomsen "%s: watchdog restart failed, ret=%d\n", 4280e735eaaSBruno Thomsen __func__, ret); 4290e735eaaSBruno Thomsen } 4300e735eaaSBruno Thomsen 4310e735eaaSBruno Thomsen return ret; 4320e735eaaSBruno Thomsen } 4330e735eaaSBruno Thomsen 4340e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd) 4350e735eaaSBruno Thomsen { 4360e735eaaSBruno Thomsen return pcf2127_wdt_ping(wdd); 4370e735eaaSBruno Thomsen } 4380e735eaaSBruno Thomsen 4390e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd) 4400e735eaaSBruno Thomsen { 4410e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 4420e735eaaSBruno Thomsen 4436b57ec29SHugo Villeneuve return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, 4440e735eaaSBruno Thomsen PCF2127_WD_VAL_STOP); 4450e735eaaSBruno Thomsen } 4460e735eaaSBruno Thomsen 4470e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, 4480e735eaaSBruno Thomsen unsigned int new_timeout) 4490e735eaaSBruno Thomsen { 4500e735eaaSBruno Thomsen dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", 4510e735eaaSBruno Thomsen new_timeout, wdd->timeout); 4520e735eaaSBruno Thomsen 4530e735eaaSBruno Thomsen wdd->timeout = new_timeout; 4540e735eaaSBruno Thomsen 4550e735eaaSBruno Thomsen return pcf2127_wdt_active_ping(wdd); 4560e735eaaSBruno Thomsen } 4570e735eaaSBruno Thomsen 4580e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = { 4590e735eaaSBruno Thomsen .identity = "NXP PCF2127/PCF2129 Watchdog", 4600e735eaaSBruno Thomsen .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, 4610e735eaaSBruno Thomsen }; 4620e735eaaSBruno Thomsen 4630e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = { 4640e735eaaSBruno Thomsen .owner = THIS_MODULE, 4650e735eaaSBruno Thomsen .start = pcf2127_wdt_start, 4660e735eaaSBruno Thomsen .stop = pcf2127_wdt_stop, 4670e735eaaSBruno Thomsen .ping = pcf2127_wdt_ping, 4680e735eaaSBruno Thomsen .set_timeout = pcf2127_wdt_set_timeout, 4690e735eaaSBruno Thomsen }; 4700e735eaaSBruno Thomsen 471adb9675dSHugo Villeneuve /* 472adb9675dSHugo Villeneuve * Compute watchdog period, t, in seconds, from the WATCHDG_TIM_VAL register 473adb9675dSHugo Villeneuve * value, n, and the clock frequency, f1000, in Hz x 1000. 474adb9675dSHugo Villeneuve * 475adb9675dSHugo Villeneuve * The PCF2127/29 datasheet gives t as: 476adb9675dSHugo Villeneuve * t = n / f 477adb9675dSHugo Villeneuve * The PCF2131 datasheet gives t as: 478adb9675dSHugo Villeneuve * t = (n - 1) / f 479adb9675dSHugo Villeneuve * For both variants, the watchdog is triggered when the WATCHDG_TIM_VAL reaches 480adb9675dSHugo Villeneuve * the value 1, and not zero. Consequently, the equation from the PCF2131 481adb9675dSHugo Villeneuve * datasheet seems to be the correct one for both variants. 482adb9675dSHugo Villeneuve */ 483adb9675dSHugo Villeneuve static int pcf2127_watchdog_get_period(int n, int f1000) 484adb9675dSHugo Villeneuve { 485adb9675dSHugo Villeneuve return (1000 * (n - 1)) / f1000; 486adb9675dSHugo Villeneuve } 487adb9675dSHugo Villeneuve 4885d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127) 4895d78533aSUwe Kleine-König { 4905d78533aSUwe Kleine-König int ret; 4915d78533aSUwe Kleine-König 49271ac1345SUwe Kleine-König if (!IS_ENABLED(CONFIG_WATCHDOG) || 49371ac1345SUwe Kleine-König !device_property_read_bool(dev, "reset-source")) 4945d78533aSUwe Kleine-König return 0; 4955d78533aSUwe Kleine-König 4965d78533aSUwe Kleine-König pcf2127->wdd.parent = dev; 4975d78533aSUwe Kleine-König pcf2127->wdd.info = &pcf2127_wdt_info; 4985d78533aSUwe Kleine-König pcf2127->wdd.ops = &pcf2127_watchdog_ops; 499adb9675dSHugo Villeneuve 500adb9675dSHugo Villeneuve pcf2127->wdd.min_timeout = 501adb9675dSHugo Villeneuve pcf2127_watchdog_get_period( 502adb9675dSHugo Villeneuve 2, pcf2127->cfg->wdd_clock_hz_x1000); 503adb9675dSHugo Villeneuve pcf2127->wdd.max_timeout = 504adb9675dSHugo Villeneuve pcf2127_watchdog_get_period( 505adb9675dSHugo Villeneuve 255, pcf2127->cfg->wdd_clock_hz_x1000); 506adb9675dSHugo Villeneuve pcf2127->wdd.timeout = PCF2127_WD_DEFAULT_TIMEOUT_S; 507adb9675dSHugo Villeneuve 508adb9675dSHugo Villeneuve dev_dbg(dev, "%s clock = %d Hz / 1000\n", __func__, 509adb9675dSHugo Villeneuve pcf2127->cfg->wdd_clock_hz_x1000); 510adb9675dSHugo Villeneuve 511adb9675dSHugo Villeneuve pcf2127->wdd.min_hw_heartbeat_ms = pcf2127->cfg->wdd_min_hw_heartbeat_ms; 5125d78533aSUwe Kleine-König pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS; 5135d78533aSUwe Kleine-König 5145d78533aSUwe Kleine-König watchdog_set_drvdata(&pcf2127->wdd, pcf2127); 5155d78533aSUwe Kleine-König 5165d78533aSUwe Kleine-König /* Test if watchdog timer is started by bootloader */ 517081602a1SHugo Villeneuve if (pcf2127->cfg->wd_val_reg_readable) { 518081602a1SHugo Villeneuve u32 wdd_timeout; 519081602a1SHugo Villeneuve 520081602a1SHugo Villeneuve ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_wd_val, 521081602a1SHugo Villeneuve &wdd_timeout); 5225d78533aSUwe Kleine-König if (ret) 5235d78533aSUwe Kleine-König return ret; 5245d78533aSUwe Kleine-König 5255d78533aSUwe Kleine-König if (wdd_timeout) 5265d78533aSUwe Kleine-König set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); 527081602a1SHugo Villeneuve } 5285d78533aSUwe Kleine-König 5295d78533aSUwe Kleine-König return devm_watchdog_register_device(dev, &pcf2127->wdd); 5305d78533aSUwe Kleine-König } 5315d78533aSUwe Kleine-König 5328a914bacSLiam Beguin /* Alarm */ 5338a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 5348a914bacSLiam Beguin { 5358a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 53673ce0530SHugo Villeneuve u8 buf[5]; 53773ce0530SHugo Villeneuve unsigned int ctrl2; 5388a914bacSLiam Beguin int ret; 5398a914bacSLiam Beguin 5408a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 5418a914bacSLiam Beguin if (ret) 5428a914bacSLiam Beguin return ret; 5438a914bacSLiam Beguin 5448a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 5458a914bacSLiam Beguin if (ret) 5468a914bacSLiam Beguin return ret; 5478a914bacSLiam Beguin 5487c6f0db4SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, 5497c6f0db4SHugo Villeneuve buf, sizeof(buf)); 5508a914bacSLiam Beguin if (ret) 5518a914bacSLiam Beguin return ret; 5528a914bacSLiam Beguin 5538a914bacSLiam Beguin alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE; 5548a914bacSLiam Beguin alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF; 5558a914bacSLiam Beguin 5568a914bacSLiam Beguin alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F); 5578a914bacSLiam Beguin alrm->time.tm_min = bcd2bin(buf[1] & 0x7F); 5588a914bacSLiam Beguin alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F); 5598a914bacSLiam Beguin alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F); 5608a914bacSLiam Beguin 5618a914bacSLiam Beguin return 0; 5628a914bacSLiam Beguin } 5638a914bacSLiam Beguin 5648a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable) 5658a914bacSLiam Beguin { 5668a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 5678a914bacSLiam Beguin int ret; 5688a914bacSLiam Beguin 5698a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 5708a914bacSLiam Beguin PCF2127_BIT_CTRL2_AIE, 5718a914bacSLiam Beguin enable ? PCF2127_BIT_CTRL2_AIE : 0); 5728a914bacSLiam Beguin if (ret) 5738a914bacSLiam Beguin return ret; 5748a914bacSLiam Beguin 5758a914bacSLiam Beguin return pcf2127_wdt_active_ping(&pcf2127->wdd); 5768a914bacSLiam Beguin } 5778a914bacSLiam Beguin 5788a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 5798a914bacSLiam Beguin { 5808a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 5818a914bacSLiam Beguin uint8_t buf[5]; 5828a914bacSLiam Beguin int ret; 5838a914bacSLiam Beguin 5848a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 5858a914bacSLiam Beguin PCF2127_BIT_CTRL2_AF, 0); 5868a914bacSLiam Beguin if (ret) 5878a914bacSLiam Beguin return ret; 5888a914bacSLiam Beguin 5898a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 5908a914bacSLiam Beguin if (ret) 5918a914bacSLiam Beguin return ret; 5928a914bacSLiam Beguin 5938a914bacSLiam Beguin buf[0] = bin2bcd(alrm->time.tm_sec); 5948a914bacSLiam Beguin buf[1] = bin2bcd(alrm->time.tm_min); 5958a914bacSLiam Beguin buf[2] = bin2bcd(alrm->time.tm_hour); 5968a914bacSLiam Beguin buf[3] = bin2bcd(alrm->time.tm_mday); 59727006416SAlexandre Belloni buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */ 5988a914bacSLiam Beguin 5997c6f0db4SHugo Villeneuve ret = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, 6007c6f0db4SHugo Villeneuve buf, sizeof(buf)); 6018a914bacSLiam Beguin if (ret) 6028a914bacSLiam Beguin return ret; 6038a914bacSLiam Beguin 6048a914bacSLiam Beguin return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled); 6058a914bacSLiam Beguin } 6068a914bacSLiam Beguin 6072f861984SMian Yousaf Kaukab /* 608420cc9e8SHugo Villeneuve * This function reads one timestamp function data, caller is responsible for 609420cc9e8SHugo Villeneuve * calling pcf2127_wdt_active_ping() 6102f861984SMian Yousaf Kaukab */ 611420cc9e8SHugo Villeneuve static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts, 612420cc9e8SHugo Villeneuve int ts_id) 6132f861984SMian Yousaf Kaukab { 6142f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 6152f861984SMian Yousaf Kaukab struct rtc_time tm; 6162f861984SMian Yousaf Kaukab int ret; 617720fb4b8SHugo Villeneuve unsigned char data[7]; 6182f861984SMian Yousaf Kaukab 619420cc9e8SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->ts[ts_id].reg_base, 620420cc9e8SHugo Villeneuve data, sizeof(data)); 6212f861984SMian Yousaf Kaukab if (ret) { 6222f861984SMian Yousaf Kaukab dev_err(dev, "%s: read error ret=%d\n", __func__, ret); 6232f861984SMian Yousaf Kaukab return ret; 6242f861984SMian Yousaf Kaukab } 6252f861984SMian Yousaf Kaukab 6262f861984SMian Yousaf Kaukab dev_dbg(dev, 627720fb4b8SHugo Villeneuve "%s: raw data is ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", 628720fb4b8SHugo Villeneuve __func__, data[1], data[2], data[3], data[4], data[5], data[6]); 6292f861984SMian Yousaf Kaukab 630720fb4b8SHugo Villeneuve tm.tm_sec = bcd2bin(data[1] & 0x7F); 631720fb4b8SHugo Villeneuve tm.tm_min = bcd2bin(data[2] & 0x7F); 632720fb4b8SHugo Villeneuve tm.tm_hour = bcd2bin(data[3] & 0x3F); 633720fb4b8SHugo Villeneuve tm.tm_mday = bcd2bin(data[4] & 0x3F); 6342f861984SMian Yousaf Kaukab /* TS_MO register (month) value range: 1-12 */ 635720fb4b8SHugo Villeneuve tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1; 636720fb4b8SHugo Villeneuve tm.tm_year = bcd2bin(data[6]); 6372f861984SMian Yousaf Kaukab if (tm.tm_year < 70) 6382f861984SMian Yousaf Kaukab tm.tm_year += 100; /* assume we are in 1970...2069 */ 6392f861984SMian Yousaf Kaukab 6402f861984SMian Yousaf Kaukab ret = rtc_valid_tm(&tm); 6412f861984SMian Yousaf Kaukab if (ret) { 6422f861984SMian Yousaf Kaukab dev_err(dev, "Invalid timestamp. ret=%d\n", ret); 6432f861984SMian Yousaf Kaukab return ret; 6442f861984SMian Yousaf Kaukab } 6452f861984SMian Yousaf Kaukab 6462f861984SMian Yousaf Kaukab *ts = rtc_tm_to_time64(&tm); 6472f861984SMian Yousaf Kaukab return 0; 6482f861984SMian Yousaf Kaukab }; 6492f861984SMian Yousaf Kaukab 650420cc9e8SHugo Villeneuve static void pcf2127_rtc_ts_snapshot(struct device *dev, int ts_id) 6512f861984SMian Yousaf Kaukab { 6522f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 6532f861984SMian Yousaf Kaukab int ret; 6542f861984SMian Yousaf Kaukab 655420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 6562f861984SMian Yousaf Kaukab return; 6572f861984SMian Yousaf Kaukab 658420cc9e8SHugo Villeneuve /* Let userspace read the first timestamp */ 659420cc9e8SHugo Villeneuve if (pcf2127->ts_valid[ts_id]) 660420cc9e8SHugo Villeneuve return; 661420cc9e8SHugo Villeneuve 662420cc9e8SHugo Villeneuve ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts[ts_id], ts_id); 6632f861984SMian Yousaf Kaukab if (!ret) 664420cc9e8SHugo Villeneuve pcf2127->ts_valid[ts_id] = true; 6652f861984SMian Yousaf Kaukab } 6662f861984SMian Yousaf Kaukab 6678a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev) 6688a914bacSLiam Beguin { 6698a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 670afc505bfSHugo Villeneuve unsigned int ctrl2; 6718a914bacSLiam Beguin int ret = 0; 6728a914bacSLiam Beguin 673afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 6742f861984SMian Yousaf Kaukab if (ret) 6752f861984SMian Yousaf Kaukab return IRQ_NONE; 6762f861984SMian Yousaf Kaukab 677afc505bfSHugo Villeneuve if (pcf2127->cfg->ts_count == 1) { 678afc505bfSHugo Villeneuve /* PCF2127/29 */ 679afc505bfSHugo Villeneuve unsigned int ctrl1; 680afc505bfSHugo Villeneuve 681afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1); 6828a914bacSLiam Beguin if (ret) 6838a914bacSLiam Beguin return IRQ_NONE; 6848a914bacSLiam Beguin 6852f861984SMian Yousaf Kaukab if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK)) 68627006416SAlexandre Belloni return IRQ_NONE; 68727006416SAlexandre Belloni 6882f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2) 689420cc9e8SHugo Villeneuve pcf2127_rtc_ts_snapshot(dev, 0); 6908a914bacSLiam Beguin 6912f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_CTRL1_IRQ_MASK) 6922f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1, 6932f861984SMian Yousaf Kaukab ctrl1 & ~PCF2127_CTRL1_IRQ_MASK); 6942f861984SMian Yousaf Kaukab 6952f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_CTRL2_IRQ_MASK) 6962f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 6972f861984SMian Yousaf Kaukab ctrl2 & ~PCF2127_CTRL2_IRQ_MASK); 698afc505bfSHugo Villeneuve } else { 699afc505bfSHugo Villeneuve /* PCF2131. */ 700afc505bfSHugo Villeneuve unsigned int ctrl4; 701afc505bfSHugo Villeneuve 702afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2131_REG_CTRL4, &ctrl4); 703afc505bfSHugo Villeneuve if (ret) 704afc505bfSHugo Villeneuve return IRQ_NONE; 705afc505bfSHugo Villeneuve 706afc505bfSHugo Villeneuve if (!(ctrl4 & PCF2131_CTRL4_IRQ_MASK || ctrl2 & PCF2131_CTRL2_IRQ_MASK)) 707afc505bfSHugo Villeneuve return IRQ_NONE; 708afc505bfSHugo Villeneuve 709afc505bfSHugo Villeneuve if (ctrl4 & PCF2131_CTRL4_IRQ_MASK) { 710afc505bfSHugo Villeneuve int i; 711afc505bfSHugo Villeneuve int tsf_bit = PCF2131_BIT_CTRL4_TSF1; /* Start at bit 7. */ 712afc505bfSHugo Villeneuve 713afc505bfSHugo Villeneuve for (i = 0; i < pcf2127->cfg->ts_count; i++) { 714afc505bfSHugo Villeneuve if (ctrl4 & tsf_bit) 715afc505bfSHugo Villeneuve pcf2127_rtc_ts_snapshot(dev, i); 716afc505bfSHugo Villeneuve 717afc505bfSHugo Villeneuve tsf_bit = tsf_bit >> 1; 718afc505bfSHugo Villeneuve } 719afc505bfSHugo Villeneuve 720afc505bfSHugo Villeneuve regmap_write(pcf2127->regmap, PCF2131_REG_CTRL4, 721afc505bfSHugo Villeneuve ctrl4 & ~PCF2131_CTRL4_IRQ_MASK); 722afc505bfSHugo Villeneuve } 723afc505bfSHugo Villeneuve 724afc505bfSHugo Villeneuve if (ctrl2 & PCF2131_CTRL2_IRQ_MASK) 725afc505bfSHugo Villeneuve regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 726afc505bfSHugo Villeneuve ctrl2 & ~PCF2131_CTRL2_IRQ_MASK); 727afc505bfSHugo Villeneuve } 7282f861984SMian Yousaf Kaukab 7292f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_BIT_CTRL2_AF) 7308a914bacSLiam Beguin rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF); 7318a914bacSLiam Beguin 73227006416SAlexandre Belloni pcf2127_wdt_active_ping(&pcf2127->wdd); 7338a914bacSLiam Beguin 7348a914bacSLiam Beguin return IRQ_HANDLED; 7358a914bacSLiam Beguin } 7368a914bacSLiam Beguin 73725cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = { 7388a914bacSLiam Beguin .ioctl = pcf2127_rtc_ioctl, 7398a914bacSLiam Beguin .read_time = pcf2127_rtc_read_time, 7408a914bacSLiam Beguin .set_time = pcf2127_rtc_set_time, 7418a914bacSLiam Beguin .read_alarm = pcf2127_rtc_read_alarm, 7428a914bacSLiam Beguin .set_alarm = pcf2127_rtc_set_alarm, 7438a914bacSLiam Beguin .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable, 7448a914bacSLiam Beguin }; 7458a914bacSLiam Beguin 74603623b4bSBruno Thomsen /* sysfs interface */ 74703623b4bSBruno Thomsen 748420cc9e8SHugo Villeneuve static ssize_t timestamp_store(struct device *dev, 74903623b4bSBruno Thomsen struct device_attribute *attr, 750420cc9e8SHugo Villeneuve const char *buf, size_t count, int ts_id) 75103623b4bSBruno Thomsen { 75203623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 75303623b4bSBruno Thomsen int ret; 75403623b4bSBruno Thomsen 755420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 756420cc9e8SHugo Villeneuve return 0; 757420cc9e8SHugo Villeneuve 7582f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 759420cc9e8SHugo Villeneuve pcf2127->ts_valid[ts_id] = false; 7602f861984SMian Yousaf Kaukab } else { 761420cc9e8SHugo Villeneuve /* Always clear GND interrupt bit. */ 762420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 763420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_reg, 764420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_bit, 765420cc9e8SHugo Villeneuve 0); 766420cc9e8SHugo Villeneuve 76703623b4bSBruno Thomsen if (ret) { 768420cc9e8SHugo Villeneuve dev_err(dev, "%s: update TS gnd detect ret=%d\n", __func__, ret); 76903623b4bSBruno Thomsen return ret; 77003623b4bSBruno Thomsen } 77103623b4bSBruno Thomsen 772420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { 773420cc9e8SHugo Villeneuve /* Clear intermediate level interrupt bit if supported. */ 774420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 775420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_reg, 776420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_bit, 777420cc9e8SHugo Villeneuve 0); 77803623b4bSBruno Thomsen if (ret) { 779420cc9e8SHugo Villeneuve dev_err(dev, "%s: update TS intermediate level detect ret=%d\n", 780420cc9e8SHugo Villeneuve __func__, ret); 78103623b4bSBruno Thomsen return ret; 78203623b4bSBruno Thomsen } 783420cc9e8SHugo Villeneuve } 78403623b4bSBruno Thomsen 78503623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 78603623b4bSBruno Thomsen if (ret) 78703623b4bSBruno Thomsen return ret; 7882f861984SMian Yousaf Kaukab } 78903623b4bSBruno Thomsen 79003623b4bSBruno Thomsen return count; 791420cc9e8SHugo Villeneuve } 792420cc9e8SHugo Villeneuve 793420cc9e8SHugo Villeneuve static ssize_t timestamp0_store(struct device *dev, 794420cc9e8SHugo Villeneuve struct device_attribute *attr, 795420cc9e8SHugo Villeneuve const char *buf, size_t count) 796420cc9e8SHugo Villeneuve { 797420cc9e8SHugo Villeneuve return timestamp_store(dev, attr, buf, count, 0); 79803623b4bSBruno Thomsen }; 79903623b4bSBruno Thomsen 800afc505bfSHugo Villeneuve static ssize_t timestamp1_store(struct device *dev, 801afc505bfSHugo Villeneuve struct device_attribute *attr, 802afc505bfSHugo Villeneuve const char *buf, size_t count) 803afc505bfSHugo Villeneuve { 804afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 1); 805afc505bfSHugo Villeneuve }; 806afc505bfSHugo Villeneuve 807afc505bfSHugo Villeneuve static ssize_t timestamp2_store(struct device *dev, 808afc505bfSHugo Villeneuve struct device_attribute *attr, 809afc505bfSHugo Villeneuve const char *buf, size_t count) 810afc505bfSHugo Villeneuve { 811afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 2); 812afc505bfSHugo Villeneuve }; 813afc505bfSHugo Villeneuve 814afc505bfSHugo Villeneuve static ssize_t timestamp3_store(struct device *dev, 815afc505bfSHugo Villeneuve struct device_attribute *attr, 816afc505bfSHugo Villeneuve const char *buf, size_t count) 817afc505bfSHugo Villeneuve { 818afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 3); 819afc505bfSHugo Villeneuve }; 820afc505bfSHugo Villeneuve 821420cc9e8SHugo Villeneuve static ssize_t timestamp_show(struct device *dev, 822420cc9e8SHugo Villeneuve struct device_attribute *attr, char *buf, 823420cc9e8SHugo Villeneuve int ts_id) 82403623b4bSBruno Thomsen { 82503623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 82603623b4bSBruno Thomsen int ret; 8272f861984SMian Yousaf Kaukab time64_t ts; 82803623b4bSBruno Thomsen 829420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 830420cc9e8SHugo Villeneuve return 0; 831420cc9e8SHugo Villeneuve 8322f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 833420cc9e8SHugo Villeneuve if (!pcf2127->ts_valid[ts_id]) 8342f861984SMian Yousaf Kaukab return 0; 835420cc9e8SHugo Villeneuve ts = pcf2127->ts[ts_id]; 8362f861984SMian Yousaf Kaukab } else { 837420cc9e8SHugo Villeneuve u8 valid_low = 0; 838420cc9e8SHugo Villeneuve u8 valid_inter = 0; 839420cc9e8SHugo Villeneuve unsigned int ctrl; 840420cc9e8SHugo Villeneuve 841420cc9e8SHugo Villeneuve /* Check if TS input pin is driven to GND, supported by all 842420cc9e8SHugo Villeneuve * variants. 843420cc9e8SHugo Villeneuve */ 844420cc9e8SHugo Villeneuve ret = regmap_read(pcf2127->regmap, 845420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_reg, 846420cc9e8SHugo Villeneuve &ctrl); 8472f861984SMian Yousaf Kaukab if (ret) 8482f861984SMian Yousaf Kaukab return 0; 84903623b4bSBruno Thomsen 850420cc9e8SHugo Villeneuve valid_low = ctrl & pcf2127->cfg->ts[ts_id].gnd_detect_bit; 851420cc9e8SHugo Villeneuve 852420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { 853420cc9e8SHugo Villeneuve /* Check if TS input pin is driven to intermediate level 854420cc9e8SHugo Villeneuve * between GND and supply, if supported by variant. 855420cc9e8SHugo Villeneuve */ 856420cc9e8SHugo Villeneuve ret = regmap_read(pcf2127->regmap, 857420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_reg, 858420cc9e8SHugo Villeneuve &ctrl); 8592f861984SMian Yousaf Kaukab if (ret) 8602f861984SMian Yousaf Kaukab return 0; 8612f861984SMian Yousaf Kaukab 862420cc9e8SHugo Villeneuve valid_inter = ctrl & pcf2127->cfg->ts[ts_id].inter_detect_bit; 863420cc9e8SHugo Villeneuve } 864420cc9e8SHugo Villeneuve 865420cc9e8SHugo Villeneuve if (!valid_low && !valid_inter) 8662f861984SMian Yousaf Kaukab return 0; 8672f861984SMian Yousaf Kaukab 868420cc9e8SHugo Villeneuve ret = pcf2127_rtc_ts_read(dev->parent, &ts, ts_id); 8692f861984SMian Yousaf Kaukab if (ret) 8702f861984SMian Yousaf Kaukab return 0; 87103623b4bSBruno Thomsen 87203623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 87303623b4bSBruno Thomsen if (ret) 87403623b4bSBruno Thomsen return ret; 8752f861984SMian Yousaf Kaukab } 8762f861984SMian Yousaf Kaukab return sprintf(buf, "%llu\n", (unsigned long long)ts); 877420cc9e8SHugo Villeneuve } 878420cc9e8SHugo Villeneuve 879420cc9e8SHugo Villeneuve static ssize_t timestamp0_show(struct device *dev, 880420cc9e8SHugo Villeneuve struct device_attribute *attr, char *buf) 881420cc9e8SHugo Villeneuve { 882420cc9e8SHugo Villeneuve return timestamp_show(dev, attr, buf, 0); 88303623b4bSBruno Thomsen }; 88403623b4bSBruno Thomsen 885afc505bfSHugo Villeneuve static ssize_t timestamp1_show(struct device *dev, 886afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 887afc505bfSHugo Villeneuve { 888afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 1); 889afc505bfSHugo Villeneuve }; 890afc505bfSHugo Villeneuve 891afc505bfSHugo Villeneuve static ssize_t timestamp2_show(struct device *dev, 892afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 893afc505bfSHugo Villeneuve { 894afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 2); 895afc505bfSHugo Villeneuve }; 896afc505bfSHugo Villeneuve 897afc505bfSHugo Villeneuve static ssize_t timestamp3_show(struct device *dev, 898afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 899afc505bfSHugo Villeneuve { 900afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 3); 901afc505bfSHugo Villeneuve }; 902afc505bfSHugo Villeneuve 90303623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0); 904afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp1); 905afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp2); 906afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp3); 90703623b4bSBruno Thomsen 90803623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = { 90903623b4bSBruno Thomsen &dev_attr_timestamp0.attr, 91003623b4bSBruno Thomsen NULL 91103623b4bSBruno Thomsen }; 91203623b4bSBruno Thomsen 913afc505bfSHugo Villeneuve static struct attribute *pcf2131_attrs[] = { 914afc505bfSHugo Villeneuve &dev_attr_timestamp0.attr, 915afc505bfSHugo Villeneuve &dev_attr_timestamp1.attr, 916afc505bfSHugo Villeneuve &dev_attr_timestamp2.attr, 917afc505bfSHugo Villeneuve &dev_attr_timestamp3.attr, 918afc505bfSHugo Villeneuve NULL 919afc505bfSHugo Villeneuve }; 920afc505bfSHugo Villeneuve 921fd28ceb4SHugo Villeneuve static struct pcf21xx_config pcf21xx_cfg[] = { 922fd28ceb4SHugo Villeneuve [PCF2127] = { 923fd28ceb4SHugo Villeneuve .type = PCF2127, 924fd28ceb4SHugo Villeneuve .max_register = 0x1d, 925fd28ceb4SHugo Villeneuve .has_nvmem = 1, 926fd28ceb4SHugo Villeneuve .has_bit_wd_ctl_cd0 = 1, 927081602a1SHugo Villeneuve .wd_val_reg_readable = 1, 928e1849b8fSHugo Villeneuve .has_int_a_b = 0, 9296211aceeSHugo Villeneuve .reg_time_base = PCF2127_REG_TIME_BASE, 9307c6f0db4SHugo Villeneuve .regs_alarm_base = PCF2127_REG_ALARM_BASE, 9316b57ec29SHugo Villeneuve .reg_wd_ctl = PCF2127_REG_WD_CTL, 9326b57ec29SHugo Villeneuve .reg_wd_val = PCF2127_REG_WD_VAL, 933fc16599eSHugo Villeneuve .reg_clkout = PCF2127_REG_CLKOUT, 934adb9675dSHugo Villeneuve .wdd_clock_hz_x1000 = PCF2127_WD_CLOCK_HZ_X1000, 935adb9675dSHugo Villeneuve .wdd_min_hw_heartbeat_ms = PCF2127_WD_MIN_HW_HEARTBEAT_MS, 936420cc9e8SHugo Villeneuve .ts_count = 1, 937420cc9e8SHugo Villeneuve .ts[0] = { 938420cc9e8SHugo Villeneuve .reg_base = PCF2127_REG_TS1_BASE, 939420cc9e8SHugo Villeneuve .gnd_detect_reg = PCF2127_REG_CTRL1, 940420cc9e8SHugo Villeneuve .gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1, 941420cc9e8SHugo Villeneuve .inter_detect_reg = PCF2127_REG_CTRL2, 942420cc9e8SHugo Villeneuve .inter_detect_bit = PCF2127_BIT_CTRL2_TSF2, 943420cc9e8SHugo Villeneuve .ie_reg = PCF2127_REG_CTRL2, 944420cc9e8SHugo Villeneuve .ie_bit = PCF2127_BIT_CTRL2_TSIE, 945420cc9e8SHugo Villeneuve }, 946420cc9e8SHugo Villeneuve .attribute_group = { 947420cc9e8SHugo Villeneuve .attrs = pcf2127_attrs, 948420cc9e8SHugo Villeneuve }, 949fd28ceb4SHugo Villeneuve }, 950fd28ceb4SHugo Villeneuve [PCF2129] = { 951fd28ceb4SHugo Villeneuve .type = PCF2129, 952fd28ceb4SHugo Villeneuve .max_register = 0x19, 953fd28ceb4SHugo Villeneuve .has_nvmem = 0, 954fd28ceb4SHugo Villeneuve .has_bit_wd_ctl_cd0 = 0, 955081602a1SHugo Villeneuve .wd_val_reg_readable = 1, 956e1849b8fSHugo Villeneuve .has_int_a_b = 0, 9576211aceeSHugo Villeneuve .reg_time_base = PCF2127_REG_TIME_BASE, 9587c6f0db4SHugo Villeneuve .regs_alarm_base = PCF2127_REG_ALARM_BASE, 9596b57ec29SHugo Villeneuve .reg_wd_ctl = PCF2127_REG_WD_CTL, 9606b57ec29SHugo Villeneuve .reg_wd_val = PCF2127_REG_WD_VAL, 961fc16599eSHugo Villeneuve .reg_clkout = PCF2127_REG_CLKOUT, 962adb9675dSHugo Villeneuve .wdd_clock_hz_x1000 = PCF2127_WD_CLOCK_HZ_X1000, 963adb9675dSHugo Villeneuve .wdd_min_hw_heartbeat_ms = PCF2127_WD_MIN_HW_HEARTBEAT_MS, 964420cc9e8SHugo Villeneuve .ts_count = 1, 965420cc9e8SHugo Villeneuve .ts[0] = { 966420cc9e8SHugo Villeneuve .reg_base = PCF2127_REG_TS1_BASE, 967420cc9e8SHugo Villeneuve .gnd_detect_reg = PCF2127_REG_CTRL1, 968420cc9e8SHugo Villeneuve .gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1, 969420cc9e8SHugo Villeneuve .inter_detect_reg = PCF2127_REG_CTRL2, 970420cc9e8SHugo Villeneuve .inter_detect_bit = PCF2127_BIT_CTRL2_TSF2, 971420cc9e8SHugo Villeneuve .ie_reg = PCF2127_REG_CTRL2, 972420cc9e8SHugo Villeneuve .ie_bit = PCF2127_BIT_CTRL2_TSIE, 973420cc9e8SHugo Villeneuve }, 974420cc9e8SHugo Villeneuve .attribute_group = { 975420cc9e8SHugo Villeneuve .attrs = pcf2127_attrs, 976420cc9e8SHugo Villeneuve }, 977fd28ceb4SHugo Villeneuve }, 978afc505bfSHugo Villeneuve [PCF2131] = { 979afc505bfSHugo Villeneuve .type = PCF2131, 980afc505bfSHugo Villeneuve .max_register = 0x36, 981afc505bfSHugo Villeneuve .has_nvmem = 0, 982afc505bfSHugo Villeneuve .has_bit_wd_ctl_cd0 = 0, 983081602a1SHugo Villeneuve .wd_val_reg_readable = 0, 984e1849b8fSHugo Villeneuve .has_int_a_b = 1, 985afc505bfSHugo Villeneuve .reg_time_base = PCF2131_REG_TIME_BASE, 986afc505bfSHugo Villeneuve .regs_alarm_base = PCF2131_REG_ALARM_BASE, 987afc505bfSHugo Villeneuve .reg_wd_ctl = PCF2131_REG_WD_CTL, 988afc505bfSHugo Villeneuve .reg_wd_val = PCF2131_REG_WD_VAL, 989afc505bfSHugo Villeneuve .reg_clkout = PCF2131_REG_CLKOUT, 990adb9675dSHugo Villeneuve .wdd_clock_hz_x1000 = PCF2131_WD_CLOCK_HZ_X1000, 991adb9675dSHugo Villeneuve .wdd_min_hw_heartbeat_ms = PCF2131_WD_MIN_HW_HEARTBEAT_MS, 992afc505bfSHugo Villeneuve .ts_count = 4, 993afc505bfSHugo Villeneuve .ts[0] = { 994afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS1_BASE, 995afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 996afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF1, 997afc505bfSHugo Villeneuve .inter_detect_bit = 0, 998afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 999afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE1, 1000afc505bfSHugo Villeneuve }, 1001afc505bfSHugo Villeneuve .ts[1] = { 1002afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS2_BASE, 1003afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 1004afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF2, 1005afc505bfSHugo Villeneuve .inter_detect_bit = 0, 1006afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 1007afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE2, 1008afc505bfSHugo Villeneuve }, 1009afc505bfSHugo Villeneuve .ts[2] = { 1010afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS3_BASE, 1011afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 1012afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF3, 1013afc505bfSHugo Villeneuve .inter_detect_bit = 0, 1014afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 1015afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE3, 1016afc505bfSHugo Villeneuve }, 1017afc505bfSHugo Villeneuve .ts[3] = { 1018afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS4_BASE, 1019afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 1020afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF4, 1021afc505bfSHugo Villeneuve .inter_detect_bit = 0, 1022afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 1023afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE4, 1024afc505bfSHugo Villeneuve }, 1025afc505bfSHugo Villeneuve .attribute_group = { 1026afc505bfSHugo Villeneuve .attrs = pcf2131_attrs, 1027afc505bfSHugo Villeneuve }, 1028afc505bfSHugo Villeneuve }, 1029fd28ceb4SHugo Villeneuve }; 1030fd28ceb4SHugo Villeneuve 1031420cc9e8SHugo Villeneuve /* 1032420cc9e8SHugo Villeneuve * Enable timestamp function and corresponding interrupt(s). 1033420cc9e8SHugo Villeneuve */ 1034420cc9e8SHugo Villeneuve static int pcf2127_enable_ts(struct device *dev, int ts_id) 1035420cc9e8SHugo Villeneuve { 1036420cc9e8SHugo Villeneuve struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 1037420cc9e8SHugo Villeneuve int ret; 1038420cc9e8SHugo Villeneuve 1039420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) { 1040420cc9e8SHugo Villeneuve dev_err(dev, "%s: invalid tamper detection ID (%d)\n", 1041420cc9e8SHugo Villeneuve __func__, ts_id); 1042420cc9e8SHugo Villeneuve return -EINVAL; 1043420cc9e8SHugo Villeneuve } 1044420cc9e8SHugo Villeneuve 1045420cc9e8SHugo Villeneuve /* Enable timestamp function. */ 1046420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 1047420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].reg_base, 1048420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSOFF | 1049420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSM, 1050420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSM); 1051420cc9e8SHugo Villeneuve if (ret) { 1052420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection config (ts%d_ctrl) failed\n", 1053420cc9e8SHugo Villeneuve __func__, ts_id); 1054420cc9e8SHugo Villeneuve return ret; 1055420cc9e8SHugo Villeneuve } 1056420cc9e8SHugo Villeneuve 1057420cc9e8SHugo Villeneuve /* TS input pin driven to GND detection is supported by all variants. 1058420cc9e8SHugo Villeneuve * Make sure that interrupt bit is defined. 1059420cc9e8SHugo Villeneuve */ 1060420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].gnd_detect_bit == 0) { 1061420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection to GND configuration invalid\n", 1062420cc9e8SHugo Villeneuve __func__); 10634e5eb7efSDan Carpenter return -EINVAL; 1064420cc9e8SHugo Villeneuve } 1065420cc9e8SHugo Villeneuve 1066420cc9e8SHugo Villeneuve /* 1067420cc9e8SHugo Villeneuve * Enable interrupt generation when TSF timestamp flag is set. 1068420cc9e8SHugo Villeneuve * Interrupt signals are open-drain outputs and can be left floating if 1069420cc9e8SHugo Villeneuve * unused. 1070420cc9e8SHugo Villeneuve */ 1071420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->ts[ts_id].ie_reg, 1072420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].ie_bit, 1073420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].ie_bit); 1074420cc9e8SHugo Villeneuve if (ret) { 1075420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection TSIE%d config failed\n", 1076420cc9e8SHugo Villeneuve __func__, ts_id); 1077420cc9e8SHugo Villeneuve return ret; 1078420cc9e8SHugo Villeneuve } 1079420cc9e8SHugo Villeneuve 1080420cc9e8SHugo Villeneuve return ret; 1081420cc9e8SHugo Villeneuve } 1082420cc9e8SHugo Villeneuve 1083e1849b8fSHugo Villeneuve /* Route all interrupt sources to INT A pin. */ 1084e1849b8fSHugo Villeneuve static int pcf2127_configure_interrupt_pins(struct device *dev) 1085e1849b8fSHugo Villeneuve { 1086e1849b8fSHugo Villeneuve struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 1087e1849b8fSHugo Villeneuve int ret; 1088e1849b8fSHugo Villeneuve 1089e1849b8fSHugo Villeneuve /* Mask bits need to be cleared to enable corresponding 1090e1849b8fSHugo Villeneuve * interrupt source. 1091e1849b8fSHugo Villeneuve */ 1092e1849b8fSHugo Villeneuve ret = regmap_write(pcf2127->regmap, 1093e1849b8fSHugo Villeneuve PCF2131_REG_INT_A_MASK1, 0); 1094e1849b8fSHugo Villeneuve if (ret) 1095e1849b8fSHugo Villeneuve return ret; 1096e1849b8fSHugo Villeneuve 1097e1849b8fSHugo Villeneuve ret = regmap_write(pcf2127->regmap, 1098e1849b8fSHugo Villeneuve PCF2131_REG_INT_A_MASK2, 0); 1099e1849b8fSHugo Villeneuve if (ret) 1100e1849b8fSHugo Villeneuve return ret; 1101e1849b8fSHugo Villeneuve 1102e1849b8fSHugo Villeneuve return ret; 1103e1849b8fSHugo Villeneuve } 1104e1849b8fSHugo Villeneuve 1105907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 1106*58dffa96SHugo Villeneuve int alarm_irq, const struct pcf21xx_config *config) 110718cb6368SRenaud Cerrato { 110818cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 1109d6c3029fSUwe Kleine-König int ret = 0; 111015f57b3eSPhilipp Rosenberger unsigned int val; 111118cb6368SRenaud Cerrato 1112907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 111318cb6368SRenaud Cerrato 1114907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 111518cb6368SRenaud Cerrato if (!pcf2127) 111618cb6368SRenaud Cerrato return -ENOMEM; 111718cb6368SRenaud Cerrato 1118907b3262SAkinobu Mita pcf2127->regmap = regmap; 1119fd28ceb4SHugo Villeneuve pcf2127->cfg = config; 112018cb6368SRenaud Cerrato 1121907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 1122907b3262SAkinobu Mita 1123e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 1124d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 1125d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 112618cb6368SRenaud Cerrato 1127e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 1128b139bb5cSAlexandre Belloni pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 1129b139bb5cSAlexandre Belloni pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; 1130b139bb5cSAlexandre Belloni pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ 1131e9a5a1b4SHugo Villeneuve 1132e9a5a1b4SHugo Villeneuve /* 1133e9a5a1b4SHugo Villeneuve * PCF2127/29 do not work correctly when setting alarms at 1s intervals. 1134e9a5a1b4SHugo Villeneuve * PCF2131 is ok. 1135e9a5a1b4SHugo Villeneuve */ 1136e9a5a1b4SHugo Villeneuve if (pcf2127->cfg->type == PCF2127 || pcf2127->cfg->type == PCF2129) { 1137bda10273SAlexandre Belloni set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features); 1138689fafd5SAlexandre Belloni clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features); 1139e9a5a1b4SHugo Villeneuve } 1140e9a5a1b4SHugo Villeneuve 114125cbe9c8SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 1142e788771cSBruno Thomsen 114335425bafSBiwen Li if (alarm_irq > 0) { 1144d4785b46SHugo Villeneuve unsigned long flags; 1145d4785b46SHugo Villeneuve 1146d4785b46SHugo Villeneuve /* 1147d4785b46SHugo Villeneuve * If flags = 0, devm_request_threaded_irq() will use IRQ flags 1148d4785b46SHugo Villeneuve * obtained from device tree. 1149d4785b46SHugo Villeneuve */ 1150d4785b46SHugo Villeneuve if (dev_fwnode(dev)) 1151d4785b46SHugo Villeneuve flags = 0; 1152d4785b46SHugo Villeneuve else 1153d4785b46SHugo Villeneuve flags = IRQF_TRIGGER_LOW; 1154d4785b46SHugo Villeneuve 115527006416SAlexandre Belloni ret = devm_request_threaded_irq(dev, alarm_irq, NULL, 115627006416SAlexandre Belloni pcf2127_rtc_irq, 1157d4785b46SHugo Villeneuve flags | IRQF_ONESHOT, 11588a914bacSLiam Beguin dev_name(dev), dev); 11598a914bacSLiam Beguin if (ret) { 11608a914bacSLiam Beguin dev_err(dev, "failed to request alarm irq\n"); 11618a914bacSLiam Beguin return ret; 11628a914bacSLiam Beguin } 11632f861984SMian Yousaf Kaukab pcf2127->irq_enabled = true; 11648a914bacSLiam Beguin } 11658a914bacSLiam Beguin 116635425bafSBiwen Li if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) { 11678a914bacSLiam Beguin device_init_wakeup(dev, true); 116825cbe9c8SAlexandre Belloni set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 11698a914bacSLiam Beguin } 11708a914bacSLiam Beguin 1171e1849b8fSHugo Villeneuve if (pcf2127->cfg->has_int_a_b) { 1172e1849b8fSHugo Villeneuve /* Configure int A/B pins, independently of alarm_irq. */ 1173e1849b8fSHugo Villeneuve ret = pcf2127_configure_interrupt_pins(dev); 1174e1849b8fSHugo Villeneuve if (ret) { 1175e1849b8fSHugo Villeneuve dev_err(dev, "failed to configure interrupt pins\n"); 1176e1849b8fSHugo Villeneuve return ret; 1177e1849b8fSHugo Villeneuve } 1178e1849b8fSHugo Villeneuve } 1179e1849b8fSHugo Villeneuve 1180fd28ceb4SHugo Villeneuve if (pcf2127->cfg->has_nvmem) { 1181d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 1182d6c3029fSUwe Kleine-König .priv = pcf2127, 1183d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 1184d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 1185d6c3029fSUwe Kleine-König .size = 512, 1186d6c3029fSUwe Kleine-König }; 1187d6c3029fSUwe Kleine-König 11883a905c2dSBartosz Golaszewski ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 1189d6c3029fSUwe Kleine-König } 1190d6c3029fSUwe Kleine-König 11910e735eaaSBruno Thomsen /* 1192b9ac079aSPhilipp Rosenberger * The "Power-On Reset Override" facility prevents the RTC to do a reset 1193b9ac079aSPhilipp Rosenberger * after power on. For normal operation the PORO must be disabled. 1194b9ac079aSPhilipp Rosenberger */ 1195e4163fc5SHugo Villeneuve ret = regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 1196b9ac079aSPhilipp Rosenberger PCF2127_BIT_CTRL1_POR_OVRD); 1197e4163fc5SHugo Villeneuve if (ret < 0) 1198e4163fc5SHugo Villeneuve return ret; 1199b9ac079aSPhilipp Rosenberger 1200fc16599eSHugo Villeneuve ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_clkout, &val); 120115f57b3eSPhilipp Rosenberger if (ret < 0) 120215f57b3eSPhilipp Rosenberger return ret; 120315f57b3eSPhilipp Rosenberger 120415f57b3eSPhilipp Rosenberger if (!(val & PCF2127_BIT_CLKOUT_OTPR)) { 1205fc16599eSHugo Villeneuve ret = regmap_set_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout, 120615f57b3eSPhilipp Rosenberger PCF2127_BIT_CLKOUT_OTPR); 120715f57b3eSPhilipp Rosenberger if (ret < 0) 120815f57b3eSPhilipp Rosenberger return ret; 120915f57b3eSPhilipp Rosenberger 121015f57b3eSPhilipp Rosenberger msleep(100); 121115f57b3eSPhilipp Rosenberger } 121215f57b3eSPhilipp Rosenberger 1213b9ac079aSPhilipp Rosenberger /* 12140e735eaaSBruno Thomsen * Watchdog timer enabled and reset pin /RST activated when timed out. 1215adb9675dSHugo Villeneuve * Select 1Hz clock source for watchdog timer (1/4Hz for PCF2131). 12160e735eaaSBruno Thomsen * Note: Countdown timer disabled and not available. 1217afc505bfSHugo Villeneuve * For pca2129, pcf2129 and pcf2131, only bit[7] is for Symbol WD_CD 12182843d565SBiwen Li * of register watchdg_tim_ctl. The bit[6] is labeled 12192843d565SBiwen Li * as T. Bits labeled as T must always be written with 12202843d565SBiwen Li * logic 0. 12210e735eaaSBruno Thomsen */ 12226b57ec29SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_wd_ctl, 12230e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 12240e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 12250e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1 | 12260e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF0, 12270e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 1228fd28ceb4SHugo Villeneuve (pcf2127->cfg->has_bit_wd_ctl_cd0 ? PCF2127_BIT_WD_CTL_CD0 : 0) | 12290e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1); 12300e735eaaSBruno Thomsen if (ret) { 12310e735eaaSBruno Thomsen dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); 12320e735eaaSBruno Thomsen return ret; 12330e735eaaSBruno Thomsen } 12340e735eaaSBruno Thomsen 12355d78533aSUwe Kleine-König pcf2127_watchdog_init(dev, pcf2127); 12360e735eaaSBruno Thomsen 123703623b4bSBruno Thomsen /* 123803623b4bSBruno Thomsen * Disable battery low/switch-over timestamp and interrupts. 123903623b4bSBruno Thomsen * Clear battery interrupt flags which can block new trigger events. 124003623b4bSBruno Thomsen * Note: This is the default chip behaviour but added to ensure 124103623b4bSBruno Thomsen * correct tamper timestamp and interrupt function. 124203623b4bSBruno Thomsen */ 124303623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 124403623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BTSE | 124503623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BIE | 124603623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BLIE, 0); 124703623b4bSBruno Thomsen if (ret) { 124803623b4bSBruno Thomsen dev_err(dev, "%s: interrupt config (ctrl3) failed\n", 124903623b4bSBruno Thomsen __func__); 125003623b4bSBruno Thomsen return ret; 125103623b4bSBruno Thomsen } 125203623b4bSBruno Thomsen 125303623b4bSBruno Thomsen /* 1254420cc9e8SHugo Villeneuve * Enable timestamp functions 1 to 4. 125503623b4bSBruno Thomsen */ 1256420cc9e8SHugo Villeneuve for (int i = 0; i < pcf2127->cfg->ts_count; i++) { 1257420cc9e8SHugo Villeneuve ret = pcf2127_enable_ts(dev, i); 1258420cc9e8SHugo Villeneuve if (ret) 125903623b4bSBruno Thomsen return ret; 126003623b4bSBruno Thomsen } 126103623b4bSBruno Thomsen 1262420cc9e8SHugo Villeneuve ret = rtc_add_group(pcf2127->rtc, &pcf2127->cfg->attribute_group); 126303623b4bSBruno Thomsen if (ret) { 126403623b4bSBruno Thomsen dev_err(dev, "%s: tamper sysfs registering failed\n", 126503623b4bSBruno Thomsen __func__); 126603623b4bSBruno Thomsen return ret; 126703623b4bSBruno Thomsen } 126803623b4bSBruno Thomsen 1269fdcfd854SBartosz Golaszewski return devm_rtc_register_device(pcf2127->rtc); 127018cb6368SRenaud Cerrato } 127118cb6368SRenaud Cerrato 127218cb6368SRenaud Cerrato #ifdef CONFIG_OF 127318cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 1274fd28ceb4SHugo Villeneuve { .compatible = "nxp,pcf2127", .data = &pcf21xx_cfg[PCF2127] }, 1275fd28ceb4SHugo Villeneuve { .compatible = "nxp,pcf2129", .data = &pcf21xx_cfg[PCF2129] }, 1276fd28ceb4SHugo Villeneuve { .compatible = "nxp,pca2129", .data = &pcf21xx_cfg[PCF2129] }, 1277afc505bfSHugo Villeneuve { .compatible = "nxp,pcf2131", .data = &pcf21xx_cfg[PCF2131] }, 127818cb6368SRenaud Cerrato {} 127918cb6368SRenaud Cerrato }; 128018cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 128118cb6368SRenaud Cerrato #endif 128218cb6368SRenaud Cerrato 12839408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 12849408ec1aSAkinobu Mita 1285907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 1286907b3262SAkinobu Mita { 1287907b3262SAkinobu Mita struct device *dev = context; 1288907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1289907b3262SAkinobu Mita int ret; 1290907b3262SAkinobu Mita 1291907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 1292907b3262SAkinobu Mita if (ret != count) 1293907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1294907b3262SAkinobu Mita 1295907b3262SAkinobu Mita return 0; 1296907b3262SAkinobu Mita } 1297907b3262SAkinobu Mita 1298907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 1299907b3262SAkinobu Mita const void *reg, size_t reg_size, 1300907b3262SAkinobu Mita const void *val, size_t val_size) 1301907b3262SAkinobu Mita { 1302907b3262SAkinobu Mita struct device *dev = context; 1303907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1304907b3262SAkinobu Mita int ret; 1305907b3262SAkinobu Mita void *buf; 1306907b3262SAkinobu Mita 1307907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 1308907b3262SAkinobu Mita return -EINVAL; 1309907b3262SAkinobu Mita 1310907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 1311907b3262SAkinobu Mita if (!buf) 1312907b3262SAkinobu Mita return -ENOMEM; 1313907b3262SAkinobu Mita 1314907b3262SAkinobu Mita memcpy(buf, reg, 1); 1315907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 1316907b3262SAkinobu Mita 1317907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 13189bde0afbSXulin Sun 13199bde0afbSXulin Sun kfree(buf); 13209bde0afbSXulin Sun 1321907b3262SAkinobu Mita if (ret != val_size + 1) 1322907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1323907b3262SAkinobu Mita 1324907b3262SAkinobu Mita return 0; 1325907b3262SAkinobu Mita } 1326907b3262SAkinobu Mita 1327907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 1328907b3262SAkinobu Mita void *val, size_t val_size) 1329907b3262SAkinobu Mita { 1330907b3262SAkinobu Mita struct device *dev = context; 1331907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1332907b3262SAkinobu Mita int ret; 1333907b3262SAkinobu Mita 1334907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 1335907b3262SAkinobu Mita return -EINVAL; 1336907b3262SAkinobu Mita 1337907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 1338907b3262SAkinobu Mita if (ret != 1) 1339907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1340907b3262SAkinobu Mita 1341907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 1342907b3262SAkinobu Mita if (ret != val_size) 1343907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1344907b3262SAkinobu Mita 1345907b3262SAkinobu Mita return 0; 1346907b3262SAkinobu Mita } 1347907b3262SAkinobu Mita 1348907b3262SAkinobu Mita /* 1349907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 1350907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 1351907b3262SAkinobu Mita * read register data when reading from registers. 1352907b3262SAkinobu Mita */ 1353907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 1354907b3262SAkinobu Mita .write = pcf2127_i2c_write, 1355907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 1356907b3262SAkinobu Mita .read = pcf2127_i2c_read, 135718cb6368SRenaud Cerrato }; 135818cb6368SRenaud Cerrato 1359907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 1360907b3262SAkinobu Mita 13615418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = { 1362fd28ceb4SHugo Villeneuve { "pcf2127", PCF2127 }, 1363fd28ceb4SHugo Villeneuve { "pcf2129", PCF2129 }, 1364fd28ceb4SHugo Villeneuve { "pca2129", PCF2129 }, 1365afc505bfSHugo Villeneuve { "pcf2131", PCF2131 }, 13665418e595SUwe Kleine-König { } 13675418e595SUwe Kleine-König }; 13685418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 13695418e595SUwe Kleine-König 13705418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client) 1371907b3262SAkinobu Mita { 1372907b3262SAkinobu Mita struct regmap *regmap; 1373fd28ceb4SHugo Villeneuve static struct regmap_config config = { 1374907b3262SAkinobu Mita .reg_bits = 8, 1375907b3262SAkinobu Mita .val_bits = 8, 1376907b3262SAkinobu Mita }; 1377fd28ceb4SHugo Villeneuve const struct pcf21xx_config *variant; 1378907b3262SAkinobu Mita 1379907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 1380907b3262SAkinobu Mita return -ENODEV; 1381907b3262SAkinobu Mita 1382fd28ceb4SHugo Villeneuve if (client->dev.of_node) { 1383fd28ceb4SHugo Villeneuve variant = of_device_get_match_data(&client->dev); 1384fd28ceb4SHugo Villeneuve if (!variant) 1385fd28ceb4SHugo Villeneuve return -ENODEV; 1386fd28ceb4SHugo Villeneuve } else { 1387fd28ceb4SHugo Villeneuve enum pcf21xx_type type = 1388fd28ceb4SHugo Villeneuve i2c_match_id(pcf2127_i2c_id, client)->driver_data; 1389fd28ceb4SHugo Villeneuve 1390fd28ceb4SHugo Villeneuve if (type >= PCF21XX_LAST_ID) 1391fd28ceb4SHugo Villeneuve return -ENODEV; 1392fd28ceb4SHugo Villeneuve variant = &pcf21xx_cfg[type]; 1393fd28ceb4SHugo Villeneuve } 1394fd28ceb4SHugo Villeneuve 1395fd28ceb4SHugo Villeneuve config.max_register = variant->max_register, 1396fd28ceb4SHugo Villeneuve 1397907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 1398907b3262SAkinobu Mita &client->dev, &config); 1399907b3262SAkinobu Mita if (IS_ERR(regmap)) { 1400907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 1401907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 1402907b3262SAkinobu Mita return PTR_ERR(regmap); 1403907b3262SAkinobu Mita } 1404907b3262SAkinobu Mita 1405*58dffa96SHugo Villeneuve return pcf2127_probe(&client->dev, regmap, client->irq, variant); 1406907b3262SAkinobu Mita } 1407907b3262SAkinobu Mita 1408907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 1409907b3262SAkinobu Mita .driver = { 1410907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 1411907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 1412907b3262SAkinobu Mita }, 141331b0cecbSUwe Kleine-König .probe = pcf2127_i2c_probe, 1414907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 1415907b3262SAkinobu Mita }; 14169408ec1aSAkinobu Mita 14179408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 14189408ec1aSAkinobu Mita { 14199408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 14209408ec1aSAkinobu Mita } 14219408ec1aSAkinobu Mita 14229408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 14239408ec1aSAkinobu Mita { 14249408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 14259408ec1aSAkinobu Mita } 14269408ec1aSAkinobu Mita 14279408ec1aSAkinobu Mita #else 14289408ec1aSAkinobu Mita 14299408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 14309408ec1aSAkinobu Mita { 14319408ec1aSAkinobu Mita return 0; 14329408ec1aSAkinobu Mita } 14339408ec1aSAkinobu Mita 14349408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 14359408ec1aSAkinobu Mita { 14369408ec1aSAkinobu Mita } 14379408ec1aSAkinobu Mita 14389408ec1aSAkinobu Mita #endif 14399408ec1aSAkinobu Mita 14409408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 14419408ec1aSAkinobu Mita 14429408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 1443fd28ceb4SHugo Villeneuve static const struct spi_device_id pcf2127_spi_id[]; 14449408ec1aSAkinobu Mita 14459408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 14469408ec1aSAkinobu Mita { 1447fd28ceb4SHugo Villeneuve static struct regmap_config config = { 14489408ec1aSAkinobu Mita .reg_bits = 8, 14499408ec1aSAkinobu Mita .val_bits = 8, 14509408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 14519408ec1aSAkinobu Mita .write_flag_mask = 0x20, 14529408ec1aSAkinobu Mita }; 14539408ec1aSAkinobu Mita struct regmap *regmap; 1454fd28ceb4SHugo Villeneuve const struct pcf21xx_config *variant; 1455fd28ceb4SHugo Villeneuve 1456fd28ceb4SHugo Villeneuve if (spi->dev.of_node) { 1457fd28ceb4SHugo Villeneuve variant = of_device_get_match_data(&spi->dev); 1458fd28ceb4SHugo Villeneuve if (!variant) 1459fd28ceb4SHugo Villeneuve return -ENODEV; 1460fd28ceb4SHugo Villeneuve } else { 1461fd28ceb4SHugo Villeneuve enum pcf21xx_type type = spi_get_device_id(spi)->driver_data; 1462fd28ceb4SHugo Villeneuve 1463fd28ceb4SHugo Villeneuve if (type >= PCF21XX_LAST_ID) 1464fd28ceb4SHugo Villeneuve return -ENODEV; 1465fd28ceb4SHugo Villeneuve variant = &pcf21xx_cfg[type]; 1466fd28ceb4SHugo Villeneuve } 1467fd28ceb4SHugo Villeneuve 1468fd28ceb4SHugo Villeneuve config.max_register = variant->max_register, 14699408ec1aSAkinobu Mita 14709408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 14719408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 14729408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 14739408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 14749408ec1aSAkinobu Mita return PTR_ERR(regmap); 14759408ec1aSAkinobu Mita } 14769408ec1aSAkinobu Mita 1477*58dffa96SHugo Villeneuve return pcf2127_probe(&spi->dev, regmap, spi->irq, variant); 14789408ec1aSAkinobu Mita } 14799408ec1aSAkinobu Mita 14809408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 1481fd28ceb4SHugo Villeneuve { "pcf2127", PCF2127 }, 1482fd28ceb4SHugo Villeneuve { "pcf2129", PCF2129 }, 1483fd28ceb4SHugo Villeneuve { "pca2129", PCF2129 }, 1484afc505bfSHugo Villeneuve { "pcf2131", PCF2131 }, 14859408ec1aSAkinobu Mita { } 14869408ec1aSAkinobu Mita }; 14879408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 14889408ec1aSAkinobu Mita 14899408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 14909408ec1aSAkinobu Mita .driver = { 14919408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 14929408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 14939408ec1aSAkinobu Mita }, 14949408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 14959408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 14969408ec1aSAkinobu Mita }; 14979408ec1aSAkinobu Mita 14989408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 14999408ec1aSAkinobu Mita { 15009408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 15019408ec1aSAkinobu Mita } 15029408ec1aSAkinobu Mita 15039408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 15049408ec1aSAkinobu Mita { 15059408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 15069408ec1aSAkinobu Mita } 15079408ec1aSAkinobu Mita 15089408ec1aSAkinobu Mita #else 15099408ec1aSAkinobu Mita 15109408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 15119408ec1aSAkinobu Mita { 15129408ec1aSAkinobu Mita return 0; 15139408ec1aSAkinobu Mita } 15149408ec1aSAkinobu Mita 15159408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 15169408ec1aSAkinobu Mita { 15179408ec1aSAkinobu Mita } 15189408ec1aSAkinobu Mita 15199408ec1aSAkinobu Mita #endif 15209408ec1aSAkinobu Mita 15219408ec1aSAkinobu Mita static int __init pcf2127_init(void) 15229408ec1aSAkinobu Mita { 15239408ec1aSAkinobu Mita int ret; 15249408ec1aSAkinobu Mita 15259408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 15269408ec1aSAkinobu Mita if (ret) { 15279408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 15289408ec1aSAkinobu Mita return ret; 15299408ec1aSAkinobu Mita } 15309408ec1aSAkinobu Mita 15319408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 15329408ec1aSAkinobu Mita if (ret) { 15339408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 15349408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 15359408ec1aSAkinobu Mita } 15369408ec1aSAkinobu Mita 15379408ec1aSAkinobu Mita return ret; 15389408ec1aSAkinobu Mita } 15399408ec1aSAkinobu Mita module_init(pcf2127_init) 15409408ec1aSAkinobu Mita 15419408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 15429408ec1aSAkinobu Mita { 15439408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 15449408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 15459408ec1aSAkinobu Mita } 15469408ec1aSAkinobu Mita module_exit(pcf2127_exit) 154718cb6368SRenaud Cerrato 154818cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 1549afc505bfSHugo Villeneuve MODULE_DESCRIPTION("NXP PCF2127/29/31 RTC driver"); 15504d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 1551