xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision afc505bf9039caf5a377d8b9705ef42f6d4ac7d4)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
218cb6368SRenaud Cerrato /*
3*afc505bfSHugo 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  *
11*afc505bfSHugo Villeneuve  * PCF2131 support
12*afc505bfSHugo Villeneuve  * Author: Hugo Villeneuve <hvilleneuve@dimonoff.com>
13*afc505bfSHugo Villeneuve  *
1418cb6368SRenaud Cerrato  * based on the other drivers in this same directory.
1518cb6368SRenaud Cerrato  *
16*afc505bfSHugo Villeneuve  * Datasheets: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf
17*afc505bfSHugo 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.
74*afc505bfSHugo 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 
93*afc505bfSHugo Villeneuve #define PCF2127_MAX_TS_SUPPORTED	4
94*afc505bfSHugo Villeneuve 
95*afc505bfSHugo Villeneuve /* Control register 4 */
96*afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL4		0x03
97*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF4			BIT(4)
98*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF3			BIT(5)
99*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF2			BIT(6)
100*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF1			BIT(7)
101*afc505bfSHugo Villeneuve /* Control register 5 */
102*afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL5		0x04
103*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE4			BIT(4)
104*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE3			BIT(5)
105*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE2			BIT(6)
106*afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE1			BIT(7)
107*afc505bfSHugo Villeneuve /* Software reset register */
108*afc505bfSHugo Villeneuve #define PCF2131_REG_SR_RESET		0x05
109*afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_READ_PATTERN	(BIT(2) | BIT(5))
110*afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_CPR_CMD	(PCF2131_SR_RESET_READ_PATTERN | BIT(7))
111*afc505bfSHugo Villeneuve /* Time and date registers */
112*afc505bfSHugo Villeneuve #define PCF2131_REG_TIME_BASE		0x07
113*afc505bfSHugo Villeneuve /* Alarm registers */
114*afc505bfSHugo Villeneuve #define PCF2131_REG_ALARM_BASE		0x0E
115*afc505bfSHugo Villeneuve /* CLKOUT control register */
116*afc505bfSHugo Villeneuve #define PCF2131_REG_CLKOUT		0x13
117*afc505bfSHugo Villeneuve /* Watchdog registers */
118*afc505bfSHugo Villeneuve #define PCF2131_REG_WD_CTL		0x35
119*afc505bfSHugo Villeneuve #define PCF2131_REG_WD_VAL		0x36
120*afc505bfSHugo Villeneuve /* Tamper timestamp1 registers */
121*afc505bfSHugo Villeneuve #define PCF2131_REG_TS1_BASE		0x14
122*afc505bfSHugo Villeneuve /* Tamper timestamp2 registers */
123*afc505bfSHugo Villeneuve #define PCF2131_REG_TS2_BASE		0x1B
124*afc505bfSHugo Villeneuve /* Tamper timestamp3 registers */
125*afc505bfSHugo Villeneuve #define PCF2131_REG_TS3_BASE		0x22
126*afc505bfSHugo Villeneuve /* Tamper timestamp4 registers */
127*afc505bfSHugo Villeneuve #define PCF2131_REG_TS4_BASE		0x29
128*afc505bfSHugo Villeneuve /* Interrupt mask registers */
129*afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK1		0x31
130*afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK2		0x32
131*afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK1		0x33
132*afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK2		0x34
133*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BLIE		BIT(0)
134*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BIE		BIT(1)
135*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_AIE		BIT(2)
136*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_WD_CD		BIT(3)
137*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_SI		BIT(4)
138*afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_MI		BIT(5)
139*afc505bfSHugo Villeneuve #define PCF2131_CTRL2_IRQ_MASK ( \
140*afc505bfSHugo Villeneuve 		PCF2127_BIT_CTRL2_AF | \
141*afc505bfSHugo Villeneuve 		PCF2127_BIT_CTRL2_WDTF)
142*afc505bfSHugo Villeneuve #define PCF2131_CTRL4_IRQ_MASK ( \
143*afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF4 | \
144*afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF3 | \
145*afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF2 | \
146*afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF1)
147420cc9e8SHugo Villeneuve 
148fd28ceb4SHugo Villeneuve enum pcf21xx_type {
149fd28ceb4SHugo Villeneuve 	PCF2127,
150fd28ceb4SHugo Villeneuve 	PCF2129,
151*afc505bfSHugo 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;
1826211aceeSHugo Villeneuve 	u8 reg_time_base; /* Time/date base register. */
1837c6f0db4SHugo Villeneuve 	u8 regs_alarm_base; /* Alarm function base registers. */
1846b57ec29SHugo Villeneuve 	u8 reg_wd_ctl; /* Watchdog control register. */
1856b57ec29SHugo Villeneuve 	u8 reg_wd_val; /* Watchdog value register. */
186fc16599eSHugo Villeneuve 	u8 reg_clkout; /* Clkout register. */
187420cc9e8SHugo Villeneuve 	unsigned int ts_count;
188420cc9e8SHugo Villeneuve 	struct pcf21xx_ts_config ts[PCF2127_MAX_TS_SUPPORTED];
189420cc9e8SHugo Villeneuve 	struct attribute_group attribute_group;
190fd28ceb4SHugo Villeneuve };
191fd28ceb4SHugo Villeneuve 
19218cb6368SRenaud Cerrato struct pcf2127 {
19318cb6368SRenaud Cerrato 	struct rtc_device *rtc;
1940e735eaaSBruno Thomsen 	struct watchdog_device wdd;
195907b3262SAkinobu Mita 	struct regmap *regmap;
196fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *cfg;
1972f861984SMian Yousaf Kaukab 	bool irq_enabled;
198420cc9e8SHugo Villeneuve 	time64_t ts[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp values. */
199420cc9e8SHugo Villeneuve 	bool ts_valid[PCF2127_MAX_TS_SUPPORTED];  /* Timestamp valid indication. */
20018cb6368SRenaud Cerrato };
20118cb6368SRenaud Cerrato 
20218cb6368SRenaud Cerrato /*
20318cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
20418cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
20518cb6368SRenaud Cerrato  */
206907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
20718cb6368SRenaud Cerrato {
208907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
20931f077c3SHugo Villeneuve 	unsigned char buf[7];
210907b3262SAkinobu Mita 	int ret;
21118cb6368SRenaud Cerrato 
2127f43020eSBruno Thomsen 	/*
2137f43020eSBruno Thomsen 	 * Avoid reading CTRL2 register as it causes WD_VAL register
2147f43020eSBruno Thomsen 	 * value to reset to 0 which means watchdog is stopped.
2157f43020eSBruno Thomsen 	 */
2166211aceeSHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->reg_time_base,
2176211aceeSHugo Villeneuve 			       buf, sizeof(buf));
218907b3262SAkinobu Mita 	if (ret) {
219907b3262SAkinobu Mita 		dev_err(dev, "%s: read error\n", __func__);
220907b3262SAkinobu Mita 		return ret;
22118cb6368SRenaud Cerrato 	}
22218cb6368SRenaud Cerrato 
223bbfe3a7aSBruno Thomsen 	/* Clock integrity is not guaranteed when OSF flag is set. */
22431f077c3SHugo Villeneuve 	if (buf[0] & PCF2127_BIT_SC_OSF) {
225653ebd75SAndrea Scian 		/*
226653ebd75SAndrea Scian 		 * no need clear the flag here,
227653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
228653ebd75SAndrea Scian 		 */
229907b3262SAkinobu Mita 		dev_warn(dev,
230653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
231653ebd75SAndrea Scian 		return -EINVAL;
23218cb6368SRenaud Cerrato 	}
23318cb6368SRenaud Cerrato 
234907b3262SAkinobu Mita 	dev_dbg(dev,
23531f077c3SHugo Villeneuve 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
23618cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
23731f077c3SHugo Villeneuve 		__func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
23818cb6368SRenaud Cerrato 
23931f077c3SHugo Villeneuve 	tm->tm_sec = bcd2bin(buf[0] & 0x7F);
24031f077c3SHugo Villeneuve 	tm->tm_min = bcd2bin(buf[1] & 0x7F);
2410476b6c8SHugo Villeneuve 	tm->tm_hour = bcd2bin(buf[2] & 0x3F);
24231f077c3SHugo Villeneuve 	tm->tm_mday = bcd2bin(buf[3] & 0x3F);
24331f077c3SHugo Villeneuve 	tm->tm_wday = buf[4] & 0x07;
2440476b6c8SHugo Villeneuve 	tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
24531f077c3SHugo Villeneuve 	tm->tm_year = bcd2bin(buf[6]);
246b139bb5cSAlexandre Belloni 	tm->tm_year += 100;
24718cb6368SRenaud Cerrato 
248907b3262SAkinobu Mita 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
24918cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
25018cb6368SRenaud Cerrato 		__func__,
25118cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
25218cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
25318cb6368SRenaud Cerrato 
25422652ba7SAlexandre Belloni 	return 0;
25518cb6368SRenaud Cerrato }
25618cb6368SRenaud Cerrato 
257907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
25818cb6368SRenaud Cerrato {
259907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
260907b3262SAkinobu Mita 	unsigned char buf[7];
26118cb6368SRenaud Cerrato 	int i = 0, err;
26218cb6368SRenaud Cerrato 
263907b3262SAkinobu Mita 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
26418cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
26518cb6368SRenaud Cerrato 		__func__,
26618cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
26718cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
26818cb6368SRenaud Cerrato 
26918cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
270653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
27118cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
27218cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
27318cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
27418cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
27518cb6368SRenaud Cerrato 
27618cb6368SRenaud Cerrato 	/* month, 1 - 12 */
27718cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
27818cb6368SRenaud Cerrato 
27918cb6368SRenaud Cerrato 	/* year */
280b139bb5cSAlexandre Belloni 	buf[i++] = bin2bcd(tm->tm_year - 100);
28118cb6368SRenaud Cerrato 
28218cb6368SRenaud Cerrato 	/* write register's data */
2836211aceeSHugo Villeneuve 	err = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->reg_time_base, buf, i);
284907b3262SAkinobu Mita 	if (err) {
2853d740c64SHugo Villeneuve 		dev_dbg(dev, "%s: err=%d", __func__, err);
286907b3262SAkinobu Mita 		return err;
28718cb6368SRenaud Cerrato 	}
28818cb6368SRenaud Cerrato 
28918cb6368SRenaud Cerrato 	return 0;
29018cb6368SRenaud Cerrato }
29118cb6368SRenaud Cerrato 
29218cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
29318cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
29418cb6368SRenaud Cerrato {
295907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
2967d65cf8cSAlexandre Belloni 	int val, touser = 0;
297f97cfddcSUwe Kleine-König 	int ret;
29818cb6368SRenaud Cerrato 
29918cb6368SRenaud Cerrato 	switch (cmd) {
30018cb6368SRenaud Cerrato 	case RTC_VL_READ:
3017d65cf8cSAlexandre Belloni 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
302907b3262SAkinobu Mita 		if (ret)
303f97cfddcSUwe Kleine-König 			return ret;
30418cb6368SRenaud Cerrato 
3057d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BLF)
3067d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_LOW;
3077d65cf8cSAlexandre Belloni 
3087d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BF)
3097d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_SWITCH;
310f97cfddcSUwe Kleine-König 
311af427311SAlexandre Belloni 		return put_user(touser, (unsigned int __user *)arg);
3127d65cf8cSAlexandre Belloni 
3137d65cf8cSAlexandre Belloni 	case RTC_VL_CLR:
3147d65cf8cSAlexandre Belloni 		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
3157d65cf8cSAlexandre Belloni 					  PCF2127_BIT_CTRL3_BF, 0);
3167d65cf8cSAlexandre Belloni 
31718cb6368SRenaud Cerrato 	default:
31818cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
31918cb6368SRenaud Cerrato 	}
32018cb6368SRenaud Cerrato }
32118cb6368SRenaud Cerrato 
322d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset,
323d6c3029fSUwe Kleine-König 			      void *val, size_t bytes)
324d6c3029fSUwe Kleine-König {
325d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
326d6c3029fSUwe Kleine-König 	int ret;
327d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
328d6c3029fSUwe Kleine-König 
329bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
330d6c3029fSUwe Kleine-König 				offsetbuf, 2);
331d6c3029fSUwe Kleine-König 	if (ret)
332d6c3029fSUwe Kleine-König 		return ret;
333d6c3029fSUwe Kleine-König 
334ba1c30bfSDan Carpenter 	return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
335d6c3029fSUwe Kleine-König 				val, bytes);
336d6c3029fSUwe Kleine-König }
337d6c3029fSUwe Kleine-König 
338d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset,
339d6c3029fSUwe Kleine-König 			       void *val, size_t bytes)
340d6c3029fSUwe Kleine-König {
341d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
342d6c3029fSUwe Kleine-König 	int ret;
343d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
344d6c3029fSUwe Kleine-König 
345bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
346d6c3029fSUwe Kleine-König 				offsetbuf, 2);
347d6c3029fSUwe Kleine-König 	if (ret)
348d6c3029fSUwe Kleine-König 		return ret;
349d6c3029fSUwe Kleine-König 
350ba1c30bfSDan Carpenter 	return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
351d6c3029fSUwe Kleine-König 				 val, bytes);
352d6c3029fSUwe Kleine-König }
353d6c3029fSUwe Kleine-König 
3540e735eaaSBruno Thomsen /* watchdog driver */
3550e735eaaSBruno Thomsen 
3560e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd)
3570e735eaaSBruno Thomsen {
3580e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
3590e735eaaSBruno Thomsen 
3606b57ec29SHugo Villeneuve 	return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, wdd->timeout);
3610e735eaaSBruno Thomsen }
3620e735eaaSBruno Thomsen 
3630e735eaaSBruno Thomsen /*
3640e735eaaSBruno Thomsen  * Restart watchdog timer if feature is active.
3650e735eaaSBruno Thomsen  *
3660e735eaaSBruno Thomsen  * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
3670e735eaaSBruno Thomsen  * since register also contain control/status flags for other features.
3680e735eaaSBruno Thomsen  * Always call this function after reading CTRL2 register.
3690e735eaaSBruno Thomsen  */
3700e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
3710e735eaaSBruno Thomsen {
3720e735eaaSBruno Thomsen 	int ret = 0;
3730e735eaaSBruno Thomsen 
3740e735eaaSBruno Thomsen 	if (watchdog_active(wdd)) {
3750e735eaaSBruno Thomsen 		ret = pcf2127_wdt_ping(wdd);
3760e735eaaSBruno Thomsen 		if (ret)
3770e735eaaSBruno Thomsen 			dev_err(wdd->parent,
3780e735eaaSBruno Thomsen 				"%s: watchdog restart failed, ret=%d\n",
3790e735eaaSBruno Thomsen 				__func__, ret);
3800e735eaaSBruno Thomsen 	}
3810e735eaaSBruno Thomsen 
3820e735eaaSBruno Thomsen 	return ret;
3830e735eaaSBruno Thomsen }
3840e735eaaSBruno Thomsen 
3850e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd)
3860e735eaaSBruno Thomsen {
3870e735eaaSBruno Thomsen 	return pcf2127_wdt_ping(wdd);
3880e735eaaSBruno Thomsen }
3890e735eaaSBruno Thomsen 
3900e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd)
3910e735eaaSBruno Thomsen {
3920e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
3930e735eaaSBruno Thomsen 
3946b57ec29SHugo Villeneuve 	return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val,
3950e735eaaSBruno Thomsen 			    PCF2127_WD_VAL_STOP);
3960e735eaaSBruno Thomsen }
3970e735eaaSBruno Thomsen 
3980e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
3990e735eaaSBruno Thomsen 				   unsigned int new_timeout)
4000e735eaaSBruno Thomsen {
4010e735eaaSBruno Thomsen 	dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
4020e735eaaSBruno Thomsen 		new_timeout, wdd->timeout);
4030e735eaaSBruno Thomsen 
4040e735eaaSBruno Thomsen 	wdd->timeout = new_timeout;
4050e735eaaSBruno Thomsen 
4060e735eaaSBruno Thomsen 	return pcf2127_wdt_active_ping(wdd);
4070e735eaaSBruno Thomsen }
4080e735eaaSBruno Thomsen 
4090e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = {
4100e735eaaSBruno Thomsen 	.identity = "NXP PCF2127/PCF2129 Watchdog",
4110e735eaaSBruno Thomsen 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
4120e735eaaSBruno Thomsen };
4130e735eaaSBruno Thomsen 
4140e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = {
4150e735eaaSBruno Thomsen 	.owner = THIS_MODULE,
4160e735eaaSBruno Thomsen 	.start = pcf2127_wdt_start,
4170e735eaaSBruno Thomsen 	.stop = pcf2127_wdt_stop,
4180e735eaaSBruno Thomsen 	.ping = pcf2127_wdt_ping,
4190e735eaaSBruno Thomsen 	.set_timeout = pcf2127_wdt_set_timeout,
4200e735eaaSBruno Thomsen };
4210e735eaaSBruno Thomsen 
4225d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
4235d78533aSUwe Kleine-König {
4245d78533aSUwe Kleine-König 	u32 wdd_timeout;
4255d78533aSUwe Kleine-König 	int ret;
4265d78533aSUwe Kleine-König 
42771ac1345SUwe Kleine-König 	if (!IS_ENABLED(CONFIG_WATCHDOG) ||
42871ac1345SUwe Kleine-König 	    !device_property_read_bool(dev, "reset-source"))
4295d78533aSUwe Kleine-König 		return 0;
4305d78533aSUwe Kleine-König 
4315d78533aSUwe Kleine-König 	pcf2127->wdd.parent = dev;
4325d78533aSUwe Kleine-König 	pcf2127->wdd.info = &pcf2127_wdt_info;
4335d78533aSUwe Kleine-König 	pcf2127->wdd.ops = &pcf2127_watchdog_ops;
4345d78533aSUwe Kleine-König 	pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN;
4355d78533aSUwe Kleine-König 	pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX;
4365d78533aSUwe Kleine-König 	pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT;
4375d78533aSUwe Kleine-König 	pcf2127->wdd.min_hw_heartbeat_ms = 500;
4385d78533aSUwe Kleine-König 	pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
4395d78533aSUwe Kleine-König 
4405d78533aSUwe Kleine-König 	watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
4415d78533aSUwe Kleine-König 
4425d78533aSUwe Kleine-König 	/* Test if watchdog timer is started by bootloader */
4436b57ec29SHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_wd_val, &wdd_timeout);
4445d78533aSUwe Kleine-König 	if (ret)
4455d78533aSUwe Kleine-König 		return ret;
4465d78533aSUwe Kleine-König 
4475d78533aSUwe Kleine-König 	if (wdd_timeout)
4485d78533aSUwe Kleine-König 		set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status);
4495d78533aSUwe Kleine-König 
4505d78533aSUwe Kleine-König 	return devm_watchdog_register_device(dev, &pcf2127->wdd);
4515d78533aSUwe Kleine-König }
4525d78533aSUwe Kleine-König 
4538a914bacSLiam Beguin /* Alarm */
4548a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4558a914bacSLiam Beguin {
4568a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
45773ce0530SHugo Villeneuve 	u8 buf[5];
45873ce0530SHugo Villeneuve 	unsigned int ctrl2;
4598a914bacSLiam Beguin 	int ret;
4608a914bacSLiam Beguin 
4618a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
4628a914bacSLiam Beguin 	if (ret)
4638a914bacSLiam Beguin 		return ret;
4648a914bacSLiam Beguin 
4658a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
4668a914bacSLiam Beguin 	if (ret)
4678a914bacSLiam Beguin 		return ret;
4688a914bacSLiam Beguin 
4697c6f0db4SHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->regs_alarm_base,
4707c6f0db4SHugo Villeneuve 			       buf, sizeof(buf));
4718a914bacSLiam Beguin 	if (ret)
4728a914bacSLiam Beguin 		return ret;
4738a914bacSLiam Beguin 
4748a914bacSLiam Beguin 	alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
4758a914bacSLiam Beguin 	alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
4768a914bacSLiam Beguin 
4778a914bacSLiam Beguin 	alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
4788a914bacSLiam Beguin 	alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
4798a914bacSLiam Beguin 	alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
4808a914bacSLiam Beguin 	alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
4818a914bacSLiam Beguin 
4828a914bacSLiam Beguin 	return 0;
4838a914bacSLiam Beguin }
4848a914bacSLiam Beguin 
4858a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
4868a914bacSLiam Beguin {
4878a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4888a914bacSLiam Beguin 	int ret;
4898a914bacSLiam Beguin 
4908a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
4918a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AIE,
4928a914bacSLiam Beguin 				 enable ? PCF2127_BIT_CTRL2_AIE : 0);
4938a914bacSLiam Beguin 	if (ret)
4948a914bacSLiam Beguin 		return ret;
4958a914bacSLiam Beguin 
4968a914bacSLiam Beguin 	return pcf2127_wdt_active_ping(&pcf2127->wdd);
4978a914bacSLiam Beguin }
4988a914bacSLiam Beguin 
4998a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
5008a914bacSLiam Beguin {
5018a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5028a914bacSLiam Beguin 	uint8_t buf[5];
5038a914bacSLiam Beguin 	int ret;
5048a914bacSLiam Beguin 
5058a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
5068a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AF, 0);
5078a914bacSLiam Beguin 	if (ret)
5088a914bacSLiam Beguin 		return ret;
5098a914bacSLiam Beguin 
5108a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
5118a914bacSLiam Beguin 	if (ret)
5128a914bacSLiam Beguin 		return ret;
5138a914bacSLiam Beguin 
5148a914bacSLiam Beguin 	buf[0] = bin2bcd(alrm->time.tm_sec);
5158a914bacSLiam Beguin 	buf[1] = bin2bcd(alrm->time.tm_min);
5168a914bacSLiam Beguin 	buf[2] = bin2bcd(alrm->time.tm_hour);
5178a914bacSLiam Beguin 	buf[3] = bin2bcd(alrm->time.tm_mday);
51827006416SAlexandre Belloni 	buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
5198a914bacSLiam Beguin 
5207c6f0db4SHugo Villeneuve 	ret = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->regs_alarm_base,
5217c6f0db4SHugo Villeneuve 				buf, sizeof(buf));
5228a914bacSLiam Beguin 	if (ret)
5238a914bacSLiam Beguin 		return ret;
5248a914bacSLiam Beguin 
5258a914bacSLiam Beguin 	return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
5268a914bacSLiam Beguin }
5278a914bacSLiam Beguin 
5282f861984SMian Yousaf Kaukab /*
529420cc9e8SHugo Villeneuve  * This function reads one timestamp function data, caller is responsible for
530420cc9e8SHugo Villeneuve  * calling pcf2127_wdt_active_ping()
5312f861984SMian Yousaf Kaukab  */
532420cc9e8SHugo Villeneuve static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts,
533420cc9e8SHugo Villeneuve 			       int ts_id)
5342f861984SMian Yousaf Kaukab {
5352f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5362f861984SMian Yousaf Kaukab 	struct rtc_time tm;
5372f861984SMian Yousaf Kaukab 	int ret;
538720fb4b8SHugo Villeneuve 	unsigned char data[7];
5392f861984SMian Yousaf Kaukab 
540420cc9e8SHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->ts[ts_id].reg_base,
541420cc9e8SHugo Villeneuve 			       data, sizeof(data));
5422f861984SMian Yousaf Kaukab 	if (ret) {
5432f861984SMian Yousaf Kaukab 		dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
5442f861984SMian Yousaf Kaukab 		return ret;
5452f861984SMian Yousaf Kaukab 	}
5462f861984SMian Yousaf Kaukab 
5472f861984SMian Yousaf Kaukab 	dev_dbg(dev,
548720fb4b8SHugo Villeneuve 		"%s: raw data is ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n",
549720fb4b8SHugo Villeneuve 		__func__, data[1], data[2], data[3], data[4], data[5], data[6]);
5502f861984SMian Yousaf Kaukab 
551720fb4b8SHugo Villeneuve 	tm.tm_sec = bcd2bin(data[1] & 0x7F);
552720fb4b8SHugo Villeneuve 	tm.tm_min = bcd2bin(data[2] & 0x7F);
553720fb4b8SHugo Villeneuve 	tm.tm_hour = bcd2bin(data[3] & 0x3F);
554720fb4b8SHugo Villeneuve 	tm.tm_mday = bcd2bin(data[4] & 0x3F);
5552f861984SMian Yousaf Kaukab 	/* TS_MO register (month) value range: 1-12 */
556720fb4b8SHugo Villeneuve 	tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1;
557720fb4b8SHugo Villeneuve 	tm.tm_year = bcd2bin(data[6]);
5582f861984SMian Yousaf Kaukab 	if (tm.tm_year < 70)
5592f861984SMian Yousaf Kaukab 		tm.tm_year += 100; /* assume we are in 1970...2069 */
5602f861984SMian Yousaf Kaukab 
5612f861984SMian Yousaf Kaukab 	ret = rtc_valid_tm(&tm);
5622f861984SMian Yousaf Kaukab 	if (ret) {
5632f861984SMian Yousaf Kaukab 		dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
5642f861984SMian Yousaf Kaukab 		return ret;
5652f861984SMian Yousaf Kaukab 	}
5662f861984SMian Yousaf Kaukab 
5672f861984SMian Yousaf Kaukab 	*ts = rtc_tm_to_time64(&tm);
5682f861984SMian Yousaf Kaukab 	return 0;
5692f861984SMian Yousaf Kaukab };
5702f861984SMian Yousaf Kaukab 
571420cc9e8SHugo Villeneuve static void pcf2127_rtc_ts_snapshot(struct device *dev, int ts_id)
5722f861984SMian Yousaf Kaukab {
5732f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5742f861984SMian Yousaf Kaukab 	int ret;
5752f861984SMian Yousaf Kaukab 
576420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count)
5772f861984SMian Yousaf Kaukab 		return;
5782f861984SMian Yousaf Kaukab 
579420cc9e8SHugo Villeneuve 	/* Let userspace read the first timestamp */
580420cc9e8SHugo Villeneuve 	if (pcf2127->ts_valid[ts_id])
581420cc9e8SHugo Villeneuve 		return;
582420cc9e8SHugo Villeneuve 
583420cc9e8SHugo Villeneuve 	ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts[ts_id], ts_id);
5842f861984SMian Yousaf Kaukab 	if (!ret)
585420cc9e8SHugo Villeneuve 		pcf2127->ts_valid[ts_id] = true;
5862f861984SMian Yousaf Kaukab }
5872f861984SMian Yousaf Kaukab 
5888a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
5898a914bacSLiam Beguin {
5908a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
591*afc505bfSHugo Villeneuve 	unsigned int ctrl2;
5928a914bacSLiam Beguin 	int ret = 0;
5938a914bacSLiam Beguin 
594*afc505bfSHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
5952f861984SMian Yousaf Kaukab 	if (ret)
5962f861984SMian Yousaf Kaukab 		return IRQ_NONE;
5972f861984SMian Yousaf Kaukab 
598*afc505bfSHugo Villeneuve 	if (pcf2127->cfg->ts_count == 1) {
599*afc505bfSHugo Villeneuve 		/* PCF2127/29 */
600*afc505bfSHugo Villeneuve 		unsigned int ctrl1;
601*afc505bfSHugo Villeneuve 
602*afc505bfSHugo Villeneuve 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
6038a914bacSLiam Beguin 		if (ret)
6048a914bacSLiam Beguin 			return IRQ_NONE;
6058a914bacSLiam Beguin 
6062f861984SMian Yousaf Kaukab 		if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
60727006416SAlexandre Belloni 			return IRQ_NONE;
60827006416SAlexandre Belloni 
6092f861984SMian Yousaf Kaukab 		if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
610420cc9e8SHugo Villeneuve 			pcf2127_rtc_ts_snapshot(dev, 0);
6118a914bacSLiam Beguin 
6122f861984SMian Yousaf Kaukab 		if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
6132f861984SMian Yousaf Kaukab 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
6142f861984SMian Yousaf Kaukab 				     ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
6152f861984SMian Yousaf Kaukab 
6162f861984SMian Yousaf Kaukab 		if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
6172f861984SMian Yousaf Kaukab 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
6182f861984SMian Yousaf Kaukab 				     ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
619*afc505bfSHugo Villeneuve 	} else {
620*afc505bfSHugo Villeneuve 		/* PCF2131. */
621*afc505bfSHugo Villeneuve 		unsigned int ctrl4;
622*afc505bfSHugo Villeneuve 
623*afc505bfSHugo Villeneuve 		ret = regmap_read(pcf2127->regmap, PCF2131_REG_CTRL4, &ctrl4);
624*afc505bfSHugo Villeneuve 		if (ret)
625*afc505bfSHugo Villeneuve 			return IRQ_NONE;
626*afc505bfSHugo Villeneuve 
627*afc505bfSHugo Villeneuve 		if (!(ctrl4 & PCF2131_CTRL4_IRQ_MASK || ctrl2 & PCF2131_CTRL2_IRQ_MASK))
628*afc505bfSHugo Villeneuve 			return IRQ_NONE;
629*afc505bfSHugo Villeneuve 
630*afc505bfSHugo Villeneuve 		if (ctrl4 & PCF2131_CTRL4_IRQ_MASK) {
631*afc505bfSHugo Villeneuve 			int i;
632*afc505bfSHugo Villeneuve 			int tsf_bit = PCF2131_BIT_CTRL4_TSF1; /* Start at bit 7. */
633*afc505bfSHugo Villeneuve 
634*afc505bfSHugo Villeneuve 			for (i = 0; i < pcf2127->cfg->ts_count; i++) {
635*afc505bfSHugo Villeneuve 				if (ctrl4 & tsf_bit)
636*afc505bfSHugo Villeneuve 					pcf2127_rtc_ts_snapshot(dev, i);
637*afc505bfSHugo Villeneuve 
638*afc505bfSHugo Villeneuve 				tsf_bit = tsf_bit >> 1;
639*afc505bfSHugo Villeneuve 			}
640*afc505bfSHugo Villeneuve 
641*afc505bfSHugo Villeneuve 			regmap_write(pcf2127->regmap, PCF2131_REG_CTRL4,
642*afc505bfSHugo Villeneuve 				     ctrl4 & ~PCF2131_CTRL4_IRQ_MASK);
643*afc505bfSHugo Villeneuve 		}
644*afc505bfSHugo Villeneuve 
645*afc505bfSHugo Villeneuve 		if (ctrl2 & PCF2131_CTRL2_IRQ_MASK)
646*afc505bfSHugo Villeneuve 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
647*afc505bfSHugo Villeneuve 				     ctrl2 & ~PCF2131_CTRL2_IRQ_MASK);
648*afc505bfSHugo Villeneuve 	}
6492f861984SMian Yousaf Kaukab 
6502f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_BIT_CTRL2_AF)
6518a914bacSLiam Beguin 		rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
6528a914bacSLiam Beguin 
65327006416SAlexandre Belloni 	pcf2127_wdt_active_ping(&pcf2127->wdd);
6548a914bacSLiam Beguin 
6558a914bacSLiam Beguin 	return IRQ_HANDLED;
6568a914bacSLiam Beguin }
6578a914bacSLiam Beguin 
65825cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = {
6598a914bacSLiam Beguin 	.ioctl            = pcf2127_rtc_ioctl,
6608a914bacSLiam Beguin 	.read_time        = pcf2127_rtc_read_time,
6618a914bacSLiam Beguin 	.set_time         = pcf2127_rtc_set_time,
6628a914bacSLiam Beguin 	.read_alarm       = pcf2127_rtc_read_alarm,
6638a914bacSLiam Beguin 	.set_alarm        = pcf2127_rtc_set_alarm,
6648a914bacSLiam Beguin 	.alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
6658a914bacSLiam Beguin };
6668a914bacSLiam Beguin 
66703623b4bSBruno Thomsen /* sysfs interface */
66803623b4bSBruno Thomsen 
669420cc9e8SHugo Villeneuve static ssize_t timestamp_store(struct device *dev,
67003623b4bSBruno Thomsen 			       struct device_attribute *attr,
671420cc9e8SHugo Villeneuve 			       const char *buf, size_t count, int ts_id)
67203623b4bSBruno Thomsen {
67303623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
67403623b4bSBruno Thomsen 	int ret;
67503623b4bSBruno Thomsen 
676420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count)
677420cc9e8SHugo Villeneuve 		return 0;
678420cc9e8SHugo Villeneuve 
6792f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
680420cc9e8SHugo Villeneuve 		pcf2127->ts_valid[ts_id] = false;
6812f861984SMian Yousaf Kaukab 	} else {
682420cc9e8SHugo Villeneuve 		/* Always clear GND interrupt bit. */
683420cc9e8SHugo Villeneuve 		ret = regmap_update_bits(pcf2127->regmap,
684420cc9e8SHugo Villeneuve 					 pcf2127->cfg->ts[ts_id].gnd_detect_reg,
685420cc9e8SHugo Villeneuve 					 pcf2127->cfg->ts[ts_id].gnd_detect_bit,
686420cc9e8SHugo Villeneuve 					 0);
687420cc9e8SHugo Villeneuve 
68803623b4bSBruno Thomsen 		if (ret) {
689420cc9e8SHugo Villeneuve 			dev_err(dev, "%s: update TS gnd detect ret=%d\n", __func__, ret);
69003623b4bSBruno Thomsen 			return ret;
69103623b4bSBruno Thomsen 		}
69203623b4bSBruno Thomsen 
693420cc9e8SHugo Villeneuve 		if (pcf2127->cfg->ts[ts_id].inter_detect_bit) {
694420cc9e8SHugo Villeneuve 			/* Clear intermediate level interrupt bit if supported. */
695420cc9e8SHugo Villeneuve 			ret = regmap_update_bits(pcf2127->regmap,
696420cc9e8SHugo Villeneuve 						 pcf2127->cfg->ts[ts_id].inter_detect_reg,
697420cc9e8SHugo Villeneuve 						 pcf2127->cfg->ts[ts_id].inter_detect_bit,
698420cc9e8SHugo Villeneuve 						 0);
69903623b4bSBruno Thomsen 			if (ret) {
700420cc9e8SHugo Villeneuve 				dev_err(dev, "%s: update TS intermediate level detect ret=%d\n",
701420cc9e8SHugo Villeneuve 					__func__, ret);
70203623b4bSBruno Thomsen 				return ret;
70303623b4bSBruno Thomsen 			}
704420cc9e8SHugo Villeneuve 		}
70503623b4bSBruno Thomsen 
70603623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
70703623b4bSBruno Thomsen 		if (ret)
70803623b4bSBruno Thomsen 			return ret;
7092f861984SMian Yousaf Kaukab 	}
71003623b4bSBruno Thomsen 
71103623b4bSBruno Thomsen 	return count;
712420cc9e8SHugo Villeneuve }
713420cc9e8SHugo Villeneuve 
714420cc9e8SHugo Villeneuve static ssize_t timestamp0_store(struct device *dev,
715420cc9e8SHugo Villeneuve 				struct device_attribute *attr,
716420cc9e8SHugo Villeneuve 				const char *buf, size_t count)
717420cc9e8SHugo Villeneuve {
718420cc9e8SHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 0);
71903623b4bSBruno Thomsen };
72003623b4bSBruno Thomsen 
721*afc505bfSHugo Villeneuve static ssize_t timestamp1_store(struct device *dev,
722*afc505bfSHugo Villeneuve 				struct device_attribute *attr,
723*afc505bfSHugo Villeneuve 				const char *buf, size_t count)
724*afc505bfSHugo Villeneuve {
725*afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 1);
726*afc505bfSHugo Villeneuve };
727*afc505bfSHugo Villeneuve 
728*afc505bfSHugo Villeneuve static ssize_t timestamp2_store(struct device *dev,
729*afc505bfSHugo Villeneuve 				struct device_attribute *attr,
730*afc505bfSHugo Villeneuve 				const char *buf, size_t count)
731*afc505bfSHugo Villeneuve {
732*afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 2);
733*afc505bfSHugo Villeneuve };
734*afc505bfSHugo Villeneuve 
735*afc505bfSHugo Villeneuve static ssize_t timestamp3_store(struct device *dev,
736*afc505bfSHugo Villeneuve 				struct device_attribute *attr,
737*afc505bfSHugo Villeneuve 				const char *buf, size_t count)
738*afc505bfSHugo Villeneuve {
739*afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 3);
740*afc505bfSHugo Villeneuve };
741*afc505bfSHugo Villeneuve 
742420cc9e8SHugo Villeneuve static ssize_t timestamp_show(struct device *dev,
743420cc9e8SHugo Villeneuve 			      struct device_attribute *attr, char *buf,
744420cc9e8SHugo Villeneuve 			      int ts_id)
74503623b4bSBruno Thomsen {
74603623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
74703623b4bSBruno Thomsen 	int ret;
7482f861984SMian Yousaf Kaukab 	time64_t ts;
74903623b4bSBruno Thomsen 
750420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count)
751420cc9e8SHugo Villeneuve 		return 0;
752420cc9e8SHugo Villeneuve 
7532f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
754420cc9e8SHugo Villeneuve 		if (!pcf2127->ts_valid[ts_id])
7552f861984SMian Yousaf Kaukab 			return 0;
756420cc9e8SHugo Villeneuve 		ts = pcf2127->ts[ts_id];
7572f861984SMian Yousaf Kaukab 	} else {
758420cc9e8SHugo Villeneuve 		u8 valid_low = 0;
759420cc9e8SHugo Villeneuve 		u8 valid_inter = 0;
760420cc9e8SHugo Villeneuve 		unsigned int ctrl;
761420cc9e8SHugo Villeneuve 
762420cc9e8SHugo Villeneuve 		/* Check if TS input pin is driven to GND, supported by all
763420cc9e8SHugo Villeneuve 		 * variants.
764420cc9e8SHugo Villeneuve 		 */
765420cc9e8SHugo Villeneuve 		ret = regmap_read(pcf2127->regmap,
766420cc9e8SHugo Villeneuve 				  pcf2127->cfg->ts[ts_id].gnd_detect_reg,
767420cc9e8SHugo Villeneuve 				  &ctrl);
7682f861984SMian Yousaf Kaukab 		if (ret)
7692f861984SMian Yousaf Kaukab 			return 0;
77003623b4bSBruno Thomsen 
771420cc9e8SHugo Villeneuve 		valid_low = ctrl & pcf2127->cfg->ts[ts_id].gnd_detect_bit;
772420cc9e8SHugo Villeneuve 
773420cc9e8SHugo Villeneuve 		if (pcf2127->cfg->ts[ts_id].inter_detect_bit) {
774420cc9e8SHugo Villeneuve 			/* Check if TS input pin is driven to intermediate level
775420cc9e8SHugo Villeneuve 			 * between GND and supply, if supported by variant.
776420cc9e8SHugo Villeneuve 			 */
777420cc9e8SHugo Villeneuve 			ret = regmap_read(pcf2127->regmap,
778420cc9e8SHugo Villeneuve 					  pcf2127->cfg->ts[ts_id].inter_detect_reg,
779420cc9e8SHugo Villeneuve 					  &ctrl);
7802f861984SMian Yousaf Kaukab 			if (ret)
7812f861984SMian Yousaf Kaukab 				return 0;
7822f861984SMian Yousaf Kaukab 
783420cc9e8SHugo Villeneuve 			valid_inter = ctrl & pcf2127->cfg->ts[ts_id].inter_detect_bit;
784420cc9e8SHugo Villeneuve 		}
785420cc9e8SHugo Villeneuve 
786420cc9e8SHugo Villeneuve 		if (!valid_low && !valid_inter)
7872f861984SMian Yousaf Kaukab 			return 0;
7882f861984SMian Yousaf Kaukab 
789420cc9e8SHugo Villeneuve 		ret = pcf2127_rtc_ts_read(dev->parent, &ts, ts_id);
7902f861984SMian Yousaf Kaukab 		if (ret)
7912f861984SMian Yousaf Kaukab 			return 0;
79203623b4bSBruno Thomsen 
79303623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
79403623b4bSBruno Thomsen 		if (ret)
79503623b4bSBruno Thomsen 			return ret;
7962f861984SMian Yousaf Kaukab 	}
7972f861984SMian Yousaf Kaukab 	return sprintf(buf, "%llu\n", (unsigned long long)ts);
798420cc9e8SHugo Villeneuve }
799420cc9e8SHugo Villeneuve 
800420cc9e8SHugo Villeneuve static ssize_t timestamp0_show(struct device *dev,
801420cc9e8SHugo Villeneuve 			       struct device_attribute *attr, char *buf)
802420cc9e8SHugo Villeneuve {
803420cc9e8SHugo Villeneuve 	return timestamp_show(dev, attr, buf, 0);
80403623b4bSBruno Thomsen };
80503623b4bSBruno Thomsen 
806*afc505bfSHugo Villeneuve static ssize_t timestamp1_show(struct device *dev,
807*afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
808*afc505bfSHugo Villeneuve {
809*afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 1);
810*afc505bfSHugo Villeneuve };
811*afc505bfSHugo Villeneuve 
812*afc505bfSHugo Villeneuve static ssize_t timestamp2_show(struct device *dev,
813*afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
814*afc505bfSHugo Villeneuve {
815*afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 2);
816*afc505bfSHugo Villeneuve };
817*afc505bfSHugo Villeneuve 
818*afc505bfSHugo Villeneuve static ssize_t timestamp3_show(struct device *dev,
819*afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
820*afc505bfSHugo Villeneuve {
821*afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 3);
822*afc505bfSHugo Villeneuve };
823*afc505bfSHugo Villeneuve 
82403623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0);
825*afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp1);
826*afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp2);
827*afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp3);
82803623b4bSBruno Thomsen 
82903623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = {
83003623b4bSBruno Thomsen 	&dev_attr_timestamp0.attr,
83103623b4bSBruno Thomsen 	NULL
83203623b4bSBruno Thomsen };
83303623b4bSBruno Thomsen 
834*afc505bfSHugo Villeneuve static struct attribute *pcf2131_attrs[] = {
835*afc505bfSHugo Villeneuve 	&dev_attr_timestamp0.attr,
836*afc505bfSHugo Villeneuve 	&dev_attr_timestamp1.attr,
837*afc505bfSHugo Villeneuve 	&dev_attr_timestamp2.attr,
838*afc505bfSHugo Villeneuve 	&dev_attr_timestamp3.attr,
839*afc505bfSHugo Villeneuve 	NULL
840*afc505bfSHugo Villeneuve };
841*afc505bfSHugo Villeneuve 
842fd28ceb4SHugo Villeneuve static struct pcf21xx_config pcf21xx_cfg[] = {
843fd28ceb4SHugo Villeneuve 	[PCF2127] = {
844fd28ceb4SHugo Villeneuve 		.type = PCF2127,
845fd28ceb4SHugo Villeneuve 		.max_register = 0x1d,
846fd28ceb4SHugo Villeneuve 		.has_nvmem = 1,
847fd28ceb4SHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 1,
8486211aceeSHugo Villeneuve 		.reg_time_base = PCF2127_REG_TIME_BASE,
8497c6f0db4SHugo Villeneuve 		.regs_alarm_base = PCF2127_REG_ALARM_BASE,
8506b57ec29SHugo Villeneuve 		.reg_wd_ctl = PCF2127_REG_WD_CTL,
8516b57ec29SHugo Villeneuve 		.reg_wd_val = PCF2127_REG_WD_VAL,
852fc16599eSHugo Villeneuve 		.reg_clkout = PCF2127_REG_CLKOUT,
853420cc9e8SHugo Villeneuve 		.ts_count = 1,
854420cc9e8SHugo Villeneuve 		.ts[0] = {
855420cc9e8SHugo Villeneuve 			.reg_base  = PCF2127_REG_TS1_BASE,
856420cc9e8SHugo Villeneuve 			.gnd_detect_reg = PCF2127_REG_CTRL1,
857420cc9e8SHugo Villeneuve 			.gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1,
858420cc9e8SHugo Villeneuve 			.inter_detect_reg = PCF2127_REG_CTRL2,
859420cc9e8SHugo Villeneuve 			.inter_detect_bit = PCF2127_BIT_CTRL2_TSF2,
860420cc9e8SHugo Villeneuve 			.ie_reg    = PCF2127_REG_CTRL2,
861420cc9e8SHugo Villeneuve 			.ie_bit    = PCF2127_BIT_CTRL2_TSIE,
862420cc9e8SHugo Villeneuve 		},
863420cc9e8SHugo Villeneuve 		.attribute_group = {
864420cc9e8SHugo Villeneuve 			.attrs	= pcf2127_attrs,
865420cc9e8SHugo Villeneuve 		},
866fd28ceb4SHugo Villeneuve 	},
867fd28ceb4SHugo Villeneuve 	[PCF2129] = {
868fd28ceb4SHugo Villeneuve 		.type = PCF2129,
869fd28ceb4SHugo Villeneuve 		.max_register = 0x19,
870fd28ceb4SHugo Villeneuve 		.has_nvmem = 0,
871fd28ceb4SHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 0,
8726211aceeSHugo Villeneuve 		.reg_time_base = PCF2127_REG_TIME_BASE,
8737c6f0db4SHugo Villeneuve 		.regs_alarm_base = PCF2127_REG_ALARM_BASE,
8746b57ec29SHugo Villeneuve 		.reg_wd_ctl = PCF2127_REG_WD_CTL,
8756b57ec29SHugo Villeneuve 		.reg_wd_val = PCF2127_REG_WD_VAL,
876fc16599eSHugo Villeneuve 		.reg_clkout = PCF2127_REG_CLKOUT,
877420cc9e8SHugo Villeneuve 		.ts_count = 1,
878420cc9e8SHugo Villeneuve 		.ts[0] = {
879420cc9e8SHugo Villeneuve 			.reg_base  = PCF2127_REG_TS1_BASE,
880420cc9e8SHugo Villeneuve 			.gnd_detect_reg = PCF2127_REG_CTRL1,
881420cc9e8SHugo Villeneuve 			.gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1,
882420cc9e8SHugo Villeneuve 			.inter_detect_reg = PCF2127_REG_CTRL2,
883420cc9e8SHugo Villeneuve 			.inter_detect_bit = PCF2127_BIT_CTRL2_TSF2,
884420cc9e8SHugo Villeneuve 			.ie_reg    = PCF2127_REG_CTRL2,
885420cc9e8SHugo Villeneuve 			.ie_bit    = PCF2127_BIT_CTRL2_TSIE,
886420cc9e8SHugo Villeneuve 		},
887420cc9e8SHugo Villeneuve 		.attribute_group = {
888420cc9e8SHugo Villeneuve 			.attrs	= pcf2127_attrs,
889420cc9e8SHugo Villeneuve 		},
890fd28ceb4SHugo Villeneuve 	},
891*afc505bfSHugo Villeneuve 	[PCF2131] = {
892*afc505bfSHugo Villeneuve 		.type = PCF2131,
893*afc505bfSHugo Villeneuve 		.max_register = 0x36,
894*afc505bfSHugo Villeneuve 		.has_nvmem = 0,
895*afc505bfSHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 0,
896*afc505bfSHugo Villeneuve 		.reg_time_base = PCF2131_REG_TIME_BASE,
897*afc505bfSHugo Villeneuve 		.regs_alarm_base = PCF2131_REG_ALARM_BASE,
898*afc505bfSHugo Villeneuve 		.reg_wd_ctl = PCF2131_REG_WD_CTL,
899*afc505bfSHugo Villeneuve 		.reg_wd_val = PCF2131_REG_WD_VAL,
900*afc505bfSHugo Villeneuve 		.reg_clkout = PCF2131_REG_CLKOUT,
901*afc505bfSHugo Villeneuve 		.ts_count = 4,
902*afc505bfSHugo Villeneuve 		.ts[0] = {
903*afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS1_BASE,
904*afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
905*afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF1,
906*afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
907*afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
908*afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE1,
909*afc505bfSHugo Villeneuve 		},
910*afc505bfSHugo Villeneuve 		.ts[1] = {
911*afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS2_BASE,
912*afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
913*afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF2,
914*afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
915*afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
916*afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE2,
917*afc505bfSHugo Villeneuve 		},
918*afc505bfSHugo Villeneuve 		.ts[2] = {
919*afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS3_BASE,
920*afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
921*afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF3,
922*afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
923*afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
924*afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE3,
925*afc505bfSHugo Villeneuve 		},
926*afc505bfSHugo Villeneuve 		.ts[3] = {
927*afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS4_BASE,
928*afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
929*afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF4,
930*afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
931*afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
932*afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE4,
933*afc505bfSHugo Villeneuve 		},
934*afc505bfSHugo Villeneuve 		.attribute_group = {
935*afc505bfSHugo Villeneuve 			.attrs	= pcf2131_attrs,
936*afc505bfSHugo Villeneuve 		},
937*afc505bfSHugo Villeneuve 	},
938fd28ceb4SHugo Villeneuve };
939fd28ceb4SHugo Villeneuve 
940420cc9e8SHugo Villeneuve /*
941420cc9e8SHugo Villeneuve  * Enable timestamp function and corresponding interrupt(s).
942420cc9e8SHugo Villeneuve  */
943420cc9e8SHugo Villeneuve static int pcf2127_enable_ts(struct device *dev, int ts_id)
944420cc9e8SHugo Villeneuve {
945420cc9e8SHugo Villeneuve 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
946420cc9e8SHugo Villeneuve 	int ret;
947420cc9e8SHugo Villeneuve 
948420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count) {
949420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: invalid tamper detection ID (%d)\n",
950420cc9e8SHugo Villeneuve 			__func__, ts_id);
951420cc9e8SHugo Villeneuve 		return -EINVAL;
952420cc9e8SHugo Villeneuve 	}
953420cc9e8SHugo Villeneuve 
954420cc9e8SHugo Villeneuve 	/* Enable timestamp function. */
955420cc9e8SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap,
956420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].reg_base,
957420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSOFF |
958420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSM,
959420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSM);
960420cc9e8SHugo Villeneuve 	if (ret) {
961420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection config (ts%d_ctrl) failed\n",
962420cc9e8SHugo Villeneuve 			__func__, ts_id);
963420cc9e8SHugo Villeneuve 		return ret;
964420cc9e8SHugo Villeneuve 	}
965420cc9e8SHugo Villeneuve 
966420cc9e8SHugo Villeneuve 	/* TS input pin driven to GND detection is supported by all variants.
967420cc9e8SHugo Villeneuve 	 * Make sure that interrupt bit is defined.
968420cc9e8SHugo Villeneuve 	 */
969420cc9e8SHugo Villeneuve 	if (pcf2127->cfg->ts[ts_id].gnd_detect_bit == 0) {
970420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection to GND configuration invalid\n",
971420cc9e8SHugo Villeneuve 			__func__);
972420cc9e8SHugo Villeneuve 		return ret;
973420cc9e8SHugo Villeneuve 	}
974420cc9e8SHugo Villeneuve 
975420cc9e8SHugo Villeneuve 	/*
976420cc9e8SHugo Villeneuve 	 * Enable interrupt generation when TSF timestamp flag is set.
977420cc9e8SHugo Villeneuve 	 * Interrupt signals are open-drain outputs and can be left floating if
978420cc9e8SHugo Villeneuve 	 * unused.
979420cc9e8SHugo Villeneuve 	 */
980420cc9e8SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->ts[ts_id].ie_reg,
981420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].ie_bit,
982420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].ie_bit);
983420cc9e8SHugo Villeneuve 	if (ret) {
984420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection TSIE%d config failed\n",
985420cc9e8SHugo Villeneuve 			__func__, ts_id);
986420cc9e8SHugo Villeneuve 		return ret;
987420cc9e8SHugo Villeneuve 	}
988420cc9e8SHugo Villeneuve 
989420cc9e8SHugo Villeneuve 	return ret;
990420cc9e8SHugo Villeneuve }
991420cc9e8SHugo Villeneuve 
992907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap,
993fd28ceb4SHugo Villeneuve 			 int alarm_irq, const char *name, const struct pcf21xx_config *config)
99418cb6368SRenaud Cerrato {
99518cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
996d6c3029fSUwe Kleine-König 	int ret = 0;
99715f57b3eSPhilipp Rosenberger 	unsigned int val;
99818cb6368SRenaud Cerrato 
999907b3262SAkinobu Mita 	dev_dbg(dev, "%s\n", __func__);
100018cb6368SRenaud Cerrato 
1001907b3262SAkinobu Mita 	pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
100218cb6368SRenaud Cerrato 	if (!pcf2127)
100318cb6368SRenaud Cerrato 		return -ENOMEM;
100418cb6368SRenaud Cerrato 
1005907b3262SAkinobu Mita 	pcf2127->regmap = regmap;
1006fd28ceb4SHugo Villeneuve 	pcf2127->cfg = config;
100718cb6368SRenaud Cerrato 
1008907b3262SAkinobu Mita 	dev_set_drvdata(dev, pcf2127);
1009907b3262SAkinobu Mita 
1010e788771cSBruno Thomsen 	pcf2127->rtc = devm_rtc_allocate_device(dev);
1011d6c3029fSUwe Kleine-König 	if (IS_ERR(pcf2127->rtc))
1012d6c3029fSUwe Kleine-König 		return PTR_ERR(pcf2127->rtc);
101318cb6368SRenaud Cerrato 
1014e788771cSBruno Thomsen 	pcf2127->rtc->ops = &pcf2127_rtc_ops;
1015b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
1016b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
1017b139bb5cSAlexandre Belloni 	pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
1018bda10273SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features);
1019689fafd5SAlexandre Belloni 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features);
102025cbe9c8SAlexandre Belloni 	clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
1021e788771cSBruno Thomsen 
102235425bafSBiwen Li 	if (alarm_irq > 0) {
1023d4785b46SHugo Villeneuve 		unsigned long flags;
1024d4785b46SHugo Villeneuve 
1025d4785b46SHugo Villeneuve 		/*
1026d4785b46SHugo Villeneuve 		 * If flags = 0, devm_request_threaded_irq() will use IRQ flags
1027d4785b46SHugo Villeneuve 		 * obtained from device tree.
1028d4785b46SHugo Villeneuve 		 */
1029d4785b46SHugo Villeneuve 		if (dev_fwnode(dev))
1030d4785b46SHugo Villeneuve 			flags = 0;
1031d4785b46SHugo Villeneuve 		else
1032d4785b46SHugo Villeneuve 			flags = IRQF_TRIGGER_LOW;
1033d4785b46SHugo Villeneuve 
103427006416SAlexandre Belloni 		ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
103527006416SAlexandre Belloni 						pcf2127_rtc_irq,
1036d4785b46SHugo Villeneuve 						flags | IRQF_ONESHOT,
10378a914bacSLiam Beguin 						dev_name(dev), dev);
10388a914bacSLiam Beguin 		if (ret) {
10398a914bacSLiam Beguin 			dev_err(dev, "failed to request alarm irq\n");
10408a914bacSLiam Beguin 			return ret;
10418a914bacSLiam Beguin 		}
10422f861984SMian Yousaf Kaukab 		pcf2127->irq_enabled = true;
10438a914bacSLiam Beguin 	}
10448a914bacSLiam Beguin 
104535425bafSBiwen Li 	if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
10468a914bacSLiam Beguin 		device_init_wakeup(dev, true);
104725cbe9c8SAlexandre Belloni 		set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
10488a914bacSLiam Beguin 	}
10498a914bacSLiam Beguin 
1050fd28ceb4SHugo Villeneuve 	if (pcf2127->cfg->has_nvmem) {
1051d6c3029fSUwe Kleine-König 		struct nvmem_config nvmem_cfg = {
1052d6c3029fSUwe Kleine-König 			.priv = pcf2127,
1053d6c3029fSUwe Kleine-König 			.reg_read = pcf2127_nvmem_read,
1054d6c3029fSUwe Kleine-König 			.reg_write = pcf2127_nvmem_write,
1055d6c3029fSUwe Kleine-König 			.size = 512,
1056d6c3029fSUwe Kleine-König 		};
1057d6c3029fSUwe Kleine-König 
10583a905c2dSBartosz Golaszewski 		ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
1059d6c3029fSUwe Kleine-König 	}
1060d6c3029fSUwe Kleine-König 
10610e735eaaSBruno Thomsen 	/*
1062b9ac079aSPhilipp Rosenberger 	 * The "Power-On Reset Override" facility prevents the RTC to do a reset
1063b9ac079aSPhilipp Rosenberger 	 * after power on. For normal operation the PORO must be disabled.
1064b9ac079aSPhilipp Rosenberger 	 */
1065b9ac079aSPhilipp Rosenberger 	regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
1066b9ac079aSPhilipp Rosenberger 				PCF2127_BIT_CTRL1_POR_OVRD);
1067b9ac079aSPhilipp Rosenberger 
1068fc16599eSHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_clkout, &val);
106915f57b3eSPhilipp Rosenberger 	if (ret < 0)
107015f57b3eSPhilipp Rosenberger 		return ret;
107115f57b3eSPhilipp Rosenberger 
107215f57b3eSPhilipp Rosenberger 	if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
1073fc16599eSHugo Villeneuve 		ret = regmap_set_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout,
107415f57b3eSPhilipp Rosenberger 				      PCF2127_BIT_CLKOUT_OTPR);
107515f57b3eSPhilipp Rosenberger 		if (ret < 0)
107615f57b3eSPhilipp Rosenberger 			return ret;
107715f57b3eSPhilipp Rosenberger 
107815f57b3eSPhilipp Rosenberger 		msleep(100);
107915f57b3eSPhilipp Rosenberger 	}
108015f57b3eSPhilipp Rosenberger 
1081b9ac079aSPhilipp Rosenberger 	/*
10820e735eaaSBruno Thomsen 	 * Watchdog timer enabled and reset pin /RST activated when timed out.
10830e735eaaSBruno Thomsen 	 * Select 1Hz clock source for watchdog timer.
10840e735eaaSBruno Thomsen 	 * Note: Countdown timer disabled and not available.
1085*afc505bfSHugo Villeneuve 	 * For pca2129, pcf2129 and pcf2131, only bit[7] is for Symbol WD_CD
10862843d565SBiwen Li 	 * of register watchdg_tim_ctl. The bit[6] is labeled
10872843d565SBiwen Li 	 * as T. Bits labeled as T must always be written with
10882843d565SBiwen Li 	 * logic 0.
10890e735eaaSBruno Thomsen 	 */
10906b57ec29SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_wd_ctl,
10910e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
10920e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD0 |
10930e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1 |
10940e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF0,
10950e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
1096fd28ceb4SHugo Villeneuve 				 (pcf2127->cfg->has_bit_wd_ctl_cd0 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
10970e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1);
10980e735eaaSBruno Thomsen 	if (ret) {
10990e735eaaSBruno Thomsen 		dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
11000e735eaaSBruno Thomsen 		return ret;
11010e735eaaSBruno Thomsen 	}
11020e735eaaSBruno Thomsen 
11035d78533aSUwe Kleine-König 	pcf2127_watchdog_init(dev, pcf2127);
11040e735eaaSBruno Thomsen 
110503623b4bSBruno Thomsen 	/*
110603623b4bSBruno Thomsen 	 * Disable battery low/switch-over timestamp and interrupts.
110703623b4bSBruno Thomsen 	 * Clear battery interrupt flags which can block new trigger events.
110803623b4bSBruno Thomsen 	 * Note: This is the default chip behaviour but added to ensure
110903623b4bSBruno Thomsen 	 * correct tamper timestamp and interrupt function.
111003623b4bSBruno Thomsen 	 */
111103623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
111203623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BTSE |
111303623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BIE |
111403623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BLIE, 0);
111503623b4bSBruno Thomsen 	if (ret) {
111603623b4bSBruno Thomsen 		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
111703623b4bSBruno Thomsen 			__func__);
111803623b4bSBruno Thomsen 		return ret;
111903623b4bSBruno Thomsen 	}
112003623b4bSBruno Thomsen 
112103623b4bSBruno Thomsen 	/*
1122420cc9e8SHugo Villeneuve 	 * Enable timestamp functions 1 to 4.
112303623b4bSBruno Thomsen 	 */
1124420cc9e8SHugo Villeneuve 	for (int i = 0; i < pcf2127->cfg->ts_count; i++) {
1125420cc9e8SHugo Villeneuve 		ret = pcf2127_enable_ts(dev, i);
1126420cc9e8SHugo Villeneuve 		if (ret)
112703623b4bSBruno Thomsen 			return ret;
112803623b4bSBruno Thomsen 	}
112903623b4bSBruno Thomsen 
1130420cc9e8SHugo Villeneuve 	ret = rtc_add_group(pcf2127->rtc, &pcf2127->cfg->attribute_group);
113103623b4bSBruno Thomsen 	if (ret) {
113203623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper sysfs registering failed\n",
113303623b4bSBruno Thomsen 			__func__);
113403623b4bSBruno Thomsen 		return ret;
113503623b4bSBruno Thomsen 	}
113603623b4bSBruno Thomsen 
1137fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(pcf2127->rtc);
113818cb6368SRenaud Cerrato }
113918cb6368SRenaud Cerrato 
114018cb6368SRenaud Cerrato #ifdef CONFIG_OF
114118cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
1142fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pcf2127", .data = &pcf21xx_cfg[PCF2127] },
1143fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pcf2129", .data = &pcf21xx_cfg[PCF2129] },
1144fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pca2129", .data = &pcf21xx_cfg[PCF2129] },
1145*afc505bfSHugo Villeneuve 	{ .compatible = "nxp,pcf2131", .data = &pcf21xx_cfg[PCF2131] },
114618cb6368SRenaud Cerrato 	{}
114718cb6368SRenaud Cerrato };
114818cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
114918cb6368SRenaud Cerrato #endif
115018cb6368SRenaud Cerrato 
11519408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C)
11529408ec1aSAkinobu Mita 
1153907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count)
1154907b3262SAkinobu Mita {
1155907b3262SAkinobu Mita 	struct device *dev = context;
1156907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1157907b3262SAkinobu Mita 	int ret;
1158907b3262SAkinobu Mita 
1159907b3262SAkinobu Mita 	ret = i2c_master_send(client, data, count);
1160907b3262SAkinobu Mita 	if (ret != count)
1161907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1162907b3262SAkinobu Mita 
1163907b3262SAkinobu Mita 	return 0;
1164907b3262SAkinobu Mita }
1165907b3262SAkinobu Mita 
1166907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context,
1167907b3262SAkinobu Mita 				const void *reg, size_t reg_size,
1168907b3262SAkinobu Mita 				const void *val, size_t val_size)
1169907b3262SAkinobu Mita {
1170907b3262SAkinobu Mita 	struct device *dev = context;
1171907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1172907b3262SAkinobu Mita 	int ret;
1173907b3262SAkinobu Mita 	void *buf;
1174907b3262SAkinobu Mita 
1175907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
1176907b3262SAkinobu Mita 		return -EINVAL;
1177907b3262SAkinobu Mita 
1178907b3262SAkinobu Mita 	buf = kmalloc(val_size + 1, GFP_KERNEL);
1179907b3262SAkinobu Mita 	if (!buf)
1180907b3262SAkinobu Mita 		return -ENOMEM;
1181907b3262SAkinobu Mita 
1182907b3262SAkinobu Mita 	memcpy(buf, reg, 1);
1183907b3262SAkinobu Mita 	memcpy(buf + 1, val, val_size);
1184907b3262SAkinobu Mita 
1185907b3262SAkinobu Mita 	ret = i2c_master_send(client, buf, val_size + 1);
11869bde0afbSXulin Sun 
11879bde0afbSXulin Sun 	kfree(buf);
11889bde0afbSXulin Sun 
1189907b3262SAkinobu Mita 	if (ret != val_size + 1)
1190907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1191907b3262SAkinobu Mita 
1192907b3262SAkinobu Mita 	return 0;
1193907b3262SAkinobu Mita }
1194907b3262SAkinobu Mita 
1195907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
1196907b3262SAkinobu Mita 				void *val, size_t val_size)
1197907b3262SAkinobu Mita {
1198907b3262SAkinobu Mita 	struct device *dev = context;
1199907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1200907b3262SAkinobu Mita 	int ret;
1201907b3262SAkinobu Mita 
1202907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
1203907b3262SAkinobu Mita 		return -EINVAL;
1204907b3262SAkinobu Mita 
1205907b3262SAkinobu Mita 	ret = i2c_master_send(client, reg, 1);
1206907b3262SAkinobu Mita 	if (ret != 1)
1207907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1208907b3262SAkinobu Mita 
1209907b3262SAkinobu Mita 	ret = i2c_master_recv(client, val, val_size);
1210907b3262SAkinobu Mita 	if (ret != val_size)
1211907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1212907b3262SAkinobu Mita 
1213907b3262SAkinobu Mita 	return 0;
1214907b3262SAkinobu Mita }
1215907b3262SAkinobu Mita 
1216907b3262SAkinobu Mita /*
1217907b3262SAkinobu Mita  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
1218907b3262SAkinobu Mita  * is that the STOP condition is required between set register address and
1219907b3262SAkinobu Mita  * read register data when reading from registers.
1220907b3262SAkinobu Mita  */
1221907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = {
1222907b3262SAkinobu Mita 	.write = pcf2127_i2c_write,
1223907b3262SAkinobu Mita 	.gather_write = pcf2127_i2c_gather_write,
1224907b3262SAkinobu Mita 	.read = pcf2127_i2c_read,
122518cb6368SRenaud Cerrato };
122618cb6368SRenaud Cerrato 
1227907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver;
1228907b3262SAkinobu Mita 
12295418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = {
1230fd28ceb4SHugo Villeneuve 	{ "pcf2127", PCF2127 },
1231fd28ceb4SHugo Villeneuve 	{ "pcf2129", PCF2129 },
1232fd28ceb4SHugo Villeneuve 	{ "pca2129", PCF2129 },
1233*afc505bfSHugo Villeneuve 	{ "pcf2131", PCF2131 },
12345418e595SUwe Kleine-König 	{ }
12355418e595SUwe Kleine-König };
12365418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
12375418e595SUwe Kleine-König 
12385418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client)
1239907b3262SAkinobu Mita {
1240907b3262SAkinobu Mita 	struct regmap *regmap;
1241fd28ceb4SHugo Villeneuve 	static struct regmap_config config = {
1242907b3262SAkinobu Mita 		.reg_bits = 8,
1243907b3262SAkinobu Mita 		.val_bits = 8,
1244907b3262SAkinobu Mita 	};
1245fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *variant;
1246907b3262SAkinobu Mita 
1247907b3262SAkinobu Mita 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1248907b3262SAkinobu Mita 		return -ENODEV;
1249907b3262SAkinobu Mita 
1250fd28ceb4SHugo Villeneuve 	if (client->dev.of_node) {
1251fd28ceb4SHugo Villeneuve 		variant = of_device_get_match_data(&client->dev);
1252fd28ceb4SHugo Villeneuve 		if (!variant)
1253fd28ceb4SHugo Villeneuve 			return -ENODEV;
1254fd28ceb4SHugo Villeneuve 	} else {
1255fd28ceb4SHugo Villeneuve 		enum pcf21xx_type type =
1256fd28ceb4SHugo Villeneuve 			i2c_match_id(pcf2127_i2c_id, client)->driver_data;
1257fd28ceb4SHugo Villeneuve 
1258fd28ceb4SHugo Villeneuve 		if (type >= PCF21XX_LAST_ID)
1259fd28ceb4SHugo Villeneuve 			return -ENODEV;
1260fd28ceb4SHugo Villeneuve 		variant = &pcf21xx_cfg[type];
1261fd28ceb4SHugo Villeneuve 	}
1262fd28ceb4SHugo Villeneuve 
1263fd28ceb4SHugo Villeneuve 	config.max_register = variant->max_register,
1264fd28ceb4SHugo Villeneuve 
1265907b3262SAkinobu Mita 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
1266907b3262SAkinobu Mita 					&client->dev, &config);
1267907b3262SAkinobu Mita 	if (IS_ERR(regmap)) {
1268907b3262SAkinobu Mita 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
1269907b3262SAkinobu Mita 			__func__, PTR_ERR(regmap));
1270907b3262SAkinobu Mita 		return PTR_ERR(regmap);
1271907b3262SAkinobu Mita 	}
1272907b3262SAkinobu Mita 
127327006416SAlexandre Belloni 	return pcf2127_probe(&client->dev, regmap, client->irq,
1274fd28ceb4SHugo Villeneuve 			     pcf2127_i2c_driver.driver.name, variant);
1275907b3262SAkinobu Mita }
1276907b3262SAkinobu Mita 
1277907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = {
1278907b3262SAkinobu Mita 	.driver		= {
1279907b3262SAkinobu Mita 		.name	= "rtc-pcf2127-i2c",
1280907b3262SAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
1281907b3262SAkinobu Mita 	},
128231b0cecbSUwe Kleine-König 	.probe		= pcf2127_i2c_probe,
1283907b3262SAkinobu Mita 	.id_table	= pcf2127_i2c_id,
1284907b3262SAkinobu Mita };
12859408ec1aSAkinobu Mita 
12869408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
12879408ec1aSAkinobu Mita {
12889408ec1aSAkinobu Mita 	return i2c_add_driver(&pcf2127_i2c_driver);
12899408ec1aSAkinobu Mita }
12909408ec1aSAkinobu Mita 
12919408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
12929408ec1aSAkinobu Mita {
12939408ec1aSAkinobu Mita 	i2c_del_driver(&pcf2127_i2c_driver);
12949408ec1aSAkinobu Mita }
12959408ec1aSAkinobu Mita 
12969408ec1aSAkinobu Mita #else
12979408ec1aSAkinobu Mita 
12989408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
12999408ec1aSAkinobu Mita {
13009408ec1aSAkinobu Mita 	return 0;
13019408ec1aSAkinobu Mita }
13029408ec1aSAkinobu Mita 
13039408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
13049408ec1aSAkinobu Mita {
13059408ec1aSAkinobu Mita }
13069408ec1aSAkinobu Mita 
13079408ec1aSAkinobu Mita #endif
13089408ec1aSAkinobu Mita 
13099408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER)
13109408ec1aSAkinobu Mita 
13119408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver;
1312fd28ceb4SHugo Villeneuve static const struct spi_device_id pcf2127_spi_id[];
13139408ec1aSAkinobu Mita 
13149408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi)
13159408ec1aSAkinobu Mita {
1316fd28ceb4SHugo Villeneuve 	static struct regmap_config config = {
13179408ec1aSAkinobu Mita 		.reg_bits = 8,
13189408ec1aSAkinobu Mita 		.val_bits = 8,
13199408ec1aSAkinobu Mita 		.read_flag_mask = 0xa0,
13209408ec1aSAkinobu Mita 		.write_flag_mask = 0x20,
13219408ec1aSAkinobu Mita 	};
13229408ec1aSAkinobu Mita 	struct regmap *regmap;
1323fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *variant;
1324fd28ceb4SHugo Villeneuve 
1325fd28ceb4SHugo Villeneuve 	if (spi->dev.of_node) {
1326fd28ceb4SHugo Villeneuve 		variant = of_device_get_match_data(&spi->dev);
1327fd28ceb4SHugo Villeneuve 		if (!variant)
1328fd28ceb4SHugo Villeneuve 			return -ENODEV;
1329fd28ceb4SHugo Villeneuve 	} else {
1330fd28ceb4SHugo Villeneuve 		enum pcf21xx_type type = spi_get_device_id(spi)->driver_data;
1331fd28ceb4SHugo Villeneuve 
1332fd28ceb4SHugo Villeneuve 		if (type >= PCF21XX_LAST_ID)
1333fd28ceb4SHugo Villeneuve 			return -ENODEV;
1334fd28ceb4SHugo Villeneuve 		variant = &pcf21xx_cfg[type];
1335fd28ceb4SHugo Villeneuve 	}
1336fd28ceb4SHugo Villeneuve 
1337fd28ceb4SHugo Villeneuve 	config.max_register = variant->max_register,
13389408ec1aSAkinobu Mita 
13399408ec1aSAkinobu Mita 	regmap = devm_regmap_init_spi(spi, &config);
13409408ec1aSAkinobu Mita 	if (IS_ERR(regmap)) {
13419408ec1aSAkinobu Mita 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
13429408ec1aSAkinobu Mita 			__func__, PTR_ERR(regmap));
13439408ec1aSAkinobu Mita 		return PTR_ERR(regmap);
13449408ec1aSAkinobu Mita 	}
13459408ec1aSAkinobu Mita 
134627006416SAlexandre Belloni 	return pcf2127_probe(&spi->dev, regmap, spi->irq,
134727006416SAlexandre Belloni 			     pcf2127_spi_driver.driver.name,
1348fd28ceb4SHugo Villeneuve 			     variant);
13499408ec1aSAkinobu Mita }
13509408ec1aSAkinobu Mita 
13519408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = {
1352fd28ceb4SHugo Villeneuve 	{ "pcf2127", PCF2127 },
1353fd28ceb4SHugo Villeneuve 	{ "pcf2129", PCF2129 },
1354fd28ceb4SHugo Villeneuve 	{ "pca2129", PCF2129 },
1355*afc505bfSHugo Villeneuve 	{ "pcf2131", PCF2131 },
13569408ec1aSAkinobu Mita 	{ }
13579408ec1aSAkinobu Mita };
13589408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
13599408ec1aSAkinobu Mita 
13609408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = {
13619408ec1aSAkinobu Mita 	.driver		= {
13629408ec1aSAkinobu Mita 		.name	= "rtc-pcf2127-spi",
13639408ec1aSAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
13649408ec1aSAkinobu Mita 	},
13659408ec1aSAkinobu Mita 	.probe		= pcf2127_spi_probe,
13669408ec1aSAkinobu Mita 	.id_table	= pcf2127_spi_id,
13679408ec1aSAkinobu Mita };
13689408ec1aSAkinobu Mita 
13699408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
13709408ec1aSAkinobu Mita {
13719408ec1aSAkinobu Mita 	return spi_register_driver(&pcf2127_spi_driver);
13729408ec1aSAkinobu Mita }
13739408ec1aSAkinobu Mita 
13749408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
13759408ec1aSAkinobu Mita {
13769408ec1aSAkinobu Mita 	spi_unregister_driver(&pcf2127_spi_driver);
13779408ec1aSAkinobu Mita }
13789408ec1aSAkinobu Mita 
13799408ec1aSAkinobu Mita #else
13809408ec1aSAkinobu Mita 
13819408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
13829408ec1aSAkinobu Mita {
13839408ec1aSAkinobu Mita 	return 0;
13849408ec1aSAkinobu Mita }
13859408ec1aSAkinobu Mita 
13869408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
13879408ec1aSAkinobu Mita {
13889408ec1aSAkinobu Mita }
13899408ec1aSAkinobu Mita 
13909408ec1aSAkinobu Mita #endif
13919408ec1aSAkinobu Mita 
13929408ec1aSAkinobu Mita static int __init pcf2127_init(void)
13939408ec1aSAkinobu Mita {
13949408ec1aSAkinobu Mita 	int ret;
13959408ec1aSAkinobu Mita 
13969408ec1aSAkinobu Mita 	ret = pcf2127_i2c_register_driver();
13979408ec1aSAkinobu Mita 	if (ret) {
13989408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
13999408ec1aSAkinobu Mita 		return ret;
14009408ec1aSAkinobu Mita 	}
14019408ec1aSAkinobu Mita 
14029408ec1aSAkinobu Mita 	ret = pcf2127_spi_register_driver();
14039408ec1aSAkinobu Mita 	if (ret) {
14049408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
14059408ec1aSAkinobu Mita 		pcf2127_i2c_unregister_driver();
14069408ec1aSAkinobu Mita 	}
14079408ec1aSAkinobu Mita 
14089408ec1aSAkinobu Mita 	return ret;
14099408ec1aSAkinobu Mita }
14109408ec1aSAkinobu Mita module_init(pcf2127_init)
14119408ec1aSAkinobu Mita 
14129408ec1aSAkinobu Mita static void __exit pcf2127_exit(void)
14139408ec1aSAkinobu Mita {
14149408ec1aSAkinobu Mita 	pcf2127_spi_unregister_driver();
14159408ec1aSAkinobu Mita 	pcf2127_i2c_unregister_driver();
14169408ec1aSAkinobu Mita }
14179408ec1aSAkinobu Mita module_exit(pcf2127_exit)
141818cb6368SRenaud Cerrato 
141918cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1420*afc505bfSHugo Villeneuve MODULE_DESCRIPTION("NXP PCF2127/29/31 RTC driver");
14214d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
1422