xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision adb9675d74e403537150f025ed2b7a2e1ed0a7b4)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
218cb6368SRenaud Cerrato /*
3afc505bfSHugo Villeneuve  * An I2C and SPI driver for the NXP PCF2127/29/31 RTC
418cb6368SRenaud Cerrato  * Copyright 2013 Til-Technologies
518cb6368SRenaud Cerrato  *
618cb6368SRenaud Cerrato  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
718cb6368SRenaud Cerrato  *
80e735eaaSBruno Thomsen  * Watchdog and tamper functions
90e735eaaSBruno Thomsen  * Author: Bruno Thomsen <bruno.thomsen@gmail.com>
100e735eaaSBruno Thomsen  *
11afc505bfSHugo Villeneuve  * PCF2131 support
12afc505bfSHugo Villeneuve  * Author: Hugo Villeneuve <hvilleneuve@dimonoff.com>
13afc505bfSHugo Villeneuve  *
1418cb6368SRenaud Cerrato  * based on the other drivers in this same directory.
1518cb6368SRenaud Cerrato  *
16afc505bfSHugo Villeneuve  * Datasheets: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf
17afc505bfSHugo Villeneuve  *             https://www.nxp.com/docs/en/data-sheet/PCF2131DS.pdf
1818cb6368SRenaud Cerrato  */
1918cb6368SRenaud Cerrato 
2018cb6368SRenaud Cerrato #include <linux/i2c.h>
219408ec1aSAkinobu Mita #include <linux/spi/spi.h>
2218cb6368SRenaud Cerrato #include <linux/bcd.h>
2318cb6368SRenaud Cerrato #include <linux/rtc.h>
2418cb6368SRenaud Cerrato #include <linux/slab.h>
2518cb6368SRenaud Cerrato #include <linux/module.h>
2618cb6368SRenaud Cerrato #include <linux/of.h>
278a914bacSLiam Beguin #include <linux/of_irq.h>
28fd28ceb4SHugo Villeneuve #include <linux/of_device.h>
29907b3262SAkinobu Mita #include <linux/regmap.h>
300e735eaaSBruno Thomsen #include <linux/watchdog.h>
3118cb6368SRenaud Cerrato 
32bbfe3a7aSBruno Thomsen /* Control register 1 */
33bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1		0x00
34b9ac079aSPhilipp Rosenberger #define PCF2127_BIT_CTRL1_POR_OVRD		BIT(3)
3503623b4bSBruno Thomsen #define PCF2127_BIT_CTRL1_TSF1			BIT(4)
363d715ebaSHugo Villeneuve #define PCF2127_BIT_CTRL1_STOP			BIT(5)
37bbfe3a7aSBruno Thomsen /* Control register 2 */
38bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2		0x01
398a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AIE			BIT(1)
4003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE			BIT(2)
418a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AF			BIT(4)
4203623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2			BIT(5)
4327006416SAlexandre Belloni #define PCF2127_BIT_CTRL2_WDTF			BIT(6)
44bbfe3a7aSBruno Thomsen /* Control register 3 */
45bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3		0x02
4603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE			BIT(0)
4703623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE			BIT(1)
48bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF			BIT(2)
4903623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF			BIT(3)
5003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE			BIT(4)
51bbfe3a7aSBruno Thomsen /* Time and date registers */
526211aceeSHugo Villeneuve #define PCF2127_REG_TIME_BASE		0x03
53bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF			BIT(7)
548a914bacSLiam Beguin /* Alarm registers */
557c6f0db4SHugo Villeneuve #define PCF2127_REG_ALARM_BASE		0x0A
5627006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE			BIT(7)
5715f57b3eSPhilipp Rosenberger /* CLKOUT control register */
5815f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT		0x0f
5915f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR			BIT(5)
600e735eaaSBruno Thomsen /* Watchdog registers */
610e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL		0x10
620e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0			BIT(0)
630e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1			BIT(1)
640e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0			BIT(6)
650e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1			BIT(7)
660e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL		0x11
67420cc9e8SHugo Villeneuve /* Tamper timestamp1 registers */
68420cc9e8SHugo Villeneuve #define PCF2127_REG_TS1_BASE		0x12
6903623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF		BIT(6)
7003623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM			BIT(7)
71bbfe3a7aSBruno Thomsen /*
72bbfe3a7aSBruno Thomsen  * RAM registers
73bbfe3a7aSBruno Thomsen  * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
74bbfe3a7aSBruno Thomsen  * battery backed and can survive a power outage.
75afc505bfSHugo Villeneuve  * PCF2129/31 doesn't have this feature.
76bbfe3a7aSBruno Thomsen  */
77bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB	0x1A
78bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD		0x1C
79bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD		0x1D
80f97cfddcSUwe Kleine-König 
810e735eaaSBruno Thomsen /* Watchdog timer value constants */
820e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP		0
83*adb9675dSHugo Villeneuve /* PCF2127/29 watchdog timer value constants */
84*adb9675dSHugo Villeneuve #define PCF2127_WD_CLOCK_HZ_X1000	1000 /* 1Hz */
85*adb9675dSHugo Villeneuve #define PCF2127_WD_MIN_HW_HEARTBEAT_MS	500
86*adb9675dSHugo Villeneuve /* PCF2131 watchdog timer value constants */
87*adb9675dSHugo Villeneuve #define PCF2131_WD_CLOCK_HZ_X1000	250  /* 1/4Hz */
88*adb9675dSHugo Villeneuve #define PCF2131_WD_MIN_HW_HEARTBEAT_MS	4000
89*adb9675dSHugo Villeneuve 
90*adb9675dSHugo Villeneuve #define PCF2127_WD_DEFAULT_TIMEOUT_S	60
91653ebd75SAndrea Scian 
922f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */
932f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1)
942f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \
952f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_AF | \
962f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_WDTF | \
972f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_TSF2)
982f861984SMian Yousaf Kaukab 
99afc505bfSHugo Villeneuve #define PCF2127_MAX_TS_SUPPORTED	4
100afc505bfSHugo Villeneuve 
101afc505bfSHugo Villeneuve /* Control register 4 */
102afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL4		0x03
103afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF4			BIT(4)
104afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF3			BIT(5)
105afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF2			BIT(6)
106afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL4_TSF1			BIT(7)
107afc505bfSHugo Villeneuve /* Control register 5 */
108afc505bfSHugo Villeneuve #define PCF2131_REG_CTRL5		0x04
109afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE4			BIT(4)
110afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE3			BIT(5)
111afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE2			BIT(6)
112afc505bfSHugo Villeneuve #define PCF2131_BIT_CTRL5_TSIE1			BIT(7)
113afc505bfSHugo Villeneuve /* Software reset register */
114afc505bfSHugo Villeneuve #define PCF2131_REG_SR_RESET		0x05
115afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_READ_PATTERN	(BIT(2) | BIT(5))
116afc505bfSHugo Villeneuve #define PCF2131_SR_RESET_CPR_CMD	(PCF2131_SR_RESET_READ_PATTERN | BIT(7))
117afc505bfSHugo Villeneuve /* Time and date registers */
118afc505bfSHugo Villeneuve #define PCF2131_REG_TIME_BASE		0x07
119afc505bfSHugo Villeneuve /* Alarm registers */
120afc505bfSHugo Villeneuve #define PCF2131_REG_ALARM_BASE		0x0E
121afc505bfSHugo Villeneuve /* CLKOUT control register */
122afc505bfSHugo Villeneuve #define PCF2131_REG_CLKOUT		0x13
123afc505bfSHugo Villeneuve /* Watchdog registers */
124afc505bfSHugo Villeneuve #define PCF2131_REG_WD_CTL		0x35
125afc505bfSHugo Villeneuve #define PCF2131_REG_WD_VAL		0x36
126afc505bfSHugo Villeneuve /* Tamper timestamp1 registers */
127afc505bfSHugo Villeneuve #define PCF2131_REG_TS1_BASE		0x14
128afc505bfSHugo Villeneuve /* Tamper timestamp2 registers */
129afc505bfSHugo Villeneuve #define PCF2131_REG_TS2_BASE		0x1B
130afc505bfSHugo Villeneuve /* Tamper timestamp3 registers */
131afc505bfSHugo Villeneuve #define PCF2131_REG_TS3_BASE		0x22
132afc505bfSHugo Villeneuve /* Tamper timestamp4 registers */
133afc505bfSHugo Villeneuve #define PCF2131_REG_TS4_BASE		0x29
134afc505bfSHugo Villeneuve /* Interrupt mask registers */
135afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK1		0x31
136afc505bfSHugo Villeneuve #define PCF2131_REG_INT_A_MASK2		0x32
137afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK1		0x33
138afc505bfSHugo Villeneuve #define PCF2131_REG_INT_B_MASK2		0x34
139afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BLIE		BIT(0)
140afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_BIE		BIT(1)
141afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_AIE		BIT(2)
142afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_WD_CD		BIT(3)
143afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_SI		BIT(4)
144afc505bfSHugo Villeneuve #define PCF2131_BIT_INT_MI		BIT(5)
145afc505bfSHugo Villeneuve #define PCF2131_CTRL2_IRQ_MASK ( \
146afc505bfSHugo Villeneuve 		PCF2127_BIT_CTRL2_AF | \
147afc505bfSHugo Villeneuve 		PCF2127_BIT_CTRL2_WDTF)
148afc505bfSHugo Villeneuve #define PCF2131_CTRL4_IRQ_MASK ( \
149afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF4 | \
150afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF3 | \
151afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF2 | \
152afc505bfSHugo Villeneuve 		PCF2131_BIT_CTRL4_TSF1)
153420cc9e8SHugo Villeneuve 
154fd28ceb4SHugo Villeneuve enum pcf21xx_type {
155fd28ceb4SHugo Villeneuve 	PCF2127,
156fd28ceb4SHugo Villeneuve 	PCF2129,
157afc505bfSHugo Villeneuve 	PCF2131,
158fd28ceb4SHugo Villeneuve 	PCF21XX_LAST_ID
159fd28ceb4SHugo Villeneuve };
160fd28ceb4SHugo Villeneuve 
161420cc9e8SHugo Villeneuve struct pcf21xx_ts_config {
162420cc9e8SHugo Villeneuve 	u8 reg_base; /* Base register to read timestamp values. */
163420cc9e8SHugo Villeneuve 
164420cc9e8SHugo Villeneuve 	/*
165420cc9e8SHugo Villeneuve 	 * If the TS input pin is driven to GND, an interrupt can be generated
166420cc9e8SHugo Villeneuve 	 * (supported by all variants).
167420cc9e8SHugo Villeneuve 	 */
168420cc9e8SHugo Villeneuve 	u8 gnd_detect_reg; /* Interrupt control register address. */
169420cc9e8SHugo Villeneuve 	u8 gnd_detect_bit; /* Interrupt bit. */
170420cc9e8SHugo Villeneuve 
171420cc9e8SHugo Villeneuve 	/*
172420cc9e8SHugo Villeneuve 	 * If the TS input pin is driven to an intermediate level between GND
173420cc9e8SHugo Villeneuve 	 * and supply, an interrupt can be generated (optional feature depending
174420cc9e8SHugo Villeneuve 	 * on variant).
175420cc9e8SHugo Villeneuve 	 */
176420cc9e8SHugo Villeneuve 	u8 inter_detect_reg; /* Interrupt control register address. */
177420cc9e8SHugo Villeneuve 	u8 inter_detect_bit; /* Interrupt bit. */
178420cc9e8SHugo Villeneuve 
179420cc9e8SHugo Villeneuve 	u8 ie_reg; /* Interrupt enable control register. */
180420cc9e8SHugo Villeneuve 	u8 ie_bit; /* Interrupt enable bit. */
181420cc9e8SHugo Villeneuve };
182420cc9e8SHugo Villeneuve 
183fd28ceb4SHugo Villeneuve struct pcf21xx_config {
184fd28ceb4SHugo Villeneuve 	int type; /* IC variant */
185fd28ceb4SHugo Villeneuve 	int max_register;
186fd28ceb4SHugo Villeneuve 	unsigned int has_nvmem:1;
187fd28ceb4SHugo Villeneuve 	unsigned int has_bit_wd_ctl_cd0:1;
188e1849b8fSHugo Villeneuve 	unsigned int has_int_a_b:1; /* PCF2131 supports two interrupt outputs. */
1896211aceeSHugo Villeneuve 	u8 reg_time_base; /* Time/date base register. */
1907c6f0db4SHugo Villeneuve 	u8 regs_alarm_base; /* Alarm function base registers. */
1916b57ec29SHugo Villeneuve 	u8 reg_wd_ctl; /* Watchdog control register. */
1926b57ec29SHugo Villeneuve 	u8 reg_wd_val; /* Watchdog value register. */
193fc16599eSHugo Villeneuve 	u8 reg_clkout; /* Clkout register. */
194*adb9675dSHugo Villeneuve 	int wdd_clock_hz_x1000; /* Watchdog clock in Hz multiplicated by 1000 */
195*adb9675dSHugo Villeneuve 	int wdd_min_hw_heartbeat_ms;
196420cc9e8SHugo Villeneuve 	unsigned int ts_count;
197420cc9e8SHugo Villeneuve 	struct pcf21xx_ts_config ts[PCF2127_MAX_TS_SUPPORTED];
198420cc9e8SHugo Villeneuve 	struct attribute_group attribute_group;
199fd28ceb4SHugo Villeneuve };
200fd28ceb4SHugo Villeneuve 
20118cb6368SRenaud Cerrato struct pcf2127 {
20218cb6368SRenaud Cerrato 	struct rtc_device *rtc;
2030e735eaaSBruno Thomsen 	struct watchdog_device wdd;
204907b3262SAkinobu Mita 	struct regmap *regmap;
205fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *cfg;
2062f861984SMian Yousaf Kaukab 	bool irq_enabled;
207420cc9e8SHugo Villeneuve 	time64_t ts[PCF2127_MAX_TS_SUPPORTED]; /* Timestamp values. */
208420cc9e8SHugo Villeneuve 	bool ts_valid[PCF2127_MAX_TS_SUPPORTED];  /* Timestamp valid indication. */
20918cb6368SRenaud Cerrato };
21018cb6368SRenaud Cerrato 
21118cb6368SRenaud Cerrato /*
21218cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
21318cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
21418cb6368SRenaud Cerrato  */
215907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
21618cb6368SRenaud Cerrato {
217907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
21831f077c3SHugo Villeneuve 	unsigned char buf[7];
219907b3262SAkinobu Mita 	int ret;
22018cb6368SRenaud Cerrato 
2217f43020eSBruno Thomsen 	/*
2227f43020eSBruno Thomsen 	 * Avoid reading CTRL2 register as it causes WD_VAL register
2237f43020eSBruno Thomsen 	 * value to reset to 0 which means watchdog is stopped.
2247f43020eSBruno Thomsen 	 */
2256211aceeSHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->reg_time_base,
2266211aceeSHugo Villeneuve 			       buf, sizeof(buf));
227907b3262SAkinobu Mita 	if (ret) {
228907b3262SAkinobu Mita 		dev_err(dev, "%s: read error\n", __func__);
229907b3262SAkinobu Mita 		return ret;
23018cb6368SRenaud Cerrato 	}
23118cb6368SRenaud Cerrato 
232bbfe3a7aSBruno Thomsen 	/* Clock integrity is not guaranteed when OSF flag is set. */
23331f077c3SHugo Villeneuve 	if (buf[0] & PCF2127_BIT_SC_OSF) {
234653ebd75SAndrea Scian 		/*
235653ebd75SAndrea Scian 		 * no need clear the flag here,
236653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
237653ebd75SAndrea Scian 		 */
238907b3262SAkinobu Mita 		dev_warn(dev,
239653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
240653ebd75SAndrea Scian 		return -EINVAL;
24118cb6368SRenaud Cerrato 	}
24218cb6368SRenaud Cerrato 
243907b3262SAkinobu Mita 	dev_dbg(dev,
24431f077c3SHugo Villeneuve 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
24518cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
24631f077c3SHugo Villeneuve 		__func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
24718cb6368SRenaud Cerrato 
24831f077c3SHugo Villeneuve 	tm->tm_sec = bcd2bin(buf[0] & 0x7F);
24931f077c3SHugo Villeneuve 	tm->tm_min = bcd2bin(buf[1] & 0x7F);
2500476b6c8SHugo Villeneuve 	tm->tm_hour = bcd2bin(buf[2] & 0x3F);
25131f077c3SHugo Villeneuve 	tm->tm_mday = bcd2bin(buf[3] & 0x3F);
25231f077c3SHugo Villeneuve 	tm->tm_wday = buf[4] & 0x07;
2530476b6c8SHugo Villeneuve 	tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
25431f077c3SHugo Villeneuve 	tm->tm_year = bcd2bin(buf[6]);
255b139bb5cSAlexandre Belloni 	tm->tm_year += 100;
25618cb6368SRenaud Cerrato 
257907b3262SAkinobu Mita 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
25818cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
25918cb6368SRenaud Cerrato 		__func__,
26018cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
26118cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
26218cb6368SRenaud Cerrato 
26322652ba7SAlexandre Belloni 	return 0;
26418cb6368SRenaud Cerrato }
26518cb6368SRenaud Cerrato 
266907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
26718cb6368SRenaud Cerrato {
268907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
269907b3262SAkinobu Mita 	unsigned char buf[7];
27018cb6368SRenaud Cerrato 	int i = 0, err;
27118cb6368SRenaud Cerrato 
272907b3262SAkinobu Mita 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
27318cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
27418cb6368SRenaud Cerrato 		__func__,
27518cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
27618cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
27718cb6368SRenaud Cerrato 
27818cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
279653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
28018cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
28118cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
28218cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
28318cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
28418cb6368SRenaud Cerrato 
28518cb6368SRenaud Cerrato 	/* month, 1 - 12 */
28618cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
28718cb6368SRenaud Cerrato 
28818cb6368SRenaud Cerrato 	/* year */
289b139bb5cSAlexandre Belloni 	buf[i++] = bin2bcd(tm->tm_year - 100);
29018cb6368SRenaud Cerrato 
2913d715ebaSHugo Villeneuve 	/* Write access to time registers:
2923d715ebaSHugo Villeneuve 	 * PCF2127/29: no special action required.
2933d715ebaSHugo Villeneuve 	 * PCF2131:    requires setting the STOP and CPR bits. STOP bit needs to
2943d715ebaSHugo Villeneuve 	 *             be cleared after time registers are updated.
2953d715ebaSHugo Villeneuve 	 */
2963d715ebaSHugo Villeneuve 	if (pcf2127->cfg->type == PCF2131) {
2973d715ebaSHugo Villeneuve 		err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
2983d715ebaSHugo Villeneuve 					 PCF2127_BIT_CTRL1_STOP,
2993d715ebaSHugo Villeneuve 					 PCF2127_BIT_CTRL1_STOP);
3003d715ebaSHugo Villeneuve 		if (err) {
3013d715ebaSHugo Villeneuve 			dev_dbg(dev, "setting STOP bit failed\n");
3023d715ebaSHugo Villeneuve 			return err;
3033d715ebaSHugo Villeneuve 		}
3043d715ebaSHugo Villeneuve 
3053d715ebaSHugo Villeneuve 		err = regmap_write(pcf2127->regmap, PCF2131_REG_SR_RESET,
3063d715ebaSHugo Villeneuve 				   PCF2131_SR_RESET_CPR_CMD);
3073d715ebaSHugo Villeneuve 		if (err) {
3083d715ebaSHugo Villeneuve 			dev_dbg(dev, "sending CPR cmd failed\n");
3093d715ebaSHugo Villeneuve 			return err;
3103d715ebaSHugo Villeneuve 		}
3113d715ebaSHugo Villeneuve 	}
3123d715ebaSHugo Villeneuve 
3133d715ebaSHugo Villeneuve 	/* write time register's data */
3146211aceeSHugo Villeneuve 	err = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->reg_time_base, buf, i);
315907b3262SAkinobu Mita 	if (err) {
3163d740c64SHugo Villeneuve 		dev_dbg(dev, "%s: err=%d", __func__, err);
317907b3262SAkinobu Mita 		return err;
31818cb6368SRenaud Cerrato 	}
31918cb6368SRenaud Cerrato 
3203d715ebaSHugo Villeneuve 	if (pcf2127->cfg->type == PCF2131) {
3213d715ebaSHugo Villeneuve 		/* Clear STOP bit (PCF2131 only) after write is completed. */
3223d715ebaSHugo Villeneuve 		err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
3233d715ebaSHugo Villeneuve 					 PCF2127_BIT_CTRL1_STOP, 0);
3243d715ebaSHugo Villeneuve 		if (err) {
3253d715ebaSHugo Villeneuve 			dev_dbg(dev, "clearing STOP bit failed\n");
3263d715ebaSHugo Villeneuve 			return err;
3273d715ebaSHugo Villeneuve 		}
3283d715ebaSHugo Villeneuve 	}
3293d715ebaSHugo Villeneuve 
33018cb6368SRenaud Cerrato 	return 0;
33118cb6368SRenaud Cerrato }
33218cb6368SRenaud Cerrato 
33318cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
33418cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
33518cb6368SRenaud Cerrato {
336907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
3377d65cf8cSAlexandre Belloni 	int val, touser = 0;
338f97cfddcSUwe Kleine-König 	int ret;
33918cb6368SRenaud Cerrato 
34018cb6368SRenaud Cerrato 	switch (cmd) {
34118cb6368SRenaud Cerrato 	case RTC_VL_READ:
3427d65cf8cSAlexandre Belloni 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
343907b3262SAkinobu Mita 		if (ret)
344f97cfddcSUwe Kleine-König 			return ret;
34518cb6368SRenaud Cerrato 
3467d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BLF)
3477d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_LOW;
3487d65cf8cSAlexandre Belloni 
3497d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BF)
3507d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_SWITCH;
351f97cfddcSUwe Kleine-König 
352af427311SAlexandre Belloni 		return put_user(touser, (unsigned int __user *)arg);
3537d65cf8cSAlexandre Belloni 
3547d65cf8cSAlexandre Belloni 	case RTC_VL_CLR:
3557d65cf8cSAlexandre Belloni 		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
3567d65cf8cSAlexandre Belloni 					  PCF2127_BIT_CTRL3_BF, 0);
3577d65cf8cSAlexandre Belloni 
35818cb6368SRenaud Cerrato 	default:
35918cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
36018cb6368SRenaud Cerrato 	}
36118cb6368SRenaud Cerrato }
36218cb6368SRenaud Cerrato 
363d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset,
364d6c3029fSUwe Kleine-König 			      void *val, size_t bytes)
365d6c3029fSUwe Kleine-König {
366d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
367d6c3029fSUwe Kleine-König 	int ret;
368d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
369d6c3029fSUwe Kleine-König 
370bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
371d6c3029fSUwe Kleine-König 				offsetbuf, 2);
372d6c3029fSUwe Kleine-König 	if (ret)
373d6c3029fSUwe Kleine-König 		return ret;
374d6c3029fSUwe Kleine-König 
375ba1c30bfSDan Carpenter 	return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
376d6c3029fSUwe Kleine-König 				val, bytes);
377d6c3029fSUwe Kleine-König }
378d6c3029fSUwe Kleine-König 
379d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset,
380d6c3029fSUwe Kleine-König 			       void *val, size_t bytes)
381d6c3029fSUwe Kleine-König {
382d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
383d6c3029fSUwe Kleine-König 	int ret;
384d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
385d6c3029fSUwe Kleine-König 
386bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
387d6c3029fSUwe Kleine-König 				offsetbuf, 2);
388d6c3029fSUwe Kleine-König 	if (ret)
389d6c3029fSUwe Kleine-König 		return ret;
390d6c3029fSUwe Kleine-König 
391ba1c30bfSDan Carpenter 	return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
392d6c3029fSUwe Kleine-König 				 val, bytes);
393d6c3029fSUwe Kleine-König }
394d6c3029fSUwe Kleine-König 
3950e735eaaSBruno Thomsen /* watchdog driver */
3960e735eaaSBruno Thomsen 
3970e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd)
3980e735eaaSBruno Thomsen {
399*adb9675dSHugo Villeneuve 	int wd_val;
4000e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
4010e735eaaSBruno Thomsen 
402*adb9675dSHugo Villeneuve 	/*
403*adb9675dSHugo Villeneuve 	 * Compute counter value of WATCHDG_TIM_VAL to obtain desired period
404*adb9675dSHugo Villeneuve 	 * in seconds, depending on the source clock frequency.
405*adb9675dSHugo Villeneuve 	 */
406*adb9675dSHugo Villeneuve 	wd_val = ((wdd->timeout * pcf2127->cfg->wdd_clock_hz_x1000) / 1000) + 1;
407*adb9675dSHugo Villeneuve 
408*adb9675dSHugo Villeneuve 	return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, wd_val);
4090e735eaaSBruno Thomsen }
4100e735eaaSBruno Thomsen 
4110e735eaaSBruno Thomsen /*
4120e735eaaSBruno Thomsen  * Restart watchdog timer if feature is active.
4130e735eaaSBruno Thomsen  *
4140e735eaaSBruno Thomsen  * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
4150e735eaaSBruno Thomsen  * since register also contain control/status flags for other features.
4160e735eaaSBruno Thomsen  * Always call this function after reading CTRL2 register.
4170e735eaaSBruno Thomsen  */
4180e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
4190e735eaaSBruno Thomsen {
4200e735eaaSBruno Thomsen 	int ret = 0;
4210e735eaaSBruno Thomsen 
4220e735eaaSBruno Thomsen 	if (watchdog_active(wdd)) {
4230e735eaaSBruno Thomsen 		ret = pcf2127_wdt_ping(wdd);
4240e735eaaSBruno Thomsen 		if (ret)
4250e735eaaSBruno Thomsen 			dev_err(wdd->parent,
4260e735eaaSBruno Thomsen 				"%s: watchdog restart failed, ret=%d\n",
4270e735eaaSBruno Thomsen 				__func__, ret);
4280e735eaaSBruno Thomsen 	}
4290e735eaaSBruno Thomsen 
4300e735eaaSBruno Thomsen 	return ret;
4310e735eaaSBruno Thomsen }
4320e735eaaSBruno Thomsen 
4330e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd)
4340e735eaaSBruno Thomsen {
4350e735eaaSBruno Thomsen 	return pcf2127_wdt_ping(wdd);
4360e735eaaSBruno Thomsen }
4370e735eaaSBruno Thomsen 
4380e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd)
4390e735eaaSBruno Thomsen {
4400e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
4410e735eaaSBruno Thomsen 
4426b57ec29SHugo Villeneuve 	return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val,
4430e735eaaSBruno Thomsen 			    PCF2127_WD_VAL_STOP);
4440e735eaaSBruno Thomsen }
4450e735eaaSBruno Thomsen 
4460e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
4470e735eaaSBruno Thomsen 				   unsigned int new_timeout)
4480e735eaaSBruno Thomsen {
4490e735eaaSBruno Thomsen 	dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
4500e735eaaSBruno Thomsen 		new_timeout, wdd->timeout);
4510e735eaaSBruno Thomsen 
4520e735eaaSBruno Thomsen 	wdd->timeout = new_timeout;
4530e735eaaSBruno Thomsen 
4540e735eaaSBruno Thomsen 	return pcf2127_wdt_active_ping(wdd);
4550e735eaaSBruno Thomsen }
4560e735eaaSBruno Thomsen 
4570e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = {
4580e735eaaSBruno Thomsen 	.identity = "NXP PCF2127/PCF2129 Watchdog",
4590e735eaaSBruno Thomsen 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
4600e735eaaSBruno Thomsen };
4610e735eaaSBruno Thomsen 
4620e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = {
4630e735eaaSBruno Thomsen 	.owner = THIS_MODULE,
4640e735eaaSBruno Thomsen 	.start = pcf2127_wdt_start,
4650e735eaaSBruno Thomsen 	.stop = pcf2127_wdt_stop,
4660e735eaaSBruno Thomsen 	.ping = pcf2127_wdt_ping,
4670e735eaaSBruno Thomsen 	.set_timeout = pcf2127_wdt_set_timeout,
4680e735eaaSBruno Thomsen };
4690e735eaaSBruno Thomsen 
470*adb9675dSHugo Villeneuve /*
471*adb9675dSHugo Villeneuve  * Compute watchdog period, t, in seconds, from the WATCHDG_TIM_VAL register
472*adb9675dSHugo Villeneuve  * value, n, and the clock frequency, f1000, in Hz x 1000.
473*adb9675dSHugo Villeneuve  *
474*adb9675dSHugo Villeneuve  * The PCF2127/29 datasheet gives t as:
475*adb9675dSHugo Villeneuve  *   t = n / f
476*adb9675dSHugo Villeneuve  * The PCF2131 datasheet gives t as:
477*adb9675dSHugo Villeneuve  *   t = (n - 1) / f
478*adb9675dSHugo Villeneuve  * For both variants, the watchdog is triggered when the WATCHDG_TIM_VAL reaches
479*adb9675dSHugo Villeneuve  * the value 1, and not zero. Consequently, the equation from the PCF2131
480*adb9675dSHugo Villeneuve  * datasheet seems to be the correct one for both variants.
481*adb9675dSHugo Villeneuve  */
482*adb9675dSHugo Villeneuve static int pcf2127_watchdog_get_period(int n, int f1000)
483*adb9675dSHugo Villeneuve {
484*adb9675dSHugo Villeneuve 	return (1000 * (n - 1)) / f1000;
485*adb9675dSHugo Villeneuve }
486*adb9675dSHugo Villeneuve 
4875d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
4885d78533aSUwe Kleine-König {
4895d78533aSUwe Kleine-König 	u32 wdd_timeout;
4905d78533aSUwe Kleine-König 	int ret;
4915d78533aSUwe Kleine-König 
49271ac1345SUwe Kleine-König 	if (!IS_ENABLED(CONFIG_WATCHDOG) ||
49371ac1345SUwe Kleine-König 	    !device_property_read_bool(dev, "reset-source"))
4945d78533aSUwe Kleine-König 		return 0;
4955d78533aSUwe Kleine-König 
4965d78533aSUwe Kleine-König 	pcf2127->wdd.parent = dev;
4975d78533aSUwe Kleine-König 	pcf2127->wdd.info = &pcf2127_wdt_info;
4985d78533aSUwe Kleine-König 	pcf2127->wdd.ops = &pcf2127_watchdog_ops;
499*adb9675dSHugo Villeneuve 
500*adb9675dSHugo Villeneuve 	pcf2127->wdd.min_timeout =
501*adb9675dSHugo Villeneuve 		pcf2127_watchdog_get_period(
502*adb9675dSHugo Villeneuve 			2, pcf2127->cfg->wdd_clock_hz_x1000);
503*adb9675dSHugo Villeneuve 	pcf2127->wdd.max_timeout =
504*adb9675dSHugo Villeneuve 		pcf2127_watchdog_get_period(
505*adb9675dSHugo Villeneuve 			255, pcf2127->cfg->wdd_clock_hz_x1000);
506*adb9675dSHugo Villeneuve 	pcf2127->wdd.timeout = PCF2127_WD_DEFAULT_TIMEOUT_S;
507*adb9675dSHugo Villeneuve 
508*adb9675dSHugo Villeneuve 	dev_dbg(dev, "%s clock = %d Hz / 1000\n", __func__,
509*adb9675dSHugo Villeneuve 		pcf2127->cfg->wdd_clock_hz_x1000);
510*adb9675dSHugo Villeneuve 
511*adb9675dSHugo Villeneuve 	pcf2127->wdd.min_hw_heartbeat_ms = pcf2127->cfg->wdd_min_hw_heartbeat_ms;
5125d78533aSUwe Kleine-König 	pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
5135d78533aSUwe Kleine-König 
5145d78533aSUwe Kleine-König 	watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
5155d78533aSUwe Kleine-König 
5165d78533aSUwe Kleine-König 	/* Test if watchdog timer is started by bootloader */
5176b57ec29SHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_wd_val, &wdd_timeout);
5185d78533aSUwe Kleine-König 	if (ret)
5195d78533aSUwe Kleine-König 		return ret;
5205d78533aSUwe Kleine-König 
5215d78533aSUwe Kleine-König 	if (wdd_timeout)
5225d78533aSUwe Kleine-König 		set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status);
5235d78533aSUwe Kleine-König 
5245d78533aSUwe Kleine-König 	return devm_watchdog_register_device(dev, &pcf2127->wdd);
5255d78533aSUwe Kleine-König }
5265d78533aSUwe Kleine-König 
5278a914bacSLiam Beguin /* Alarm */
5288a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
5298a914bacSLiam Beguin {
5308a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
53173ce0530SHugo Villeneuve 	u8 buf[5];
53273ce0530SHugo Villeneuve 	unsigned int ctrl2;
5338a914bacSLiam Beguin 	int ret;
5348a914bacSLiam Beguin 
5358a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
5368a914bacSLiam Beguin 	if (ret)
5378a914bacSLiam Beguin 		return ret;
5388a914bacSLiam Beguin 
5398a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
5408a914bacSLiam Beguin 	if (ret)
5418a914bacSLiam Beguin 		return ret;
5428a914bacSLiam Beguin 
5437c6f0db4SHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->regs_alarm_base,
5447c6f0db4SHugo Villeneuve 			       buf, sizeof(buf));
5458a914bacSLiam Beguin 	if (ret)
5468a914bacSLiam Beguin 		return ret;
5478a914bacSLiam Beguin 
5488a914bacSLiam Beguin 	alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
5498a914bacSLiam Beguin 	alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
5508a914bacSLiam Beguin 
5518a914bacSLiam Beguin 	alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
5528a914bacSLiam Beguin 	alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
5538a914bacSLiam Beguin 	alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
5548a914bacSLiam Beguin 	alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
5558a914bacSLiam Beguin 
5568a914bacSLiam Beguin 	return 0;
5578a914bacSLiam Beguin }
5588a914bacSLiam Beguin 
5598a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
5608a914bacSLiam Beguin {
5618a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5628a914bacSLiam Beguin 	int ret;
5638a914bacSLiam Beguin 
5648a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
5658a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AIE,
5668a914bacSLiam Beguin 				 enable ? PCF2127_BIT_CTRL2_AIE : 0);
5678a914bacSLiam Beguin 	if (ret)
5688a914bacSLiam Beguin 		return ret;
5698a914bacSLiam Beguin 
5708a914bacSLiam Beguin 	return pcf2127_wdt_active_ping(&pcf2127->wdd);
5718a914bacSLiam Beguin }
5728a914bacSLiam Beguin 
5738a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
5748a914bacSLiam Beguin {
5758a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5768a914bacSLiam Beguin 	uint8_t buf[5];
5778a914bacSLiam Beguin 	int ret;
5788a914bacSLiam Beguin 
5798a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
5808a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AF, 0);
5818a914bacSLiam Beguin 	if (ret)
5828a914bacSLiam Beguin 		return ret;
5838a914bacSLiam Beguin 
5848a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
5858a914bacSLiam Beguin 	if (ret)
5868a914bacSLiam Beguin 		return ret;
5878a914bacSLiam Beguin 
5888a914bacSLiam Beguin 	buf[0] = bin2bcd(alrm->time.tm_sec);
5898a914bacSLiam Beguin 	buf[1] = bin2bcd(alrm->time.tm_min);
5908a914bacSLiam Beguin 	buf[2] = bin2bcd(alrm->time.tm_hour);
5918a914bacSLiam Beguin 	buf[3] = bin2bcd(alrm->time.tm_mday);
59227006416SAlexandre Belloni 	buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
5938a914bacSLiam Beguin 
5947c6f0db4SHugo Villeneuve 	ret = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->regs_alarm_base,
5957c6f0db4SHugo Villeneuve 				buf, sizeof(buf));
5968a914bacSLiam Beguin 	if (ret)
5978a914bacSLiam Beguin 		return ret;
5988a914bacSLiam Beguin 
5998a914bacSLiam Beguin 	return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
6008a914bacSLiam Beguin }
6018a914bacSLiam Beguin 
6022f861984SMian Yousaf Kaukab /*
603420cc9e8SHugo Villeneuve  * This function reads one timestamp function data, caller is responsible for
604420cc9e8SHugo Villeneuve  * calling pcf2127_wdt_active_ping()
6052f861984SMian Yousaf Kaukab  */
606420cc9e8SHugo Villeneuve static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts,
607420cc9e8SHugo Villeneuve 			       int ts_id)
6082f861984SMian Yousaf Kaukab {
6092f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
6102f861984SMian Yousaf Kaukab 	struct rtc_time tm;
6112f861984SMian Yousaf Kaukab 	int ret;
612720fb4b8SHugo Villeneuve 	unsigned char data[7];
6132f861984SMian Yousaf Kaukab 
614420cc9e8SHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->ts[ts_id].reg_base,
615420cc9e8SHugo Villeneuve 			       data, sizeof(data));
6162f861984SMian Yousaf Kaukab 	if (ret) {
6172f861984SMian Yousaf Kaukab 		dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
6182f861984SMian Yousaf Kaukab 		return ret;
6192f861984SMian Yousaf Kaukab 	}
6202f861984SMian Yousaf Kaukab 
6212f861984SMian Yousaf Kaukab 	dev_dbg(dev,
622720fb4b8SHugo Villeneuve 		"%s: raw data is ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n",
623720fb4b8SHugo Villeneuve 		__func__, data[1], data[2], data[3], data[4], data[5], data[6]);
6242f861984SMian Yousaf Kaukab 
625720fb4b8SHugo Villeneuve 	tm.tm_sec = bcd2bin(data[1] & 0x7F);
626720fb4b8SHugo Villeneuve 	tm.tm_min = bcd2bin(data[2] & 0x7F);
627720fb4b8SHugo Villeneuve 	tm.tm_hour = bcd2bin(data[3] & 0x3F);
628720fb4b8SHugo Villeneuve 	tm.tm_mday = bcd2bin(data[4] & 0x3F);
6292f861984SMian Yousaf Kaukab 	/* TS_MO register (month) value range: 1-12 */
630720fb4b8SHugo Villeneuve 	tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1;
631720fb4b8SHugo Villeneuve 	tm.tm_year = bcd2bin(data[6]);
6322f861984SMian Yousaf Kaukab 	if (tm.tm_year < 70)
6332f861984SMian Yousaf Kaukab 		tm.tm_year += 100; /* assume we are in 1970...2069 */
6342f861984SMian Yousaf Kaukab 
6352f861984SMian Yousaf Kaukab 	ret = rtc_valid_tm(&tm);
6362f861984SMian Yousaf Kaukab 	if (ret) {
6372f861984SMian Yousaf Kaukab 		dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
6382f861984SMian Yousaf Kaukab 		return ret;
6392f861984SMian Yousaf Kaukab 	}
6402f861984SMian Yousaf Kaukab 
6412f861984SMian Yousaf Kaukab 	*ts = rtc_tm_to_time64(&tm);
6422f861984SMian Yousaf Kaukab 	return 0;
6432f861984SMian Yousaf Kaukab };
6442f861984SMian Yousaf Kaukab 
645420cc9e8SHugo Villeneuve static void pcf2127_rtc_ts_snapshot(struct device *dev, int ts_id)
6462f861984SMian Yousaf Kaukab {
6472f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
6482f861984SMian Yousaf Kaukab 	int ret;
6492f861984SMian Yousaf Kaukab 
650420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count)
6512f861984SMian Yousaf Kaukab 		return;
6522f861984SMian Yousaf Kaukab 
653420cc9e8SHugo Villeneuve 	/* Let userspace read the first timestamp */
654420cc9e8SHugo Villeneuve 	if (pcf2127->ts_valid[ts_id])
655420cc9e8SHugo Villeneuve 		return;
656420cc9e8SHugo Villeneuve 
657420cc9e8SHugo Villeneuve 	ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts[ts_id], ts_id);
6582f861984SMian Yousaf Kaukab 	if (!ret)
659420cc9e8SHugo Villeneuve 		pcf2127->ts_valid[ts_id] = true;
6602f861984SMian Yousaf Kaukab }
6612f861984SMian Yousaf Kaukab 
6628a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
6638a914bacSLiam Beguin {
6648a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
665afc505bfSHugo Villeneuve 	unsigned int ctrl2;
6668a914bacSLiam Beguin 	int ret = 0;
6678a914bacSLiam Beguin 
668afc505bfSHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
6692f861984SMian Yousaf Kaukab 	if (ret)
6702f861984SMian Yousaf Kaukab 		return IRQ_NONE;
6712f861984SMian Yousaf Kaukab 
672afc505bfSHugo Villeneuve 	if (pcf2127->cfg->ts_count == 1) {
673afc505bfSHugo Villeneuve 		/* PCF2127/29 */
674afc505bfSHugo Villeneuve 		unsigned int ctrl1;
675afc505bfSHugo Villeneuve 
676afc505bfSHugo Villeneuve 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
6778a914bacSLiam Beguin 		if (ret)
6788a914bacSLiam Beguin 			return IRQ_NONE;
6798a914bacSLiam Beguin 
6802f861984SMian Yousaf Kaukab 		if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
68127006416SAlexandre Belloni 			return IRQ_NONE;
68227006416SAlexandre Belloni 
6832f861984SMian Yousaf Kaukab 		if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
684420cc9e8SHugo Villeneuve 			pcf2127_rtc_ts_snapshot(dev, 0);
6858a914bacSLiam Beguin 
6862f861984SMian Yousaf Kaukab 		if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
6872f861984SMian Yousaf Kaukab 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
6882f861984SMian Yousaf Kaukab 				     ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
6892f861984SMian Yousaf Kaukab 
6902f861984SMian Yousaf Kaukab 		if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
6912f861984SMian Yousaf Kaukab 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
6922f861984SMian Yousaf Kaukab 				     ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
693afc505bfSHugo Villeneuve 	} else {
694afc505bfSHugo Villeneuve 		/* PCF2131. */
695afc505bfSHugo Villeneuve 		unsigned int ctrl4;
696afc505bfSHugo Villeneuve 
697afc505bfSHugo Villeneuve 		ret = regmap_read(pcf2127->regmap, PCF2131_REG_CTRL4, &ctrl4);
698afc505bfSHugo Villeneuve 		if (ret)
699afc505bfSHugo Villeneuve 			return IRQ_NONE;
700afc505bfSHugo Villeneuve 
701afc505bfSHugo Villeneuve 		if (!(ctrl4 & PCF2131_CTRL4_IRQ_MASK || ctrl2 & PCF2131_CTRL2_IRQ_MASK))
702afc505bfSHugo Villeneuve 			return IRQ_NONE;
703afc505bfSHugo Villeneuve 
704afc505bfSHugo Villeneuve 		if (ctrl4 & PCF2131_CTRL4_IRQ_MASK) {
705afc505bfSHugo Villeneuve 			int i;
706afc505bfSHugo Villeneuve 			int tsf_bit = PCF2131_BIT_CTRL4_TSF1; /* Start at bit 7. */
707afc505bfSHugo Villeneuve 
708afc505bfSHugo Villeneuve 			for (i = 0; i < pcf2127->cfg->ts_count; i++) {
709afc505bfSHugo Villeneuve 				if (ctrl4 & tsf_bit)
710afc505bfSHugo Villeneuve 					pcf2127_rtc_ts_snapshot(dev, i);
711afc505bfSHugo Villeneuve 
712afc505bfSHugo Villeneuve 				tsf_bit = tsf_bit >> 1;
713afc505bfSHugo Villeneuve 			}
714afc505bfSHugo Villeneuve 
715afc505bfSHugo Villeneuve 			regmap_write(pcf2127->regmap, PCF2131_REG_CTRL4,
716afc505bfSHugo Villeneuve 				     ctrl4 & ~PCF2131_CTRL4_IRQ_MASK);
717afc505bfSHugo Villeneuve 		}
718afc505bfSHugo Villeneuve 
719afc505bfSHugo Villeneuve 		if (ctrl2 & PCF2131_CTRL2_IRQ_MASK)
720afc505bfSHugo Villeneuve 			regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
721afc505bfSHugo Villeneuve 				     ctrl2 & ~PCF2131_CTRL2_IRQ_MASK);
722afc505bfSHugo Villeneuve 	}
7232f861984SMian Yousaf Kaukab 
7242f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_BIT_CTRL2_AF)
7258a914bacSLiam Beguin 		rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
7268a914bacSLiam Beguin 
72727006416SAlexandre Belloni 	pcf2127_wdt_active_ping(&pcf2127->wdd);
7288a914bacSLiam Beguin 
7298a914bacSLiam Beguin 	return IRQ_HANDLED;
7308a914bacSLiam Beguin }
7318a914bacSLiam Beguin 
73225cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = {
7338a914bacSLiam Beguin 	.ioctl            = pcf2127_rtc_ioctl,
7348a914bacSLiam Beguin 	.read_time        = pcf2127_rtc_read_time,
7358a914bacSLiam Beguin 	.set_time         = pcf2127_rtc_set_time,
7368a914bacSLiam Beguin 	.read_alarm       = pcf2127_rtc_read_alarm,
7378a914bacSLiam Beguin 	.set_alarm        = pcf2127_rtc_set_alarm,
7388a914bacSLiam Beguin 	.alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
7398a914bacSLiam Beguin };
7408a914bacSLiam Beguin 
74103623b4bSBruno Thomsen /* sysfs interface */
74203623b4bSBruno Thomsen 
743420cc9e8SHugo Villeneuve static ssize_t timestamp_store(struct device *dev,
74403623b4bSBruno Thomsen 			       struct device_attribute *attr,
745420cc9e8SHugo Villeneuve 			       const char *buf, size_t count, int ts_id)
74603623b4bSBruno Thomsen {
74703623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
74803623b4bSBruno Thomsen 	int ret;
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 		pcf2127->ts_valid[ts_id] = false;
7552f861984SMian Yousaf Kaukab 	} else {
756420cc9e8SHugo Villeneuve 		/* Always clear GND interrupt bit. */
757420cc9e8SHugo Villeneuve 		ret = regmap_update_bits(pcf2127->regmap,
758420cc9e8SHugo Villeneuve 					 pcf2127->cfg->ts[ts_id].gnd_detect_reg,
759420cc9e8SHugo Villeneuve 					 pcf2127->cfg->ts[ts_id].gnd_detect_bit,
760420cc9e8SHugo Villeneuve 					 0);
761420cc9e8SHugo Villeneuve 
76203623b4bSBruno Thomsen 		if (ret) {
763420cc9e8SHugo Villeneuve 			dev_err(dev, "%s: update TS gnd detect ret=%d\n", __func__, ret);
76403623b4bSBruno Thomsen 			return ret;
76503623b4bSBruno Thomsen 		}
76603623b4bSBruno Thomsen 
767420cc9e8SHugo Villeneuve 		if (pcf2127->cfg->ts[ts_id].inter_detect_bit) {
768420cc9e8SHugo Villeneuve 			/* Clear intermediate level interrupt bit if supported. */
769420cc9e8SHugo Villeneuve 			ret = regmap_update_bits(pcf2127->regmap,
770420cc9e8SHugo Villeneuve 						 pcf2127->cfg->ts[ts_id].inter_detect_reg,
771420cc9e8SHugo Villeneuve 						 pcf2127->cfg->ts[ts_id].inter_detect_bit,
772420cc9e8SHugo Villeneuve 						 0);
77303623b4bSBruno Thomsen 			if (ret) {
774420cc9e8SHugo Villeneuve 				dev_err(dev, "%s: update TS intermediate level detect ret=%d\n",
775420cc9e8SHugo Villeneuve 					__func__, ret);
77603623b4bSBruno Thomsen 				return ret;
77703623b4bSBruno Thomsen 			}
778420cc9e8SHugo Villeneuve 		}
77903623b4bSBruno Thomsen 
78003623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
78103623b4bSBruno Thomsen 		if (ret)
78203623b4bSBruno Thomsen 			return ret;
7832f861984SMian Yousaf Kaukab 	}
78403623b4bSBruno Thomsen 
78503623b4bSBruno Thomsen 	return count;
786420cc9e8SHugo Villeneuve }
787420cc9e8SHugo Villeneuve 
788420cc9e8SHugo Villeneuve static ssize_t timestamp0_store(struct device *dev,
789420cc9e8SHugo Villeneuve 				struct device_attribute *attr,
790420cc9e8SHugo Villeneuve 				const char *buf, size_t count)
791420cc9e8SHugo Villeneuve {
792420cc9e8SHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 0);
79303623b4bSBruno Thomsen };
79403623b4bSBruno Thomsen 
795afc505bfSHugo Villeneuve static ssize_t timestamp1_store(struct device *dev,
796afc505bfSHugo Villeneuve 				struct device_attribute *attr,
797afc505bfSHugo Villeneuve 				const char *buf, size_t count)
798afc505bfSHugo Villeneuve {
799afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 1);
800afc505bfSHugo Villeneuve };
801afc505bfSHugo Villeneuve 
802afc505bfSHugo Villeneuve static ssize_t timestamp2_store(struct device *dev,
803afc505bfSHugo Villeneuve 				struct device_attribute *attr,
804afc505bfSHugo Villeneuve 				const char *buf, size_t count)
805afc505bfSHugo Villeneuve {
806afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 2);
807afc505bfSHugo Villeneuve };
808afc505bfSHugo Villeneuve 
809afc505bfSHugo Villeneuve static ssize_t timestamp3_store(struct device *dev,
810afc505bfSHugo Villeneuve 				struct device_attribute *attr,
811afc505bfSHugo Villeneuve 				const char *buf, size_t count)
812afc505bfSHugo Villeneuve {
813afc505bfSHugo Villeneuve 	return timestamp_store(dev, attr, buf, count, 3);
814afc505bfSHugo Villeneuve };
815afc505bfSHugo Villeneuve 
816420cc9e8SHugo Villeneuve static ssize_t timestamp_show(struct device *dev,
817420cc9e8SHugo Villeneuve 			      struct device_attribute *attr, char *buf,
818420cc9e8SHugo Villeneuve 			      int ts_id)
81903623b4bSBruno Thomsen {
82003623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
82103623b4bSBruno Thomsen 	int ret;
8222f861984SMian Yousaf Kaukab 	time64_t ts;
82303623b4bSBruno Thomsen 
824420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count)
825420cc9e8SHugo Villeneuve 		return 0;
826420cc9e8SHugo Villeneuve 
8272f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
828420cc9e8SHugo Villeneuve 		if (!pcf2127->ts_valid[ts_id])
8292f861984SMian Yousaf Kaukab 			return 0;
830420cc9e8SHugo Villeneuve 		ts = pcf2127->ts[ts_id];
8312f861984SMian Yousaf Kaukab 	} else {
832420cc9e8SHugo Villeneuve 		u8 valid_low = 0;
833420cc9e8SHugo Villeneuve 		u8 valid_inter = 0;
834420cc9e8SHugo Villeneuve 		unsigned int ctrl;
835420cc9e8SHugo Villeneuve 
836420cc9e8SHugo Villeneuve 		/* Check if TS input pin is driven to GND, supported by all
837420cc9e8SHugo Villeneuve 		 * variants.
838420cc9e8SHugo Villeneuve 		 */
839420cc9e8SHugo Villeneuve 		ret = regmap_read(pcf2127->regmap,
840420cc9e8SHugo Villeneuve 				  pcf2127->cfg->ts[ts_id].gnd_detect_reg,
841420cc9e8SHugo Villeneuve 				  &ctrl);
8422f861984SMian Yousaf Kaukab 		if (ret)
8432f861984SMian Yousaf Kaukab 			return 0;
84403623b4bSBruno Thomsen 
845420cc9e8SHugo Villeneuve 		valid_low = ctrl & pcf2127->cfg->ts[ts_id].gnd_detect_bit;
846420cc9e8SHugo Villeneuve 
847420cc9e8SHugo Villeneuve 		if (pcf2127->cfg->ts[ts_id].inter_detect_bit) {
848420cc9e8SHugo Villeneuve 			/* Check if TS input pin is driven to intermediate level
849420cc9e8SHugo Villeneuve 			 * between GND and supply, if supported by variant.
850420cc9e8SHugo Villeneuve 			 */
851420cc9e8SHugo Villeneuve 			ret = regmap_read(pcf2127->regmap,
852420cc9e8SHugo Villeneuve 					  pcf2127->cfg->ts[ts_id].inter_detect_reg,
853420cc9e8SHugo Villeneuve 					  &ctrl);
8542f861984SMian Yousaf Kaukab 			if (ret)
8552f861984SMian Yousaf Kaukab 				return 0;
8562f861984SMian Yousaf Kaukab 
857420cc9e8SHugo Villeneuve 			valid_inter = ctrl & pcf2127->cfg->ts[ts_id].inter_detect_bit;
858420cc9e8SHugo Villeneuve 		}
859420cc9e8SHugo Villeneuve 
860420cc9e8SHugo Villeneuve 		if (!valid_low && !valid_inter)
8612f861984SMian Yousaf Kaukab 			return 0;
8622f861984SMian Yousaf Kaukab 
863420cc9e8SHugo Villeneuve 		ret = pcf2127_rtc_ts_read(dev->parent, &ts, ts_id);
8642f861984SMian Yousaf Kaukab 		if (ret)
8652f861984SMian Yousaf Kaukab 			return 0;
86603623b4bSBruno Thomsen 
86703623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
86803623b4bSBruno Thomsen 		if (ret)
86903623b4bSBruno Thomsen 			return ret;
8702f861984SMian Yousaf Kaukab 	}
8712f861984SMian Yousaf Kaukab 	return sprintf(buf, "%llu\n", (unsigned long long)ts);
872420cc9e8SHugo Villeneuve }
873420cc9e8SHugo Villeneuve 
874420cc9e8SHugo Villeneuve static ssize_t timestamp0_show(struct device *dev,
875420cc9e8SHugo Villeneuve 			       struct device_attribute *attr, char *buf)
876420cc9e8SHugo Villeneuve {
877420cc9e8SHugo Villeneuve 	return timestamp_show(dev, attr, buf, 0);
87803623b4bSBruno Thomsen };
87903623b4bSBruno Thomsen 
880afc505bfSHugo Villeneuve static ssize_t timestamp1_show(struct device *dev,
881afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
882afc505bfSHugo Villeneuve {
883afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 1);
884afc505bfSHugo Villeneuve };
885afc505bfSHugo Villeneuve 
886afc505bfSHugo Villeneuve static ssize_t timestamp2_show(struct device *dev,
887afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
888afc505bfSHugo Villeneuve {
889afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 2);
890afc505bfSHugo Villeneuve };
891afc505bfSHugo Villeneuve 
892afc505bfSHugo Villeneuve static ssize_t timestamp3_show(struct device *dev,
893afc505bfSHugo Villeneuve 			       struct device_attribute *attr, char *buf)
894afc505bfSHugo Villeneuve {
895afc505bfSHugo Villeneuve 	return timestamp_show(dev, attr, buf, 3);
896afc505bfSHugo Villeneuve };
897afc505bfSHugo Villeneuve 
89803623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0);
899afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp1);
900afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp2);
901afc505bfSHugo Villeneuve static DEVICE_ATTR_RW(timestamp3);
90203623b4bSBruno Thomsen 
90303623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = {
90403623b4bSBruno Thomsen 	&dev_attr_timestamp0.attr,
90503623b4bSBruno Thomsen 	NULL
90603623b4bSBruno Thomsen };
90703623b4bSBruno Thomsen 
908afc505bfSHugo Villeneuve static struct attribute *pcf2131_attrs[] = {
909afc505bfSHugo Villeneuve 	&dev_attr_timestamp0.attr,
910afc505bfSHugo Villeneuve 	&dev_attr_timestamp1.attr,
911afc505bfSHugo Villeneuve 	&dev_attr_timestamp2.attr,
912afc505bfSHugo Villeneuve 	&dev_attr_timestamp3.attr,
913afc505bfSHugo Villeneuve 	NULL
914afc505bfSHugo Villeneuve };
915afc505bfSHugo Villeneuve 
916fd28ceb4SHugo Villeneuve static struct pcf21xx_config pcf21xx_cfg[] = {
917fd28ceb4SHugo Villeneuve 	[PCF2127] = {
918fd28ceb4SHugo Villeneuve 		.type = PCF2127,
919fd28ceb4SHugo Villeneuve 		.max_register = 0x1d,
920fd28ceb4SHugo Villeneuve 		.has_nvmem = 1,
921fd28ceb4SHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 1,
922e1849b8fSHugo Villeneuve 		.has_int_a_b = 0,
9236211aceeSHugo Villeneuve 		.reg_time_base = PCF2127_REG_TIME_BASE,
9247c6f0db4SHugo Villeneuve 		.regs_alarm_base = PCF2127_REG_ALARM_BASE,
9256b57ec29SHugo Villeneuve 		.reg_wd_ctl = PCF2127_REG_WD_CTL,
9266b57ec29SHugo Villeneuve 		.reg_wd_val = PCF2127_REG_WD_VAL,
927fc16599eSHugo Villeneuve 		.reg_clkout = PCF2127_REG_CLKOUT,
928*adb9675dSHugo Villeneuve 		.wdd_clock_hz_x1000 = PCF2127_WD_CLOCK_HZ_X1000,
929*adb9675dSHugo Villeneuve 		.wdd_min_hw_heartbeat_ms = PCF2127_WD_MIN_HW_HEARTBEAT_MS,
930420cc9e8SHugo Villeneuve 		.ts_count = 1,
931420cc9e8SHugo Villeneuve 		.ts[0] = {
932420cc9e8SHugo Villeneuve 			.reg_base  = PCF2127_REG_TS1_BASE,
933420cc9e8SHugo Villeneuve 			.gnd_detect_reg = PCF2127_REG_CTRL1,
934420cc9e8SHugo Villeneuve 			.gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1,
935420cc9e8SHugo Villeneuve 			.inter_detect_reg = PCF2127_REG_CTRL2,
936420cc9e8SHugo Villeneuve 			.inter_detect_bit = PCF2127_BIT_CTRL2_TSF2,
937420cc9e8SHugo Villeneuve 			.ie_reg    = PCF2127_REG_CTRL2,
938420cc9e8SHugo Villeneuve 			.ie_bit    = PCF2127_BIT_CTRL2_TSIE,
939420cc9e8SHugo Villeneuve 		},
940420cc9e8SHugo Villeneuve 		.attribute_group = {
941420cc9e8SHugo Villeneuve 			.attrs	= pcf2127_attrs,
942420cc9e8SHugo Villeneuve 		},
943fd28ceb4SHugo Villeneuve 	},
944fd28ceb4SHugo Villeneuve 	[PCF2129] = {
945fd28ceb4SHugo Villeneuve 		.type = PCF2129,
946fd28ceb4SHugo Villeneuve 		.max_register = 0x19,
947fd28ceb4SHugo Villeneuve 		.has_nvmem = 0,
948fd28ceb4SHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 0,
949e1849b8fSHugo Villeneuve 		.has_int_a_b = 0,
9506211aceeSHugo Villeneuve 		.reg_time_base = PCF2127_REG_TIME_BASE,
9517c6f0db4SHugo Villeneuve 		.regs_alarm_base = PCF2127_REG_ALARM_BASE,
9526b57ec29SHugo Villeneuve 		.reg_wd_ctl = PCF2127_REG_WD_CTL,
9536b57ec29SHugo Villeneuve 		.reg_wd_val = PCF2127_REG_WD_VAL,
954fc16599eSHugo Villeneuve 		.reg_clkout = PCF2127_REG_CLKOUT,
955*adb9675dSHugo Villeneuve 		.wdd_clock_hz_x1000 = PCF2127_WD_CLOCK_HZ_X1000,
956*adb9675dSHugo Villeneuve 		.wdd_min_hw_heartbeat_ms = PCF2127_WD_MIN_HW_HEARTBEAT_MS,
957420cc9e8SHugo Villeneuve 		.ts_count = 1,
958420cc9e8SHugo Villeneuve 		.ts[0] = {
959420cc9e8SHugo Villeneuve 			.reg_base  = PCF2127_REG_TS1_BASE,
960420cc9e8SHugo Villeneuve 			.gnd_detect_reg = PCF2127_REG_CTRL1,
961420cc9e8SHugo Villeneuve 			.gnd_detect_bit = PCF2127_BIT_CTRL1_TSF1,
962420cc9e8SHugo Villeneuve 			.inter_detect_reg = PCF2127_REG_CTRL2,
963420cc9e8SHugo Villeneuve 			.inter_detect_bit = PCF2127_BIT_CTRL2_TSF2,
964420cc9e8SHugo Villeneuve 			.ie_reg    = PCF2127_REG_CTRL2,
965420cc9e8SHugo Villeneuve 			.ie_bit    = PCF2127_BIT_CTRL2_TSIE,
966420cc9e8SHugo Villeneuve 		},
967420cc9e8SHugo Villeneuve 		.attribute_group = {
968420cc9e8SHugo Villeneuve 			.attrs	= pcf2127_attrs,
969420cc9e8SHugo Villeneuve 		},
970fd28ceb4SHugo Villeneuve 	},
971afc505bfSHugo Villeneuve 	[PCF2131] = {
972afc505bfSHugo Villeneuve 		.type = PCF2131,
973afc505bfSHugo Villeneuve 		.max_register = 0x36,
974afc505bfSHugo Villeneuve 		.has_nvmem = 0,
975afc505bfSHugo Villeneuve 		.has_bit_wd_ctl_cd0 = 0,
976e1849b8fSHugo Villeneuve 		.has_int_a_b = 1,
977afc505bfSHugo Villeneuve 		.reg_time_base = PCF2131_REG_TIME_BASE,
978afc505bfSHugo Villeneuve 		.regs_alarm_base = PCF2131_REG_ALARM_BASE,
979afc505bfSHugo Villeneuve 		.reg_wd_ctl = PCF2131_REG_WD_CTL,
980afc505bfSHugo Villeneuve 		.reg_wd_val = PCF2131_REG_WD_VAL,
981afc505bfSHugo Villeneuve 		.reg_clkout = PCF2131_REG_CLKOUT,
982*adb9675dSHugo Villeneuve 		.wdd_clock_hz_x1000 = PCF2131_WD_CLOCK_HZ_X1000,
983*adb9675dSHugo Villeneuve 		.wdd_min_hw_heartbeat_ms = PCF2131_WD_MIN_HW_HEARTBEAT_MS,
984afc505bfSHugo Villeneuve 		.ts_count = 4,
985afc505bfSHugo Villeneuve 		.ts[0] = {
986afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS1_BASE,
987afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
988afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF1,
989afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
990afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
991afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE1,
992afc505bfSHugo Villeneuve 		},
993afc505bfSHugo Villeneuve 		.ts[1] = {
994afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS2_BASE,
995afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
996afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF2,
997afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
998afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
999afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE2,
1000afc505bfSHugo Villeneuve 		},
1001afc505bfSHugo Villeneuve 		.ts[2] = {
1002afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS3_BASE,
1003afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
1004afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF3,
1005afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
1006afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
1007afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE3,
1008afc505bfSHugo Villeneuve 		},
1009afc505bfSHugo Villeneuve 		.ts[3] = {
1010afc505bfSHugo Villeneuve 			.reg_base  = PCF2131_REG_TS4_BASE,
1011afc505bfSHugo Villeneuve 			.gnd_detect_reg = PCF2131_REG_CTRL4,
1012afc505bfSHugo Villeneuve 			.gnd_detect_bit = PCF2131_BIT_CTRL4_TSF4,
1013afc505bfSHugo Villeneuve 			.inter_detect_bit = 0,
1014afc505bfSHugo Villeneuve 			.ie_reg    = PCF2131_REG_CTRL5,
1015afc505bfSHugo Villeneuve 			.ie_bit    = PCF2131_BIT_CTRL5_TSIE4,
1016afc505bfSHugo Villeneuve 		},
1017afc505bfSHugo Villeneuve 		.attribute_group = {
1018afc505bfSHugo Villeneuve 			.attrs	= pcf2131_attrs,
1019afc505bfSHugo Villeneuve 		},
1020afc505bfSHugo Villeneuve 	},
1021fd28ceb4SHugo Villeneuve };
1022fd28ceb4SHugo Villeneuve 
1023420cc9e8SHugo Villeneuve /*
1024420cc9e8SHugo Villeneuve  * Enable timestamp function and corresponding interrupt(s).
1025420cc9e8SHugo Villeneuve  */
1026420cc9e8SHugo Villeneuve static int pcf2127_enable_ts(struct device *dev, int ts_id)
1027420cc9e8SHugo Villeneuve {
1028420cc9e8SHugo Villeneuve 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
1029420cc9e8SHugo Villeneuve 	int ret;
1030420cc9e8SHugo Villeneuve 
1031420cc9e8SHugo Villeneuve 	if (ts_id >= pcf2127->cfg->ts_count) {
1032420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: invalid tamper detection ID (%d)\n",
1033420cc9e8SHugo Villeneuve 			__func__, ts_id);
1034420cc9e8SHugo Villeneuve 		return -EINVAL;
1035420cc9e8SHugo Villeneuve 	}
1036420cc9e8SHugo Villeneuve 
1037420cc9e8SHugo Villeneuve 	/* Enable timestamp function. */
1038420cc9e8SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap,
1039420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].reg_base,
1040420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSOFF |
1041420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSM,
1042420cc9e8SHugo Villeneuve 				 PCF2127_BIT_TS_CTRL_TSM);
1043420cc9e8SHugo Villeneuve 	if (ret) {
1044420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection config (ts%d_ctrl) failed\n",
1045420cc9e8SHugo Villeneuve 			__func__, ts_id);
1046420cc9e8SHugo Villeneuve 		return ret;
1047420cc9e8SHugo Villeneuve 	}
1048420cc9e8SHugo Villeneuve 
1049420cc9e8SHugo Villeneuve 	/* TS input pin driven to GND detection is supported by all variants.
1050420cc9e8SHugo Villeneuve 	 * Make sure that interrupt bit is defined.
1051420cc9e8SHugo Villeneuve 	 */
1052420cc9e8SHugo Villeneuve 	if (pcf2127->cfg->ts[ts_id].gnd_detect_bit == 0) {
1053420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection to GND configuration invalid\n",
1054420cc9e8SHugo Villeneuve 			__func__);
1055420cc9e8SHugo Villeneuve 		return ret;
1056420cc9e8SHugo Villeneuve 	}
1057420cc9e8SHugo Villeneuve 
1058420cc9e8SHugo Villeneuve 	/*
1059420cc9e8SHugo Villeneuve 	 * Enable interrupt generation when TSF timestamp flag is set.
1060420cc9e8SHugo Villeneuve 	 * Interrupt signals are open-drain outputs and can be left floating if
1061420cc9e8SHugo Villeneuve 	 * unused.
1062420cc9e8SHugo Villeneuve 	 */
1063420cc9e8SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->ts[ts_id].ie_reg,
1064420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].ie_bit,
1065420cc9e8SHugo Villeneuve 				 pcf2127->cfg->ts[ts_id].ie_bit);
1066420cc9e8SHugo Villeneuve 	if (ret) {
1067420cc9e8SHugo Villeneuve 		dev_err(dev, "%s: tamper detection TSIE%d config failed\n",
1068420cc9e8SHugo Villeneuve 			__func__, ts_id);
1069420cc9e8SHugo Villeneuve 		return ret;
1070420cc9e8SHugo Villeneuve 	}
1071420cc9e8SHugo Villeneuve 
1072420cc9e8SHugo Villeneuve 	return ret;
1073420cc9e8SHugo Villeneuve }
1074420cc9e8SHugo Villeneuve 
1075e1849b8fSHugo Villeneuve /* Route all interrupt sources to INT A pin. */
1076e1849b8fSHugo Villeneuve static int pcf2127_configure_interrupt_pins(struct device *dev)
1077e1849b8fSHugo Villeneuve {
1078e1849b8fSHugo Villeneuve 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
1079e1849b8fSHugo Villeneuve 	int ret;
1080e1849b8fSHugo Villeneuve 
1081e1849b8fSHugo Villeneuve 	/* Mask bits need to be cleared to enable corresponding
1082e1849b8fSHugo Villeneuve 	 * interrupt source.
1083e1849b8fSHugo Villeneuve 	 */
1084e1849b8fSHugo Villeneuve 	ret = regmap_write(pcf2127->regmap,
1085e1849b8fSHugo Villeneuve 			   PCF2131_REG_INT_A_MASK1, 0);
1086e1849b8fSHugo Villeneuve 	if (ret)
1087e1849b8fSHugo Villeneuve 		return ret;
1088e1849b8fSHugo Villeneuve 
1089e1849b8fSHugo Villeneuve 	ret = regmap_write(pcf2127->regmap,
1090e1849b8fSHugo Villeneuve 			   PCF2131_REG_INT_A_MASK2, 0);
1091e1849b8fSHugo Villeneuve 	if (ret)
1092e1849b8fSHugo Villeneuve 		return ret;
1093e1849b8fSHugo Villeneuve 
1094e1849b8fSHugo Villeneuve 	return ret;
1095e1849b8fSHugo Villeneuve }
1096e1849b8fSHugo Villeneuve 
1097907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap,
1098fd28ceb4SHugo Villeneuve 			 int alarm_irq, const char *name, const struct pcf21xx_config *config)
109918cb6368SRenaud Cerrato {
110018cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
1101d6c3029fSUwe Kleine-König 	int ret = 0;
110215f57b3eSPhilipp Rosenberger 	unsigned int val;
110318cb6368SRenaud Cerrato 
1104907b3262SAkinobu Mita 	dev_dbg(dev, "%s\n", __func__);
110518cb6368SRenaud Cerrato 
1106907b3262SAkinobu Mita 	pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
110718cb6368SRenaud Cerrato 	if (!pcf2127)
110818cb6368SRenaud Cerrato 		return -ENOMEM;
110918cb6368SRenaud Cerrato 
1110907b3262SAkinobu Mita 	pcf2127->regmap = regmap;
1111fd28ceb4SHugo Villeneuve 	pcf2127->cfg = config;
111218cb6368SRenaud Cerrato 
1113907b3262SAkinobu Mita 	dev_set_drvdata(dev, pcf2127);
1114907b3262SAkinobu Mita 
1115e788771cSBruno Thomsen 	pcf2127->rtc = devm_rtc_allocate_device(dev);
1116d6c3029fSUwe Kleine-König 	if (IS_ERR(pcf2127->rtc))
1117d6c3029fSUwe Kleine-König 		return PTR_ERR(pcf2127->rtc);
111818cb6368SRenaud Cerrato 
1119e788771cSBruno Thomsen 	pcf2127->rtc->ops = &pcf2127_rtc_ops;
1120b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
1121b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
1122b139bb5cSAlexandre Belloni 	pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
1123bda10273SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features);
1124689fafd5SAlexandre Belloni 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features);
112525cbe9c8SAlexandre Belloni 	clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
1126e788771cSBruno Thomsen 
112735425bafSBiwen Li 	if (alarm_irq > 0) {
1128d4785b46SHugo Villeneuve 		unsigned long flags;
1129d4785b46SHugo Villeneuve 
1130d4785b46SHugo Villeneuve 		/*
1131d4785b46SHugo Villeneuve 		 * If flags = 0, devm_request_threaded_irq() will use IRQ flags
1132d4785b46SHugo Villeneuve 		 * obtained from device tree.
1133d4785b46SHugo Villeneuve 		 */
1134d4785b46SHugo Villeneuve 		if (dev_fwnode(dev))
1135d4785b46SHugo Villeneuve 			flags = 0;
1136d4785b46SHugo Villeneuve 		else
1137d4785b46SHugo Villeneuve 			flags = IRQF_TRIGGER_LOW;
1138d4785b46SHugo Villeneuve 
113927006416SAlexandre Belloni 		ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
114027006416SAlexandre Belloni 						pcf2127_rtc_irq,
1141d4785b46SHugo Villeneuve 						flags | IRQF_ONESHOT,
11428a914bacSLiam Beguin 						dev_name(dev), dev);
11438a914bacSLiam Beguin 		if (ret) {
11448a914bacSLiam Beguin 			dev_err(dev, "failed to request alarm irq\n");
11458a914bacSLiam Beguin 			return ret;
11468a914bacSLiam Beguin 		}
11472f861984SMian Yousaf Kaukab 		pcf2127->irq_enabled = true;
11488a914bacSLiam Beguin 	}
11498a914bacSLiam Beguin 
115035425bafSBiwen Li 	if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
11518a914bacSLiam Beguin 		device_init_wakeup(dev, true);
115225cbe9c8SAlexandre Belloni 		set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
11538a914bacSLiam Beguin 	}
11548a914bacSLiam Beguin 
1155e1849b8fSHugo Villeneuve 	if (pcf2127->cfg->has_int_a_b) {
1156e1849b8fSHugo Villeneuve 		/* Configure int A/B pins, independently of alarm_irq. */
1157e1849b8fSHugo Villeneuve 		ret = pcf2127_configure_interrupt_pins(dev);
1158e1849b8fSHugo Villeneuve 		if (ret) {
1159e1849b8fSHugo Villeneuve 			dev_err(dev, "failed to configure interrupt pins\n");
1160e1849b8fSHugo Villeneuve 			return ret;
1161e1849b8fSHugo Villeneuve 		}
1162e1849b8fSHugo Villeneuve 	}
1163e1849b8fSHugo Villeneuve 
1164fd28ceb4SHugo Villeneuve 	if (pcf2127->cfg->has_nvmem) {
1165d6c3029fSUwe Kleine-König 		struct nvmem_config nvmem_cfg = {
1166d6c3029fSUwe Kleine-König 			.priv = pcf2127,
1167d6c3029fSUwe Kleine-König 			.reg_read = pcf2127_nvmem_read,
1168d6c3029fSUwe Kleine-König 			.reg_write = pcf2127_nvmem_write,
1169d6c3029fSUwe Kleine-König 			.size = 512,
1170d6c3029fSUwe Kleine-König 		};
1171d6c3029fSUwe Kleine-König 
11723a905c2dSBartosz Golaszewski 		ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
1173d6c3029fSUwe Kleine-König 	}
1174d6c3029fSUwe Kleine-König 
11750e735eaaSBruno Thomsen 	/*
1176b9ac079aSPhilipp Rosenberger 	 * The "Power-On Reset Override" facility prevents the RTC to do a reset
1177b9ac079aSPhilipp Rosenberger 	 * after power on. For normal operation the PORO must be disabled.
1178b9ac079aSPhilipp Rosenberger 	 */
1179b9ac079aSPhilipp Rosenberger 	regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
1180b9ac079aSPhilipp Rosenberger 				PCF2127_BIT_CTRL1_POR_OVRD);
1181b9ac079aSPhilipp Rosenberger 
1182fc16599eSHugo Villeneuve 	ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_clkout, &val);
118315f57b3eSPhilipp Rosenberger 	if (ret < 0)
118415f57b3eSPhilipp Rosenberger 		return ret;
118515f57b3eSPhilipp Rosenberger 
118615f57b3eSPhilipp Rosenberger 	if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
1187fc16599eSHugo Villeneuve 		ret = regmap_set_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout,
118815f57b3eSPhilipp Rosenberger 				      PCF2127_BIT_CLKOUT_OTPR);
118915f57b3eSPhilipp Rosenberger 		if (ret < 0)
119015f57b3eSPhilipp Rosenberger 			return ret;
119115f57b3eSPhilipp Rosenberger 
119215f57b3eSPhilipp Rosenberger 		msleep(100);
119315f57b3eSPhilipp Rosenberger 	}
119415f57b3eSPhilipp Rosenberger 
1195b9ac079aSPhilipp Rosenberger 	/*
11960e735eaaSBruno Thomsen 	 * Watchdog timer enabled and reset pin /RST activated when timed out.
1197*adb9675dSHugo Villeneuve 	 * Select 1Hz clock source for watchdog timer (1/4Hz for PCF2131).
11980e735eaaSBruno Thomsen 	 * Note: Countdown timer disabled and not available.
1199afc505bfSHugo Villeneuve 	 * For pca2129, pcf2129 and pcf2131, only bit[7] is for Symbol WD_CD
12002843d565SBiwen Li 	 * of register watchdg_tim_ctl. The bit[6] is labeled
12012843d565SBiwen Li 	 * as T. Bits labeled as T must always be written with
12022843d565SBiwen Li 	 * logic 0.
12030e735eaaSBruno Thomsen 	 */
12046b57ec29SHugo Villeneuve 	ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_wd_ctl,
12050e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
12060e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD0 |
12070e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1 |
12080e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF0,
12090e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
1210fd28ceb4SHugo Villeneuve 				 (pcf2127->cfg->has_bit_wd_ctl_cd0 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
12110e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1);
12120e735eaaSBruno Thomsen 	if (ret) {
12130e735eaaSBruno Thomsen 		dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
12140e735eaaSBruno Thomsen 		return ret;
12150e735eaaSBruno Thomsen 	}
12160e735eaaSBruno Thomsen 
12175d78533aSUwe Kleine-König 	pcf2127_watchdog_init(dev, pcf2127);
12180e735eaaSBruno Thomsen 
121903623b4bSBruno Thomsen 	/*
122003623b4bSBruno Thomsen 	 * Disable battery low/switch-over timestamp and interrupts.
122103623b4bSBruno Thomsen 	 * Clear battery interrupt flags which can block new trigger events.
122203623b4bSBruno Thomsen 	 * Note: This is the default chip behaviour but added to ensure
122303623b4bSBruno Thomsen 	 * correct tamper timestamp and interrupt function.
122403623b4bSBruno Thomsen 	 */
122503623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
122603623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BTSE |
122703623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BIE |
122803623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BLIE, 0);
122903623b4bSBruno Thomsen 	if (ret) {
123003623b4bSBruno Thomsen 		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
123103623b4bSBruno Thomsen 			__func__);
123203623b4bSBruno Thomsen 		return ret;
123303623b4bSBruno Thomsen 	}
123403623b4bSBruno Thomsen 
123503623b4bSBruno Thomsen 	/*
1236420cc9e8SHugo Villeneuve 	 * Enable timestamp functions 1 to 4.
123703623b4bSBruno Thomsen 	 */
1238420cc9e8SHugo Villeneuve 	for (int i = 0; i < pcf2127->cfg->ts_count; i++) {
1239420cc9e8SHugo Villeneuve 		ret = pcf2127_enable_ts(dev, i);
1240420cc9e8SHugo Villeneuve 		if (ret)
124103623b4bSBruno Thomsen 			return ret;
124203623b4bSBruno Thomsen 	}
124303623b4bSBruno Thomsen 
1244420cc9e8SHugo Villeneuve 	ret = rtc_add_group(pcf2127->rtc, &pcf2127->cfg->attribute_group);
124503623b4bSBruno Thomsen 	if (ret) {
124603623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper sysfs registering failed\n",
124703623b4bSBruno Thomsen 			__func__);
124803623b4bSBruno Thomsen 		return ret;
124903623b4bSBruno Thomsen 	}
125003623b4bSBruno Thomsen 
1251fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(pcf2127->rtc);
125218cb6368SRenaud Cerrato }
125318cb6368SRenaud Cerrato 
125418cb6368SRenaud Cerrato #ifdef CONFIG_OF
125518cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
1256fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pcf2127", .data = &pcf21xx_cfg[PCF2127] },
1257fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pcf2129", .data = &pcf21xx_cfg[PCF2129] },
1258fd28ceb4SHugo Villeneuve 	{ .compatible = "nxp,pca2129", .data = &pcf21xx_cfg[PCF2129] },
1259afc505bfSHugo Villeneuve 	{ .compatible = "nxp,pcf2131", .data = &pcf21xx_cfg[PCF2131] },
126018cb6368SRenaud Cerrato 	{}
126118cb6368SRenaud Cerrato };
126218cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
126318cb6368SRenaud Cerrato #endif
126418cb6368SRenaud Cerrato 
12659408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C)
12669408ec1aSAkinobu Mita 
1267907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count)
1268907b3262SAkinobu Mita {
1269907b3262SAkinobu Mita 	struct device *dev = context;
1270907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1271907b3262SAkinobu Mita 	int ret;
1272907b3262SAkinobu Mita 
1273907b3262SAkinobu Mita 	ret = i2c_master_send(client, data, count);
1274907b3262SAkinobu Mita 	if (ret != count)
1275907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1276907b3262SAkinobu Mita 
1277907b3262SAkinobu Mita 	return 0;
1278907b3262SAkinobu Mita }
1279907b3262SAkinobu Mita 
1280907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context,
1281907b3262SAkinobu Mita 				const void *reg, size_t reg_size,
1282907b3262SAkinobu Mita 				const void *val, size_t val_size)
1283907b3262SAkinobu Mita {
1284907b3262SAkinobu Mita 	struct device *dev = context;
1285907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1286907b3262SAkinobu Mita 	int ret;
1287907b3262SAkinobu Mita 	void *buf;
1288907b3262SAkinobu Mita 
1289907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
1290907b3262SAkinobu Mita 		return -EINVAL;
1291907b3262SAkinobu Mita 
1292907b3262SAkinobu Mita 	buf = kmalloc(val_size + 1, GFP_KERNEL);
1293907b3262SAkinobu Mita 	if (!buf)
1294907b3262SAkinobu Mita 		return -ENOMEM;
1295907b3262SAkinobu Mita 
1296907b3262SAkinobu Mita 	memcpy(buf, reg, 1);
1297907b3262SAkinobu Mita 	memcpy(buf + 1, val, val_size);
1298907b3262SAkinobu Mita 
1299907b3262SAkinobu Mita 	ret = i2c_master_send(client, buf, val_size + 1);
13009bde0afbSXulin Sun 
13019bde0afbSXulin Sun 	kfree(buf);
13029bde0afbSXulin Sun 
1303907b3262SAkinobu Mita 	if (ret != val_size + 1)
1304907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1305907b3262SAkinobu Mita 
1306907b3262SAkinobu Mita 	return 0;
1307907b3262SAkinobu Mita }
1308907b3262SAkinobu Mita 
1309907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
1310907b3262SAkinobu Mita 				void *val, size_t val_size)
1311907b3262SAkinobu Mita {
1312907b3262SAkinobu Mita 	struct device *dev = context;
1313907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
1314907b3262SAkinobu Mita 	int ret;
1315907b3262SAkinobu Mita 
1316907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
1317907b3262SAkinobu Mita 		return -EINVAL;
1318907b3262SAkinobu Mita 
1319907b3262SAkinobu Mita 	ret = i2c_master_send(client, reg, 1);
1320907b3262SAkinobu Mita 	if (ret != 1)
1321907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1322907b3262SAkinobu Mita 
1323907b3262SAkinobu Mita 	ret = i2c_master_recv(client, val, val_size);
1324907b3262SAkinobu Mita 	if (ret != val_size)
1325907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
1326907b3262SAkinobu Mita 
1327907b3262SAkinobu Mita 	return 0;
1328907b3262SAkinobu Mita }
1329907b3262SAkinobu Mita 
1330907b3262SAkinobu Mita /*
1331907b3262SAkinobu Mita  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
1332907b3262SAkinobu Mita  * is that the STOP condition is required between set register address and
1333907b3262SAkinobu Mita  * read register data when reading from registers.
1334907b3262SAkinobu Mita  */
1335907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = {
1336907b3262SAkinobu Mita 	.write = pcf2127_i2c_write,
1337907b3262SAkinobu Mita 	.gather_write = pcf2127_i2c_gather_write,
1338907b3262SAkinobu Mita 	.read = pcf2127_i2c_read,
133918cb6368SRenaud Cerrato };
134018cb6368SRenaud Cerrato 
1341907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver;
1342907b3262SAkinobu Mita 
13435418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = {
1344fd28ceb4SHugo Villeneuve 	{ "pcf2127", PCF2127 },
1345fd28ceb4SHugo Villeneuve 	{ "pcf2129", PCF2129 },
1346fd28ceb4SHugo Villeneuve 	{ "pca2129", PCF2129 },
1347afc505bfSHugo Villeneuve 	{ "pcf2131", PCF2131 },
13485418e595SUwe Kleine-König 	{ }
13495418e595SUwe Kleine-König };
13505418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
13515418e595SUwe Kleine-König 
13525418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client)
1353907b3262SAkinobu Mita {
1354907b3262SAkinobu Mita 	struct regmap *regmap;
1355fd28ceb4SHugo Villeneuve 	static struct regmap_config config = {
1356907b3262SAkinobu Mita 		.reg_bits = 8,
1357907b3262SAkinobu Mita 		.val_bits = 8,
1358907b3262SAkinobu Mita 	};
1359fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *variant;
1360907b3262SAkinobu Mita 
1361907b3262SAkinobu Mita 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1362907b3262SAkinobu Mita 		return -ENODEV;
1363907b3262SAkinobu Mita 
1364fd28ceb4SHugo Villeneuve 	if (client->dev.of_node) {
1365fd28ceb4SHugo Villeneuve 		variant = of_device_get_match_data(&client->dev);
1366fd28ceb4SHugo Villeneuve 		if (!variant)
1367fd28ceb4SHugo Villeneuve 			return -ENODEV;
1368fd28ceb4SHugo Villeneuve 	} else {
1369fd28ceb4SHugo Villeneuve 		enum pcf21xx_type type =
1370fd28ceb4SHugo Villeneuve 			i2c_match_id(pcf2127_i2c_id, client)->driver_data;
1371fd28ceb4SHugo Villeneuve 
1372fd28ceb4SHugo Villeneuve 		if (type >= PCF21XX_LAST_ID)
1373fd28ceb4SHugo Villeneuve 			return -ENODEV;
1374fd28ceb4SHugo Villeneuve 		variant = &pcf21xx_cfg[type];
1375fd28ceb4SHugo Villeneuve 	}
1376fd28ceb4SHugo Villeneuve 
1377fd28ceb4SHugo Villeneuve 	config.max_register = variant->max_register,
1378fd28ceb4SHugo Villeneuve 
1379907b3262SAkinobu Mita 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
1380907b3262SAkinobu Mita 					&client->dev, &config);
1381907b3262SAkinobu Mita 	if (IS_ERR(regmap)) {
1382907b3262SAkinobu Mita 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
1383907b3262SAkinobu Mita 			__func__, PTR_ERR(regmap));
1384907b3262SAkinobu Mita 		return PTR_ERR(regmap);
1385907b3262SAkinobu Mita 	}
1386907b3262SAkinobu Mita 
138727006416SAlexandre Belloni 	return pcf2127_probe(&client->dev, regmap, client->irq,
1388fd28ceb4SHugo Villeneuve 			     pcf2127_i2c_driver.driver.name, variant);
1389907b3262SAkinobu Mita }
1390907b3262SAkinobu Mita 
1391907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = {
1392907b3262SAkinobu Mita 	.driver		= {
1393907b3262SAkinobu Mita 		.name	= "rtc-pcf2127-i2c",
1394907b3262SAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
1395907b3262SAkinobu Mita 	},
139631b0cecbSUwe Kleine-König 	.probe		= pcf2127_i2c_probe,
1397907b3262SAkinobu Mita 	.id_table	= pcf2127_i2c_id,
1398907b3262SAkinobu Mita };
13999408ec1aSAkinobu Mita 
14009408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
14019408ec1aSAkinobu Mita {
14029408ec1aSAkinobu Mita 	return i2c_add_driver(&pcf2127_i2c_driver);
14039408ec1aSAkinobu Mita }
14049408ec1aSAkinobu Mita 
14059408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
14069408ec1aSAkinobu Mita {
14079408ec1aSAkinobu Mita 	i2c_del_driver(&pcf2127_i2c_driver);
14089408ec1aSAkinobu Mita }
14099408ec1aSAkinobu Mita 
14109408ec1aSAkinobu Mita #else
14119408ec1aSAkinobu Mita 
14129408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
14139408ec1aSAkinobu Mita {
14149408ec1aSAkinobu Mita 	return 0;
14159408ec1aSAkinobu Mita }
14169408ec1aSAkinobu Mita 
14179408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
14189408ec1aSAkinobu Mita {
14199408ec1aSAkinobu Mita }
14209408ec1aSAkinobu Mita 
14219408ec1aSAkinobu Mita #endif
14229408ec1aSAkinobu Mita 
14239408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER)
14249408ec1aSAkinobu Mita 
14259408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver;
1426fd28ceb4SHugo Villeneuve static const struct spi_device_id pcf2127_spi_id[];
14279408ec1aSAkinobu Mita 
14289408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi)
14299408ec1aSAkinobu Mita {
1430fd28ceb4SHugo Villeneuve 	static struct regmap_config config = {
14319408ec1aSAkinobu Mita 		.reg_bits = 8,
14329408ec1aSAkinobu Mita 		.val_bits = 8,
14339408ec1aSAkinobu Mita 		.read_flag_mask = 0xa0,
14349408ec1aSAkinobu Mita 		.write_flag_mask = 0x20,
14359408ec1aSAkinobu Mita 	};
14369408ec1aSAkinobu Mita 	struct regmap *regmap;
1437fd28ceb4SHugo Villeneuve 	const struct pcf21xx_config *variant;
1438fd28ceb4SHugo Villeneuve 
1439fd28ceb4SHugo Villeneuve 	if (spi->dev.of_node) {
1440fd28ceb4SHugo Villeneuve 		variant = of_device_get_match_data(&spi->dev);
1441fd28ceb4SHugo Villeneuve 		if (!variant)
1442fd28ceb4SHugo Villeneuve 			return -ENODEV;
1443fd28ceb4SHugo Villeneuve 	} else {
1444fd28ceb4SHugo Villeneuve 		enum pcf21xx_type type = spi_get_device_id(spi)->driver_data;
1445fd28ceb4SHugo Villeneuve 
1446fd28ceb4SHugo Villeneuve 		if (type >= PCF21XX_LAST_ID)
1447fd28ceb4SHugo Villeneuve 			return -ENODEV;
1448fd28ceb4SHugo Villeneuve 		variant = &pcf21xx_cfg[type];
1449fd28ceb4SHugo Villeneuve 	}
1450fd28ceb4SHugo Villeneuve 
1451fd28ceb4SHugo Villeneuve 	config.max_register = variant->max_register,
14529408ec1aSAkinobu Mita 
14539408ec1aSAkinobu Mita 	regmap = devm_regmap_init_spi(spi, &config);
14549408ec1aSAkinobu Mita 	if (IS_ERR(regmap)) {
14559408ec1aSAkinobu Mita 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
14569408ec1aSAkinobu Mita 			__func__, PTR_ERR(regmap));
14579408ec1aSAkinobu Mita 		return PTR_ERR(regmap);
14589408ec1aSAkinobu Mita 	}
14599408ec1aSAkinobu Mita 
146027006416SAlexandre Belloni 	return pcf2127_probe(&spi->dev, regmap, spi->irq,
146127006416SAlexandre Belloni 			     pcf2127_spi_driver.driver.name,
1462fd28ceb4SHugo Villeneuve 			     variant);
14639408ec1aSAkinobu Mita }
14649408ec1aSAkinobu Mita 
14659408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = {
1466fd28ceb4SHugo Villeneuve 	{ "pcf2127", PCF2127 },
1467fd28ceb4SHugo Villeneuve 	{ "pcf2129", PCF2129 },
1468fd28ceb4SHugo Villeneuve 	{ "pca2129", PCF2129 },
1469afc505bfSHugo Villeneuve 	{ "pcf2131", PCF2131 },
14709408ec1aSAkinobu Mita 	{ }
14719408ec1aSAkinobu Mita };
14729408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
14739408ec1aSAkinobu Mita 
14749408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = {
14759408ec1aSAkinobu Mita 	.driver		= {
14769408ec1aSAkinobu Mita 		.name	= "rtc-pcf2127-spi",
14779408ec1aSAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
14789408ec1aSAkinobu Mita 	},
14799408ec1aSAkinobu Mita 	.probe		= pcf2127_spi_probe,
14809408ec1aSAkinobu Mita 	.id_table	= pcf2127_spi_id,
14819408ec1aSAkinobu Mita };
14829408ec1aSAkinobu Mita 
14839408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
14849408ec1aSAkinobu Mita {
14859408ec1aSAkinobu Mita 	return spi_register_driver(&pcf2127_spi_driver);
14869408ec1aSAkinobu Mita }
14879408ec1aSAkinobu Mita 
14889408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
14899408ec1aSAkinobu Mita {
14909408ec1aSAkinobu Mita 	spi_unregister_driver(&pcf2127_spi_driver);
14919408ec1aSAkinobu Mita }
14929408ec1aSAkinobu Mita 
14939408ec1aSAkinobu Mita #else
14949408ec1aSAkinobu Mita 
14959408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
14969408ec1aSAkinobu Mita {
14979408ec1aSAkinobu Mita 	return 0;
14989408ec1aSAkinobu Mita }
14999408ec1aSAkinobu Mita 
15009408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
15019408ec1aSAkinobu Mita {
15029408ec1aSAkinobu Mita }
15039408ec1aSAkinobu Mita 
15049408ec1aSAkinobu Mita #endif
15059408ec1aSAkinobu Mita 
15069408ec1aSAkinobu Mita static int __init pcf2127_init(void)
15079408ec1aSAkinobu Mita {
15089408ec1aSAkinobu Mita 	int ret;
15099408ec1aSAkinobu Mita 
15109408ec1aSAkinobu Mita 	ret = pcf2127_i2c_register_driver();
15119408ec1aSAkinobu Mita 	if (ret) {
15129408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
15139408ec1aSAkinobu Mita 		return ret;
15149408ec1aSAkinobu Mita 	}
15159408ec1aSAkinobu Mita 
15169408ec1aSAkinobu Mita 	ret = pcf2127_spi_register_driver();
15179408ec1aSAkinobu Mita 	if (ret) {
15189408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
15199408ec1aSAkinobu Mita 		pcf2127_i2c_unregister_driver();
15209408ec1aSAkinobu Mita 	}
15219408ec1aSAkinobu Mita 
15229408ec1aSAkinobu Mita 	return ret;
15239408ec1aSAkinobu Mita }
15249408ec1aSAkinobu Mita module_init(pcf2127_init)
15259408ec1aSAkinobu Mita 
15269408ec1aSAkinobu Mita static void __exit pcf2127_exit(void)
15279408ec1aSAkinobu Mita {
15289408ec1aSAkinobu Mita 	pcf2127_spi_unregister_driver();
15299408ec1aSAkinobu Mita 	pcf2127_i2c_unregister_driver();
15309408ec1aSAkinobu Mita }
15319408ec1aSAkinobu Mita module_exit(pcf2127_exit)
153218cb6368SRenaud Cerrato 
153318cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1534afc505bfSHugo Villeneuve MODULE_DESCRIPTION("NXP PCF2127/29/31 RTC driver");
15354d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
1536