1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 218cb6368SRenaud Cerrato /* 3cee2cc21SAkinobu Mita * An I2C and SPI driver for the NXP PCF2127/29 RTC 418cb6368SRenaud Cerrato * Copyright 2013 Til-Technologies 518cb6368SRenaud Cerrato * 618cb6368SRenaud Cerrato * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> 718cb6368SRenaud Cerrato * 80e735eaaSBruno Thomsen * Watchdog and tamper functions 90e735eaaSBruno Thomsen * Author: Bruno Thomsen <bruno.thomsen@gmail.com> 100e735eaaSBruno Thomsen * 1118cb6368SRenaud Cerrato * based on the other drivers in this same directory. 1218cb6368SRenaud Cerrato * 13cee2cc21SAkinobu Mita * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf 1418cb6368SRenaud Cerrato */ 1518cb6368SRenaud Cerrato 1618cb6368SRenaud Cerrato #include <linux/i2c.h> 179408ec1aSAkinobu Mita #include <linux/spi/spi.h> 1818cb6368SRenaud Cerrato #include <linux/bcd.h> 1918cb6368SRenaud Cerrato #include <linux/rtc.h> 2018cb6368SRenaud Cerrato #include <linux/slab.h> 2118cb6368SRenaud Cerrato #include <linux/module.h> 2218cb6368SRenaud Cerrato #include <linux/of.h> 23907b3262SAkinobu Mita #include <linux/regmap.h> 240e735eaaSBruno Thomsen #include <linux/watchdog.h> 2518cb6368SRenaud Cerrato 26bbfe3a7aSBruno Thomsen /* Control register 1 */ 27bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1 0x00 2803623b4bSBruno Thomsen #define PCF2127_BIT_CTRL1_TSF1 BIT(4) 29bbfe3a7aSBruno Thomsen /* Control register 2 */ 30bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2 0x01 3103623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE BIT(2) 3203623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2 BIT(5) 33bbfe3a7aSBruno Thomsen /* Control register 3 */ 34bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3 0x02 3503623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE BIT(0) 3603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE BIT(1) 37bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF BIT(2) 3803623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF BIT(3) 3903623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE BIT(4) 40bbfe3a7aSBruno Thomsen /* Time and date registers */ 41bbfe3a7aSBruno Thomsen #define PCF2127_REG_SC 0x03 42bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF BIT(7) 43bbfe3a7aSBruno Thomsen #define PCF2127_REG_MN 0x04 44bbfe3a7aSBruno Thomsen #define PCF2127_REG_HR 0x05 45bbfe3a7aSBruno Thomsen #define PCF2127_REG_DM 0x06 46bbfe3a7aSBruno Thomsen #define PCF2127_REG_DW 0x07 47bbfe3a7aSBruno Thomsen #define PCF2127_REG_MO 0x08 48bbfe3a7aSBruno Thomsen #define PCF2127_REG_YR 0x09 490e735eaaSBruno Thomsen /* Watchdog registers */ 500e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL 0x10 510e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0 BIT(0) 520e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1 BIT(1) 530e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0 BIT(6) 540e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1 BIT(7) 550e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL 0x11 5603623b4bSBruno Thomsen /* Tamper timestamp registers */ 5703623b4bSBruno Thomsen #define PCF2127_REG_TS_CTRL 0x12 5803623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) 5903623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM BIT(7) 6003623b4bSBruno Thomsen #define PCF2127_REG_TS_SC 0x13 6103623b4bSBruno Thomsen #define PCF2127_REG_TS_MN 0x14 6203623b4bSBruno Thomsen #define PCF2127_REG_TS_HR 0x15 6303623b4bSBruno Thomsen #define PCF2127_REG_TS_DM 0x16 6403623b4bSBruno Thomsen #define PCF2127_REG_TS_MO 0x17 6503623b4bSBruno Thomsen #define PCF2127_REG_TS_YR 0x18 66bbfe3a7aSBruno Thomsen /* 67bbfe3a7aSBruno Thomsen * RAM registers 68bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 69bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 70bbfe3a7aSBruno Thomsen * PCF2129 doesn't have this feature. 71bbfe3a7aSBruno Thomsen */ 72bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 73bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 74bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 75f97cfddcSUwe Kleine-König 760e735eaaSBruno Thomsen /* Watchdog timer value constants */ 770e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP 0 780e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN 2 790e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX 255 800e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT 60 81653ebd75SAndrea Scian 8218cb6368SRenaud Cerrato struct pcf2127 { 8318cb6368SRenaud Cerrato struct rtc_device *rtc; 840e735eaaSBruno Thomsen struct watchdog_device wdd; 85907b3262SAkinobu Mita struct regmap *regmap; 8618cb6368SRenaud Cerrato }; 8718cb6368SRenaud Cerrato 8818cb6368SRenaud Cerrato /* 8918cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 9018cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 9118cb6368SRenaud Cerrato */ 92907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 9318cb6368SRenaud Cerrato { 94907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 95907b3262SAkinobu Mita unsigned char buf[10]; 96907b3262SAkinobu Mita int ret; 9718cb6368SRenaud Cerrato 987f43020eSBruno Thomsen /* 997f43020eSBruno Thomsen * Avoid reading CTRL2 register as it causes WD_VAL register 1007f43020eSBruno Thomsen * value to reset to 0 which means watchdog is stopped. 1017f43020eSBruno Thomsen */ 1027f43020eSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3, 1037f43020eSBruno Thomsen (buf + PCF2127_REG_CTRL3), 1047f43020eSBruno Thomsen ARRAY_SIZE(buf) - PCF2127_REG_CTRL3); 105907b3262SAkinobu Mita if (ret) { 106907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 107907b3262SAkinobu Mita return ret; 10818cb6368SRenaud Cerrato } 10918cb6368SRenaud Cerrato 110bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF) 111907b3262SAkinobu Mita dev_info(dev, 112653ebd75SAndrea Scian "low voltage detected, check/replace RTC battery.\n"); 113653ebd75SAndrea Scian 114bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 115bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) { 116653ebd75SAndrea Scian /* 117653ebd75SAndrea Scian * no need clear the flag here, 118653ebd75SAndrea Scian * it will be cleared once the new date is saved 119653ebd75SAndrea Scian */ 120907b3262SAkinobu Mita dev_warn(dev, 121653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 122653ebd75SAndrea Scian return -EINVAL; 12318cb6368SRenaud Cerrato } 12418cb6368SRenaud Cerrato 125907b3262SAkinobu Mita dev_dbg(dev, 1267f43020eSBruno Thomsen "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, " 12718cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 1287f43020eSBruno Thomsen __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC], 1297f43020eSBruno Thomsen buf[PCF2127_REG_MN], buf[PCF2127_REG_HR], 1307f43020eSBruno Thomsen buf[PCF2127_REG_DM], buf[PCF2127_REG_DW], 1317f43020eSBruno Thomsen buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]); 13218cb6368SRenaud Cerrato 13318cb6368SRenaud Cerrato tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); 13418cb6368SRenaud Cerrato tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); 13518cb6368SRenaud Cerrato tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ 13618cb6368SRenaud Cerrato tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); 13718cb6368SRenaud Cerrato tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; 13818cb6368SRenaud Cerrato tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 13918cb6368SRenaud Cerrato tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); 140b139bb5cSAlexandre Belloni tm->tm_year += 100; 14118cb6368SRenaud Cerrato 142907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 14318cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 14418cb6368SRenaud Cerrato __func__, 14518cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 14618cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 14718cb6368SRenaud Cerrato 14822652ba7SAlexandre Belloni return 0; 14918cb6368SRenaud Cerrato } 15018cb6368SRenaud Cerrato 151907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 15218cb6368SRenaud Cerrato { 153907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 154907b3262SAkinobu Mita unsigned char buf[7]; 15518cb6368SRenaud Cerrato int i = 0, err; 15618cb6368SRenaud Cerrato 157907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 15818cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 15918cb6368SRenaud Cerrato __func__, 16018cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 16118cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 16218cb6368SRenaud Cerrato 16318cb6368SRenaud Cerrato /* hours, minutes and seconds */ 164653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 16518cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 16618cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 16718cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 16818cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 16918cb6368SRenaud Cerrato 17018cb6368SRenaud Cerrato /* month, 1 - 12 */ 17118cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 17218cb6368SRenaud Cerrato 17318cb6368SRenaud Cerrato /* year */ 174b139bb5cSAlexandre Belloni buf[i++] = bin2bcd(tm->tm_year - 100); 17518cb6368SRenaud Cerrato 17618cb6368SRenaud Cerrato /* write register's data */ 177907b3262SAkinobu Mita err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); 178907b3262SAkinobu Mita if (err) { 179907b3262SAkinobu Mita dev_err(dev, 18018cb6368SRenaud Cerrato "%s: err=%d", __func__, err); 181907b3262SAkinobu Mita return err; 18218cb6368SRenaud Cerrato } 18318cb6368SRenaud Cerrato 18418cb6368SRenaud Cerrato return 0; 18518cb6368SRenaud Cerrato } 18618cb6368SRenaud Cerrato 18718cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 18818cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 18918cb6368SRenaud Cerrato { 190907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 191*7d65cf8cSAlexandre Belloni int val, touser = 0; 192f97cfddcSUwe Kleine-König int ret; 19318cb6368SRenaud Cerrato 19418cb6368SRenaud Cerrato switch (cmd) { 19518cb6368SRenaud Cerrato case RTC_VL_READ: 196*7d65cf8cSAlexandre Belloni ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); 197907b3262SAkinobu Mita if (ret) 198f97cfddcSUwe Kleine-König return ret; 19918cb6368SRenaud Cerrato 200*7d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BLF) 201*7d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_LOW; 202*7d65cf8cSAlexandre Belloni 203*7d65cf8cSAlexandre Belloni if (val & PCF2127_BIT_CTRL3_BF) 204*7d65cf8cSAlexandre Belloni touser |= RTC_VL_BACKUP_SWITCH; 205f97cfddcSUwe Kleine-König 206af427311SAlexandre Belloni return put_user(touser, (unsigned int __user *)arg); 207*7d65cf8cSAlexandre Belloni 208*7d65cf8cSAlexandre Belloni case RTC_VL_CLR: 209*7d65cf8cSAlexandre Belloni return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 210*7d65cf8cSAlexandre Belloni PCF2127_BIT_CTRL3_BF, 0); 211*7d65cf8cSAlexandre Belloni 21218cb6368SRenaud Cerrato default: 21318cb6368SRenaud Cerrato return -ENOIOCTLCMD; 21418cb6368SRenaud Cerrato } 21518cb6368SRenaud Cerrato } 21618cb6368SRenaud Cerrato 21718cb6368SRenaud Cerrato static const struct rtc_class_ops pcf2127_rtc_ops = { 21818cb6368SRenaud Cerrato .ioctl = pcf2127_rtc_ioctl, 21918cb6368SRenaud Cerrato .read_time = pcf2127_rtc_read_time, 22018cb6368SRenaud Cerrato .set_time = pcf2127_rtc_set_time, 22118cb6368SRenaud Cerrato }; 22218cb6368SRenaud Cerrato 223d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 224d6c3029fSUwe Kleine-König void *val, size_t bytes) 225d6c3029fSUwe Kleine-König { 226d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 227d6c3029fSUwe Kleine-König int ret; 228d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 229d6c3029fSUwe Kleine-König 230bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 231d6c3029fSUwe Kleine-König offsetbuf, 2); 232d6c3029fSUwe Kleine-König if (ret) 233d6c3029fSUwe Kleine-König return ret; 234d6c3029fSUwe Kleine-König 235bbfe3a7aSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 236d6c3029fSUwe Kleine-König val, bytes); 237d6c3029fSUwe Kleine-König 238d6c3029fSUwe Kleine-König return ret ?: bytes; 239d6c3029fSUwe Kleine-König } 240d6c3029fSUwe Kleine-König 241d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 242d6c3029fSUwe Kleine-König void *val, size_t bytes) 243d6c3029fSUwe Kleine-König { 244d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 245d6c3029fSUwe Kleine-König int ret; 246d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 247d6c3029fSUwe Kleine-König 248bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 249d6c3029fSUwe Kleine-König offsetbuf, 2); 250d6c3029fSUwe Kleine-König if (ret) 251d6c3029fSUwe Kleine-König return ret; 252d6c3029fSUwe Kleine-König 253bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 254d6c3029fSUwe Kleine-König val, bytes); 255d6c3029fSUwe Kleine-König 256d6c3029fSUwe Kleine-König return ret ?: bytes; 257d6c3029fSUwe Kleine-König } 258d6c3029fSUwe Kleine-König 2590e735eaaSBruno Thomsen /* watchdog driver */ 2600e735eaaSBruno Thomsen 2610e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd) 2620e735eaaSBruno Thomsen { 2630e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 2640e735eaaSBruno Thomsen 2650e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout); 2660e735eaaSBruno Thomsen } 2670e735eaaSBruno Thomsen 2680e735eaaSBruno Thomsen /* 2690e735eaaSBruno Thomsen * Restart watchdog timer if feature is active. 2700e735eaaSBruno Thomsen * 2710e735eaaSBruno Thomsen * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, 2720e735eaaSBruno Thomsen * since register also contain control/status flags for other features. 2730e735eaaSBruno Thomsen * Always call this function after reading CTRL2 register. 2740e735eaaSBruno Thomsen */ 2750e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) 2760e735eaaSBruno Thomsen { 2770e735eaaSBruno Thomsen int ret = 0; 2780e735eaaSBruno Thomsen 2790e735eaaSBruno Thomsen if (watchdog_active(wdd)) { 2800e735eaaSBruno Thomsen ret = pcf2127_wdt_ping(wdd); 2810e735eaaSBruno Thomsen if (ret) 2820e735eaaSBruno Thomsen dev_err(wdd->parent, 2830e735eaaSBruno Thomsen "%s: watchdog restart failed, ret=%d\n", 2840e735eaaSBruno Thomsen __func__, ret); 2850e735eaaSBruno Thomsen } 2860e735eaaSBruno Thomsen 2870e735eaaSBruno Thomsen return ret; 2880e735eaaSBruno Thomsen } 2890e735eaaSBruno Thomsen 2900e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd) 2910e735eaaSBruno Thomsen { 2920e735eaaSBruno Thomsen return pcf2127_wdt_ping(wdd); 2930e735eaaSBruno Thomsen } 2940e735eaaSBruno Thomsen 2950e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd) 2960e735eaaSBruno Thomsen { 2970e735eaaSBruno Thomsen struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); 2980e735eaaSBruno Thomsen 2990e735eaaSBruno Thomsen return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, 3000e735eaaSBruno Thomsen PCF2127_WD_VAL_STOP); 3010e735eaaSBruno Thomsen } 3020e735eaaSBruno Thomsen 3030e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, 3040e735eaaSBruno Thomsen unsigned int new_timeout) 3050e735eaaSBruno Thomsen { 3060e735eaaSBruno Thomsen dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", 3070e735eaaSBruno Thomsen new_timeout, wdd->timeout); 3080e735eaaSBruno Thomsen 3090e735eaaSBruno Thomsen wdd->timeout = new_timeout; 3100e735eaaSBruno Thomsen 3110e735eaaSBruno Thomsen return pcf2127_wdt_active_ping(wdd); 3120e735eaaSBruno Thomsen } 3130e735eaaSBruno Thomsen 3140e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = { 3150e735eaaSBruno Thomsen .identity = "NXP PCF2127/PCF2129 Watchdog", 3160e735eaaSBruno Thomsen .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, 3170e735eaaSBruno Thomsen }; 3180e735eaaSBruno Thomsen 3190e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = { 3200e735eaaSBruno Thomsen .owner = THIS_MODULE, 3210e735eaaSBruno Thomsen .start = pcf2127_wdt_start, 3220e735eaaSBruno Thomsen .stop = pcf2127_wdt_stop, 3230e735eaaSBruno Thomsen .ping = pcf2127_wdt_ping, 3240e735eaaSBruno Thomsen .set_timeout = pcf2127_wdt_set_timeout, 3250e735eaaSBruno Thomsen }; 3260e735eaaSBruno Thomsen 32703623b4bSBruno Thomsen /* sysfs interface */ 32803623b4bSBruno Thomsen 32903623b4bSBruno Thomsen static ssize_t timestamp0_store(struct device *dev, 33003623b4bSBruno Thomsen struct device_attribute *attr, 33103623b4bSBruno Thomsen const char *buf, size_t count) 33203623b4bSBruno Thomsen { 33303623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 33403623b4bSBruno Thomsen int ret; 33503623b4bSBruno Thomsen 33603623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, 33703623b4bSBruno Thomsen PCF2127_BIT_CTRL1_TSF1, 0); 33803623b4bSBruno Thomsen if (ret) { 33903623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret); 34003623b4bSBruno Thomsen return ret; 34103623b4bSBruno Thomsen } 34203623b4bSBruno Thomsen 34303623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 34403623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSF2, 0); 34503623b4bSBruno Thomsen if (ret) { 34603623b4bSBruno Thomsen dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret); 34703623b4bSBruno Thomsen return ret; 34803623b4bSBruno Thomsen } 34903623b4bSBruno Thomsen 35003623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 35103623b4bSBruno Thomsen if (ret) 35203623b4bSBruno Thomsen return ret; 35303623b4bSBruno Thomsen 35403623b4bSBruno Thomsen return count; 35503623b4bSBruno Thomsen }; 35603623b4bSBruno Thomsen 35703623b4bSBruno Thomsen static ssize_t timestamp0_show(struct device *dev, 35803623b4bSBruno Thomsen struct device_attribute *attr, char *buf) 35903623b4bSBruno Thomsen { 36003623b4bSBruno Thomsen struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); 36103623b4bSBruno Thomsen struct rtc_time tm; 36203623b4bSBruno Thomsen int ret; 36303623b4bSBruno Thomsen unsigned char data[25]; 36403623b4bSBruno Thomsen 36503623b4bSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data, 36603623b4bSBruno Thomsen sizeof(data)); 36703623b4bSBruno Thomsen if (ret) { 36803623b4bSBruno Thomsen dev_err(dev, "%s: read error ret=%d\n", __func__, ret); 36903623b4bSBruno Thomsen return ret; 37003623b4bSBruno Thomsen } 37103623b4bSBruno Thomsen 37203623b4bSBruno Thomsen dev_dbg(dev, 37303623b4bSBruno Thomsen "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, ts_sc=%02x, " 37403623b4bSBruno Thomsen "ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", 37503623b4bSBruno Thomsen __func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2], 37603623b4bSBruno Thomsen data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC], 37703623b4bSBruno Thomsen data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR], 37803623b4bSBruno Thomsen data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO], 37903623b4bSBruno Thomsen data[PCF2127_REG_TS_YR]); 38003623b4bSBruno Thomsen 38103623b4bSBruno Thomsen ret = pcf2127_wdt_active_ping(&pcf2127->wdd); 38203623b4bSBruno Thomsen if (ret) 38303623b4bSBruno Thomsen return ret; 38403623b4bSBruno Thomsen 38503623b4bSBruno Thomsen if (!(data[PCF2127_REG_CTRL1] & PCF2127_BIT_CTRL1_TSF1) && 38603623b4bSBruno Thomsen !(data[PCF2127_REG_CTRL2] & PCF2127_BIT_CTRL2_TSF2)) 38703623b4bSBruno Thomsen return 0; 38803623b4bSBruno Thomsen 38903623b4bSBruno Thomsen tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F); 39003623b4bSBruno Thomsen tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F); 39103623b4bSBruno Thomsen tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F); 39203623b4bSBruno Thomsen tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F); 39303623b4bSBruno Thomsen /* TS_MO register (month) value range: 1-12 */ 39403623b4bSBruno Thomsen tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1; 39503623b4bSBruno Thomsen tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]); 39603623b4bSBruno Thomsen if (tm.tm_year < 70) 39703623b4bSBruno Thomsen tm.tm_year += 100; /* assume we are in 1970...2069 */ 39803623b4bSBruno Thomsen 39903623b4bSBruno Thomsen ret = rtc_valid_tm(&tm); 40003623b4bSBruno Thomsen if (ret) 40103623b4bSBruno Thomsen return ret; 40203623b4bSBruno Thomsen 40303623b4bSBruno Thomsen return sprintf(buf, "%llu\n", 40403623b4bSBruno Thomsen (unsigned long long)rtc_tm_to_time64(&tm)); 40503623b4bSBruno Thomsen }; 40603623b4bSBruno Thomsen 40703623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0); 40803623b4bSBruno Thomsen 40903623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = { 41003623b4bSBruno Thomsen &dev_attr_timestamp0.attr, 41103623b4bSBruno Thomsen NULL 41203623b4bSBruno Thomsen }; 41303623b4bSBruno Thomsen 41403623b4bSBruno Thomsen static const struct attribute_group pcf2127_attr_group = { 41503623b4bSBruno Thomsen .attrs = pcf2127_attrs, 41603623b4bSBruno Thomsen }; 41703623b4bSBruno Thomsen 418907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 419d6c3029fSUwe Kleine-König const char *name, bool has_nvmem) 42018cb6368SRenaud Cerrato { 42118cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 42262409933SMartin Hundebøll u32 wdd_timeout; 423d6c3029fSUwe Kleine-König int ret = 0; 42418cb6368SRenaud Cerrato 425907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 42618cb6368SRenaud Cerrato 427907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 42818cb6368SRenaud Cerrato if (!pcf2127) 42918cb6368SRenaud Cerrato return -ENOMEM; 43018cb6368SRenaud Cerrato 431907b3262SAkinobu Mita pcf2127->regmap = regmap; 43218cb6368SRenaud Cerrato 433907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 434907b3262SAkinobu Mita 435e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 436d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 437d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 43818cb6368SRenaud Cerrato 439e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 440b139bb5cSAlexandre Belloni pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 441b139bb5cSAlexandre Belloni pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; 442b139bb5cSAlexandre Belloni pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ 443e788771cSBruno Thomsen 4440e735eaaSBruno Thomsen pcf2127->wdd.parent = dev; 4450e735eaaSBruno Thomsen pcf2127->wdd.info = &pcf2127_wdt_info; 4460e735eaaSBruno Thomsen pcf2127->wdd.ops = &pcf2127_watchdog_ops; 4470e735eaaSBruno Thomsen pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN; 4480e735eaaSBruno Thomsen pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX; 4490e735eaaSBruno Thomsen pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT; 4500e735eaaSBruno Thomsen pcf2127->wdd.min_hw_heartbeat_ms = 500; 4510e735eaaSBruno Thomsen 4520e735eaaSBruno Thomsen watchdog_set_drvdata(&pcf2127->wdd, pcf2127); 4530e735eaaSBruno Thomsen 454d6c3029fSUwe Kleine-König if (has_nvmem) { 455d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 456d6c3029fSUwe Kleine-König .priv = pcf2127, 457d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 458d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 459d6c3029fSUwe Kleine-König .size = 512, 460d6c3029fSUwe Kleine-König }; 461d6c3029fSUwe Kleine-König 462d6c3029fSUwe Kleine-König ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 463d6c3029fSUwe Kleine-König } 464d6c3029fSUwe Kleine-König 4650e735eaaSBruno Thomsen /* 4660e735eaaSBruno Thomsen * Watchdog timer enabled and reset pin /RST activated when timed out. 4670e735eaaSBruno Thomsen * Select 1Hz clock source for watchdog timer. 4680e735eaaSBruno Thomsen * Note: Countdown timer disabled and not available. 4690e735eaaSBruno Thomsen */ 4700e735eaaSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL, 4710e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 4720e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 4730e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1 | 4740e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF0, 4750e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD1 | 4760e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_CD0 | 4770e735eaaSBruno Thomsen PCF2127_BIT_WD_CTL_TF1); 4780e735eaaSBruno Thomsen if (ret) { 4790e735eaaSBruno Thomsen dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); 4800e735eaaSBruno Thomsen return ret; 4810e735eaaSBruno Thomsen } 4820e735eaaSBruno Thomsen 48362409933SMartin Hundebøll /* Test if watchdog timer is started by bootloader */ 48462409933SMartin Hundebøll ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout); 48562409933SMartin Hundebøll if (ret) 48662409933SMartin Hundebøll return ret; 48762409933SMartin Hundebøll 48862409933SMartin Hundebøll if (wdd_timeout) 48962409933SMartin Hundebøll set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); 49062409933SMartin Hundebøll 49128abbba3SBruno Thomsen #ifdef CONFIG_WATCHDOG 4920e735eaaSBruno Thomsen ret = devm_watchdog_register_device(dev, &pcf2127->wdd); 4930e735eaaSBruno Thomsen if (ret) 4940e735eaaSBruno Thomsen return ret; 49528abbba3SBruno Thomsen #endif /* CONFIG_WATCHDOG */ 4960e735eaaSBruno Thomsen 49703623b4bSBruno Thomsen /* 49803623b4bSBruno Thomsen * Disable battery low/switch-over timestamp and interrupts. 49903623b4bSBruno Thomsen * Clear battery interrupt flags which can block new trigger events. 50003623b4bSBruno Thomsen * Note: This is the default chip behaviour but added to ensure 50103623b4bSBruno Thomsen * correct tamper timestamp and interrupt function. 50203623b4bSBruno Thomsen */ 50303623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 50403623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BTSE | 50503623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BIE | 50603623b4bSBruno Thomsen PCF2127_BIT_CTRL3_BLIE, 0); 50703623b4bSBruno Thomsen if (ret) { 50803623b4bSBruno Thomsen dev_err(dev, "%s: interrupt config (ctrl3) failed\n", 50903623b4bSBruno Thomsen __func__); 51003623b4bSBruno Thomsen return ret; 51103623b4bSBruno Thomsen } 51203623b4bSBruno Thomsen 51303623b4bSBruno Thomsen /* 51403623b4bSBruno Thomsen * Enable timestamp function and store timestamp of first trigger 51503623b4bSBruno Thomsen * event until TSF1 and TFS2 interrupt flags are cleared. 51603623b4bSBruno Thomsen */ 51703623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL, 51803623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSOFF | 51903623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM, 52003623b4bSBruno Thomsen PCF2127_BIT_TS_CTRL_TSM); 52103623b4bSBruno Thomsen if (ret) { 52203623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n", 52303623b4bSBruno Thomsen __func__); 52403623b4bSBruno Thomsen return ret; 52503623b4bSBruno Thomsen } 52603623b4bSBruno Thomsen 52703623b4bSBruno Thomsen /* 52803623b4bSBruno Thomsen * Enable interrupt generation when TSF1 or TSF2 timestamp flags 52903623b4bSBruno Thomsen * are set. Interrupt signal is an open-drain output and can be 53003623b4bSBruno Thomsen * left floating if unused. 53103623b4bSBruno Thomsen */ 53203623b4bSBruno Thomsen ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, 53303623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE, 53403623b4bSBruno Thomsen PCF2127_BIT_CTRL2_TSIE); 53503623b4bSBruno Thomsen if (ret) { 53603623b4bSBruno Thomsen dev_err(dev, "%s: tamper detection config (ctrl2) failed\n", 53703623b4bSBruno Thomsen __func__); 53803623b4bSBruno Thomsen return ret; 53903623b4bSBruno Thomsen } 54003623b4bSBruno Thomsen 54103623b4bSBruno Thomsen ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group); 54203623b4bSBruno Thomsen if (ret) { 54303623b4bSBruno Thomsen dev_err(dev, "%s: tamper sysfs registering failed\n", 54403623b4bSBruno Thomsen __func__); 54503623b4bSBruno Thomsen return ret; 54603623b4bSBruno Thomsen } 54703623b4bSBruno Thomsen 548e788771cSBruno Thomsen return rtc_register_device(pcf2127->rtc); 54918cb6368SRenaud Cerrato } 55018cb6368SRenaud Cerrato 55118cb6368SRenaud Cerrato #ifdef CONFIG_OF 55218cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 55318cb6368SRenaud Cerrato { .compatible = "nxp,pcf2127" }, 554cee2cc21SAkinobu Mita { .compatible = "nxp,pcf2129" }, 55518cb6368SRenaud Cerrato {} 55618cb6368SRenaud Cerrato }; 55718cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 55818cb6368SRenaud Cerrato #endif 55918cb6368SRenaud Cerrato 5609408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 5619408ec1aSAkinobu Mita 562907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 563907b3262SAkinobu Mita { 564907b3262SAkinobu Mita struct device *dev = context; 565907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 566907b3262SAkinobu Mita int ret; 567907b3262SAkinobu Mita 568907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 569907b3262SAkinobu Mita if (ret != count) 570907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 571907b3262SAkinobu Mita 572907b3262SAkinobu Mita return 0; 573907b3262SAkinobu Mita } 574907b3262SAkinobu Mita 575907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 576907b3262SAkinobu Mita const void *reg, size_t reg_size, 577907b3262SAkinobu Mita const void *val, size_t val_size) 578907b3262SAkinobu Mita { 579907b3262SAkinobu Mita struct device *dev = context; 580907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 581907b3262SAkinobu Mita int ret; 582907b3262SAkinobu Mita void *buf; 583907b3262SAkinobu Mita 584907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 585907b3262SAkinobu Mita return -EINVAL; 586907b3262SAkinobu Mita 587907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 588907b3262SAkinobu Mita if (!buf) 589907b3262SAkinobu Mita return -ENOMEM; 590907b3262SAkinobu Mita 591907b3262SAkinobu Mita memcpy(buf, reg, 1); 592907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 593907b3262SAkinobu Mita 594907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 5959bde0afbSXulin Sun 5969bde0afbSXulin Sun kfree(buf); 5979bde0afbSXulin Sun 598907b3262SAkinobu Mita if (ret != val_size + 1) 599907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 600907b3262SAkinobu Mita 601907b3262SAkinobu Mita return 0; 602907b3262SAkinobu Mita } 603907b3262SAkinobu Mita 604907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 605907b3262SAkinobu Mita void *val, size_t val_size) 606907b3262SAkinobu Mita { 607907b3262SAkinobu Mita struct device *dev = context; 608907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 609907b3262SAkinobu Mita int ret; 610907b3262SAkinobu Mita 611907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 612907b3262SAkinobu Mita return -EINVAL; 613907b3262SAkinobu Mita 614907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 615907b3262SAkinobu Mita if (ret != 1) 616907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 617907b3262SAkinobu Mita 618907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 619907b3262SAkinobu Mita if (ret != val_size) 620907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 621907b3262SAkinobu Mita 622907b3262SAkinobu Mita return 0; 623907b3262SAkinobu Mita } 624907b3262SAkinobu Mita 625907b3262SAkinobu Mita /* 626907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 627907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 628907b3262SAkinobu Mita * read register data when reading from registers. 629907b3262SAkinobu Mita */ 630907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 631907b3262SAkinobu Mita .write = pcf2127_i2c_write, 632907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 633907b3262SAkinobu Mita .read = pcf2127_i2c_read, 63418cb6368SRenaud Cerrato }; 63518cb6368SRenaud Cerrato 636907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 637907b3262SAkinobu Mita 638907b3262SAkinobu Mita static int pcf2127_i2c_probe(struct i2c_client *client, 639907b3262SAkinobu Mita const struct i2c_device_id *id) 640907b3262SAkinobu Mita { 641907b3262SAkinobu Mita struct regmap *regmap; 642907b3262SAkinobu Mita static const struct regmap_config config = { 643907b3262SAkinobu Mita .reg_bits = 8, 644907b3262SAkinobu Mita .val_bits = 8, 645040e6dc0SAlexandre Belloni .max_register = 0x1d, 646907b3262SAkinobu Mita }; 647907b3262SAkinobu Mita 648907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 649907b3262SAkinobu Mita return -ENODEV; 650907b3262SAkinobu Mita 651907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 652907b3262SAkinobu Mita &client->dev, &config); 653907b3262SAkinobu Mita if (IS_ERR(regmap)) { 654907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 655907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 656907b3262SAkinobu Mita return PTR_ERR(regmap); 657907b3262SAkinobu Mita } 658907b3262SAkinobu Mita 659907b3262SAkinobu Mita return pcf2127_probe(&client->dev, regmap, 660d6c3029fSUwe Kleine-König pcf2127_i2c_driver.driver.name, id->driver_data); 661907b3262SAkinobu Mita } 662907b3262SAkinobu Mita 663907b3262SAkinobu Mita static const struct i2c_device_id pcf2127_i2c_id[] = { 664d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 665cee2cc21SAkinobu Mita { "pcf2129", 0 }, 666907b3262SAkinobu Mita { } 667907b3262SAkinobu Mita }; 668907b3262SAkinobu Mita MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 669907b3262SAkinobu Mita 670907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 671907b3262SAkinobu Mita .driver = { 672907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 673907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 674907b3262SAkinobu Mita }, 675907b3262SAkinobu Mita .probe = pcf2127_i2c_probe, 676907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 677907b3262SAkinobu Mita }; 6789408ec1aSAkinobu Mita 6799408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 6809408ec1aSAkinobu Mita { 6819408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 6829408ec1aSAkinobu Mita } 6839408ec1aSAkinobu Mita 6849408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 6859408ec1aSAkinobu Mita { 6869408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 6879408ec1aSAkinobu Mita } 6889408ec1aSAkinobu Mita 6899408ec1aSAkinobu Mita #else 6909408ec1aSAkinobu Mita 6919408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 6929408ec1aSAkinobu Mita { 6939408ec1aSAkinobu Mita return 0; 6949408ec1aSAkinobu Mita } 6959408ec1aSAkinobu Mita 6969408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 6979408ec1aSAkinobu Mita { 6989408ec1aSAkinobu Mita } 6999408ec1aSAkinobu Mita 7009408ec1aSAkinobu Mita #endif 7019408ec1aSAkinobu Mita 7029408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 7039408ec1aSAkinobu Mita 7049408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 7059408ec1aSAkinobu Mita 7069408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 7079408ec1aSAkinobu Mita { 7089408ec1aSAkinobu Mita static const struct regmap_config config = { 7099408ec1aSAkinobu Mita .reg_bits = 8, 7109408ec1aSAkinobu Mita .val_bits = 8, 7119408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 7129408ec1aSAkinobu Mita .write_flag_mask = 0x20, 713040e6dc0SAlexandre Belloni .max_register = 0x1d, 7149408ec1aSAkinobu Mita }; 7159408ec1aSAkinobu Mita struct regmap *regmap; 7169408ec1aSAkinobu Mita 7179408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 7189408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 7199408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 7209408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 7219408ec1aSAkinobu Mita return PTR_ERR(regmap); 7229408ec1aSAkinobu Mita } 7239408ec1aSAkinobu Mita 724d6c3029fSUwe Kleine-König return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name, 725d6c3029fSUwe Kleine-König spi_get_device_id(spi)->driver_data); 7269408ec1aSAkinobu Mita } 7279408ec1aSAkinobu Mita 7289408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 729d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 730cee2cc21SAkinobu Mita { "pcf2129", 0 }, 7319408ec1aSAkinobu Mita { } 7329408ec1aSAkinobu Mita }; 7339408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 7349408ec1aSAkinobu Mita 7359408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 7369408ec1aSAkinobu Mita .driver = { 7379408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 7389408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 7399408ec1aSAkinobu Mita }, 7409408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 7419408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 7429408ec1aSAkinobu Mita }; 7439408ec1aSAkinobu Mita 7449408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 7459408ec1aSAkinobu Mita { 7469408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 7479408ec1aSAkinobu Mita } 7489408ec1aSAkinobu Mita 7499408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 7509408ec1aSAkinobu Mita { 7519408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 7529408ec1aSAkinobu Mita } 7539408ec1aSAkinobu Mita 7549408ec1aSAkinobu Mita #else 7559408ec1aSAkinobu Mita 7569408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 7579408ec1aSAkinobu Mita { 7589408ec1aSAkinobu Mita return 0; 7599408ec1aSAkinobu Mita } 7609408ec1aSAkinobu Mita 7619408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 7629408ec1aSAkinobu Mita { 7639408ec1aSAkinobu Mita } 7649408ec1aSAkinobu Mita 7659408ec1aSAkinobu Mita #endif 7669408ec1aSAkinobu Mita 7679408ec1aSAkinobu Mita static int __init pcf2127_init(void) 7689408ec1aSAkinobu Mita { 7699408ec1aSAkinobu Mita int ret; 7709408ec1aSAkinobu Mita 7719408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 7729408ec1aSAkinobu Mita if (ret) { 7739408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 7749408ec1aSAkinobu Mita return ret; 7759408ec1aSAkinobu Mita } 7769408ec1aSAkinobu Mita 7779408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 7789408ec1aSAkinobu Mita if (ret) { 7799408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 7809408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 7819408ec1aSAkinobu Mita } 7829408ec1aSAkinobu Mita 7839408ec1aSAkinobu Mita return ret; 7849408ec1aSAkinobu Mita } 7859408ec1aSAkinobu Mita module_init(pcf2127_init) 7869408ec1aSAkinobu Mita 7879408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 7889408ec1aSAkinobu Mita { 7899408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 7909408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 7919408ec1aSAkinobu Mita } 7929408ec1aSAkinobu Mita module_exit(pcf2127_exit) 79318cb6368SRenaud Cerrato 79418cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 795cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); 7964d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 797