xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision 5418e595f30bf4fde83ebb0121417c0c95cff98e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
218cb6368SRenaud Cerrato /*
3cee2cc21SAkinobu Mita  * An I2C and SPI driver for the NXP PCF2127/29 RTC
418cb6368SRenaud Cerrato  * Copyright 2013 Til-Technologies
518cb6368SRenaud Cerrato  *
618cb6368SRenaud Cerrato  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
718cb6368SRenaud Cerrato  *
80e735eaaSBruno Thomsen  * Watchdog and tamper functions
90e735eaaSBruno Thomsen  * Author: Bruno Thomsen <bruno.thomsen@gmail.com>
100e735eaaSBruno Thomsen  *
1118cb6368SRenaud Cerrato  * based on the other drivers in this same directory.
1218cb6368SRenaud Cerrato  *
13836e9ea3SFabio Estevam  * Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf
1418cb6368SRenaud Cerrato  */
1518cb6368SRenaud Cerrato 
1618cb6368SRenaud Cerrato #include <linux/i2c.h>
179408ec1aSAkinobu Mita #include <linux/spi/spi.h>
1818cb6368SRenaud Cerrato #include <linux/bcd.h>
1918cb6368SRenaud Cerrato #include <linux/rtc.h>
2018cb6368SRenaud Cerrato #include <linux/slab.h>
2118cb6368SRenaud Cerrato #include <linux/module.h>
2218cb6368SRenaud Cerrato #include <linux/of.h>
238a914bacSLiam Beguin #include <linux/of_irq.h>
24907b3262SAkinobu Mita #include <linux/regmap.h>
250e735eaaSBruno Thomsen #include <linux/watchdog.h>
2618cb6368SRenaud Cerrato 
27bbfe3a7aSBruno Thomsen /* Control register 1 */
28bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1		0x00
29b9ac079aSPhilipp Rosenberger #define PCF2127_BIT_CTRL1_POR_OVRD		BIT(3)
3003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL1_TSF1			BIT(4)
31bbfe3a7aSBruno Thomsen /* Control register 2 */
32bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2		0x01
338a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AIE			BIT(1)
3403623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSIE			BIT(2)
358a914bacSLiam Beguin #define PCF2127_BIT_CTRL2_AF			BIT(4)
3603623b4bSBruno Thomsen #define PCF2127_BIT_CTRL2_TSF2			BIT(5)
3727006416SAlexandre Belloni #define PCF2127_BIT_CTRL2_WDTF			BIT(6)
38bbfe3a7aSBruno Thomsen /* Control register 3 */
39bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3		0x02
4003623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BLIE			BIT(0)
4103623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BIE			BIT(1)
42bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF			BIT(2)
4303623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BF			BIT(3)
4403623b4bSBruno Thomsen #define PCF2127_BIT_CTRL3_BTSE			BIT(4)
45bbfe3a7aSBruno Thomsen /* Time and date registers */
46bbfe3a7aSBruno Thomsen #define PCF2127_REG_SC			0x03
47bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF			BIT(7)
48bbfe3a7aSBruno Thomsen #define PCF2127_REG_MN			0x04
49bbfe3a7aSBruno Thomsen #define PCF2127_REG_HR			0x05
50bbfe3a7aSBruno Thomsen #define PCF2127_REG_DM			0x06
51bbfe3a7aSBruno Thomsen #define PCF2127_REG_DW			0x07
52bbfe3a7aSBruno Thomsen #define PCF2127_REG_MO			0x08
53bbfe3a7aSBruno Thomsen #define PCF2127_REG_YR			0x09
548a914bacSLiam Beguin /* Alarm registers */
558a914bacSLiam Beguin #define PCF2127_REG_ALARM_SC		0x0A
568a914bacSLiam Beguin #define PCF2127_REG_ALARM_MN		0x0B
578a914bacSLiam Beguin #define PCF2127_REG_ALARM_HR		0x0C
588a914bacSLiam Beguin #define PCF2127_REG_ALARM_DM		0x0D
598a914bacSLiam Beguin #define PCF2127_REG_ALARM_DW		0x0E
6027006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE			BIT(7)
6115f57b3eSPhilipp Rosenberger /* CLKOUT control register */
6215f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT		0x0f
6315f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR			BIT(5)
640e735eaaSBruno Thomsen /* Watchdog registers */
650e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL		0x10
660e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0			BIT(0)
670e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1			BIT(1)
680e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0			BIT(6)
690e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1			BIT(7)
700e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL		0x11
7103623b4bSBruno Thomsen /* Tamper timestamp registers */
7203623b4bSBruno Thomsen #define PCF2127_REG_TS_CTRL		0x12
7303623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF		BIT(6)
7403623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM			BIT(7)
7503623b4bSBruno Thomsen #define PCF2127_REG_TS_SC		0x13
7603623b4bSBruno Thomsen #define PCF2127_REG_TS_MN		0x14
7703623b4bSBruno Thomsen #define PCF2127_REG_TS_HR		0x15
7803623b4bSBruno Thomsen #define PCF2127_REG_TS_DM		0x16
7903623b4bSBruno Thomsen #define PCF2127_REG_TS_MO		0x17
8003623b4bSBruno Thomsen #define PCF2127_REG_TS_YR		0x18
81bbfe3a7aSBruno Thomsen /*
82bbfe3a7aSBruno Thomsen  * RAM registers
83bbfe3a7aSBruno Thomsen  * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
84bbfe3a7aSBruno Thomsen  * battery backed and can survive a power outage.
85bbfe3a7aSBruno Thomsen  * PCF2129 doesn't have this feature.
86bbfe3a7aSBruno Thomsen  */
87bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB	0x1A
88bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD		0x1C
89bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD		0x1D
90f97cfddcSUwe Kleine-König 
910e735eaaSBruno Thomsen /* Watchdog timer value constants */
920e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP		0
930e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN		2
940e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX		255
950e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT		60
96653ebd75SAndrea Scian 
972f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */
982f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1)
992f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \
1002f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_AF | \
1012f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_WDTF | \
1022f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_TSF2)
1032f861984SMian Yousaf Kaukab 
10418cb6368SRenaud Cerrato struct pcf2127 {
10518cb6368SRenaud Cerrato 	struct rtc_device *rtc;
1060e735eaaSBruno Thomsen 	struct watchdog_device wdd;
107907b3262SAkinobu Mita 	struct regmap *regmap;
1082f861984SMian Yousaf Kaukab 	time64_t ts;
1092f861984SMian Yousaf Kaukab 	bool ts_valid;
1102f861984SMian Yousaf Kaukab 	bool irq_enabled;
11118cb6368SRenaud Cerrato };
11218cb6368SRenaud Cerrato 
11318cb6368SRenaud Cerrato /*
11418cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
11518cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
11618cb6368SRenaud Cerrato  */
117907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
11818cb6368SRenaud Cerrato {
119907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
120907b3262SAkinobu Mita 	unsigned char buf[10];
121907b3262SAkinobu Mita 	int ret;
12218cb6368SRenaud Cerrato 
1237f43020eSBruno Thomsen 	/*
1247f43020eSBruno Thomsen 	 * Avoid reading CTRL2 register as it causes WD_VAL register
1257f43020eSBruno Thomsen 	 * value to reset to 0 which means watchdog is stopped.
1267f43020eSBruno Thomsen 	 */
1277f43020eSBruno Thomsen 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3,
1287f43020eSBruno Thomsen 			       (buf + PCF2127_REG_CTRL3),
1297f43020eSBruno Thomsen 			       ARRAY_SIZE(buf) - PCF2127_REG_CTRL3);
130907b3262SAkinobu Mita 	if (ret) {
131907b3262SAkinobu Mita 		dev_err(dev, "%s: read error\n", __func__);
132907b3262SAkinobu Mita 		return ret;
13318cb6368SRenaud Cerrato 	}
13418cb6368SRenaud Cerrato 
135bbfe3a7aSBruno Thomsen 	if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF)
136907b3262SAkinobu Mita 		dev_info(dev,
137653ebd75SAndrea Scian 			"low voltage detected, check/replace RTC battery.\n");
138653ebd75SAndrea Scian 
139bbfe3a7aSBruno Thomsen 	/* Clock integrity is not guaranteed when OSF flag is set. */
140bbfe3a7aSBruno Thomsen 	if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) {
141653ebd75SAndrea Scian 		/*
142653ebd75SAndrea Scian 		 * no need clear the flag here,
143653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
144653ebd75SAndrea Scian 		 */
145907b3262SAkinobu Mita 		dev_warn(dev,
146653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
147653ebd75SAndrea Scian 		return -EINVAL;
14818cb6368SRenaud Cerrato 	}
14918cb6368SRenaud Cerrato 
150907b3262SAkinobu Mita 	dev_dbg(dev,
1517f43020eSBruno Thomsen 		"%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, "
15218cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
1537f43020eSBruno Thomsen 		__func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC],
1547f43020eSBruno Thomsen 		buf[PCF2127_REG_MN], buf[PCF2127_REG_HR],
1557f43020eSBruno Thomsen 		buf[PCF2127_REG_DM], buf[PCF2127_REG_DW],
1567f43020eSBruno Thomsen 		buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]);
15718cb6368SRenaud Cerrato 
15818cb6368SRenaud Cerrato 	tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
15918cb6368SRenaud Cerrato 	tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
16018cb6368SRenaud Cerrato 	tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
16118cb6368SRenaud Cerrato 	tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
16218cb6368SRenaud Cerrato 	tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
16318cb6368SRenaud Cerrato 	tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
16418cb6368SRenaud Cerrato 	tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
165b139bb5cSAlexandre Belloni 	tm->tm_year += 100;
16618cb6368SRenaud Cerrato 
167907b3262SAkinobu Mita 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
16818cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
16918cb6368SRenaud Cerrato 		__func__,
17018cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
17118cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
17218cb6368SRenaud Cerrato 
17322652ba7SAlexandre Belloni 	return 0;
17418cb6368SRenaud Cerrato }
17518cb6368SRenaud Cerrato 
176907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
17718cb6368SRenaud Cerrato {
178907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
179907b3262SAkinobu Mita 	unsigned char buf[7];
18018cb6368SRenaud Cerrato 	int i = 0, err;
18118cb6368SRenaud Cerrato 
182907b3262SAkinobu Mita 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
18318cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
18418cb6368SRenaud Cerrato 		__func__,
18518cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
18618cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
18718cb6368SRenaud Cerrato 
18818cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
189653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
19018cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
19118cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
19218cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
19318cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
19418cb6368SRenaud Cerrato 
19518cb6368SRenaud Cerrato 	/* month, 1 - 12 */
19618cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
19718cb6368SRenaud Cerrato 
19818cb6368SRenaud Cerrato 	/* year */
199b139bb5cSAlexandre Belloni 	buf[i++] = bin2bcd(tm->tm_year - 100);
20018cb6368SRenaud Cerrato 
20118cb6368SRenaud Cerrato 	/* write register's data */
202907b3262SAkinobu Mita 	err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
203907b3262SAkinobu Mita 	if (err) {
204907b3262SAkinobu Mita 		dev_err(dev,
20518cb6368SRenaud Cerrato 			"%s: err=%d", __func__, err);
206907b3262SAkinobu Mita 		return err;
20718cb6368SRenaud Cerrato 	}
20818cb6368SRenaud Cerrato 
20918cb6368SRenaud Cerrato 	return 0;
21018cb6368SRenaud Cerrato }
21118cb6368SRenaud Cerrato 
21218cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
21318cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
21418cb6368SRenaud Cerrato {
215907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
2167d65cf8cSAlexandre Belloni 	int val, touser = 0;
217f97cfddcSUwe Kleine-König 	int ret;
21818cb6368SRenaud Cerrato 
21918cb6368SRenaud Cerrato 	switch (cmd) {
22018cb6368SRenaud Cerrato 	case RTC_VL_READ:
2217d65cf8cSAlexandre Belloni 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
222907b3262SAkinobu Mita 		if (ret)
223f97cfddcSUwe Kleine-König 			return ret;
22418cb6368SRenaud Cerrato 
2257d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BLF)
2267d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_LOW;
2277d65cf8cSAlexandre Belloni 
2287d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BF)
2297d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_SWITCH;
230f97cfddcSUwe Kleine-König 
231af427311SAlexandre Belloni 		return put_user(touser, (unsigned int __user *)arg);
2327d65cf8cSAlexandre Belloni 
2337d65cf8cSAlexandre Belloni 	case RTC_VL_CLR:
2347d65cf8cSAlexandre Belloni 		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
2357d65cf8cSAlexandre Belloni 					  PCF2127_BIT_CTRL3_BF, 0);
2367d65cf8cSAlexandre Belloni 
23718cb6368SRenaud Cerrato 	default:
23818cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
23918cb6368SRenaud Cerrato 	}
24018cb6368SRenaud Cerrato }
24118cb6368SRenaud Cerrato 
242d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset,
243d6c3029fSUwe Kleine-König 			      void *val, size_t bytes)
244d6c3029fSUwe Kleine-König {
245d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
246d6c3029fSUwe Kleine-König 	int ret;
247d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
248d6c3029fSUwe Kleine-König 
249bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
250d6c3029fSUwe Kleine-König 				offsetbuf, 2);
251d6c3029fSUwe Kleine-König 	if (ret)
252d6c3029fSUwe Kleine-König 		return ret;
253d6c3029fSUwe Kleine-König 
254ba1c30bfSDan Carpenter 	return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
255d6c3029fSUwe Kleine-König 				val, bytes);
256d6c3029fSUwe Kleine-König }
257d6c3029fSUwe Kleine-König 
258d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset,
259d6c3029fSUwe Kleine-König 			       void *val, size_t bytes)
260d6c3029fSUwe Kleine-König {
261d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
262d6c3029fSUwe Kleine-König 	int ret;
263d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
264d6c3029fSUwe Kleine-König 
265bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
266d6c3029fSUwe Kleine-König 				offsetbuf, 2);
267d6c3029fSUwe Kleine-König 	if (ret)
268d6c3029fSUwe Kleine-König 		return ret;
269d6c3029fSUwe Kleine-König 
270ba1c30bfSDan Carpenter 	return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
271d6c3029fSUwe Kleine-König 				 val, bytes);
272d6c3029fSUwe Kleine-König }
273d6c3029fSUwe Kleine-König 
2740e735eaaSBruno Thomsen /* watchdog driver */
2750e735eaaSBruno Thomsen 
2760e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd)
2770e735eaaSBruno Thomsen {
2780e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
2790e735eaaSBruno Thomsen 
2800e735eaaSBruno Thomsen 	return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout);
2810e735eaaSBruno Thomsen }
2820e735eaaSBruno Thomsen 
2830e735eaaSBruno Thomsen /*
2840e735eaaSBruno Thomsen  * Restart watchdog timer if feature is active.
2850e735eaaSBruno Thomsen  *
2860e735eaaSBruno Thomsen  * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
2870e735eaaSBruno Thomsen  * since register also contain control/status flags for other features.
2880e735eaaSBruno Thomsen  * Always call this function after reading CTRL2 register.
2890e735eaaSBruno Thomsen  */
2900e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
2910e735eaaSBruno Thomsen {
2920e735eaaSBruno Thomsen 	int ret = 0;
2930e735eaaSBruno Thomsen 
2940e735eaaSBruno Thomsen 	if (watchdog_active(wdd)) {
2950e735eaaSBruno Thomsen 		ret = pcf2127_wdt_ping(wdd);
2960e735eaaSBruno Thomsen 		if (ret)
2970e735eaaSBruno Thomsen 			dev_err(wdd->parent,
2980e735eaaSBruno Thomsen 				"%s: watchdog restart failed, ret=%d\n",
2990e735eaaSBruno Thomsen 				__func__, ret);
3000e735eaaSBruno Thomsen 	}
3010e735eaaSBruno Thomsen 
3020e735eaaSBruno Thomsen 	return ret;
3030e735eaaSBruno Thomsen }
3040e735eaaSBruno Thomsen 
3050e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd)
3060e735eaaSBruno Thomsen {
3070e735eaaSBruno Thomsen 	return pcf2127_wdt_ping(wdd);
3080e735eaaSBruno Thomsen }
3090e735eaaSBruno Thomsen 
3100e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd)
3110e735eaaSBruno Thomsen {
3120e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
3130e735eaaSBruno Thomsen 
3140e735eaaSBruno Thomsen 	return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL,
3150e735eaaSBruno Thomsen 			    PCF2127_WD_VAL_STOP);
3160e735eaaSBruno Thomsen }
3170e735eaaSBruno Thomsen 
3180e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
3190e735eaaSBruno Thomsen 				   unsigned int new_timeout)
3200e735eaaSBruno Thomsen {
3210e735eaaSBruno Thomsen 	dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
3220e735eaaSBruno Thomsen 		new_timeout, wdd->timeout);
3230e735eaaSBruno Thomsen 
3240e735eaaSBruno Thomsen 	wdd->timeout = new_timeout;
3250e735eaaSBruno Thomsen 
3260e735eaaSBruno Thomsen 	return pcf2127_wdt_active_ping(wdd);
3270e735eaaSBruno Thomsen }
3280e735eaaSBruno Thomsen 
3290e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = {
3300e735eaaSBruno Thomsen 	.identity = "NXP PCF2127/PCF2129 Watchdog",
3310e735eaaSBruno Thomsen 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
3320e735eaaSBruno Thomsen };
3330e735eaaSBruno Thomsen 
3340e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = {
3350e735eaaSBruno Thomsen 	.owner = THIS_MODULE,
3360e735eaaSBruno Thomsen 	.start = pcf2127_wdt_start,
3370e735eaaSBruno Thomsen 	.stop = pcf2127_wdt_stop,
3380e735eaaSBruno Thomsen 	.ping = pcf2127_wdt_ping,
3390e735eaaSBruno Thomsen 	.set_timeout = pcf2127_wdt_set_timeout,
3400e735eaaSBruno Thomsen };
3410e735eaaSBruno Thomsen 
3425d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
3435d78533aSUwe Kleine-König {
3445d78533aSUwe Kleine-König 	u32 wdd_timeout;
3455d78533aSUwe Kleine-König 	int ret;
3465d78533aSUwe Kleine-König 
34771ac1345SUwe Kleine-König 	if (!IS_ENABLED(CONFIG_WATCHDOG) ||
34871ac1345SUwe Kleine-König 	    !device_property_read_bool(dev, "reset-source"))
3495d78533aSUwe Kleine-König 		return 0;
3505d78533aSUwe Kleine-König 
3515d78533aSUwe Kleine-König 	pcf2127->wdd.parent = dev;
3525d78533aSUwe Kleine-König 	pcf2127->wdd.info = &pcf2127_wdt_info;
3535d78533aSUwe Kleine-König 	pcf2127->wdd.ops = &pcf2127_watchdog_ops;
3545d78533aSUwe Kleine-König 	pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN;
3555d78533aSUwe Kleine-König 	pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX;
3565d78533aSUwe Kleine-König 	pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT;
3575d78533aSUwe Kleine-König 	pcf2127->wdd.min_hw_heartbeat_ms = 500;
3585d78533aSUwe Kleine-König 	pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
3595d78533aSUwe Kleine-König 
3605d78533aSUwe Kleine-König 	watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
3615d78533aSUwe Kleine-König 
3625d78533aSUwe Kleine-König 	/* Test if watchdog timer is started by bootloader */
3635d78533aSUwe Kleine-König 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout);
3645d78533aSUwe Kleine-König 	if (ret)
3655d78533aSUwe Kleine-König 		return ret;
3665d78533aSUwe Kleine-König 
3675d78533aSUwe Kleine-König 	if (wdd_timeout)
3685d78533aSUwe Kleine-König 		set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status);
3695d78533aSUwe Kleine-König 
3705d78533aSUwe Kleine-König 	return devm_watchdog_register_device(dev, &pcf2127->wdd);
3715d78533aSUwe Kleine-König }
3725d78533aSUwe Kleine-König 
3738a914bacSLiam Beguin /* Alarm */
3748a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3758a914bacSLiam Beguin {
3768a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
37773ce0530SHugo Villeneuve 	u8 buf[5];
37873ce0530SHugo Villeneuve 	unsigned int ctrl2;
3798a914bacSLiam Beguin 	int ret;
3808a914bacSLiam Beguin 
3818a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
3828a914bacSLiam Beguin 	if (ret)
3838a914bacSLiam Beguin 		return ret;
3848a914bacSLiam Beguin 
3858a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
3868a914bacSLiam Beguin 	if (ret)
3878a914bacSLiam Beguin 		return ret;
3888a914bacSLiam Beguin 
3898a914bacSLiam Beguin 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
3908a914bacSLiam Beguin 			       sizeof(buf));
3918a914bacSLiam Beguin 	if (ret)
3928a914bacSLiam Beguin 		return ret;
3938a914bacSLiam Beguin 
3948a914bacSLiam Beguin 	alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
3958a914bacSLiam Beguin 	alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
3968a914bacSLiam Beguin 
3978a914bacSLiam Beguin 	alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
3988a914bacSLiam Beguin 	alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
3998a914bacSLiam Beguin 	alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
4008a914bacSLiam Beguin 	alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
4018a914bacSLiam Beguin 
4028a914bacSLiam Beguin 	return 0;
4038a914bacSLiam Beguin }
4048a914bacSLiam Beguin 
4058a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
4068a914bacSLiam Beguin {
4078a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4088a914bacSLiam Beguin 	int ret;
4098a914bacSLiam Beguin 
4108a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
4118a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AIE,
4128a914bacSLiam Beguin 				 enable ? PCF2127_BIT_CTRL2_AIE : 0);
4138a914bacSLiam Beguin 	if (ret)
4148a914bacSLiam Beguin 		return ret;
4158a914bacSLiam Beguin 
4168a914bacSLiam Beguin 	return pcf2127_wdt_active_ping(&pcf2127->wdd);
4178a914bacSLiam Beguin }
4188a914bacSLiam Beguin 
4198a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4208a914bacSLiam Beguin {
4218a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4228a914bacSLiam Beguin 	uint8_t buf[5];
4238a914bacSLiam Beguin 	int ret;
4248a914bacSLiam Beguin 
4258a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
4268a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AF, 0);
4278a914bacSLiam Beguin 	if (ret)
4288a914bacSLiam Beguin 		return ret;
4298a914bacSLiam Beguin 
4308a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
4318a914bacSLiam Beguin 	if (ret)
4328a914bacSLiam Beguin 		return ret;
4338a914bacSLiam Beguin 
4348a914bacSLiam Beguin 	buf[0] = bin2bcd(alrm->time.tm_sec);
4358a914bacSLiam Beguin 	buf[1] = bin2bcd(alrm->time.tm_min);
4368a914bacSLiam Beguin 	buf[2] = bin2bcd(alrm->time.tm_hour);
4378a914bacSLiam Beguin 	buf[3] = bin2bcd(alrm->time.tm_mday);
43827006416SAlexandre Belloni 	buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
4398a914bacSLiam Beguin 
4408a914bacSLiam Beguin 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
4418a914bacSLiam Beguin 				sizeof(buf));
4428a914bacSLiam Beguin 	if (ret)
4438a914bacSLiam Beguin 		return ret;
4448a914bacSLiam Beguin 
4458a914bacSLiam Beguin 	return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
4468a914bacSLiam Beguin }
4478a914bacSLiam Beguin 
4482f861984SMian Yousaf Kaukab /*
4492f861984SMian Yousaf Kaukab  * This function reads ctrl2 register, caller is responsible for calling
4502f861984SMian Yousaf Kaukab  * pcf2127_wdt_active_ping()
4512f861984SMian Yousaf Kaukab  */
4522f861984SMian Yousaf Kaukab static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts)
4532f861984SMian Yousaf Kaukab {
4542f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4552f861984SMian Yousaf Kaukab 	struct rtc_time tm;
4562f861984SMian Yousaf Kaukab 	int ret;
4572f861984SMian Yousaf Kaukab 	unsigned char data[25];
4582f861984SMian Yousaf Kaukab 
4592f861984SMian Yousaf Kaukab 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data,
4602f861984SMian Yousaf Kaukab 			       sizeof(data));
4612f861984SMian Yousaf Kaukab 	if (ret) {
4622f861984SMian Yousaf Kaukab 		dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
4632f861984SMian Yousaf Kaukab 		return ret;
4642f861984SMian Yousaf Kaukab 	}
4652f861984SMian Yousaf Kaukab 
4662f861984SMian Yousaf Kaukab 	dev_dbg(dev,
4672f861984SMian Yousaf Kaukab 		"%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, ts_sc=%02x, ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n",
4682f861984SMian Yousaf Kaukab 		__func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2],
4692f861984SMian Yousaf Kaukab 		data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC],
4702f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR],
4712f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO],
4722f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_YR]);
4732f861984SMian Yousaf Kaukab 
4742f861984SMian Yousaf Kaukab 	tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F);
4752f861984SMian Yousaf Kaukab 	tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F);
4762f861984SMian Yousaf Kaukab 	tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F);
4772f861984SMian Yousaf Kaukab 	tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F);
4782f861984SMian Yousaf Kaukab 	/* TS_MO register (month) value range: 1-12 */
4792f861984SMian Yousaf Kaukab 	tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1;
4802f861984SMian Yousaf Kaukab 	tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]);
4812f861984SMian Yousaf Kaukab 	if (tm.tm_year < 70)
4822f861984SMian Yousaf Kaukab 		tm.tm_year += 100; /* assume we are in 1970...2069 */
4832f861984SMian Yousaf Kaukab 
4842f861984SMian Yousaf Kaukab 	ret = rtc_valid_tm(&tm);
4852f861984SMian Yousaf Kaukab 	if (ret) {
4862f861984SMian Yousaf Kaukab 		dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
4872f861984SMian Yousaf Kaukab 		return ret;
4882f861984SMian Yousaf Kaukab 	}
4892f861984SMian Yousaf Kaukab 
4902f861984SMian Yousaf Kaukab 	*ts = rtc_tm_to_time64(&tm);
4912f861984SMian Yousaf Kaukab 	return 0;
4922f861984SMian Yousaf Kaukab };
4932f861984SMian Yousaf Kaukab 
4942f861984SMian Yousaf Kaukab static void pcf2127_rtc_ts_snapshot(struct device *dev)
4952f861984SMian Yousaf Kaukab {
4962f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4972f861984SMian Yousaf Kaukab 	int ret;
4982f861984SMian Yousaf Kaukab 
4992f861984SMian Yousaf Kaukab 	/* Let userspace read the first timestamp */
5002f861984SMian Yousaf Kaukab 	if (pcf2127->ts_valid)
5012f861984SMian Yousaf Kaukab 		return;
5022f861984SMian Yousaf Kaukab 
5032f861984SMian Yousaf Kaukab 	ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts);
5042f861984SMian Yousaf Kaukab 	if (!ret)
5052f861984SMian Yousaf Kaukab 		pcf2127->ts_valid = true;
5062f861984SMian Yousaf Kaukab }
5072f861984SMian Yousaf Kaukab 
5088a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
5098a914bacSLiam Beguin {
5108a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
5112f861984SMian Yousaf Kaukab 	unsigned int ctrl1, ctrl2;
5128a914bacSLiam Beguin 	int ret = 0;
5138a914bacSLiam Beguin 
5142f861984SMian Yousaf Kaukab 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
5152f861984SMian Yousaf Kaukab 	if (ret)
5162f861984SMian Yousaf Kaukab 		return IRQ_NONE;
5172f861984SMian Yousaf Kaukab 
5188a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
5198a914bacSLiam Beguin 	if (ret)
5208a914bacSLiam Beguin 		return IRQ_NONE;
5218a914bacSLiam Beguin 
5222f861984SMian Yousaf Kaukab 	if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
52327006416SAlexandre Belloni 		return IRQ_NONE;
52427006416SAlexandre Belloni 
5252f861984SMian Yousaf Kaukab 	if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
5262f861984SMian Yousaf Kaukab 		pcf2127_rtc_ts_snapshot(dev);
5278a914bacSLiam Beguin 
5282f861984SMian Yousaf Kaukab 	if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
5292f861984SMian Yousaf Kaukab 		regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
5302f861984SMian Yousaf Kaukab 			ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
5312f861984SMian Yousaf Kaukab 
5322f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
5332f861984SMian Yousaf Kaukab 		regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
5342f861984SMian Yousaf Kaukab 			ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
5352f861984SMian Yousaf Kaukab 
5362f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_BIT_CTRL2_AF)
5378a914bacSLiam Beguin 		rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
5388a914bacSLiam Beguin 
53927006416SAlexandre Belloni 	pcf2127_wdt_active_ping(&pcf2127->wdd);
5408a914bacSLiam Beguin 
5418a914bacSLiam Beguin 	return IRQ_HANDLED;
5428a914bacSLiam Beguin }
5438a914bacSLiam Beguin 
54425cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = {
5458a914bacSLiam Beguin 	.ioctl            = pcf2127_rtc_ioctl,
5468a914bacSLiam Beguin 	.read_time        = pcf2127_rtc_read_time,
5478a914bacSLiam Beguin 	.set_time         = pcf2127_rtc_set_time,
5488a914bacSLiam Beguin 	.read_alarm       = pcf2127_rtc_read_alarm,
5498a914bacSLiam Beguin 	.set_alarm        = pcf2127_rtc_set_alarm,
5508a914bacSLiam Beguin 	.alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
5518a914bacSLiam Beguin };
5528a914bacSLiam Beguin 
55303623b4bSBruno Thomsen /* sysfs interface */
55403623b4bSBruno Thomsen 
55503623b4bSBruno Thomsen static ssize_t timestamp0_store(struct device *dev,
55603623b4bSBruno Thomsen 				struct device_attribute *attr,
55703623b4bSBruno Thomsen 				const char *buf, size_t count)
55803623b4bSBruno Thomsen {
55903623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
56003623b4bSBruno Thomsen 	int ret;
56103623b4bSBruno Thomsen 
5622f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
5632f861984SMian Yousaf Kaukab 		pcf2127->ts_valid = false;
5642f861984SMian Yousaf Kaukab 	} else {
56503623b4bSBruno Thomsen 		ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
56603623b4bSBruno Thomsen 			PCF2127_BIT_CTRL1_TSF1, 0);
56703623b4bSBruno Thomsen 		if (ret) {
56803623b4bSBruno Thomsen 			dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret);
56903623b4bSBruno Thomsen 			return ret;
57003623b4bSBruno Thomsen 		}
57103623b4bSBruno Thomsen 
57203623b4bSBruno Thomsen 		ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
57303623b4bSBruno Thomsen 			PCF2127_BIT_CTRL2_TSF2, 0);
57403623b4bSBruno Thomsen 		if (ret) {
57503623b4bSBruno Thomsen 			dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret);
57603623b4bSBruno Thomsen 			return ret;
57703623b4bSBruno Thomsen 		}
57803623b4bSBruno Thomsen 
57903623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
58003623b4bSBruno Thomsen 		if (ret)
58103623b4bSBruno Thomsen 			return ret;
5822f861984SMian Yousaf Kaukab 	}
58303623b4bSBruno Thomsen 
58403623b4bSBruno Thomsen 	return count;
58503623b4bSBruno Thomsen };
58603623b4bSBruno Thomsen 
58703623b4bSBruno Thomsen static ssize_t timestamp0_show(struct device *dev,
58803623b4bSBruno Thomsen 			       struct device_attribute *attr, char *buf)
58903623b4bSBruno Thomsen {
59003623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
5912f861984SMian Yousaf Kaukab 	unsigned int ctrl1, ctrl2;
59203623b4bSBruno Thomsen 	int ret;
5932f861984SMian Yousaf Kaukab 	time64_t ts;
59403623b4bSBruno Thomsen 
5952f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
5962f861984SMian Yousaf Kaukab 		if (!pcf2127->ts_valid)
5972f861984SMian Yousaf Kaukab 			return 0;
5982f861984SMian Yousaf Kaukab 		ts = pcf2127->ts;
5992f861984SMian Yousaf Kaukab 	} else {
6002f861984SMian Yousaf Kaukab 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
6012f861984SMian Yousaf Kaukab 		if (ret)
6022f861984SMian Yousaf Kaukab 			return 0;
60303623b4bSBruno Thomsen 
6042f861984SMian Yousaf Kaukab 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
6052f861984SMian Yousaf Kaukab 		if (ret)
6062f861984SMian Yousaf Kaukab 			return 0;
6072f861984SMian Yousaf Kaukab 
6082f861984SMian Yousaf Kaukab 		if (!(ctrl1 & PCF2127_BIT_CTRL1_TSF1) &&
6092f861984SMian Yousaf Kaukab 		    !(ctrl2 & PCF2127_BIT_CTRL2_TSF2))
6102f861984SMian Yousaf Kaukab 			return 0;
6112f861984SMian Yousaf Kaukab 
6122f861984SMian Yousaf Kaukab 		ret = pcf2127_rtc_ts_read(dev->parent, &ts);
6132f861984SMian Yousaf Kaukab 		if (ret)
6142f861984SMian Yousaf Kaukab 			return 0;
61503623b4bSBruno Thomsen 
61603623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
61703623b4bSBruno Thomsen 		if (ret)
61803623b4bSBruno Thomsen 			return ret;
6192f861984SMian Yousaf Kaukab 	}
6202f861984SMian Yousaf Kaukab 	return sprintf(buf, "%llu\n", (unsigned long long)ts);
62103623b4bSBruno Thomsen };
62203623b4bSBruno Thomsen 
62303623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0);
62403623b4bSBruno Thomsen 
62503623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = {
62603623b4bSBruno Thomsen 	&dev_attr_timestamp0.attr,
62703623b4bSBruno Thomsen 	NULL
62803623b4bSBruno Thomsen };
62903623b4bSBruno Thomsen 
63003623b4bSBruno Thomsen static const struct attribute_group pcf2127_attr_group = {
63103623b4bSBruno Thomsen 	.attrs	= pcf2127_attrs,
63203623b4bSBruno Thomsen };
63303623b4bSBruno Thomsen 
634907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap,
6352843d565SBiwen Li 			 int alarm_irq, const char *name, bool is_pcf2127)
63618cb6368SRenaud Cerrato {
63718cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
638d6c3029fSUwe Kleine-König 	int ret = 0;
63915f57b3eSPhilipp Rosenberger 	unsigned int val;
64018cb6368SRenaud Cerrato 
641907b3262SAkinobu Mita 	dev_dbg(dev, "%s\n", __func__);
64218cb6368SRenaud Cerrato 
643907b3262SAkinobu Mita 	pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
64418cb6368SRenaud Cerrato 	if (!pcf2127)
64518cb6368SRenaud Cerrato 		return -ENOMEM;
64618cb6368SRenaud Cerrato 
647907b3262SAkinobu Mita 	pcf2127->regmap = regmap;
64818cb6368SRenaud Cerrato 
649907b3262SAkinobu Mita 	dev_set_drvdata(dev, pcf2127);
650907b3262SAkinobu Mita 
651e788771cSBruno Thomsen 	pcf2127->rtc = devm_rtc_allocate_device(dev);
652d6c3029fSUwe Kleine-König 	if (IS_ERR(pcf2127->rtc))
653d6c3029fSUwe Kleine-König 		return PTR_ERR(pcf2127->rtc);
65418cb6368SRenaud Cerrato 
655e788771cSBruno Thomsen 	pcf2127->rtc->ops = &pcf2127_rtc_ops;
656b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
657b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
658b139bb5cSAlexandre Belloni 	pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
659bda10273SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features);
660689fafd5SAlexandre Belloni 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features);
66125cbe9c8SAlexandre Belloni 	clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
662e788771cSBruno Thomsen 
66335425bafSBiwen Li 	if (alarm_irq > 0) {
664d4785b46SHugo Villeneuve 		unsigned long flags;
665d4785b46SHugo Villeneuve 
666d4785b46SHugo Villeneuve 		/*
667d4785b46SHugo Villeneuve 		 * If flags = 0, devm_request_threaded_irq() will use IRQ flags
668d4785b46SHugo Villeneuve 		 * obtained from device tree.
669d4785b46SHugo Villeneuve 		 */
670d4785b46SHugo Villeneuve 		if (dev_fwnode(dev))
671d4785b46SHugo Villeneuve 			flags = 0;
672d4785b46SHugo Villeneuve 		else
673d4785b46SHugo Villeneuve 			flags = IRQF_TRIGGER_LOW;
674d4785b46SHugo Villeneuve 
67527006416SAlexandre Belloni 		ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
67627006416SAlexandre Belloni 						pcf2127_rtc_irq,
677d4785b46SHugo Villeneuve 						flags | IRQF_ONESHOT,
6788a914bacSLiam Beguin 						dev_name(dev), dev);
6798a914bacSLiam Beguin 		if (ret) {
6808a914bacSLiam Beguin 			dev_err(dev, "failed to request alarm irq\n");
6818a914bacSLiam Beguin 			return ret;
6828a914bacSLiam Beguin 		}
6832f861984SMian Yousaf Kaukab 		pcf2127->irq_enabled = true;
6848a914bacSLiam Beguin 	}
6858a914bacSLiam Beguin 
68635425bafSBiwen Li 	if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
6878a914bacSLiam Beguin 		device_init_wakeup(dev, true);
68825cbe9c8SAlexandre Belloni 		set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
6898a914bacSLiam Beguin 	}
6908a914bacSLiam Beguin 
6912843d565SBiwen Li 	if (is_pcf2127) {
692d6c3029fSUwe Kleine-König 		struct nvmem_config nvmem_cfg = {
693d6c3029fSUwe Kleine-König 			.priv = pcf2127,
694d6c3029fSUwe Kleine-König 			.reg_read = pcf2127_nvmem_read,
695d6c3029fSUwe Kleine-König 			.reg_write = pcf2127_nvmem_write,
696d6c3029fSUwe Kleine-König 			.size = 512,
697d6c3029fSUwe Kleine-König 		};
698d6c3029fSUwe Kleine-König 
6993a905c2dSBartosz Golaszewski 		ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
700d6c3029fSUwe Kleine-König 	}
701d6c3029fSUwe Kleine-König 
7020e735eaaSBruno Thomsen 	/*
703b9ac079aSPhilipp Rosenberger 	 * The "Power-On Reset Override" facility prevents the RTC to do a reset
704b9ac079aSPhilipp Rosenberger 	 * after power on. For normal operation the PORO must be disabled.
705b9ac079aSPhilipp Rosenberger 	 */
706b9ac079aSPhilipp Rosenberger 	regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
707b9ac079aSPhilipp Rosenberger 				PCF2127_BIT_CTRL1_POR_OVRD);
708b9ac079aSPhilipp Rosenberger 
70915f57b3eSPhilipp Rosenberger 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val);
71015f57b3eSPhilipp Rosenberger 	if (ret < 0)
71115f57b3eSPhilipp Rosenberger 		return ret;
71215f57b3eSPhilipp Rosenberger 
71315f57b3eSPhilipp Rosenberger 	if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
71415f57b3eSPhilipp Rosenberger 		ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT,
71515f57b3eSPhilipp Rosenberger 				      PCF2127_BIT_CLKOUT_OTPR);
71615f57b3eSPhilipp Rosenberger 		if (ret < 0)
71715f57b3eSPhilipp Rosenberger 			return ret;
71815f57b3eSPhilipp Rosenberger 
71915f57b3eSPhilipp Rosenberger 		msleep(100);
72015f57b3eSPhilipp Rosenberger 	}
72115f57b3eSPhilipp Rosenberger 
722b9ac079aSPhilipp Rosenberger 	/*
7230e735eaaSBruno Thomsen 	 * Watchdog timer enabled and reset pin /RST activated when timed out.
7240e735eaaSBruno Thomsen 	 * Select 1Hz clock source for watchdog timer.
7250e735eaaSBruno Thomsen 	 * Note: Countdown timer disabled and not available.
7262843d565SBiwen Li 	 * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD
7272843d565SBiwen Li 	 * of register watchdg_tim_ctl. The bit[6] is labeled
7282843d565SBiwen Li 	 * as T. Bits labeled as T must always be written with
7292843d565SBiwen Li 	 * logic 0.
7300e735eaaSBruno Thomsen 	 */
7310e735eaaSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL,
7320e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
7330e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD0 |
7340e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1 |
7350e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF0,
7360e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
7372843d565SBiwen Li 				 (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
7380e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1);
7390e735eaaSBruno Thomsen 	if (ret) {
7400e735eaaSBruno Thomsen 		dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
7410e735eaaSBruno Thomsen 		return ret;
7420e735eaaSBruno Thomsen 	}
7430e735eaaSBruno Thomsen 
7445d78533aSUwe Kleine-König 	pcf2127_watchdog_init(dev, pcf2127);
7450e735eaaSBruno Thomsen 
74603623b4bSBruno Thomsen 	/*
74703623b4bSBruno Thomsen 	 * Disable battery low/switch-over timestamp and interrupts.
74803623b4bSBruno Thomsen 	 * Clear battery interrupt flags which can block new trigger events.
74903623b4bSBruno Thomsen 	 * Note: This is the default chip behaviour but added to ensure
75003623b4bSBruno Thomsen 	 * correct tamper timestamp and interrupt function.
75103623b4bSBruno Thomsen 	 */
75203623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
75303623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BTSE |
75403623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BIE |
75503623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BLIE, 0);
75603623b4bSBruno Thomsen 	if (ret) {
75703623b4bSBruno Thomsen 		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
75803623b4bSBruno Thomsen 			__func__);
75903623b4bSBruno Thomsen 		return ret;
76003623b4bSBruno Thomsen 	}
76103623b4bSBruno Thomsen 
76203623b4bSBruno Thomsen 	/*
76303623b4bSBruno Thomsen 	 * Enable timestamp function and store timestamp of first trigger
7647b69b54aSHugo Villeneuve 	 * event until TSF1 and TSF2 interrupt flags are cleared.
76503623b4bSBruno Thomsen 	 */
76603623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL,
76703623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSOFF |
76803623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSM,
76903623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSM);
77003623b4bSBruno Thomsen 	if (ret) {
77103623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n",
77203623b4bSBruno Thomsen 			__func__);
77303623b4bSBruno Thomsen 		return ret;
77403623b4bSBruno Thomsen 	}
77503623b4bSBruno Thomsen 
77603623b4bSBruno Thomsen 	/*
77703623b4bSBruno Thomsen 	 * Enable interrupt generation when TSF1 or TSF2 timestamp flags
77803623b4bSBruno Thomsen 	 * are set. Interrupt signal is an open-drain output and can be
77903623b4bSBruno Thomsen 	 * left floating if unused.
78003623b4bSBruno Thomsen 	 */
78103623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
78203623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL2_TSIE,
78303623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL2_TSIE);
78403623b4bSBruno Thomsen 	if (ret) {
78503623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper detection config (ctrl2) failed\n",
78603623b4bSBruno Thomsen 			__func__);
78703623b4bSBruno Thomsen 		return ret;
78803623b4bSBruno Thomsen 	}
78903623b4bSBruno Thomsen 
79003623b4bSBruno Thomsen 	ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group);
79103623b4bSBruno Thomsen 	if (ret) {
79203623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper sysfs registering failed\n",
79303623b4bSBruno Thomsen 			__func__);
79403623b4bSBruno Thomsen 		return ret;
79503623b4bSBruno Thomsen 	}
79603623b4bSBruno Thomsen 
797fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(pcf2127->rtc);
79818cb6368SRenaud Cerrato }
79918cb6368SRenaud Cerrato 
80018cb6368SRenaud Cerrato #ifdef CONFIG_OF
80118cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
80218cb6368SRenaud Cerrato 	{ .compatible = "nxp,pcf2127" },
803cee2cc21SAkinobu Mita 	{ .compatible = "nxp,pcf2129" },
804985b30dbSLiam Beguin 	{ .compatible = "nxp,pca2129" },
80518cb6368SRenaud Cerrato 	{}
80618cb6368SRenaud Cerrato };
80718cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
80818cb6368SRenaud Cerrato #endif
80918cb6368SRenaud Cerrato 
8109408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C)
8119408ec1aSAkinobu Mita 
812907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count)
813907b3262SAkinobu Mita {
814907b3262SAkinobu Mita 	struct device *dev = context;
815907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
816907b3262SAkinobu Mita 	int ret;
817907b3262SAkinobu Mita 
818907b3262SAkinobu Mita 	ret = i2c_master_send(client, data, count);
819907b3262SAkinobu Mita 	if (ret != count)
820907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
821907b3262SAkinobu Mita 
822907b3262SAkinobu Mita 	return 0;
823907b3262SAkinobu Mita }
824907b3262SAkinobu Mita 
825907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context,
826907b3262SAkinobu Mita 				const void *reg, size_t reg_size,
827907b3262SAkinobu Mita 				const void *val, size_t val_size)
828907b3262SAkinobu Mita {
829907b3262SAkinobu Mita 	struct device *dev = context;
830907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
831907b3262SAkinobu Mita 	int ret;
832907b3262SAkinobu Mita 	void *buf;
833907b3262SAkinobu Mita 
834907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
835907b3262SAkinobu Mita 		return -EINVAL;
836907b3262SAkinobu Mita 
837907b3262SAkinobu Mita 	buf = kmalloc(val_size + 1, GFP_KERNEL);
838907b3262SAkinobu Mita 	if (!buf)
839907b3262SAkinobu Mita 		return -ENOMEM;
840907b3262SAkinobu Mita 
841907b3262SAkinobu Mita 	memcpy(buf, reg, 1);
842907b3262SAkinobu Mita 	memcpy(buf + 1, val, val_size);
843907b3262SAkinobu Mita 
844907b3262SAkinobu Mita 	ret = i2c_master_send(client, buf, val_size + 1);
8459bde0afbSXulin Sun 
8469bde0afbSXulin Sun 	kfree(buf);
8479bde0afbSXulin Sun 
848907b3262SAkinobu Mita 	if (ret != val_size + 1)
849907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
850907b3262SAkinobu Mita 
851907b3262SAkinobu Mita 	return 0;
852907b3262SAkinobu Mita }
853907b3262SAkinobu Mita 
854907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
855907b3262SAkinobu Mita 				void *val, size_t val_size)
856907b3262SAkinobu Mita {
857907b3262SAkinobu Mita 	struct device *dev = context;
858907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
859907b3262SAkinobu Mita 	int ret;
860907b3262SAkinobu Mita 
861907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
862907b3262SAkinobu Mita 		return -EINVAL;
863907b3262SAkinobu Mita 
864907b3262SAkinobu Mita 	ret = i2c_master_send(client, reg, 1);
865907b3262SAkinobu Mita 	if (ret != 1)
866907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
867907b3262SAkinobu Mita 
868907b3262SAkinobu Mita 	ret = i2c_master_recv(client, val, val_size);
869907b3262SAkinobu Mita 	if (ret != val_size)
870907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
871907b3262SAkinobu Mita 
872907b3262SAkinobu Mita 	return 0;
873907b3262SAkinobu Mita }
874907b3262SAkinobu Mita 
875907b3262SAkinobu Mita /*
876907b3262SAkinobu Mita  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
877907b3262SAkinobu Mita  * is that the STOP condition is required between set register address and
878907b3262SAkinobu Mita  * read register data when reading from registers.
879907b3262SAkinobu Mita  */
880907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = {
881907b3262SAkinobu Mita 	.write = pcf2127_i2c_write,
882907b3262SAkinobu Mita 	.gather_write = pcf2127_i2c_gather_write,
883907b3262SAkinobu Mita 	.read = pcf2127_i2c_read,
88418cb6368SRenaud Cerrato };
88518cb6368SRenaud Cerrato 
886907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver;
887907b3262SAkinobu Mita 
888*5418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = {
889*5418e595SUwe Kleine-König 	{ "pcf2127", 1 },
890*5418e595SUwe Kleine-König 	{ "pcf2129", 0 },
891*5418e595SUwe Kleine-König 	{ "pca2129", 0 },
892*5418e595SUwe Kleine-König 	{ }
893*5418e595SUwe Kleine-König };
894*5418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
895*5418e595SUwe Kleine-König 
896*5418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client)
897907b3262SAkinobu Mita {
898*5418e595SUwe Kleine-König 	const struct i2c_device_id *id = i2c_match_id(pcf2127_i2c_id, client);
899907b3262SAkinobu Mita 	struct regmap *regmap;
900907b3262SAkinobu Mita 	static const struct regmap_config config = {
901907b3262SAkinobu Mita 		.reg_bits = 8,
902907b3262SAkinobu Mita 		.val_bits = 8,
903040e6dc0SAlexandre Belloni 		.max_register = 0x1d,
904907b3262SAkinobu Mita 	};
905907b3262SAkinobu Mita 
906907b3262SAkinobu Mita 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
907907b3262SAkinobu Mita 		return -ENODEV;
908907b3262SAkinobu Mita 
909907b3262SAkinobu Mita 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
910907b3262SAkinobu Mita 					&client->dev, &config);
911907b3262SAkinobu Mita 	if (IS_ERR(regmap)) {
912907b3262SAkinobu Mita 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
913907b3262SAkinobu Mita 			__func__, PTR_ERR(regmap));
914907b3262SAkinobu Mita 		return PTR_ERR(regmap);
915907b3262SAkinobu Mita 	}
916907b3262SAkinobu Mita 
91727006416SAlexandre Belloni 	return pcf2127_probe(&client->dev, regmap, client->irq,
918d6c3029fSUwe Kleine-König 			     pcf2127_i2c_driver.driver.name, id->driver_data);
919907b3262SAkinobu Mita }
920907b3262SAkinobu Mita 
921907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = {
922907b3262SAkinobu Mita 	.driver		= {
923907b3262SAkinobu Mita 		.name	= "rtc-pcf2127-i2c",
924907b3262SAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
925907b3262SAkinobu Mita 	},
926*5418e595SUwe Kleine-König 	.probe_new	= pcf2127_i2c_probe,
927907b3262SAkinobu Mita 	.id_table	= pcf2127_i2c_id,
928907b3262SAkinobu Mita };
9299408ec1aSAkinobu Mita 
9309408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
9319408ec1aSAkinobu Mita {
9329408ec1aSAkinobu Mita 	return i2c_add_driver(&pcf2127_i2c_driver);
9339408ec1aSAkinobu Mita }
9349408ec1aSAkinobu Mita 
9359408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
9369408ec1aSAkinobu Mita {
9379408ec1aSAkinobu Mita 	i2c_del_driver(&pcf2127_i2c_driver);
9389408ec1aSAkinobu Mita }
9399408ec1aSAkinobu Mita 
9409408ec1aSAkinobu Mita #else
9419408ec1aSAkinobu Mita 
9429408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
9439408ec1aSAkinobu Mita {
9449408ec1aSAkinobu Mita 	return 0;
9459408ec1aSAkinobu Mita }
9469408ec1aSAkinobu Mita 
9479408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
9489408ec1aSAkinobu Mita {
9499408ec1aSAkinobu Mita }
9509408ec1aSAkinobu Mita 
9519408ec1aSAkinobu Mita #endif
9529408ec1aSAkinobu Mita 
9539408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER)
9549408ec1aSAkinobu Mita 
9559408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver;
9569408ec1aSAkinobu Mita 
9579408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi)
9589408ec1aSAkinobu Mita {
9599408ec1aSAkinobu Mita 	static const struct regmap_config config = {
9609408ec1aSAkinobu Mita 		.reg_bits = 8,
9619408ec1aSAkinobu Mita 		.val_bits = 8,
9629408ec1aSAkinobu Mita 		.read_flag_mask = 0xa0,
9639408ec1aSAkinobu Mita 		.write_flag_mask = 0x20,
964040e6dc0SAlexandre Belloni 		.max_register = 0x1d,
9659408ec1aSAkinobu Mita 	};
9669408ec1aSAkinobu Mita 	struct regmap *regmap;
9679408ec1aSAkinobu Mita 
9689408ec1aSAkinobu Mita 	regmap = devm_regmap_init_spi(spi, &config);
9699408ec1aSAkinobu Mita 	if (IS_ERR(regmap)) {
9709408ec1aSAkinobu Mita 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
9719408ec1aSAkinobu Mita 			__func__, PTR_ERR(regmap));
9729408ec1aSAkinobu Mita 		return PTR_ERR(regmap);
9739408ec1aSAkinobu Mita 	}
9749408ec1aSAkinobu Mita 
97527006416SAlexandre Belloni 	return pcf2127_probe(&spi->dev, regmap, spi->irq,
97627006416SAlexandre Belloni 			     pcf2127_spi_driver.driver.name,
977d6c3029fSUwe Kleine-König 			     spi_get_device_id(spi)->driver_data);
9789408ec1aSAkinobu Mita }
9799408ec1aSAkinobu Mita 
9809408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = {
981d6c3029fSUwe Kleine-König 	{ "pcf2127", 1 },
982cee2cc21SAkinobu Mita 	{ "pcf2129", 0 },
983985b30dbSLiam Beguin 	{ "pca2129", 0 },
9849408ec1aSAkinobu Mita 	{ }
9859408ec1aSAkinobu Mita };
9869408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
9879408ec1aSAkinobu Mita 
9889408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = {
9899408ec1aSAkinobu Mita 	.driver		= {
9909408ec1aSAkinobu Mita 		.name	= "rtc-pcf2127-spi",
9919408ec1aSAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
9929408ec1aSAkinobu Mita 	},
9939408ec1aSAkinobu Mita 	.probe		= pcf2127_spi_probe,
9949408ec1aSAkinobu Mita 	.id_table	= pcf2127_spi_id,
9959408ec1aSAkinobu Mita };
9969408ec1aSAkinobu Mita 
9979408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
9989408ec1aSAkinobu Mita {
9999408ec1aSAkinobu Mita 	return spi_register_driver(&pcf2127_spi_driver);
10009408ec1aSAkinobu Mita }
10019408ec1aSAkinobu Mita 
10029408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
10039408ec1aSAkinobu Mita {
10049408ec1aSAkinobu Mita 	spi_unregister_driver(&pcf2127_spi_driver);
10059408ec1aSAkinobu Mita }
10069408ec1aSAkinobu Mita 
10079408ec1aSAkinobu Mita #else
10089408ec1aSAkinobu Mita 
10099408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
10109408ec1aSAkinobu Mita {
10119408ec1aSAkinobu Mita 	return 0;
10129408ec1aSAkinobu Mita }
10139408ec1aSAkinobu Mita 
10149408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
10159408ec1aSAkinobu Mita {
10169408ec1aSAkinobu Mita }
10179408ec1aSAkinobu Mita 
10189408ec1aSAkinobu Mita #endif
10199408ec1aSAkinobu Mita 
10209408ec1aSAkinobu Mita static int __init pcf2127_init(void)
10219408ec1aSAkinobu Mita {
10229408ec1aSAkinobu Mita 	int ret;
10239408ec1aSAkinobu Mita 
10249408ec1aSAkinobu Mita 	ret = pcf2127_i2c_register_driver();
10259408ec1aSAkinobu Mita 	if (ret) {
10269408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
10279408ec1aSAkinobu Mita 		return ret;
10289408ec1aSAkinobu Mita 	}
10299408ec1aSAkinobu Mita 
10309408ec1aSAkinobu Mita 	ret = pcf2127_spi_register_driver();
10319408ec1aSAkinobu Mita 	if (ret) {
10329408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
10339408ec1aSAkinobu Mita 		pcf2127_i2c_unregister_driver();
10349408ec1aSAkinobu Mita 	}
10359408ec1aSAkinobu Mita 
10369408ec1aSAkinobu Mita 	return ret;
10379408ec1aSAkinobu Mita }
10389408ec1aSAkinobu Mita module_init(pcf2127_init)
10399408ec1aSAkinobu Mita 
10409408ec1aSAkinobu Mita static void __exit pcf2127_exit(void)
10419408ec1aSAkinobu Mita {
10429408ec1aSAkinobu Mita 	pcf2127_spi_unregister_driver();
10439408ec1aSAkinobu Mita 	pcf2127_i2c_unregister_driver();
10449408ec1aSAkinobu Mita }
10459408ec1aSAkinobu Mita module_exit(pcf2127_exit)
104618cb6368SRenaud Cerrato 
104718cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1048cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
10494d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
1050