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) 36bbfe3a7aSBruno Thomsen /* Control register 2 */ 37bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2 0x01 388a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AIE BIT(1) 3903623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE BIT(2) 408a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AF BIT(4) 4103623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2 BIT(5) 4227006416SAlexandre Belloni #define PCF2127_BIT_CTRL2_WDTF BIT(6) 43bbfe3a7aSBruno Thomsen /* Control register 3 */ 44bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3 0x02 4503623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE BIT(0) 4603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE BIT(1) 47bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF BIT(2) 4803623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF BIT(3) 4903623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE BIT(4) 50bbfe3a7aSBruno Thomsen /* Time and date registers */ 516211aceeSHugo Villeneuve #define PCF2127_REG_TIME_BASE 0x03 52bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF BIT(7) 538a914bacSLiam Beguin /* Alarm registers */ 547c6f0db4SHugo Villeneuve #define PCF2127_REG_ALARM_BASE 0x0A 5527006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE BIT(7) 5615f57b3eSPhilipp Rosenberger /* CLKOUT control register */ 5715f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT 0x0f 5815f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR BIT(5) 590e735eaaSBruno Thomsen /* Watchdog registers */ 600e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL 0x10 610e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0 BIT(0) 620e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1 BIT(1) 630e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0 BIT(6) 640e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1 BIT(7) 650e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL 0x11 66420cc9e8SHugo Villeneuve /* Tamper timestamp1 registers */ 67420cc9e8SHugo Villeneuve #define PCF2127_REG_TS1_BASE 0x12 6803623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) 6903623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM BIT(7) 70bbfe3a7aSBruno Thomsen /* 71bbfe3a7aSBruno Thomsen * RAM registers 72bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 73bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 74afc505bfSHugo Villeneuve * PCF2129/31 doesn't have this feature. 75bbfe3a7aSBruno Thomsen */ 76bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 77bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 78bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 79f97cfddcSUwe Kleine-König 800e735eaaSBruno Thomsen /* Watchdog timer value constants */ 810e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP 0 820e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN 2 830e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX 255 840e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT 60 85653ebd75SAndrea Scian 862f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */ 872f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1) 882f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \ 892f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_AF | \ 902f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_WDTF | \ 912f861984SMian Yousaf Kaukab PCF2127_BIT_CTRL2_TSF2) 922f861984SMian Yousaf Kaukab 93afc505bfSHugo Villeneuve #define PCF2127_MAX_TS_SUPPORTED 4 94afc505bfSHugo Villeneuve 95afc505bfSHugo Villeneuve /* Control register 4 */ 96afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL4 0x03 97afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF4 BIT(4) 98afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF3 BIT(5) 99afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF2 BIT(6) 100afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF1 BIT(7) 101afc505bfSHugo Villeneuve /* Control register 5 */ 102afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL5 0x04 103afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE4 BIT(4) 104afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE3 BIT(5) 105afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE2 BIT(6) 106afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE1 BIT(7) 107afc505bfSHugo Villeneuve /* Software reset register */ 108afc505bfSHugo Villeneuve #define PCF2131_REG_SR_RESET 0x05 109afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_READ_PATTERN (BIT(2) | BIT(5)) 110afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_CPR_CMD (PCF2131_SR_RESET_READ_PATTERN | BIT(7)) 111afc505bfSHugo Villeneuve /* Time and date registers */ 112afc505bfSHugo Villeneuve #define PCF2131_REG_TIME_BASE 0x07 113afc505bfSHugo Villeneuve /* Alarm registers */ 114afc505bfSHugo Villeneuve #define PCF2131_REG_ALARM_BASE 0x0E 115afc505bfSHugo Villeneuve /* CLKOUT control register */ 116afc505bfSHugo Villeneuve #define PCF2131_REG_CLKOUT 0x13 117afc505bfSHugo Villeneuve /* Watchdog registers */ 118afc505bfSHugo Villeneuve #define PCF2131_REG_WD_CTL 0x35 119afc505bfSHugo Villeneuve #define PCF2131_REG_WD_VAL 0x36 120afc505bfSHugo Villeneuve /* Tamper timestamp1 registers */ 121afc505bfSHugo Villeneuve #define PCF2131_REG_TS1_BASE 0x14 122afc505bfSHugo Villeneuve /* Tamper timestamp2 registers */ 123afc505bfSHugo Villeneuve #define PCF2131_REG_TS2_BASE 0x1B 124afc505bfSHugo Villeneuve /* Tamper timestamp3 registers */ 125afc505bfSHugo Villeneuve #define PCF2131_REG_TS3_BASE 0x22 126afc505bfSHugo Villeneuve /* Tamper timestamp4 registers */ 127afc505bfSHugo Villeneuve #define PCF2131_REG_TS4_BASE 0x29 128afc505bfSHugo Villeneuve /* Interrupt mask registers */ 129afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK1 0x31 130afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK2 0x32 131afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK1 0x33 132afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK2 0x34 133afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BLIE BIT(0) 134afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BIE BIT(1) 135afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_AIE BIT(2) 136afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_WD_CD BIT(3) 137afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_SI BIT(4) 138afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_MI BIT(5) 139afc505bfSHugo Villeneuve #define PCF2131_CTRL2_IRQ_MASK ( \ 140afc505bfSHugo Villeneuve PCF2127_BIT_CTRL2_AF | \ 141afc505bfSHugo Villeneuve PCF2127_BIT_CTRL2_WDTF) 142afc505bfSHugo Villeneuve #define PCF2131_CTRL4_IRQ_MASK ( \ 143afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF4 | \ 144afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF3 | \ 145afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF2 | \ 146afc505bfSHugo Villeneuve PCF2131_BIT_CTRL4_TSF1) 147420cc9e8SHugo Villeneuve 148fd28ceb4SHugo Villeneuve enum pcf21xx_type { 149fd28ceb4SHugo Villeneuve PCF2127, 150fd28ceb4SHugo Villeneuve PCF2129, 151afc505bfSHugo Villeneuve PCF2131, 152fd28ceb4SHugo Villeneuve PCF21XX_LAST_ID 153fd28ceb4SHugo Villeneuve }; 154fd28ceb4SHugo Villeneuve 155420cc9e8SHugo Villeneuve struct pcf21xx_ts_config { 156420cc9e8SHugo Villeneuve u8 reg_base; /* Base register to read timestamp values. */ 157420cc9e8SHugo Villeneuve 158420cc9e8SHugo Villeneuve /* 159420cc9e8SHugo Villeneuve * If the TS input pin is driven to GND, an interrupt can be generated 160420cc9e8SHugo Villeneuve * (supported by all variants). 161420cc9e8SHugo Villeneuve */ 162420cc9e8SHugo Villeneuve u8 gnd_detect_reg; /* Interrupt control register address. */ 163420cc9e8SHugo Villeneuve u8 gnd_detect_bit; /* Interrupt bit. */ 164420cc9e8SHugo Villeneuve 165420cc9e8SHugo Villeneuve /* 166420cc9e8SHugo Villeneuve * If the TS input pin is driven to an intermediate level between GND 167420cc9e8SHugo Villeneuve * and supply, an interrupt can be generated (optional feature depending 168420cc9e8SHugo Villeneuve * on variant). 169420cc9e8SHugo Villeneuve */ 170420cc9e8SHugo Villeneuve u8 inter_detect_reg; /* Interrupt control register address. */ 171420cc9e8SHugo Villeneuve u8 inter_detect_bit; /* Interrupt bit. */ 172420cc9e8SHugo Villeneuve 173420cc9e8SHugo Villeneuve u8 ie_reg; /* Interrupt enable control register. */ 174420cc9e8SHugo Villeneuve u8 ie_bit; /* Interrupt enable bit. */ 175420cc9e8SHugo Villeneuve }; 176420cc9e8SHugo Villeneuve 177fd28ceb4SHugo Villeneuve struct pcf21xx_config { 178fd28ceb4SHugo Villeneuve int type; /* IC variant */ 179fd28ceb4SHugo Villeneuve int max_register; 180fd28ceb4SHugo Villeneuve unsigned int has_nvmem:1; 181fd28ceb4SHugo Villeneuve unsigned int has_bit_wd_ctl_cd0:1; 182*e1849b8fSHugo Villeneuve unsigned int has_int_a_b:1; /* PCF2131 supports two interrupt outputs. */ 1836211aceeSHugo Villeneuve u8 reg_time_base; /* Time/date base register. */ 1847c6f0db4SHugo Villeneuve u8 regs_alarm_base; /* Alarm function base registers. */ 1856b57ec29SHugo Villeneuve u8 reg_wd_ctl; /* Watchdog control register. */ 1866b57ec29SHugo Villeneuve u8 reg_wd_val; /* Watchdog value register. */ 187fc16599eSHugo Villeneuve u8 reg_clkout; /* Clkout register. */ 188420cc9e8SHugo Villeneuve unsigned int ts_count; 189420cc9e8SHugo Villeneuve struct pcf21xx_ts_config ts[PCF2127_MAX_TS_SUPPORTED]; 190420cc9e8SHugo Villeneuve struct attribute_group attribute_group; 191fd28ceb4SHugo Villeneuve }; 192fd28ceb4SHugo Villeneuve 19318cb6368SRenaud Cerrato struct pcf2127 { 19418cb6368SRenaud Cerrato struct rtc_device *rtc; 1950e735eaaSBruno Thomsen struct watchdog_device wdd; 196907b3262SAkinobu Mita struct regmap *regmap; 197fd28ceb4SHugo Villeneuve const struct pcf21xx_config *cfg; 1982f861984SMian Yousaf Kaukab bool irq_enabled; 199420cc9e8SHugo Villeneuve time64_t ts[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp values. */ 200420cc9e8SHugo Villeneuve bool ts_valid[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp valid indication. */ 20118cb6368SRenaud Cerrato }; 20218cb6368SRenaud Cerrato 20318cb6368SRenaud Cerrato /* 20418cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 20518cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 20618cb6368SRenaud Cerrato */ 207907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 20818cb6368SRenaud Cerrato { 209907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 21031f077c3SHugo Villeneuve unsigned char buf[7]; 211907b3262SAkinobu Mita int ret; 21218cb6368SRenaud Cerrato 2137f43020eSBruno Thomsen /* 2147f43020eSBruno Thomsen * Avoid reading CTRL2 register as it causes WD_VAL register 2157f43020eSBruno Thomsen * value to reset to 0 which means watchdog is stopped. 2167f43020eSBruno Thomsen */ 2176211aceeSHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->reg_time_base, 2186211aceeSHugo Villeneuve buf, sizeof(buf)); 219907b3262SAkinobu Mita if (ret) { 220907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 221907b3262SAkinobu Mita return ret; 22218cb6368SRenaud Cerrato } 22318cb6368SRenaud Cerrato 224bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 22531f077c3SHugo Villeneuve if (buf[0] & PCF2127_BIT_SC_OSF) { 226653ebd75SAndrea Scian /* 227653ebd75SAndrea Scian * no need clear the flag here, 228653ebd75SAndrea Scian * it will be cleared once the new date is saved 229653ebd75SAndrea Scian */ 230907b3262SAkinobu Mita dev_warn(dev, 231653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 232653ebd75SAndrea Scian return -EINVAL; 23318cb6368SRenaud Cerrato } 23418cb6368SRenaud Cerrato 235907b3262SAkinobu Mita dev_dbg(dev, 23631f077c3SHugo Villeneuve "%s: raw data is sec=%02x, min=%02x, hr=%02x, " 23718cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 23831f077c3SHugo Villeneuve __func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); 23918cb6368SRenaud Cerrato 24031f077c3SHugo Villeneuve tm->tm_sec = bcd2bin(buf[0] & 0x7F); 24131f077c3SHugo Villeneuve tm->tm_min = bcd2bin(buf[1] & 0x7F); 2420476b6c8SHugo Villeneuve tm->tm_hour = bcd2bin(buf[2] & 0x3F); 24331f077c3SHugo Villeneuve tm->tm_mday = bcd2bin(buf[3] & 0x3F); 24431f077c3SHugo Villeneuve tm->tm_wday = buf[4] & 0x07; 2450476b6c8SHugo Villeneuve tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; 24631f077c3SHugo Villeneuve tm->tm_year = bcd2bin(buf[6]); 247b139bb5cSAlexandre Belloni tm->tm_year += 100; 24818cb6368SRenaud Cerrato 249907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 25018cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 25118cb6368SRenaud Cerrato __func__, 25218cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 25318cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 25418cb6368SRenaud Cerrato 25522652ba7SAlexandre Belloni return 0; 25618cb6368SRenaud Cerrato } 25718cb6368SRenaud Cerrato 258907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 25918cb6368SRenaud Cerrato { 260907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 261907b3262SAkinobu Mita unsigned char buf[7]; 26218cb6368SRenaud Cerrato int i = 0, err; 26318cb6368SRenaud Cerrato 264907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 26518cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 26618cb6368SRenaud Cerrato __func__, 26718cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 26818cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 26918cb6368SRenaud Cerrato 27018cb6368SRenaud Cerrato /* hours, minutes and seconds */ 271653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 27218cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 27318cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 27418cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 27518cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 27618cb6368SRenaud Cerrato 27718cb6368SRenaud Cerrato /* month, 1 - 12 */ 27818cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 27918cb6368SRenaud Cerrato 28018cb6368SRenaud Cerrato /* year */ 281b139bb5cSAlexandre Belloni buf[i++] = bin2bcd(tm->tm_year - 100); 28218cb6368SRenaud Cerrato 28318cb6368SRenaud Cerrato /* write register's data */ 2846211aceeSHugo Villeneuve err = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->reg_time_base, buf, i); 285907b3262SAkinobu Mita if (err) { 2863d740c64SHugo Villeneuve dev_dbg(dev, "%s: err=%d", __func__, err); 287907b3262SAkinobu Mita return err; 28818cb6368SRenaud Cerrato } 28918cb6368SRenaud Cerrato 29018cb6368SRenaud Cerrato return 0; 29118cb6368SRenaud Cerrato } 29218cb6368SRenaud Cerrato 29318cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 29418cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 29518cb6368SRenaud Cerrato { 296907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 2977d65cf8cSAlexandre Belloni int val, touser = 0; 298f97cfddcSUwe Kleine-König int ret; 29918cb6368SRenaud Cerrato 30018cb6368SRenaud Cerrato switch (cmd) { 30118cb6368SRenaud Cerrato case RTC_VL_READ: 3027d65cf8cSAlexandre Belloni ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); 303907b3262SAkinobu Mita if (ret) 304f97cfddcSUwe Kleine-König return ret; 30518cb6368SRenaud Cerrato 3067d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BLF) 3077d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_LOW; 3087d65cf8cSAlexandre Belloni 3097d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BF) 3107d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_SWITCH; 311f97cfddcSUwe Kleine-König 312af427311SAlexandre Belloni return put_user(touser, (unsigned int __user *)arg); 3137d65cf8cSAlexandre Belloni 3147d65cf8cSAlexandre Belloni case RTC_VL_CLR: 3157d65cf8cSAlexandre Belloni return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 3167d65cf8cSAlexandre Belloni PCF2127_BIT_CTRL3_BF, 0); 3177d65cf8cSAlexandre Belloni 31818cb6368SRenaud Cerrato default: 31918cb6368SRenaud Cerrato return -ENOIOCTLCMD; 32018cb6368SRenaud Cerrato } 32118cb6368SRenaud Cerrato } 32218cb6368SRenaud Cerrato 323d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 324d6c3029fSUwe Kleine-König void *val, size_t bytes) 325d6c3029fSUwe Kleine-König { 326d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 327d6c3029fSUwe Kleine-König int ret; 328d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 329d6c3029fSUwe Kleine-König 330bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 331d6c3029fSUwe Kleine-König offsetbuf, 2); 332d6c3029fSUwe Kleine-König if (ret) 333d6c3029fSUwe Kleine-König return ret; 334d6c3029fSUwe Kleine-König 335ba1c30bfSDan Carpenter return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 336d6c3029fSUwe Kleine-König val, bytes); 337d6c3029fSUwe Kleine-König } 338d6c3029fSUwe Kleine-König 339d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 340d6c3029fSUwe Kleine-König void *val, size_t bytes) 341d6c3029fSUwe Kleine-König { 342d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 343d6c3029fSUwe Kleine-König int ret; 344d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 345d6c3029fSUwe Kleine-König 346bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 347d6c3029fSUwe Kleine-König offsetbuf, 2); 348d6c3029fSUwe Kleine-König if (ret) 349d6c3029fSUwe Kleine-König return ret; 350d6c3029fSUwe Kleine-König 351ba1c30bfSDan Carpenter return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 352d6c3029fSUwe Kleine-König val, bytes); 353d6c3029fSUwe Kleine-König } 354d6c3029fSUwe Kleine-König 3550e735eaaSBruno Thomsen /* watchdog driver */ 3560e735eaaSBruno Thomsen 3570e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd) 3580e735eaaSBruno Thomsen { 3590e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 3600e735eaaSBruno Thomsen 3616b57ec29SHugo Villeneuve return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, wdd->timeout); 3620e735eaaSBruno Thomsen } 3630e735eaaSBruno Thomsen 3640e735eaaSBruno Thomsen /* 3650e735eaaSBruno Thomsen * Restart watchdog timer if feature is active. 3660e735eaaSBruno Thomsen * 3670e735eaaSBruno Thomsen * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, 3680e735eaaSBruno Thomsen * since register also contain control/status flags for other features. 3690e735eaaSBruno Thomsen * Always call this function after reading CTRL2 register. 3700e735eaaSBruno Thomsen */ 3710e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) 3720e735eaaSBruno Thomsen { 3730e735eaaSBruno Thomsen int ret = 0; 3740e735eaaSBruno Thomsen 3750e735eaaSBruno Thomsen if (watchdog_active(wdd)) { 3760e735eaaSBruno Thomsen ret = pcf2127_wdt_ping(wdd); 3770e735eaaSBruno Thomsen if (ret) 3780e735eaaSBruno Thomsen dev_err(wdd->parent, 3790e735eaaSBruno Thomsen "%s: watchdog restart failed, ret=%d\n", 3800e735eaaSBruno Thomsen __func__, ret); 3810e735eaaSBruno Thomsen } 3820e735eaaSBruno Thomsen 3830e735eaaSBruno Thomsen return ret; 3840e735eaaSBruno Thomsen } 3850e735eaaSBruno Thomsen 3860e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd) 3870e735eaaSBruno Thomsen { 3880e735eaaSBruno Thomsen return pcf2127_wdt_ping(wdd); 3890e735eaaSBruno Thomsen } 3900e735eaaSBruno Thomsen 3910e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd) 3920e735eaaSBruno Thomsen { 3930e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 3940e735eaaSBruno Thomsen 3956b57ec29SHugo Villeneuve return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, 3960e735eaaSBruno Thomsen PCF2127_WD_VAL_STOP); 3970e735eaaSBruno Thomsen } 3980e735eaaSBruno Thomsen 3990e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, 4000e735eaaSBruno Thomsen unsigned int new_timeout) 4010e735eaaSBruno Thomsen { 4020e735eaaSBruno Thomsen dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", 4030e735eaaSBruno Thomsen new_timeout, wdd->timeout); 4040e735eaaSBruno Thomsen 4050e735eaaSBruno Thomsen wdd->timeout = new_timeout; 4060e735eaaSBruno Thomsen 4070e735eaaSBruno Thomsen return pcf2127_wdt_active_ping(wdd); 4080e735eaaSBruno Thomsen } 4090e735eaaSBruno Thomsen 4100e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = { 4110e735eaaSBruno Thomsen .identity = "NXP PCF2127/PCF2129 Watchdog", 4120e735eaaSBruno Thomsen .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, 4130e735eaaSBruno Thomsen }; 4140e735eaaSBruno Thomsen 4150e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = { 4160e735eaaSBruno Thomsen .owner = THIS_MODULE, 4170e735eaaSBruno Thomsen .start = pcf2127_wdt_start, 4180e735eaaSBruno Thomsen .stop = pcf2127_wdt_stop, 4190e735eaaSBruno Thomsen .ping = pcf2127_wdt_ping, 4200e735eaaSBruno Thomsen .set_timeout = pcf2127_wdt_set_timeout, 4210e735eaaSBruno Thomsen }; 4220e735eaaSBruno Thomsen 4235d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127) 4245d78533aSUwe Kleine-König { 4255d78533aSUwe Kleine-König u32 wdd_timeout; 4265d78533aSUwe Kleine-König int ret; 4275d78533aSUwe Kleine-König 42871ac1345SUwe Kleine-König if (!IS_ENABLED(CONFIG_WATCHDOG) || 42971ac1345SUwe Kleine-König !device_property_read_bool(dev, "reset-source")) 4305d78533aSUwe Kleine-König return 0; 4315d78533aSUwe Kleine-König 4325d78533aSUwe Kleine-König pcf2127->wdd.parent = dev; 4335d78533aSUwe Kleine-König pcf2127->wdd.info = &pcf2127_wdt_info; 4345d78533aSUwe Kleine-König pcf2127->wdd.ops = &pcf2127_watchdog_ops; 4355d78533aSUwe Kleine-König pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN; 4365d78533aSUwe Kleine-König pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX; 4375d78533aSUwe Kleine-König pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT; 4385d78533aSUwe Kleine-König pcf2127->wdd.min_hw_heartbeat_ms = 500; 4395d78533aSUwe Kleine-König pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS; 4405d78533aSUwe Kleine-König 4415d78533aSUwe Kleine-König watchdog_set_drvdata(&pcf2127->wdd, pcf2127); 4425d78533aSUwe Kleine-König 4435d78533aSUwe Kleine-König /* Test if watchdog timer is started by bootloader */ 4446b57ec29SHugo Villeneuve ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_wd_val, &wdd_timeout); 4455d78533aSUwe Kleine-König if (ret) 4465d78533aSUwe Kleine-König return ret; 4475d78533aSUwe Kleine-König 4485d78533aSUwe Kleine-König if (wdd_timeout) 4495d78533aSUwe Kleine-König set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); 4505d78533aSUwe Kleine-König 4515d78533aSUwe Kleine-König return devm_watchdog_register_device(dev, &pcf2127->wdd); 4525d78533aSUwe Kleine-König } 4535d78533aSUwe Kleine-König 4548a914bacSLiam Beguin /* Alarm */ 4558a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 4568a914bacSLiam Beguin { 4578a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 45873ce0530SHugo Villeneuve u8 buf[5]; 45973ce0530SHugo Villeneuve unsigned int ctrl2; 4608a914bacSLiam Beguin int ret; 4618a914bacSLiam Beguin 4628a914bacSLiam Beguin ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 4638a914bacSLiam Beguin if (ret) 4648a914bacSLiam Beguin return ret; 4658a914bacSLiam Beguin 4668a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 4678a914bacSLiam Beguin if (ret) 4688a914bacSLiam Beguin return ret; 4698a914bacSLiam Beguin 4707c6f0db4SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, 4717c6f0db4SHugo Villeneuve buf, sizeof(buf)); 4728a914bacSLiam Beguin if (ret) 4738a914bacSLiam Beguin return ret; 4748a914bacSLiam Beguin 4758a914bacSLiam Beguin alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE; 4768a914bacSLiam Beguin alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF; 4778a914bacSLiam Beguin 4788a914bacSLiam Beguin alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F); 4798a914bacSLiam Beguin alrm->time.tm_min = bcd2bin(buf[1] & 0x7F); 4808a914bacSLiam Beguin alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F); 4818a914bacSLiam Beguin alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F); 4828a914bacSLiam Beguin 4838a914bacSLiam Beguin return 0; 4848a914bacSLiam Beguin } 4858a914bacSLiam Beguin 4868a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable) 4878a914bacSLiam Beguin { 4888a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 4898a914bacSLiam Beguin int ret; 4908a914bacSLiam Beguin 4918a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 4928a914bacSLiam Beguin PCF2127_BIT_CTRL2_AIE, 4938a914bacSLiam Beguin enable ? PCF2127_BIT_CTRL2_AIE : 0); 4948a914bacSLiam Beguin if (ret) 4958a914bacSLiam Beguin return ret; 4968a914bacSLiam Beguin 4978a914bacSLiam Beguin return pcf2127_wdt_active_ping(&pcf2127->wdd); 4988a914bacSLiam Beguin } 4998a914bacSLiam Beguin 5008a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 5018a914bacSLiam Beguin { 5028a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 5038a914bacSLiam Beguin uint8_t buf[5]; 5048a914bacSLiam Beguin int ret; 5058a914bacSLiam Beguin 5068a914bacSLiam Beguin ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 5078a914bacSLiam Beguin PCF2127_BIT_CTRL2_AF, 0); 5088a914bacSLiam Beguin if (ret) 5098a914bacSLiam Beguin return ret; 5108a914bacSLiam Beguin 5118a914bacSLiam Beguin ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 5128a914bacSLiam Beguin if (ret) 5138a914bacSLiam Beguin return ret; 5148a914bacSLiam Beguin 5158a914bacSLiam Beguin buf[0] = bin2bcd(alrm->time.tm_sec); 5168a914bacSLiam Beguin buf[1] = bin2bcd(alrm->time.tm_min); 5178a914bacSLiam Beguin buf[2] = bin2bcd(alrm->time.tm_hour); 5188a914bacSLiam Beguin buf[3] = bin2bcd(alrm->time.tm_mday); 51927006416SAlexandre Belloni buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */ 5208a914bacSLiam Beguin 5217c6f0db4SHugo Villeneuve ret = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, 5227c6f0db4SHugo Villeneuve buf, sizeof(buf)); 5238a914bacSLiam Beguin if (ret) 5248a914bacSLiam Beguin return ret; 5258a914bacSLiam Beguin 5268a914bacSLiam Beguin return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled); 5278a914bacSLiam Beguin } 5288a914bacSLiam Beguin 5292f861984SMian Yousaf Kaukab /* 530420cc9e8SHugo Villeneuve * This function reads one timestamp function data, caller is responsible for 531420cc9e8SHugo Villeneuve * calling pcf2127_wdt_active_ping() 5322f861984SMian Yousaf Kaukab */ 533420cc9e8SHugo Villeneuve static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts, 534420cc9e8SHugo Villeneuve int ts_id) 5352f861984SMian Yousaf Kaukab { 5362f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 5372f861984SMian Yousaf Kaukab struct rtc_time tm; 5382f861984SMian Yousaf Kaukab int ret; 539720fb4b8SHugo Villeneuve unsigned char data[7]; 5402f861984SMian Yousaf Kaukab 541420cc9e8SHugo Villeneuve ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->ts[ts_id].reg_base, 542420cc9e8SHugo Villeneuve data, sizeof(data)); 5432f861984SMian Yousaf Kaukab if (ret) { 5442f861984SMian Yousaf Kaukab dev_err(dev, "%s: read error ret=%d\n", __func__, ret); 5452f861984SMian Yousaf Kaukab return ret; 5462f861984SMian Yousaf Kaukab } 5472f861984SMian Yousaf Kaukab 5482f861984SMian Yousaf Kaukab dev_dbg(dev, 549720fb4b8SHugo Villeneuve "%s: raw data is ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", 550720fb4b8SHugo Villeneuve __func__, data[1], data[2], data[3], data[4], data[5], data[6]); 5512f861984SMian Yousaf Kaukab 552720fb4b8SHugo Villeneuve tm.tm_sec = bcd2bin(data[1] & 0x7F); 553720fb4b8SHugo Villeneuve tm.tm_min = bcd2bin(data[2] & 0x7F); 554720fb4b8SHugo Villeneuve tm.tm_hour = bcd2bin(data[3] & 0x3F); 555720fb4b8SHugo Villeneuve tm.tm_mday = bcd2bin(data[4] & 0x3F); 5562f861984SMian Yousaf Kaukab /* TS_MO register (month) value range: 1-12 */ 557720fb4b8SHugo Villeneuve tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1; 558720fb4b8SHugo Villeneuve tm.tm_year = bcd2bin(data[6]); 5592f861984SMian Yousaf Kaukab if (tm.tm_year < 70) 5602f861984SMian Yousaf Kaukab tm.tm_year += 100; /* assume we are in 1970...2069 */ 5612f861984SMian Yousaf Kaukab 5622f861984SMian Yousaf Kaukab ret = rtc_valid_tm(&tm); 5632f861984SMian Yousaf Kaukab if (ret) { 5642f861984SMian Yousaf Kaukab dev_err(dev, "Invalid timestamp. ret=%d\n", ret); 5652f861984SMian Yousaf Kaukab return ret; 5662f861984SMian Yousaf Kaukab } 5672f861984SMian Yousaf Kaukab 5682f861984SMian Yousaf Kaukab *ts = rtc_tm_to_time64(&tm); 5692f861984SMian Yousaf Kaukab return 0; 5702f861984SMian Yousaf Kaukab }; 5712f861984SMian Yousaf Kaukab 572420cc9e8SHugo Villeneuve static void pcf2127_rtc_ts_snapshot(struct device *dev, int ts_id) 5732f861984SMian Yousaf Kaukab { 5742f861984SMian Yousaf Kaukab struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 5752f861984SMian Yousaf Kaukab int ret; 5762f861984SMian Yousaf Kaukab 577420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 5782f861984SMian Yousaf Kaukab return; 5792f861984SMian Yousaf Kaukab 580420cc9e8SHugo Villeneuve /* Let userspace read the first timestamp */ 581420cc9e8SHugo Villeneuve if (pcf2127->ts_valid[ts_id]) 582420cc9e8SHugo Villeneuve return; 583420cc9e8SHugo Villeneuve 584420cc9e8SHugo Villeneuve ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts[ts_id], ts_id); 5852f861984SMian Yousaf Kaukab if (!ret) 586420cc9e8SHugo Villeneuve pcf2127->ts_valid[ts_id] = true; 5872f861984SMian Yousaf Kaukab } 5882f861984SMian Yousaf Kaukab 5898a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev) 5908a914bacSLiam Beguin { 5918a914bacSLiam Beguin struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 592afc505bfSHugo Villeneuve unsigned int ctrl2; 5938a914bacSLiam Beguin int ret = 0; 5948a914bacSLiam Beguin 595afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); 5962f861984SMian Yousaf Kaukab if (ret) 5972f861984SMian Yousaf Kaukab return IRQ_NONE; 5982f861984SMian Yousaf Kaukab 599afc505bfSHugo Villeneuve if (pcf2127->cfg->ts_count == 1) { 600afc505bfSHugo Villeneuve /* PCF2127/29 */ 601afc505bfSHugo Villeneuve unsigned int ctrl1; 602afc505bfSHugo Villeneuve 603afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1); 6048a914bacSLiam Beguin if (ret) 6058a914bacSLiam Beguin return IRQ_NONE; 6068a914bacSLiam Beguin 6072f861984SMian Yousaf Kaukab if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK)) 60827006416SAlexandre Belloni return IRQ_NONE; 60927006416SAlexandre Belloni 6102f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2) 611420cc9e8SHugo Villeneuve pcf2127_rtc_ts_snapshot(dev, 0); 6128a914bacSLiam Beguin 6132f861984SMian Yousaf Kaukab if (ctrl1 & PCF2127_CTRL1_IRQ_MASK) 6142f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1, 6152f861984SMian Yousaf Kaukab ctrl1 & ~PCF2127_CTRL1_IRQ_MASK); 6162f861984SMian Yousaf Kaukab 6172f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_CTRL2_IRQ_MASK) 6182f861984SMian Yousaf Kaukab regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 6192f861984SMian Yousaf Kaukab ctrl2 & ~PCF2127_CTRL2_IRQ_MASK); 620afc505bfSHugo Villeneuve } else { 621afc505bfSHugo Villeneuve /* PCF2131. */ 622afc505bfSHugo Villeneuve unsigned int ctrl4; 623afc505bfSHugo Villeneuve 624afc505bfSHugo Villeneuve ret = regmap_read(pcf2127->regmap, PCF2131_REG_CTRL4, &ctrl4); 625afc505bfSHugo Villeneuve if (ret) 626afc505bfSHugo Villeneuve return IRQ_NONE; 627afc505bfSHugo Villeneuve 628afc505bfSHugo Villeneuve if (!(ctrl4 & PCF2131_CTRL4_IRQ_MASK || ctrl2 & PCF2131_CTRL2_IRQ_MASK)) 629afc505bfSHugo Villeneuve return IRQ_NONE; 630afc505bfSHugo Villeneuve 631afc505bfSHugo Villeneuve if (ctrl4 & PCF2131_CTRL4_IRQ_MASK) { 632afc505bfSHugo Villeneuve int i; 633afc505bfSHugo Villeneuve int tsf_bit = PCF2131_BIT_CTRL4_TSF1; /* Start at bit 7. */ 634afc505bfSHugo Villeneuve 635afc505bfSHugo Villeneuve for (i = 0; i < pcf2127->cfg->ts_count; i++) { 636afc505bfSHugo Villeneuve if (ctrl4 & tsf_bit) 637afc505bfSHugo Villeneuve pcf2127_rtc_ts_snapshot(dev, i); 638afc505bfSHugo Villeneuve 639afc505bfSHugo Villeneuve tsf_bit = tsf_bit >> 1; 640afc505bfSHugo Villeneuve } 641afc505bfSHugo Villeneuve 642afc505bfSHugo Villeneuve regmap_write(pcf2127->regmap, PCF2131_REG_CTRL4, 643afc505bfSHugo Villeneuve ctrl4 & ~PCF2131_CTRL4_IRQ_MASK); 644afc505bfSHugo Villeneuve } 645afc505bfSHugo Villeneuve 646afc505bfSHugo Villeneuve if (ctrl2 & PCF2131_CTRL2_IRQ_MASK) 647afc505bfSHugo Villeneuve regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, 648afc505bfSHugo Villeneuve ctrl2 & ~PCF2131_CTRL2_IRQ_MASK); 649afc505bfSHugo Villeneuve } 6502f861984SMian Yousaf Kaukab 6512f861984SMian Yousaf Kaukab if (ctrl2 & PCF2127_BIT_CTRL2_AF) 6528a914bacSLiam Beguin rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF); 6538a914bacSLiam Beguin 65427006416SAlexandre Belloni pcf2127_wdt_active_ping(&pcf2127->wdd); 6558a914bacSLiam Beguin 6568a914bacSLiam Beguin return IRQ_HANDLED; 6578a914bacSLiam Beguin } 6588a914bacSLiam Beguin 65925cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = { 6608a914bacSLiam Beguin .ioctl = pcf2127_rtc_ioctl, 6618a914bacSLiam Beguin .read_time = pcf2127_rtc_read_time, 6628a914bacSLiam Beguin .set_time = pcf2127_rtc_set_time, 6638a914bacSLiam Beguin .read_alarm = pcf2127_rtc_read_alarm, 6648a914bacSLiam Beguin .set_alarm = pcf2127_rtc_set_alarm, 6658a914bacSLiam Beguin .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable, 6668a914bacSLiam Beguin }; 6678a914bacSLiam Beguin 66803623b4bSBruno Thomsen /* sysfs interface */ 66903623b4bSBruno Thomsen 670420cc9e8SHugo Villeneuve static ssize_t timestamp_store(struct device *dev, 67103623b4bSBruno Thomsen struct device_attribute *attr, 672420cc9e8SHugo Villeneuve const char *buf, size_t count, int ts_id) 67303623b4bSBruno Thomsen { 67403623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 67503623b4bSBruno Thomsen int ret; 67603623b4bSBruno Thomsen 677420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 678420cc9e8SHugo Villeneuve return 0; 679420cc9e8SHugo Villeneuve 6802f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 681420cc9e8SHugo Villeneuve pcf2127->ts_valid[ts_id] = false; 6822f861984SMian Yousaf Kaukab } else { 683420cc9e8SHugo Villeneuve /* Always clear GND interrupt bit. */ 684420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 685420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_reg, 686420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_bit, 687420cc9e8SHugo Villeneuve 0); 688420cc9e8SHugo Villeneuve 68903623b4bSBruno Thomsen if (ret) { 690420cc9e8SHugo Villeneuve dev_err(dev, "%s: update TS gnd detect ret=%d\n", __func__, ret); 69103623b4bSBruno Thomsen return ret; 69203623b4bSBruno Thomsen } 69303623b4bSBruno Thomsen 694420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { 695420cc9e8SHugo Villeneuve /* Clear intermediate level interrupt bit if supported. */ 696420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 697420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_reg, 698420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_bit, 699420cc9e8SHugo Villeneuve 0); 70003623b4bSBruno Thomsen if (ret) { 701420cc9e8SHugo Villeneuve dev_err(dev, "%s: update TS intermediate level detect ret=%d\n", 702420cc9e8SHugo Villeneuve __func__, ret); 70303623b4bSBruno Thomsen return ret; 70403623b4bSBruno Thomsen } 705420cc9e8SHugo Villeneuve } 70603623b4bSBruno Thomsen 70703623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 70803623b4bSBruno Thomsen if (ret) 70903623b4bSBruno Thomsen return ret; 7102f861984SMian Yousaf Kaukab } 71103623b4bSBruno Thomsen 71203623b4bSBruno Thomsen return count; 713420cc9e8SHugo Villeneuve } 714420cc9e8SHugo Villeneuve 715420cc9e8SHugo Villeneuve static ssize_t timestamp0_store(struct device *dev, 716420cc9e8SHugo Villeneuve struct device_attribute *attr, 717420cc9e8SHugo Villeneuve const char *buf, size_t count) 718420cc9e8SHugo Villeneuve { 719420cc9e8SHugo Villeneuve return timestamp_store(dev, attr, buf, count, 0); 72003623b4bSBruno Thomsen }; 72103623b4bSBruno Thomsen 722afc505bfSHugo Villeneuve static ssize_t timestamp1_store(struct device *dev, 723afc505bfSHugo Villeneuve struct device_attribute *attr, 724afc505bfSHugo Villeneuve const char *buf, size_t count) 725afc505bfSHugo Villeneuve { 726afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 1); 727afc505bfSHugo Villeneuve }; 728afc505bfSHugo Villeneuve 729afc505bfSHugo Villeneuve static ssize_t timestamp2_store(struct device *dev, 730afc505bfSHugo Villeneuve struct device_attribute *attr, 731afc505bfSHugo Villeneuve const char *buf, size_t count) 732afc505bfSHugo Villeneuve { 733afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 2); 734afc505bfSHugo Villeneuve }; 735afc505bfSHugo Villeneuve 736afc505bfSHugo Villeneuve static ssize_t timestamp3_store(struct device *dev, 737afc505bfSHugo Villeneuve struct device_attribute *attr, 738afc505bfSHugo Villeneuve const char *buf, size_t count) 739afc505bfSHugo Villeneuve { 740afc505bfSHugo Villeneuve return timestamp_store(dev, attr, buf, count, 3); 741afc505bfSHugo Villeneuve }; 742afc505bfSHugo Villeneuve 743420cc9e8SHugo Villeneuve static ssize_t timestamp_show(struct device *dev, 744420cc9e8SHugo Villeneuve struct device_attribute *attr, char *buf, 745420cc9e8SHugo Villeneuve int ts_id) 74603623b4bSBruno Thomsen { 74703623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 74803623b4bSBruno Thomsen int ret; 7492f861984SMian Yousaf Kaukab time64_t ts; 75003623b4bSBruno Thomsen 751420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) 752420cc9e8SHugo Villeneuve return 0; 753420cc9e8SHugo Villeneuve 7542f861984SMian Yousaf Kaukab if (pcf2127->irq_enabled) { 755420cc9e8SHugo Villeneuve if (!pcf2127->ts_valid[ts_id]) 7562f861984SMian Yousaf Kaukab return 0; 757420cc9e8SHugo Villeneuve ts = pcf2127->ts[ts_id]; 7582f861984SMian Yousaf Kaukab } else { 759420cc9e8SHugo Villeneuve u8 valid_low = 0; 760420cc9e8SHugo Villeneuve u8 valid_inter = 0; 761420cc9e8SHugo Villeneuve unsigned int ctrl; 762420cc9e8SHugo Villeneuve 763420cc9e8SHugo Villeneuve /* Check if TS input pin is driven to GND, supported by all 764420cc9e8SHugo Villeneuve * variants. 765420cc9e8SHugo Villeneuve */ 766420cc9e8SHugo Villeneuve ret = regmap_read(pcf2127->regmap, 767420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].gnd_detect_reg, 768420cc9e8SHugo Villeneuve &ctrl); 7692f861984SMian Yousaf Kaukab if (ret) 7702f861984SMian Yousaf Kaukab return 0; 77103623b4bSBruno Thomsen 772420cc9e8SHugo Villeneuve valid_low = ctrl & pcf2127->cfg->ts[ts_id].gnd_detect_bit; 773420cc9e8SHugo Villeneuve 774420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { 775420cc9e8SHugo Villeneuve /* Check if TS input pin is driven to intermediate level 776420cc9e8SHugo Villeneuve * between GND and supply, if supported by variant. 777420cc9e8SHugo Villeneuve */ 778420cc9e8SHugo Villeneuve ret = regmap_read(pcf2127->regmap, 779420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].inter_detect_reg, 780420cc9e8SHugo Villeneuve &ctrl); 7812f861984SMian Yousaf Kaukab if (ret) 7822f861984SMian Yousaf Kaukab return 0; 7832f861984SMian Yousaf Kaukab 784420cc9e8SHugo Villeneuve valid_inter = ctrl & pcf2127->cfg->ts[ts_id].inter_detect_bit; 785420cc9e8SHugo Villeneuve } 786420cc9e8SHugo Villeneuve 787420cc9e8SHugo Villeneuve if (!valid_low && !valid_inter) 7882f861984SMian Yousaf Kaukab return 0; 7892f861984SMian Yousaf Kaukab 790420cc9e8SHugo Villeneuve ret = pcf2127_rtc_ts_read(dev->parent, &ts, ts_id); 7912f861984SMian Yousaf Kaukab if (ret) 7922f861984SMian Yousaf Kaukab return 0; 79303623b4bSBruno Thomsen 79403623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 79503623b4bSBruno Thomsen if (ret) 79603623b4bSBruno Thomsen return ret; 7972f861984SMian Yousaf Kaukab } 7982f861984SMian Yousaf Kaukab return sprintf(buf, "%llu\n", (unsigned long long)ts); 799420cc9e8SHugo Villeneuve } 800420cc9e8SHugo Villeneuve 801420cc9e8SHugo Villeneuve static ssize_t timestamp0_show(struct device *dev, 802420cc9e8SHugo Villeneuve struct device_attribute *attr, char *buf) 803420cc9e8SHugo Villeneuve { 804420cc9e8SHugo Villeneuve return timestamp_show(dev, attr, buf, 0); 80503623b4bSBruno Thomsen }; 80603623b4bSBruno Thomsen 807afc505bfSHugo Villeneuve static ssize_t timestamp1_show(struct device *dev, 808afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 809afc505bfSHugo Villeneuve { 810afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 1); 811afc505bfSHugo Villeneuve }; 812afc505bfSHugo Villeneuve 813afc505bfSHugo Villeneuve static ssize_t timestamp2_show(struct device *dev, 814afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 815afc505bfSHugo Villeneuve { 816afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 2); 817afc505bfSHugo Villeneuve }; 818afc505bfSHugo Villeneuve 819afc505bfSHugo Villeneuve static ssize_t timestamp3_show(struct device *dev, 820afc505bfSHugo Villeneuve struct device_attribute *attr, char *buf) 821afc505bfSHugo Villeneuve { 822afc505bfSHugo Villeneuve return timestamp_show(dev, attr, buf, 3); 823afc505bfSHugo Villeneuve }; 824afc505bfSHugo Villeneuve 82503623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0); 826afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp1); 827afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp2); 828afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp3); 82903623b4bSBruno Thomsen 83003623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = { 83103623b4bSBruno Thomsen &dev_attr_timestamp0.attr, 83203623b4bSBruno Thomsen NULL 83303623b4bSBruno Thomsen }; 83403623b4bSBruno Thomsen 835afc505bfSHugo Villeneuve static struct attribute *pcf2131_attrs[] = { 836afc505bfSHugo Villeneuve &dev_attr_timestamp0.attr, 837afc505bfSHugo Villeneuve &dev_attr_timestamp1.attr, 838afc505bfSHugo Villeneuve &dev_attr_timestamp2.attr, 839afc505bfSHugo Villeneuve &dev_attr_timestamp3.attr, 840afc505bfSHugo Villeneuve NULL 841afc505bfSHugo Villeneuve }; 842afc505bfSHugo Villeneuve 843fd28ceb4SHugo Villeneuve static struct pcf21xx_config pcf21xx_cfg[] = { 844fd28ceb4SHugo Villeneuve [PCF2127] = { 845fd28ceb4SHugo Villeneuve .type = PCF2127, 846fd28ceb4SHugo Villeneuve .max_register = 0x1d, 847fd28ceb4SHugo Villeneuve .has_nvmem = 1, 848fd28ceb4SHugo Villeneuve .has_bit_wd_ctl_cd0 = 1, 849*e1849b8fSHugo Villeneuve .has_int_a_b = 0, 8506211aceeSHugo Villeneuve .reg_time_base = PCF2127_REG_TIME_BASE, 8517c6f0db4SHugo Villeneuve .regs_alarm_base = PCF2127_REG_ALARM_BASE, 8526b57ec29SHugo Villeneuve .reg_wd_ctl = PCF2127_REG_WD_CTL, 8536b57ec29SHugo Villeneuve .reg_wd_val = PCF2127_REG_WD_VAL, 854fc16599eSHugo Villeneuve .reg_clkout = PCF2127_REG_CLKOUT, 855420cc9e8SHugo Villeneuve .ts_count = 1, 856420cc9e8SHugo Villeneuve .ts[0] = { 857420cc9e8SHugo Villeneuve .reg_base = PCF2127_REG_TS1_BASE, 858420cc9e8SHugo Villeneuve .gnd_detect_reg = PCF2127_REG_CTRL1, 859420cc9e8SHugo Villeneuve .gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1, 860420cc9e8SHugo Villeneuve .inter_detect_reg = PCF2127_REG_CTRL2, 861420cc9e8SHugo Villeneuve .inter_detect_bit = PCF2127_BIT_CTRL2_TSF2, 862420cc9e8SHugo Villeneuve .ie_reg = PCF2127_REG_CTRL2, 863420cc9e8SHugo Villeneuve .ie_bit = PCF2127_BIT_CTRL2_TSIE, 864420cc9e8SHugo Villeneuve }, 865420cc9e8SHugo Villeneuve .attribute_group = { 866420cc9e8SHugo Villeneuve .attrs = pcf2127_attrs, 867420cc9e8SHugo Villeneuve }, 868fd28ceb4SHugo Villeneuve }, 869fd28ceb4SHugo Villeneuve [PCF2129] = { 870fd28ceb4SHugo Villeneuve .type = PCF2129, 871fd28ceb4SHugo Villeneuve .max_register = 0x19, 872fd28ceb4SHugo Villeneuve .has_nvmem = 0, 873fd28ceb4SHugo Villeneuve .has_bit_wd_ctl_cd0 = 0, 874*e1849b8fSHugo Villeneuve .has_int_a_b = 0, 8756211aceeSHugo Villeneuve .reg_time_base = PCF2127_REG_TIME_BASE, 8767c6f0db4SHugo Villeneuve .regs_alarm_base = PCF2127_REG_ALARM_BASE, 8776b57ec29SHugo Villeneuve .reg_wd_ctl = PCF2127_REG_WD_CTL, 8786b57ec29SHugo Villeneuve .reg_wd_val = PCF2127_REG_WD_VAL, 879fc16599eSHugo Villeneuve .reg_clkout = PCF2127_REG_CLKOUT, 880420cc9e8SHugo Villeneuve .ts_count = 1, 881420cc9e8SHugo Villeneuve .ts[0] = { 882420cc9e8SHugo Villeneuve .reg_base = PCF2127_REG_TS1_BASE, 883420cc9e8SHugo Villeneuve .gnd_detect_reg = PCF2127_REG_CTRL1, 884420cc9e8SHugo Villeneuve .gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1, 885420cc9e8SHugo Villeneuve .inter_detect_reg = PCF2127_REG_CTRL2, 886420cc9e8SHugo Villeneuve .inter_detect_bit = PCF2127_BIT_CTRL2_TSF2, 887420cc9e8SHugo Villeneuve .ie_reg = PCF2127_REG_CTRL2, 888420cc9e8SHugo Villeneuve .ie_bit = PCF2127_BIT_CTRL2_TSIE, 889420cc9e8SHugo Villeneuve }, 890420cc9e8SHugo Villeneuve .attribute_group = { 891420cc9e8SHugo Villeneuve .attrs = pcf2127_attrs, 892420cc9e8SHugo Villeneuve }, 893fd28ceb4SHugo Villeneuve }, 894afc505bfSHugo Villeneuve [PCF2131] = { 895afc505bfSHugo Villeneuve .type = PCF2131, 896afc505bfSHugo Villeneuve .max_register = 0x36, 897afc505bfSHugo Villeneuve .has_nvmem = 0, 898afc505bfSHugo Villeneuve .has_bit_wd_ctl_cd0 = 0, 899*e1849b8fSHugo Villeneuve .has_int_a_b = 1, 900afc505bfSHugo Villeneuve .reg_time_base = PCF2131_REG_TIME_BASE, 901afc505bfSHugo Villeneuve .regs_alarm_base = PCF2131_REG_ALARM_BASE, 902afc505bfSHugo Villeneuve .reg_wd_ctl = PCF2131_REG_WD_CTL, 903afc505bfSHugo Villeneuve .reg_wd_val = PCF2131_REG_WD_VAL, 904afc505bfSHugo Villeneuve .reg_clkout = PCF2131_REG_CLKOUT, 905afc505bfSHugo Villeneuve .ts_count = 4, 906afc505bfSHugo Villeneuve .ts[0] = { 907afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS1_BASE, 908afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 909afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF1, 910afc505bfSHugo Villeneuve .inter_detect_bit = 0, 911afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 912afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE1, 913afc505bfSHugo Villeneuve }, 914afc505bfSHugo Villeneuve .ts[1] = { 915afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS2_BASE, 916afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 917afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF2, 918afc505bfSHugo Villeneuve .inter_detect_bit = 0, 919afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 920afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE2, 921afc505bfSHugo Villeneuve }, 922afc505bfSHugo Villeneuve .ts[2] = { 923afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS3_BASE, 924afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 925afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF3, 926afc505bfSHugo Villeneuve .inter_detect_bit = 0, 927afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 928afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE3, 929afc505bfSHugo Villeneuve }, 930afc505bfSHugo Villeneuve .ts[3] = { 931afc505bfSHugo Villeneuve .reg_base = PCF2131_REG_TS4_BASE, 932afc505bfSHugo Villeneuve .gnd_detect_reg = PCF2131_REG_CTRL4, 933afc505bfSHugo Villeneuve .gnd_detect_bit = PCF2131_BIT_CTRL4_TSF4, 934afc505bfSHugo Villeneuve .inter_detect_bit = 0, 935afc505bfSHugo Villeneuve .ie_reg = PCF2131_REG_CTRL5, 936afc505bfSHugo Villeneuve .ie_bit = PCF2131_BIT_CTRL5_TSIE4, 937afc505bfSHugo Villeneuve }, 938afc505bfSHugo Villeneuve .attribute_group = { 939afc505bfSHugo Villeneuve .attrs = pcf2131_attrs, 940afc505bfSHugo Villeneuve }, 941afc505bfSHugo Villeneuve }, 942fd28ceb4SHugo Villeneuve }; 943fd28ceb4SHugo Villeneuve 944420cc9e8SHugo Villeneuve /* 945420cc9e8SHugo Villeneuve * Enable timestamp function and corresponding interrupt(s). 946420cc9e8SHugo Villeneuve */ 947420cc9e8SHugo Villeneuve static int pcf2127_enable_ts(struct device *dev, int ts_id) 948420cc9e8SHugo Villeneuve { 949420cc9e8SHugo Villeneuve struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 950420cc9e8SHugo Villeneuve int ret; 951420cc9e8SHugo Villeneuve 952420cc9e8SHugo Villeneuve if (ts_id >= pcf2127->cfg->ts_count) { 953420cc9e8SHugo Villeneuve dev_err(dev, "%s: invalid tamper detection ID (%d)\n", 954420cc9e8SHugo Villeneuve __func__, ts_id); 955420cc9e8SHugo Villeneuve return -EINVAL; 956420cc9e8SHugo Villeneuve } 957420cc9e8SHugo Villeneuve 958420cc9e8SHugo Villeneuve /* Enable timestamp function. */ 959420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, 960420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].reg_base, 961420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSOFF | 962420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSM, 963420cc9e8SHugo Villeneuve PCF2127_BIT_TS_CTRL_TSM); 964420cc9e8SHugo Villeneuve if (ret) { 965420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection config (ts%d_ctrl) failed\n", 966420cc9e8SHugo Villeneuve __func__, ts_id); 967420cc9e8SHugo Villeneuve return ret; 968420cc9e8SHugo Villeneuve } 969420cc9e8SHugo Villeneuve 970420cc9e8SHugo Villeneuve /* TS input pin driven to GND detection is supported by all variants. 971420cc9e8SHugo Villeneuve * Make sure that interrupt bit is defined. 972420cc9e8SHugo Villeneuve */ 973420cc9e8SHugo Villeneuve if (pcf2127->cfg->ts[ts_id].gnd_detect_bit == 0) { 974420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection to GND configuration invalid\n", 975420cc9e8SHugo Villeneuve __func__); 976420cc9e8SHugo Villeneuve return ret; 977420cc9e8SHugo Villeneuve } 978420cc9e8SHugo Villeneuve 979420cc9e8SHugo Villeneuve /* 980420cc9e8SHugo Villeneuve * Enable interrupt generation when TSF timestamp flag is set. 981420cc9e8SHugo Villeneuve * Interrupt signals are open-drain outputs and can be left floating if 982420cc9e8SHugo Villeneuve * unused. 983420cc9e8SHugo Villeneuve */ 984420cc9e8SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->ts[ts_id].ie_reg, 985420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].ie_bit, 986420cc9e8SHugo Villeneuve pcf2127->cfg->ts[ts_id].ie_bit); 987420cc9e8SHugo Villeneuve if (ret) { 988420cc9e8SHugo Villeneuve dev_err(dev, "%s: tamper detection TSIE%d config failed\n", 989420cc9e8SHugo Villeneuve __func__, ts_id); 990420cc9e8SHugo Villeneuve return ret; 991420cc9e8SHugo Villeneuve } 992420cc9e8SHugo Villeneuve 993420cc9e8SHugo Villeneuve return ret; 994420cc9e8SHugo Villeneuve } 995420cc9e8SHugo Villeneuve 996*e1849b8fSHugo Villeneuve /* Route all interrupt sources to INT A pin. */ 997*e1849b8fSHugo Villeneuve static int pcf2127_configure_interrupt_pins(struct device *dev) 998*e1849b8fSHugo Villeneuve { 999*e1849b8fSHugo Villeneuve struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 1000*e1849b8fSHugo Villeneuve int ret; 1001*e1849b8fSHugo Villeneuve 1002*e1849b8fSHugo Villeneuve /* Mask bits need to be cleared to enable corresponding 1003*e1849b8fSHugo Villeneuve * interrupt source. 1004*e1849b8fSHugo Villeneuve */ 1005*e1849b8fSHugo Villeneuve ret = regmap_write(pcf2127->regmap, 1006*e1849b8fSHugo Villeneuve PCF2131_REG_INT_A_MASK1, 0); 1007*e1849b8fSHugo Villeneuve if (ret) 1008*e1849b8fSHugo Villeneuve return ret; 1009*e1849b8fSHugo Villeneuve 1010*e1849b8fSHugo Villeneuve ret = regmap_write(pcf2127->regmap, 1011*e1849b8fSHugo Villeneuve PCF2131_REG_INT_A_MASK2, 0); 1012*e1849b8fSHugo Villeneuve if (ret) 1013*e1849b8fSHugo Villeneuve return ret; 1014*e1849b8fSHugo Villeneuve 1015*e1849b8fSHugo Villeneuve return ret; 1016*e1849b8fSHugo Villeneuve } 1017*e1849b8fSHugo Villeneuve 1018907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 1019fd28ceb4SHugo Villeneuve int alarm_irq, const char *name, const struct pcf21xx_config *config) 102018cb6368SRenaud Cerrato { 102118cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 1022d6c3029fSUwe Kleine-König int ret = 0; 102315f57b3eSPhilipp Rosenberger unsigned int val; 102418cb6368SRenaud Cerrato 1025907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 102618cb6368SRenaud Cerrato 1027907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 102818cb6368SRenaud Cerrato if (!pcf2127) 102918cb6368SRenaud Cerrato return -ENOMEM; 103018cb6368SRenaud Cerrato 1031907b3262SAkinobu Mita pcf2127->regmap = regmap; 1032fd28ceb4SHugo Villeneuve pcf2127->cfg = config; 103318cb6368SRenaud Cerrato 1034907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 1035907b3262SAkinobu Mita 1036e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 1037d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 1038d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 103918cb6368SRenaud Cerrato 1040e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 1041b139bb5cSAlexandre Belloni pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 1042b139bb5cSAlexandre Belloni pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; 1043b139bb5cSAlexandre Belloni pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ 1044bda10273SAlexandre Belloni set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features); 1045689fafd5SAlexandre Belloni clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features); 104625cbe9c8SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 1047e788771cSBruno Thomsen 104835425bafSBiwen Li if (alarm_irq > 0) { 1049d4785b46SHugo Villeneuve unsigned long flags; 1050d4785b46SHugo Villeneuve 1051d4785b46SHugo Villeneuve /* 1052d4785b46SHugo Villeneuve * If flags = 0, devm_request_threaded_irq() will use IRQ flags 1053d4785b46SHugo Villeneuve * obtained from device tree. 1054d4785b46SHugo Villeneuve */ 1055d4785b46SHugo Villeneuve if (dev_fwnode(dev)) 1056d4785b46SHugo Villeneuve flags = 0; 1057d4785b46SHugo Villeneuve else 1058d4785b46SHugo Villeneuve flags = IRQF_TRIGGER_LOW; 1059d4785b46SHugo Villeneuve 106027006416SAlexandre Belloni ret = devm_request_threaded_irq(dev, alarm_irq, NULL, 106127006416SAlexandre Belloni pcf2127_rtc_irq, 1062d4785b46SHugo Villeneuve flags | IRQF_ONESHOT, 10638a914bacSLiam Beguin dev_name(dev), dev); 10648a914bacSLiam Beguin if (ret) { 10658a914bacSLiam Beguin dev_err(dev, "failed to request alarm irq\n"); 10668a914bacSLiam Beguin return ret; 10678a914bacSLiam Beguin } 10682f861984SMian Yousaf Kaukab pcf2127->irq_enabled = true; 10698a914bacSLiam Beguin } 10708a914bacSLiam Beguin 107135425bafSBiwen Li if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) { 10728a914bacSLiam Beguin device_init_wakeup(dev, true); 107325cbe9c8SAlexandre Belloni set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); 10748a914bacSLiam Beguin } 10758a914bacSLiam Beguin 1076*e1849b8fSHugo Villeneuve if (pcf2127->cfg->has_int_a_b) { 1077*e1849b8fSHugo Villeneuve /* Configure int A/B pins, independently of alarm_irq. */ 1078*e1849b8fSHugo Villeneuve ret = pcf2127_configure_interrupt_pins(dev); 1079*e1849b8fSHugo Villeneuve if (ret) { 1080*e1849b8fSHugo Villeneuve dev_err(dev, "failed to configure interrupt pins\n"); 1081*e1849b8fSHugo Villeneuve return ret; 1082*e1849b8fSHugo Villeneuve } 1083*e1849b8fSHugo Villeneuve } 1084*e1849b8fSHugo Villeneuve 1085fd28ceb4SHugo Villeneuve if (pcf2127->cfg->has_nvmem) { 1086d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 1087d6c3029fSUwe Kleine-König .priv = pcf2127, 1088d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 1089d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 1090d6c3029fSUwe Kleine-König .size = 512, 1091d6c3029fSUwe Kleine-König }; 1092d6c3029fSUwe Kleine-König 10933a905c2dSBartosz Golaszewski ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 1094d6c3029fSUwe Kleine-König } 1095d6c3029fSUwe Kleine-König 10960e735eaaSBruno Thomsen /* 1097b9ac079aSPhilipp Rosenberger * The "Power-On Reset Override" facility prevents the RTC to do a reset 1098b9ac079aSPhilipp Rosenberger * after power on. For normal operation the PORO must be disabled. 1099b9ac079aSPhilipp Rosenberger */ 1100b9ac079aSPhilipp Rosenberger regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 1101b9ac079aSPhilipp Rosenberger PCF2127_BIT_CTRL1_POR_OVRD); 1102b9ac079aSPhilipp Rosenberger 1103fc16599eSHugo Villeneuve ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_clkout, &val); 110415f57b3eSPhilipp Rosenberger if (ret < 0) 110515f57b3eSPhilipp Rosenberger return ret; 110615f57b3eSPhilipp Rosenberger 110715f57b3eSPhilipp Rosenberger if (!(val & PCF2127_BIT_CLKOUT_OTPR)) { 1108fc16599eSHugo Villeneuve ret = regmap_set_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout, 110915f57b3eSPhilipp Rosenberger PCF2127_BIT_CLKOUT_OTPR); 111015f57b3eSPhilipp Rosenberger if (ret < 0) 111115f57b3eSPhilipp Rosenberger return ret; 111215f57b3eSPhilipp Rosenberger 111315f57b3eSPhilipp Rosenberger msleep(100); 111415f57b3eSPhilipp Rosenberger } 111515f57b3eSPhilipp Rosenberger 1116b9ac079aSPhilipp Rosenberger /* 11170e735eaaSBruno Thomsen * Watchdog timer enabled and reset pin /RST activated when timed out. 11180e735eaaSBruno Thomsen * Select 1Hz clock source for watchdog timer. 11190e735eaaSBruno Thomsen * Note: Countdown timer disabled and not available. 1120afc505bfSHugo Villeneuve * For pca2129, pcf2129 and pcf2131, only bit[7] is for Symbol WD_CD 11212843d565SBiwen Li * of register watchdg_tim_ctl. The bit[6] is labeled 11222843d565SBiwen Li * as T. Bits labeled as T must always be written with 11232843d565SBiwen Li * logic 0. 11240e735eaaSBruno Thomsen */ 11256b57ec29SHugo Villeneuve ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_wd_ctl, 11260e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 11270e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 11280e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1 | 11290e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF0, 11300e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 1131fd28ceb4SHugo Villeneuve (pcf2127->cfg->has_bit_wd_ctl_cd0 ? PCF2127_BIT_WD_CTL_CD0 : 0) | 11320e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1); 11330e735eaaSBruno Thomsen if (ret) { 11340e735eaaSBruno Thomsen dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); 11350e735eaaSBruno Thomsen return ret; 11360e735eaaSBruno Thomsen } 11370e735eaaSBruno Thomsen 11385d78533aSUwe Kleine-König pcf2127_watchdog_init(dev, pcf2127); 11390e735eaaSBruno Thomsen 114003623b4bSBruno Thomsen /* 114103623b4bSBruno Thomsen * Disable battery low/switch-over timestamp and interrupts. 114203623b4bSBruno Thomsen * Clear battery interrupt flags which can block new trigger events. 114303623b4bSBruno Thomsen * Note: This is the default chip behaviour but added to ensure 114403623b4bSBruno Thomsen * correct tamper timestamp and interrupt function. 114503623b4bSBruno Thomsen */ 114603623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 114703623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BTSE | 114803623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BIE | 114903623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BLIE, 0); 115003623b4bSBruno Thomsen if (ret) { 115103623b4bSBruno Thomsen dev_err(dev, "%s: interrupt config (ctrl3) failed\n", 115203623b4bSBruno Thomsen __func__); 115303623b4bSBruno Thomsen return ret; 115403623b4bSBruno Thomsen } 115503623b4bSBruno Thomsen 115603623b4bSBruno Thomsen /* 1157420cc9e8SHugo Villeneuve * Enable timestamp functions 1 to 4. 115803623b4bSBruno Thomsen */ 1159420cc9e8SHugo Villeneuve for (int i = 0; i < pcf2127->cfg->ts_count; i++) { 1160420cc9e8SHugo Villeneuve ret = pcf2127_enable_ts(dev, i); 1161420cc9e8SHugo Villeneuve if (ret) 116203623b4bSBruno Thomsen return ret; 116303623b4bSBruno Thomsen } 116403623b4bSBruno Thomsen 1165420cc9e8SHugo Villeneuve ret = rtc_add_group(pcf2127->rtc, &pcf2127->cfg->attribute_group); 116603623b4bSBruno Thomsen if (ret) { 116703623b4bSBruno Thomsen dev_err(dev, "%s: tamper sysfs registering failed\n", 116803623b4bSBruno Thomsen __func__); 116903623b4bSBruno Thomsen return ret; 117003623b4bSBruno Thomsen } 117103623b4bSBruno Thomsen 1172fdcfd854SBartosz Golaszewski return devm_rtc_register_device(pcf2127->rtc); 117318cb6368SRenaud Cerrato } 117418cb6368SRenaud Cerrato 117518cb6368SRenaud Cerrato #ifdef CONFIG_OF 117618cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 1177fd28ceb4SHugo Villeneuve { .compatible = "nxp,pcf2127", .data = &pcf21xx_cfg[PCF2127] }, 1178fd28ceb4SHugo Villeneuve { .compatible = "nxp,pcf2129", .data = &pcf21xx_cfg[PCF2129] }, 1179fd28ceb4SHugo Villeneuve { .compatible = "nxp,pca2129", .data = &pcf21xx_cfg[PCF2129] }, 1180afc505bfSHugo Villeneuve { .compatible = "nxp,pcf2131", .data = &pcf21xx_cfg[PCF2131] }, 118118cb6368SRenaud Cerrato {} 118218cb6368SRenaud Cerrato }; 118318cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 118418cb6368SRenaud Cerrato #endif 118518cb6368SRenaud Cerrato 11869408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 11879408ec1aSAkinobu Mita 1188907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 1189907b3262SAkinobu Mita { 1190907b3262SAkinobu Mita struct device *dev = context; 1191907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1192907b3262SAkinobu Mita int ret; 1193907b3262SAkinobu Mita 1194907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 1195907b3262SAkinobu Mita if (ret != count) 1196907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1197907b3262SAkinobu Mita 1198907b3262SAkinobu Mita return 0; 1199907b3262SAkinobu Mita } 1200907b3262SAkinobu Mita 1201907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 1202907b3262SAkinobu Mita const void *reg, size_t reg_size, 1203907b3262SAkinobu Mita const void *val, size_t val_size) 1204907b3262SAkinobu Mita { 1205907b3262SAkinobu Mita struct device *dev = context; 1206907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1207907b3262SAkinobu Mita int ret; 1208907b3262SAkinobu Mita void *buf; 1209907b3262SAkinobu Mita 1210907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 1211907b3262SAkinobu Mita return -EINVAL; 1212907b3262SAkinobu Mita 1213907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 1214907b3262SAkinobu Mita if (!buf) 1215907b3262SAkinobu Mita return -ENOMEM; 1216907b3262SAkinobu Mita 1217907b3262SAkinobu Mita memcpy(buf, reg, 1); 1218907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 1219907b3262SAkinobu Mita 1220907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 12219bde0afbSXulin Sun 12229bde0afbSXulin Sun kfree(buf); 12239bde0afbSXulin Sun 1224907b3262SAkinobu Mita if (ret != val_size + 1) 1225907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1226907b3262SAkinobu Mita 1227907b3262SAkinobu Mita return 0; 1228907b3262SAkinobu Mita } 1229907b3262SAkinobu Mita 1230907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 1231907b3262SAkinobu Mita void *val, size_t val_size) 1232907b3262SAkinobu Mita { 1233907b3262SAkinobu Mita struct device *dev = context; 1234907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 1235907b3262SAkinobu Mita int ret; 1236907b3262SAkinobu Mita 1237907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 1238907b3262SAkinobu Mita return -EINVAL; 1239907b3262SAkinobu Mita 1240907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 1241907b3262SAkinobu Mita if (ret != 1) 1242907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1243907b3262SAkinobu Mita 1244907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 1245907b3262SAkinobu Mita if (ret != val_size) 1246907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 1247907b3262SAkinobu Mita 1248907b3262SAkinobu Mita return 0; 1249907b3262SAkinobu Mita } 1250907b3262SAkinobu Mita 1251907b3262SAkinobu Mita /* 1252907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 1253907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 1254907b3262SAkinobu Mita * read register data when reading from registers. 1255907b3262SAkinobu Mita */ 1256907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 1257907b3262SAkinobu Mita .write = pcf2127_i2c_write, 1258907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 1259907b3262SAkinobu Mita .read = pcf2127_i2c_read, 126018cb6368SRenaud Cerrato }; 126118cb6368SRenaud Cerrato 1262907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 1263907b3262SAkinobu Mita 12645418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = { 1265fd28ceb4SHugo Villeneuve { "pcf2127", PCF2127 }, 1266fd28ceb4SHugo Villeneuve { "pcf2129", PCF2129 }, 1267fd28ceb4SHugo Villeneuve { "pca2129", PCF2129 }, 1268afc505bfSHugo Villeneuve { "pcf2131", PCF2131 }, 12695418e595SUwe Kleine-König { } 12705418e595SUwe Kleine-König }; 12715418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 12725418e595SUwe Kleine-König 12735418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client) 1274907b3262SAkinobu Mita { 1275907b3262SAkinobu Mita struct regmap *regmap; 1276fd28ceb4SHugo Villeneuve static struct regmap_config config = { 1277907b3262SAkinobu Mita .reg_bits = 8, 1278907b3262SAkinobu Mita .val_bits = 8, 1279907b3262SAkinobu Mita }; 1280fd28ceb4SHugo Villeneuve const struct pcf21xx_config *variant; 1281907b3262SAkinobu Mita 1282907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 1283907b3262SAkinobu Mita return -ENODEV; 1284907b3262SAkinobu Mita 1285fd28ceb4SHugo Villeneuve if (client->dev.of_node) { 1286fd28ceb4SHugo Villeneuve variant = of_device_get_match_data(&client->dev); 1287fd28ceb4SHugo Villeneuve if (!variant) 1288fd28ceb4SHugo Villeneuve return -ENODEV; 1289fd28ceb4SHugo Villeneuve } else { 1290fd28ceb4SHugo Villeneuve enum pcf21xx_type type = 1291fd28ceb4SHugo Villeneuve i2c_match_id(pcf2127_i2c_id, client)->driver_data; 1292fd28ceb4SHugo Villeneuve 1293fd28ceb4SHugo Villeneuve if (type >= PCF21XX_LAST_ID) 1294fd28ceb4SHugo Villeneuve return -ENODEV; 1295fd28ceb4SHugo Villeneuve variant = &pcf21xx_cfg[type]; 1296fd28ceb4SHugo Villeneuve } 1297fd28ceb4SHugo Villeneuve 1298fd28ceb4SHugo Villeneuve config.max_register = variant->max_register, 1299fd28ceb4SHugo Villeneuve 1300907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 1301907b3262SAkinobu Mita &client->dev, &config); 1302907b3262SAkinobu Mita if (IS_ERR(regmap)) { 1303907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 1304907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 1305907b3262SAkinobu Mita return PTR_ERR(regmap); 1306907b3262SAkinobu Mita } 1307907b3262SAkinobu Mita 130827006416SAlexandre Belloni return pcf2127_probe(&client->dev, regmap, client->irq, 1309fd28ceb4SHugo Villeneuve pcf2127_i2c_driver.driver.name, variant); 1310907b3262SAkinobu Mita } 1311907b3262SAkinobu Mita 1312907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 1313907b3262SAkinobu Mita .driver = { 1314907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 1315907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 1316907b3262SAkinobu Mita }, 131731b0cecbSUwe Kleine-König .probe = pcf2127_i2c_probe, 1318907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 1319907b3262SAkinobu Mita }; 13209408ec1aSAkinobu Mita 13219408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 13229408ec1aSAkinobu Mita { 13239408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 13249408ec1aSAkinobu Mita } 13259408ec1aSAkinobu Mita 13269408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 13279408ec1aSAkinobu Mita { 13289408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 13299408ec1aSAkinobu Mita } 13309408ec1aSAkinobu Mita 13319408ec1aSAkinobu Mita #else 13329408ec1aSAkinobu Mita 13339408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 13349408ec1aSAkinobu Mita { 13359408ec1aSAkinobu Mita return 0; 13369408ec1aSAkinobu Mita } 13379408ec1aSAkinobu Mita 13389408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 13399408ec1aSAkinobu Mita { 13409408ec1aSAkinobu Mita } 13419408ec1aSAkinobu Mita 13429408ec1aSAkinobu Mita #endif 13439408ec1aSAkinobu Mita 13449408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 13459408ec1aSAkinobu Mita 13469408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 1347fd28ceb4SHugo Villeneuve static const struct spi_device_id pcf2127_spi_id[]; 13489408ec1aSAkinobu Mita 13499408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 13509408ec1aSAkinobu Mita { 1351fd28ceb4SHugo Villeneuve static struct regmap_config config = { 13529408ec1aSAkinobu Mita .reg_bits = 8, 13539408ec1aSAkinobu Mita .val_bits = 8, 13549408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 13559408ec1aSAkinobu Mita .write_flag_mask = 0x20, 13569408ec1aSAkinobu Mita }; 13579408ec1aSAkinobu Mita struct regmap *regmap; 1358fd28ceb4SHugo Villeneuve const struct pcf21xx_config *variant; 1359fd28ceb4SHugo Villeneuve 1360fd28ceb4SHugo Villeneuve if (spi->dev.of_node) { 1361fd28ceb4SHugo Villeneuve variant = of_device_get_match_data(&spi->dev); 1362fd28ceb4SHugo Villeneuve if (!variant) 1363fd28ceb4SHugo Villeneuve return -ENODEV; 1364fd28ceb4SHugo Villeneuve } else { 1365fd28ceb4SHugo Villeneuve enum pcf21xx_type type = spi_get_device_id(spi)->driver_data; 1366fd28ceb4SHugo Villeneuve 1367fd28ceb4SHugo Villeneuve if (type >= PCF21XX_LAST_ID) 1368fd28ceb4SHugo Villeneuve return -ENODEV; 1369fd28ceb4SHugo Villeneuve variant = &pcf21xx_cfg[type]; 1370fd28ceb4SHugo Villeneuve } 1371fd28ceb4SHugo Villeneuve 1372fd28ceb4SHugo Villeneuve config.max_register = variant->max_register, 13739408ec1aSAkinobu Mita 13749408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 13759408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 13769408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 13779408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 13789408ec1aSAkinobu Mita return PTR_ERR(regmap); 13799408ec1aSAkinobu Mita } 13809408ec1aSAkinobu Mita 138127006416SAlexandre Belloni return pcf2127_probe(&spi->dev, regmap, spi->irq, 138227006416SAlexandre Belloni pcf2127_spi_driver.driver.name, 1383fd28ceb4SHugo Villeneuve variant); 13849408ec1aSAkinobu Mita } 13859408ec1aSAkinobu Mita 13869408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 1387fd28ceb4SHugo Villeneuve { "pcf2127", PCF2127 }, 1388fd28ceb4SHugo Villeneuve { "pcf2129", PCF2129 }, 1389fd28ceb4SHugo Villeneuve { "pca2129", PCF2129 }, 1390afc505bfSHugo Villeneuve { "pcf2131", PCF2131 }, 13919408ec1aSAkinobu Mita { } 13929408ec1aSAkinobu Mita }; 13939408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 13949408ec1aSAkinobu Mita 13959408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 13969408ec1aSAkinobu Mita .driver = { 13979408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 13989408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 13999408ec1aSAkinobu Mita }, 14009408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 14019408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 14029408ec1aSAkinobu Mita }; 14039408ec1aSAkinobu Mita 14049408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 14059408ec1aSAkinobu Mita { 14069408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 14079408ec1aSAkinobu Mita } 14089408ec1aSAkinobu Mita 14099408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 14109408ec1aSAkinobu Mita { 14119408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 14129408ec1aSAkinobu Mita } 14139408ec1aSAkinobu Mita 14149408ec1aSAkinobu Mita #else 14159408ec1aSAkinobu Mita 14169408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 14179408ec1aSAkinobu Mita { 14189408ec1aSAkinobu Mita return 0; 14199408ec1aSAkinobu Mita } 14209408ec1aSAkinobu Mita 14219408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 14229408ec1aSAkinobu Mita { 14239408ec1aSAkinobu Mita } 14249408ec1aSAkinobu Mita 14259408ec1aSAkinobu Mita #endif 14269408ec1aSAkinobu Mita 14279408ec1aSAkinobu Mita static int __init pcf2127_init(void) 14289408ec1aSAkinobu Mita { 14299408ec1aSAkinobu Mita int ret; 14309408ec1aSAkinobu Mita 14319408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 14329408ec1aSAkinobu Mita if (ret) { 14339408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 14349408ec1aSAkinobu Mita return ret; 14359408ec1aSAkinobu Mita } 14369408ec1aSAkinobu Mita 14379408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 14389408ec1aSAkinobu Mita if (ret) { 14399408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 14409408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 14419408ec1aSAkinobu Mita } 14429408ec1aSAkinobu Mita 14439408ec1aSAkinobu Mita return ret; 14449408ec1aSAkinobu Mita } 14459408ec1aSAkinobu Mita module_init(pcf2127_init) 14469408ec1aSAkinobu Mita 14479408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 14489408ec1aSAkinobu Mita { 14499408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 14509408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 14519408ec1aSAkinobu Mita } 14529408ec1aSAkinobu Mita module_exit(pcf2127_exit) 145318cb6368SRenaud Cerrato 145418cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 1455afc505bfSHugo Villeneuve MODULE_DESCRIPTION("NXP PCF2127/29/31 RTC driver"); 14564d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 1457