xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision 31f077c374a8e7cf1960560c0c5e4065048557c0)
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)
488a914bacSLiam Beguin /* Alarm registers */
498a914bacSLiam Beguin #define PCF2127_REG_ALARM_SC		0x0A
508a914bacSLiam Beguin #define PCF2127_REG_ALARM_MN		0x0B
518a914bacSLiam Beguin #define PCF2127_REG_ALARM_HR		0x0C
528a914bacSLiam Beguin #define PCF2127_REG_ALARM_DM		0x0D
538a914bacSLiam Beguin #define PCF2127_REG_ALARM_DW		0x0E
5427006416SAlexandre Belloni #define PCF2127_BIT_ALARM_AE			BIT(7)
5515f57b3eSPhilipp Rosenberger /* CLKOUT control register */
5615f57b3eSPhilipp Rosenberger #define PCF2127_REG_CLKOUT		0x0f
5715f57b3eSPhilipp Rosenberger #define PCF2127_BIT_CLKOUT_OTPR			BIT(5)
580e735eaaSBruno Thomsen /* Watchdog registers */
590e735eaaSBruno Thomsen #define PCF2127_REG_WD_CTL		0x10
600e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF0			BIT(0)
610e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_TF1			BIT(1)
620e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD0			BIT(6)
630e735eaaSBruno Thomsen #define PCF2127_BIT_WD_CTL_CD1			BIT(7)
640e735eaaSBruno Thomsen #define PCF2127_REG_WD_VAL		0x11
6503623b4bSBruno Thomsen /* Tamper timestamp registers */
6603623b4bSBruno Thomsen #define PCF2127_REG_TS_CTRL		0x12
6703623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSOFF		BIT(6)
6803623b4bSBruno Thomsen #define PCF2127_BIT_TS_CTRL_TSM			BIT(7)
6903623b4bSBruno Thomsen #define PCF2127_REG_TS_SC		0x13
7003623b4bSBruno Thomsen #define PCF2127_REG_TS_MN		0x14
7103623b4bSBruno Thomsen #define PCF2127_REG_TS_HR		0x15
7203623b4bSBruno Thomsen #define PCF2127_REG_TS_DM		0x16
7303623b4bSBruno Thomsen #define PCF2127_REG_TS_MO		0x17
7403623b4bSBruno Thomsen #define PCF2127_REG_TS_YR		0x18
75bbfe3a7aSBruno Thomsen /*
76bbfe3a7aSBruno Thomsen  * RAM registers
77bbfe3a7aSBruno Thomsen  * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
78bbfe3a7aSBruno Thomsen  * battery backed and can survive a power outage.
79bbfe3a7aSBruno Thomsen  * PCF2129 doesn't have this feature.
80bbfe3a7aSBruno Thomsen  */
81bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB	0x1A
82bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD		0x1C
83bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD		0x1D
84f97cfddcSUwe Kleine-König 
850e735eaaSBruno Thomsen /* Watchdog timer value constants */
860e735eaaSBruno Thomsen #define PCF2127_WD_VAL_STOP		0
870e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MIN		2
880e735eaaSBruno Thomsen #define PCF2127_WD_VAL_MAX		255
890e735eaaSBruno Thomsen #define PCF2127_WD_VAL_DEFAULT		60
90653ebd75SAndrea Scian 
912f861984SMian Yousaf Kaukab /* Mask for currently enabled interrupts */
922f861984SMian Yousaf Kaukab #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1)
932f861984SMian Yousaf Kaukab #define PCF2127_CTRL2_IRQ_MASK ( \
942f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_AF | \
952f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_WDTF | \
962f861984SMian Yousaf Kaukab 		PCF2127_BIT_CTRL2_TSF2)
972f861984SMian Yousaf Kaukab 
9818cb6368SRenaud Cerrato struct pcf2127 {
9918cb6368SRenaud Cerrato 	struct rtc_device *rtc;
1000e735eaaSBruno Thomsen 	struct watchdog_device wdd;
101907b3262SAkinobu Mita 	struct regmap *regmap;
1022f861984SMian Yousaf Kaukab 	time64_t ts;
1032f861984SMian Yousaf Kaukab 	bool ts_valid;
1042f861984SMian Yousaf Kaukab 	bool irq_enabled;
10518cb6368SRenaud Cerrato };
10618cb6368SRenaud Cerrato 
10718cb6368SRenaud Cerrato /*
10818cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
10918cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
11018cb6368SRenaud Cerrato  */
111907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
11218cb6368SRenaud Cerrato {
113907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
114*31f077c3SHugo Villeneuve 	unsigned char buf[7];
115907b3262SAkinobu Mita 	int ret;
11618cb6368SRenaud Cerrato 
1177f43020eSBruno Thomsen 	/*
1187f43020eSBruno Thomsen 	 * Avoid reading CTRL2 register as it causes WD_VAL register
1197f43020eSBruno Thomsen 	 * value to reset to 0 which means watchdog is stopped.
1207f43020eSBruno Thomsen 	 */
121*31f077c3SHugo Villeneuve 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC, buf,
122*31f077c3SHugo Villeneuve 			       sizeof(buf));
123907b3262SAkinobu Mita 	if (ret) {
124907b3262SAkinobu Mita 		dev_err(dev, "%s: read error\n", __func__);
125907b3262SAkinobu Mita 		return ret;
12618cb6368SRenaud Cerrato 	}
12718cb6368SRenaud Cerrato 
128bbfe3a7aSBruno Thomsen 	/* Clock integrity is not guaranteed when OSF flag is set. */
129*31f077c3SHugo Villeneuve 	if (buf[0] & PCF2127_BIT_SC_OSF) {
130653ebd75SAndrea Scian 		/*
131653ebd75SAndrea Scian 		 * no need clear the flag here,
132653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
133653ebd75SAndrea Scian 		 */
134907b3262SAkinobu Mita 		dev_warn(dev,
135653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
136653ebd75SAndrea Scian 		return -EINVAL;
13718cb6368SRenaud Cerrato 	}
13818cb6368SRenaud Cerrato 
139907b3262SAkinobu Mita 	dev_dbg(dev,
140*31f077c3SHugo Villeneuve 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
14118cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
142*31f077c3SHugo Villeneuve 		__func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
14318cb6368SRenaud Cerrato 
144*31f077c3SHugo Villeneuve 	tm->tm_sec = bcd2bin(buf[0] & 0x7F);
145*31f077c3SHugo Villeneuve 	tm->tm_min = bcd2bin(buf[1] & 0x7F);
146*31f077c3SHugo Villeneuve 	tm->tm_hour = bcd2bin(buf[2] & 0x3F); /* rtc hr 0-23 */
147*31f077c3SHugo Villeneuve 	tm->tm_mday = bcd2bin(buf[3] & 0x3F);
148*31f077c3SHugo Villeneuve 	tm->tm_wday = buf[4] & 0x07;
149*31f077c3SHugo Villeneuve 	tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; /* rtc mn 1-12 */
150*31f077c3SHugo Villeneuve 	tm->tm_year = bcd2bin(buf[6]);
151b139bb5cSAlexandre Belloni 	tm->tm_year += 100;
15218cb6368SRenaud Cerrato 
153907b3262SAkinobu Mita 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
15418cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
15518cb6368SRenaud Cerrato 		__func__,
15618cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
15718cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
15818cb6368SRenaud Cerrato 
15922652ba7SAlexandre Belloni 	return 0;
16018cb6368SRenaud Cerrato }
16118cb6368SRenaud Cerrato 
162907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
16318cb6368SRenaud Cerrato {
164907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
165907b3262SAkinobu Mita 	unsigned char buf[7];
16618cb6368SRenaud Cerrato 	int i = 0, err;
16718cb6368SRenaud Cerrato 
168907b3262SAkinobu Mita 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
16918cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
17018cb6368SRenaud Cerrato 		__func__,
17118cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
17218cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
17318cb6368SRenaud Cerrato 
17418cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
175653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
17618cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
17718cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
17818cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
17918cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
18018cb6368SRenaud Cerrato 
18118cb6368SRenaud Cerrato 	/* month, 1 - 12 */
18218cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
18318cb6368SRenaud Cerrato 
18418cb6368SRenaud Cerrato 	/* year */
185b139bb5cSAlexandre Belloni 	buf[i++] = bin2bcd(tm->tm_year - 100);
18618cb6368SRenaud Cerrato 
18718cb6368SRenaud Cerrato 	/* write register's data */
188907b3262SAkinobu Mita 	err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
189907b3262SAkinobu Mita 	if (err) {
190907b3262SAkinobu Mita 		dev_err(dev,
19118cb6368SRenaud Cerrato 			"%s: err=%d", __func__, err);
192907b3262SAkinobu Mita 		return err;
19318cb6368SRenaud Cerrato 	}
19418cb6368SRenaud Cerrato 
19518cb6368SRenaud Cerrato 	return 0;
19618cb6368SRenaud Cerrato }
19718cb6368SRenaud Cerrato 
19818cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
19918cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
20018cb6368SRenaud Cerrato {
201907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
2027d65cf8cSAlexandre Belloni 	int val, touser = 0;
203f97cfddcSUwe Kleine-König 	int ret;
20418cb6368SRenaud Cerrato 
20518cb6368SRenaud Cerrato 	switch (cmd) {
20618cb6368SRenaud Cerrato 	case RTC_VL_READ:
2077d65cf8cSAlexandre Belloni 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
208907b3262SAkinobu Mita 		if (ret)
209f97cfddcSUwe Kleine-König 			return ret;
21018cb6368SRenaud Cerrato 
2117d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BLF)
2127d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_LOW;
2137d65cf8cSAlexandre Belloni 
2147d65cf8cSAlexandre Belloni 		if (val & PCF2127_BIT_CTRL3_BF)
2157d65cf8cSAlexandre Belloni 			touser |= RTC_VL_BACKUP_SWITCH;
216f97cfddcSUwe Kleine-König 
217af427311SAlexandre Belloni 		return put_user(touser, (unsigned int __user *)arg);
2187d65cf8cSAlexandre Belloni 
2197d65cf8cSAlexandre Belloni 	case RTC_VL_CLR:
2207d65cf8cSAlexandre Belloni 		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
2217d65cf8cSAlexandre Belloni 					  PCF2127_BIT_CTRL3_BF, 0);
2227d65cf8cSAlexandre Belloni 
22318cb6368SRenaud Cerrato 	default:
22418cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
22518cb6368SRenaud Cerrato 	}
22618cb6368SRenaud Cerrato }
22718cb6368SRenaud Cerrato 
228d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset,
229d6c3029fSUwe Kleine-König 			      void *val, size_t bytes)
230d6c3029fSUwe Kleine-König {
231d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
232d6c3029fSUwe Kleine-König 	int ret;
233d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
234d6c3029fSUwe Kleine-König 
235bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
236d6c3029fSUwe Kleine-König 				offsetbuf, 2);
237d6c3029fSUwe Kleine-König 	if (ret)
238d6c3029fSUwe Kleine-König 		return ret;
239d6c3029fSUwe Kleine-König 
240ba1c30bfSDan Carpenter 	return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
241d6c3029fSUwe Kleine-König 				val, bytes);
242d6c3029fSUwe Kleine-König }
243d6c3029fSUwe Kleine-König 
244d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset,
245d6c3029fSUwe Kleine-König 			       void *val, size_t bytes)
246d6c3029fSUwe Kleine-König {
247d6c3029fSUwe Kleine-König 	struct pcf2127 *pcf2127 = priv;
248d6c3029fSUwe Kleine-König 	int ret;
249d6c3029fSUwe Kleine-König 	unsigned char offsetbuf[] = { offset >> 8, offset };
250d6c3029fSUwe Kleine-König 
251bbfe3a7aSBruno Thomsen 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
252d6c3029fSUwe Kleine-König 				offsetbuf, 2);
253d6c3029fSUwe Kleine-König 	if (ret)
254d6c3029fSUwe Kleine-König 		return ret;
255d6c3029fSUwe Kleine-König 
256ba1c30bfSDan Carpenter 	return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
257d6c3029fSUwe Kleine-König 				 val, bytes);
258d6c3029fSUwe Kleine-König }
259d6c3029fSUwe Kleine-König 
2600e735eaaSBruno Thomsen /* watchdog driver */
2610e735eaaSBruno Thomsen 
2620e735eaaSBruno Thomsen static int pcf2127_wdt_ping(struct watchdog_device *wdd)
2630e735eaaSBruno Thomsen {
2640e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
2650e735eaaSBruno Thomsen 
2660e735eaaSBruno Thomsen 	return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout);
2670e735eaaSBruno Thomsen }
2680e735eaaSBruno Thomsen 
2690e735eaaSBruno Thomsen /*
2700e735eaaSBruno Thomsen  * Restart watchdog timer if feature is active.
2710e735eaaSBruno Thomsen  *
2720e735eaaSBruno Thomsen  * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
2730e735eaaSBruno Thomsen  * since register also contain control/status flags for other features.
2740e735eaaSBruno Thomsen  * Always call this function after reading CTRL2 register.
2750e735eaaSBruno Thomsen  */
2760e735eaaSBruno Thomsen static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
2770e735eaaSBruno Thomsen {
2780e735eaaSBruno Thomsen 	int ret = 0;
2790e735eaaSBruno Thomsen 
2800e735eaaSBruno Thomsen 	if (watchdog_active(wdd)) {
2810e735eaaSBruno Thomsen 		ret = pcf2127_wdt_ping(wdd);
2820e735eaaSBruno Thomsen 		if (ret)
2830e735eaaSBruno Thomsen 			dev_err(wdd->parent,
2840e735eaaSBruno Thomsen 				"%s: watchdog restart failed, ret=%d\n",
2850e735eaaSBruno Thomsen 				__func__, ret);
2860e735eaaSBruno Thomsen 	}
2870e735eaaSBruno Thomsen 
2880e735eaaSBruno Thomsen 	return ret;
2890e735eaaSBruno Thomsen }
2900e735eaaSBruno Thomsen 
2910e735eaaSBruno Thomsen static int pcf2127_wdt_start(struct watchdog_device *wdd)
2920e735eaaSBruno Thomsen {
2930e735eaaSBruno Thomsen 	return pcf2127_wdt_ping(wdd);
2940e735eaaSBruno Thomsen }
2950e735eaaSBruno Thomsen 
2960e735eaaSBruno Thomsen static int pcf2127_wdt_stop(struct watchdog_device *wdd)
2970e735eaaSBruno Thomsen {
2980e735eaaSBruno Thomsen 	struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
2990e735eaaSBruno Thomsen 
3000e735eaaSBruno Thomsen 	return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL,
3010e735eaaSBruno Thomsen 			    PCF2127_WD_VAL_STOP);
3020e735eaaSBruno Thomsen }
3030e735eaaSBruno Thomsen 
3040e735eaaSBruno Thomsen static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
3050e735eaaSBruno Thomsen 				   unsigned int new_timeout)
3060e735eaaSBruno Thomsen {
3070e735eaaSBruno Thomsen 	dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
3080e735eaaSBruno Thomsen 		new_timeout, wdd->timeout);
3090e735eaaSBruno Thomsen 
3100e735eaaSBruno Thomsen 	wdd->timeout = new_timeout;
3110e735eaaSBruno Thomsen 
3120e735eaaSBruno Thomsen 	return pcf2127_wdt_active_ping(wdd);
3130e735eaaSBruno Thomsen }
3140e735eaaSBruno Thomsen 
3150e735eaaSBruno Thomsen static const struct watchdog_info pcf2127_wdt_info = {
3160e735eaaSBruno Thomsen 	.identity = "NXP PCF2127/PCF2129 Watchdog",
3170e735eaaSBruno Thomsen 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
3180e735eaaSBruno Thomsen };
3190e735eaaSBruno Thomsen 
3200e735eaaSBruno Thomsen static const struct watchdog_ops pcf2127_watchdog_ops = {
3210e735eaaSBruno Thomsen 	.owner = THIS_MODULE,
3220e735eaaSBruno Thomsen 	.start = pcf2127_wdt_start,
3230e735eaaSBruno Thomsen 	.stop = pcf2127_wdt_stop,
3240e735eaaSBruno Thomsen 	.ping = pcf2127_wdt_ping,
3250e735eaaSBruno Thomsen 	.set_timeout = pcf2127_wdt_set_timeout,
3260e735eaaSBruno Thomsen };
3270e735eaaSBruno Thomsen 
3285d78533aSUwe Kleine-König static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
3295d78533aSUwe Kleine-König {
3305d78533aSUwe Kleine-König 	u32 wdd_timeout;
3315d78533aSUwe Kleine-König 	int ret;
3325d78533aSUwe Kleine-König 
33371ac1345SUwe Kleine-König 	if (!IS_ENABLED(CONFIG_WATCHDOG) ||
33471ac1345SUwe Kleine-König 	    !device_property_read_bool(dev, "reset-source"))
3355d78533aSUwe Kleine-König 		return 0;
3365d78533aSUwe Kleine-König 
3375d78533aSUwe Kleine-König 	pcf2127->wdd.parent = dev;
3385d78533aSUwe Kleine-König 	pcf2127->wdd.info = &pcf2127_wdt_info;
3395d78533aSUwe Kleine-König 	pcf2127->wdd.ops = &pcf2127_watchdog_ops;
3405d78533aSUwe Kleine-König 	pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN;
3415d78533aSUwe Kleine-König 	pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX;
3425d78533aSUwe Kleine-König 	pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT;
3435d78533aSUwe Kleine-König 	pcf2127->wdd.min_hw_heartbeat_ms = 500;
3445d78533aSUwe Kleine-König 	pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
3455d78533aSUwe Kleine-König 
3465d78533aSUwe Kleine-König 	watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
3475d78533aSUwe Kleine-König 
3485d78533aSUwe Kleine-König 	/* Test if watchdog timer is started by bootloader */
3495d78533aSUwe Kleine-König 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_WD_VAL, &wdd_timeout);
3505d78533aSUwe Kleine-König 	if (ret)
3515d78533aSUwe Kleine-König 		return ret;
3525d78533aSUwe Kleine-König 
3535d78533aSUwe Kleine-König 	if (wdd_timeout)
3545d78533aSUwe Kleine-König 		set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status);
3555d78533aSUwe Kleine-König 
3565d78533aSUwe Kleine-König 	return devm_watchdog_register_device(dev, &pcf2127->wdd);
3575d78533aSUwe Kleine-König }
3585d78533aSUwe Kleine-König 
3598a914bacSLiam Beguin /* Alarm */
3608a914bacSLiam Beguin static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3618a914bacSLiam Beguin {
3628a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
36373ce0530SHugo Villeneuve 	u8 buf[5];
36473ce0530SHugo Villeneuve 	unsigned int ctrl2;
3658a914bacSLiam Beguin 	int ret;
3668a914bacSLiam Beguin 
3678a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
3688a914bacSLiam Beguin 	if (ret)
3698a914bacSLiam Beguin 		return ret;
3708a914bacSLiam Beguin 
3718a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
3728a914bacSLiam Beguin 	if (ret)
3738a914bacSLiam Beguin 		return ret;
3748a914bacSLiam Beguin 
3758a914bacSLiam Beguin 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
3768a914bacSLiam Beguin 			       sizeof(buf));
3778a914bacSLiam Beguin 	if (ret)
3788a914bacSLiam Beguin 		return ret;
3798a914bacSLiam Beguin 
3808a914bacSLiam Beguin 	alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE;
3818a914bacSLiam Beguin 	alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF;
3828a914bacSLiam Beguin 
3838a914bacSLiam Beguin 	alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
3848a914bacSLiam Beguin 	alrm->time.tm_min = bcd2bin(buf[1] & 0x7F);
3858a914bacSLiam Beguin 	alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F);
3868a914bacSLiam Beguin 	alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
3878a914bacSLiam Beguin 
3888a914bacSLiam Beguin 	return 0;
3898a914bacSLiam Beguin }
3908a914bacSLiam Beguin 
3918a914bacSLiam Beguin static int pcf2127_rtc_alarm_irq_enable(struct device *dev, u32 enable)
3928a914bacSLiam Beguin {
3938a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
3948a914bacSLiam Beguin 	int ret;
3958a914bacSLiam Beguin 
3968a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
3978a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AIE,
3988a914bacSLiam Beguin 				 enable ? PCF2127_BIT_CTRL2_AIE : 0);
3998a914bacSLiam Beguin 	if (ret)
4008a914bacSLiam Beguin 		return ret;
4018a914bacSLiam Beguin 
4028a914bacSLiam Beguin 	return pcf2127_wdt_active_ping(&pcf2127->wdd);
4038a914bacSLiam Beguin }
4048a914bacSLiam Beguin 
4058a914bacSLiam Beguin static int pcf2127_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4068a914bacSLiam Beguin {
4078a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4088a914bacSLiam Beguin 	uint8_t buf[5];
4098a914bacSLiam Beguin 	int ret;
4108a914bacSLiam Beguin 
4118a914bacSLiam Beguin 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
4128a914bacSLiam Beguin 				 PCF2127_BIT_CTRL2_AF, 0);
4138a914bacSLiam Beguin 	if (ret)
4148a914bacSLiam Beguin 		return ret;
4158a914bacSLiam Beguin 
4168a914bacSLiam Beguin 	ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
4178a914bacSLiam Beguin 	if (ret)
4188a914bacSLiam Beguin 		return ret;
4198a914bacSLiam Beguin 
4208a914bacSLiam Beguin 	buf[0] = bin2bcd(alrm->time.tm_sec);
4218a914bacSLiam Beguin 	buf[1] = bin2bcd(alrm->time.tm_min);
4228a914bacSLiam Beguin 	buf[2] = bin2bcd(alrm->time.tm_hour);
4238a914bacSLiam Beguin 	buf[3] = bin2bcd(alrm->time.tm_mday);
42427006416SAlexandre Belloni 	buf[4] = PCF2127_BIT_ALARM_AE; /* Do not match on week day */
4258a914bacSLiam Beguin 
4268a914bacSLiam Beguin 	ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_ALARM_SC, buf,
4278a914bacSLiam Beguin 				sizeof(buf));
4288a914bacSLiam Beguin 	if (ret)
4298a914bacSLiam Beguin 		return ret;
4308a914bacSLiam Beguin 
4318a914bacSLiam Beguin 	return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled);
4328a914bacSLiam Beguin }
4338a914bacSLiam Beguin 
4342f861984SMian Yousaf Kaukab /*
4352f861984SMian Yousaf Kaukab  * This function reads ctrl2 register, caller is responsible for calling
4362f861984SMian Yousaf Kaukab  * pcf2127_wdt_active_ping()
4372f861984SMian Yousaf Kaukab  */
4382f861984SMian Yousaf Kaukab static int pcf2127_rtc_ts_read(struct device *dev, time64_t *ts)
4392f861984SMian Yousaf Kaukab {
4402f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4412f861984SMian Yousaf Kaukab 	struct rtc_time tm;
4422f861984SMian Yousaf Kaukab 	int ret;
4432f861984SMian Yousaf Kaukab 	unsigned char data[25];
4442f861984SMian Yousaf Kaukab 
4452f861984SMian Yousaf Kaukab 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data,
4462f861984SMian Yousaf Kaukab 			       sizeof(data));
4472f861984SMian Yousaf Kaukab 	if (ret) {
4482f861984SMian Yousaf Kaukab 		dev_err(dev, "%s: read error ret=%d\n", __func__, ret);
4492f861984SMian Yousaf Kaukab 		return ret;
4502f861984SMian Yousaf Kaukab 	}
4512f861984SMian Yousaf Kaukab 
4522f861984SMian Yousaf Kaukab 	dev_dbg(dev,
4532f861984SMian 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",
4542f861984SMian Yousaf Kaukab 		__func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2],
4552f861984SMian Yousaf Kaukab 		data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC],
4562f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR],
4572f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO],
4582f861984SMian Yousaf Kaukab 		data[PCF2127_REG_TS_YR]);
4592f861984SMian Yousaf Kaukab 
4602f861984SMian Yousaf Kaukab 	tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F);
4612f861984SMian Yousaf Kaukab 	tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F);
4622f861984SMian Yousaf Kaukab 	tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F);
4632f861984SMian Yousaf Kaukab 	tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F);
4642f861984SMian Yousaf Kaukab 	/* TS_MO register (month) value range: 1-12 */
4652f861984SMian Yousaf Kaukab 	tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1;
4662f861984SMian Yousaf Kaukab 	tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]);
4672f861984SMian Yousaf Kaukab 	if (tm.tm_year < 70)
4682f861984SMian Yousaf Kaukab 		tm.tm_year += 100; /* assume we are in 1970...2069 */
4692f861984SMian Yousaf Kaukab 
4702f861984SMian Yousaf Kaukab 	ret = rtc_valid_tm(&tm);
4712f861984SMian Yousaf Kaukab 	if (ret) {
4722f861984SMian Yousaf Kaukab 		dev_err(dev, "Invalid timestamp. ret=%d\n", ret);
4732f861984SMian Yousaf Kaukab 		return ret;
4742f861984SMian Yousaf Kaukab 	}
4752f861984SMian Yousaf Kaukab 
4762f861984SMian Yousaf Kaukab 	*ts = rtc_tm_to_time64(&tm);
4772f861984SMian Yousaf Kaukab 	return 0;
4782f861984SMian Yousaf Kaukab };
4792f861984SMian Yousaf Kaukab 
4802f861984SMian Yousaf Kaukab static void pcf2127_rtc_ts_snapshot(struct device *dev)
4812f861984SMian Yousaf Kaukab {
4822f861984SMian Yousaf Kaukab 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4832f861984SMian Yousaf Kaukab 	int ret;
4842f861984SMian Yousaf Kaukab 
4852f861984SMian Yousaf Kaukab 	/* Let userspace read the first timestamp */
4862f861984SMian Yousaf Kaukab 	if (pcf2127->ts_valid)
4872f861984SMian Yousaf Kaukab 		return;
4882f861984SMian Yousaf Kaukab 
4892f861984SMian Yousaf Kaukab 	ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts);
4902f861984SMian Yousaf Kaukab 	if (!ret)
4912f861984SMian Yousaf Kaukab 		pcf2127->ts_valid = true;
4922f861984SMian Yousaf Kaukab }
4932f861984SMian Yousaf Kaukab 
4948a914bacSLiam Beguin static irqreturn_t pcf2127_rtc_irq(int irq, void *dev)
4958a914bacSLiam Beguin {
4968a914bacSLiam Beguin 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
4972f861984SMian Yousaf Kaukab 	unsigned int ctrl1, ctrl2;
4988a914bacSLiam Beguin 	int ret = 0;
4998a914bacSLiam Beguin 
5002f861984SMian Yousaf Kaukab 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
5012f861984SMian Yousaf Kaukab 	if (ret)
5022f861984SMian Yousaf Kaukab 		return IRQ_NONE;
5032f861984SMian Yousaf Kaukab 
5048a914bacSLiam Beguin 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
5058a914bacSLiam Beguin 	if (ret)
5068a914bacSLiam Beguin 		return IRQ_NONE;
5078a914bacSLiam Beguin 
5082f861984SMian Yousaf Kaukab 	if (!(ctrl1 & PCF2127_CTRL1_IRQ_MASK || ctrl2 & PCF2127_CTRL2_IRQ_MASK))
50927006416SAlexandre Belloni 		return IRQ_NONE;
51027006416SAlexandre Belloni 
5112f861984SMian Yousaf Kaukab 	if (ctrl1 & PCF2127_BIT_CTRL1_TSF1 || ctrl2 & PCF2127_BIT_CTRL2_TSF2)
5122f861984SMian Yousaf Kaukab 		pcf2127_rtc_ts_snapshot(dev);
5138a914bacSLiam Beguin 
5142f861984SMian Yousaf Kaukab 	if (ctrl1 & PCF2127_CTRL1_IRQ_MASK)
5152f861984SMian Yousaf Kaukab 		regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1,
5162f861984SMian Yousaf Kaukab 			ctrl1 & ~PCF2127_CTRL1_IRQ_MASK);
5172f861984SMian Yousaf Kaukab 
5182f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_CTRL2_IRQ_MASK)
5192f861984SMian Yousaf Kaukab 		regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2,
5202f861984SMian Yousaf Kaukab 			ctrl2 & ~PCF2127_CTRL2_IRQ_MASK);
5212f861984SMian Yousaf Kaukab 
5222f861984SMian Yousaf Kaukab 	if (ctrl2 & PCF2127_BIT_CTRL2_AF)
5238a914bacSLiam Beguin 		rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF);
5248a914bacSLiam Beguin 
52527006416SAlexandre Belloni 	pcf2127_wdt_active_ping(&pcf2127->wdd);
5268a914bacSLiam Beguin 
5278a914bacSLiam Beguin 	return IRQ_HANDLED;
5288a914bacSLiam Beguin }
5298a914bacSLiam Beguin 
53025cbe9c8SAlexandre Belloni static const struct rtc_class_ops pcf2127_rtc_ops = {
5318a914bacSLiam Beguin 	.ioctl            = pcf2127_rtc_ioctl,
5328a914bacSLiam Beguin 	.read_time        = pcf2127_rtc_read_time,
5338a914bacSLiam Beguin 	.set_time         = pcf2127_rtc_set_time,
5348a914bacSLiam Beguin 	.read_alarm       = pcf2127_rtc_read_alarm,
5358a914bacSLiam Beguin 	.set_alarm        = pcf2127_rtc_set_alarm,
5368a914bacSLiam Beguin 	.alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
5378a914bacSLiam Beguin };
5388a914bacSLiam Beguin 
53903623b4bSBruno Thomsen /* sysfs interface */
54003623b4bSBruno Thomsen 
54103623b4bSBruno Thomsen static ssize_t timestamp0_store(struct device *dev,
54203623b4bSBruno Thomsen 				struct device_attribute *attr,
54303623b4bSBruno Thomsen 				const char *buf, size_t count)
54403623b4bSBruno Thomsen {
54503623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
54603623b4bSBruno Thomsen 	int ret;
54703623b4bSBruno Thomsen 
5482f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
5492f861984SMian Yousaf Kaukab 		pcf2127->ts_valid = false;
5502f861984SMian Yousaf Kaukab 	} else {
55103623b4bSBruno Thomsen 		ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
55203623b4bSBruno Thomsen 			PCF2127_BIT_CTRL1_TSF1, 0);
55303623b4bSBruno Thomsen 		if (ret) {
55403623b4bSBruno Thomsen 			dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret);
55503623b4bSBruno Thomsen 			return ret;
55603623b4bSBruno Thomsen 		}
55703623b4bSBruno Thomsen 
55803623b4bSBruno Thomsen 		ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
55903623b4bSBruno Thomsen 			PCF2127_BIT_CTRL2_TSF2, 0);
56003623b4bSBruno Thomsen 		if (ret) {
56103623b4bSBruno Thomsen 			dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret);
56203623b4bSBruno Thomsen 			return ret;
56303623b4bSBruno Thomsen 		}
56403623b4bSBruno Thomsen 
56503623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
56603623b4bSBruno Thomsen 		if (ret)
56703623b4bSBruno Thomsen 			return ret;
5682f861984SMian Yousaf Kaukab 	}
56903623b4bSBruno Thomsen 
57003623b4bSBruno Thomsen 	return count;
57103623b4bSBruno Thomsen };
57203623b4bSBruno Thomsen 
57303623b4bSBruno Thomsen static ssize_t timestamp0_show(struct device *dev,
57403623b4bSBruno Thomsen 			       struct device_attribute *attr, char *buf)
57503623b4bSBruno Thomsen {
57603623b4bSBruno Thomsen 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent);
5772f861984SMian Yousaf Kaukab 	unsigned int ctrl1, ctrl2;
57803623b4bSBruno Thomsen 	int ret;
5792f861984SMian Yousaf Kaukab 	time64_t ts;
58003623b4bSBruno Thomsen 
5812f861984SMian Yousaf Kaukab 	if (pcf2127->irq_enabled) {
5822f861984SMian Yousaf Kaukab 		if (!pcf2127->ts_valid)
5832f861984SMian Yousaf Kaukab 			return 0;
5842f861984SMian Yousaf Kaukab 		ts = pcf2127->ts;
5852f861984SMian Yousaf Kaukab 	} else {
5862f861984SMian Yousaf Kaukab 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1);
5872f861984SMian Yousaf Kaukab 		if (ret)
5882f861984SMian Yousaf Kaukab 			return 0;
58903623b4bSBruno Thomsen 
5902f861984SMian Yousaf Kaukab 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2);
5912f861984SMian Yousaf Kaukab 		if (ret)
5922f861984SMian Yousaf Kaukab 			return 0;
5932f861984SMian Yousaf Kaukab 
5942f861984SMian Yousaf Kaukab 		if (!(ctrl1 & PCF2127_BIT_CTRL1_TSF1) &&
5952f861984SMian Yousaf Kaukab 		    !(ctrl2 & PCF2127_BIT_CTRL2_TSF2))
5962f861984SMian Yousaf Kaukab 			return 0;
5972f861984SMian Yousaf Kaukab 
5982f861984SMian Yousaf Kaukab 		ret = pcf2127_rtc_ts_read(dev->parent, &ts);
5992f861984SMian Yousaf Kaukab 		if (ret)
6002f861984SMian Yousaf Kaukab 			return 0;
60103623b4bSBruno Thomsen 
60203623b4bSBruno Thomsen 		ret = pcf2127_wdt_active_ping(&pcf2127->wdd);
60303623b4bSBruno Thomsen 		if (ret)
60403623b4bSBruno Thomsen 			return ret;
6052f861984SMian Yousaf Kaukab 	}
6062f861984SMian Yousaf Kaukab 	return sprintf(buf, "%llu\n", (unsigned long long)ts);
60703623b4bSBruno Thomsen };
60803623b4bSBruno Thomsen 
60903623b4bSBruno Thomsen static DEVICE_ATTR_RW(timestamp0);
61003623b4bSBruno Thomsen 
61103623b4bSBruno Thomsen static struct attribute *pcf2127_attrs[] = {
61203623b4bSBruno Thomsen 	&dev_attr_timestamp0.attr,
61303623b4bSBruno Thomsen 	NULL
61403623b4bSBruno Thomsen };
61503623b4bSBruno Thomsen 
61603623b4bSBruno Thomsen static const struct attribute_group pcf2127_attr_group = {
61703623b4bSBruno Thomsen 	.attrs	= pcf2127_attrs,
61803623b4bSBruno Thomsen };
61903623b4bSBruno Thomsen 
620907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap,
6212843d565SBiwen Li 			 int alarm_irq, const char *name, bool is_pcf2127)
62218cb6368SRenaud Cerrato {
62318cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
624d6c3029fSUwe Kleine-König 	int ret = 0;
62515f57b3eSPhilipp Rosenberger 	unsigned int val;
62618cb6368SRenaud Cerrato 
627907b3262SAkinobu Mita 	dev_dbg(dev, "%s\n", __func__);
62818cb6368SRenaud Cerrato 
629907b3262SAkinobu Mita 	pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
63018cb6368SRenaud Cerrato 	if (!pcf2127)
63118cb6368SRenaud Cerrato 		return -ENOMEM;
63218cb6368SRenaud Cerrato 
633907b3262SAkinobu Mita 	pcf2127->regmap = regmap;
63418cb6368SRenaud Cerrato 
635907b3262SAkinobu Mita 	dev_set_drvdata(dev, pcf2127);
636907b3262SAkinobu Mita 
637e788771cSBruno Thomsen 	pcf2127->rtc = devm_rtc_allocate_device(dev);
638d6c3029fSUwe Kleine-König 	if (IS_ERR(pcf2127->rtc))
639d6c3029fSUwe Kleine-König 		return PTR_ERR(pcf2127->rtc);
64018cb6368SRenaud Cerrato 
641e788771cSBruno Thomsen 	pcf2127->rtc->ops = &pcf2127_rtc_ops;
642b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
643b139bb5cSAlexandre Belloni 	pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
644b139bb5cSAlexandre Belloni 	pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
645bda10273SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features);
646689fafd5SAlexandre Belloni 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features);
64725cbe9c8SAlexandre Belloni 	clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
648e788771cSBruno Thomsen 
64935425bafSBiwen Li 	if (alarm_irq > 0) {
650d4785b46SHugo Villeneuve 		unsigned long flags;
651d4785b46SHugo Villeneuve 
652d4785b46SHugo Villeneuve 		/*
653d4785b46SHugo Villeneuve 		 * If flags = 0, devm_request_threaded_irq() will use IRQ flags
654d4785b46SHugo Villeneuve 		 * obtained from device tree.
655d4785b46SHugo Villeneuve 		 */
656d4785b46SHugo Villeneuve 		if (dev_fwnode(dev))
657d4785b46SHugo Villeneuve 			flags = 0;
658d4785b46SHugo Villeneuve 		else
659d4785b46SHugo Villeneuve 			flags = IRQF_TRIGGER_LOW;
660d4785b46SHugo Villeneuve 
66127006416SAlexandre Belloni 		ret = devm_request_threaded_irq(dev, alarm_irq, NULL,
66227006416SAlexandre Belloni 						pcf2127_rtc_irq,
663d4785b46SHugo Villeneuve 						flags | IRQF_ONESHOT,
6648a914bacSLiam Beguin 						dev_name(dev), dev);
6658a914bacSLiam Beguin 		if (ret) {
6668a914bacSLiam Beguin 			dev_err(dev, "failed to request alarm irq\n");
6678a914bacSLiam Beguin 			return ret;
6688a914bacSLiam Beguin 		}
6692f861984SMian Yousaf Kaukab 		pcf2127->irq_enabled = true;
6708a914bacSLiam Beguin 	}
6718a914bacSLiam Beguin 
67235425bafSBiwen Li 	if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
6738a914bacSLiam Beguin 		device_init_wakeup(dev, true);
67425cbe9c8SAlexandre Belloni 		set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
6758a914bacSLiam Beguin 	}
6768a914bacSLiam Beguin 
6772843d565SBiwen Li 	if (is_pcf2127) {
678d6c3029fSUwe Kleine-König 		struct nvmem_config nvmem_cfg = {
679d6c3029fSUwe Kleine-König 			.priv = pcf2127,
680d6c3029fSUwe Kleine-König 			.reg_read = pcf2127_nvmem_read,
681d6c3029fSUwe Kleine-König 			.reg_write = pcf2127_nvmem_write,
682d6c3029fSUwe Kleine-König 			.size = 512,
683d6c3029fSUwe Kleine-König 		};
684d6c3029fSUwe Kleine-König 
6853a905c2dSBartosz Golaszewski 		ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
686d6c3029fSUwe Kleine-König 	}
687d6c3029fSUwe Kleine-König 
6880e735eaaSBruno Thomsen 	/*
689b9ac079aSPhilipp Rosenberger 	 * The "Power-On Reset Override" facility prevents the RTC to do a reset
690b9ac079aSPhilipp Rosenberger 	 * after power on. For normal operation the PORO must be disabled.
691b9ac079aSPhilipp Rosenberger 	 */
692b9ac079aSPhilipp Rosenberger 	regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1,
693b9ac079aSPhilipp Rosenberger 				PCF2127_BIT_CTRL1_POR_OVRD);
694b9ac079aSPhilipp Rosenberger 
69515f57b3eSPhilipp Rosenberger 	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CLKOUT, &val);
69615f57b3eSPhilipp Rosenberger 	if (ret < 0)
69715f57b3eSPhilipp Rosenberger 		return ret;
69815f57b3eSPhilipp Rosenberger 
69915f57b3eSPhilipp Rosenberger 	if (!(val & PCF2127_BIT_CLKOUT_OTPR)) {
70015f57b3eSPhilipp Rosenberger 		ret = regmap_set_bits(pcf2127->regmap, PCF2127_REG_CLKOUT,
70115f57b3eSPhilipp Rosenberger 				      PCF2127_BIT_CLKOUT_OTPR);
70215f57b3eSPhilipp Rosenberger 		if (ret < 0)
70315f57b3eSPhilipp Rosenberger 			return ret;
70415f57b3eSPhilipp Rosenberger 
70515f57b3eSPhilipp Rosenberger 		msleep(100);
70615f57b3eSPhilipp Rosenberger 	}
70715f57b3eSPhilipp Rosenberger 
708b9ac079aSPhilipp Rosenberger 	/*
7090e735eaaSBruno Thomsen 	 * Watchdog timer enabled and reset pin /RST activated when timed out.
7100e735eaaSBruno Thomsen 	 * Select 1Hz clock source for watchdog timer.
7110e735eaaSBruno Thomsen 	 * Note: Countdown timer disabled and not available.
7122843d565SBiwen Li 	 * For pca2129, pcf2129, only bit[7] is for Symbol WD_CD
7132843d565SBiwen Li 	 * of register watchdg_tim_ctl. The bit[6] is labeled
7142843d565SBiwen Li 	 * as T. Bits labeled as T must always be written with
7152843d565SBiwen Li 	 * logic 0.
7160e735eaaSBruno Thomsen 	 */
7170e735eaaSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL,
7180e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
7190e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD0 |
7200e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1 |
7210e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF0,
7220e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_CD1 |
7232843d565SBiwen Li 				 (is_pcf2127 ? PCF2127_BIT_WD_CTL_CD0 : 0) |
7240e735eaaSBruno Thomsen 				 PCF2127_BIT_WD_CTL_TF1);
7250e735eaaSBruno Thomsen 	if (ret) {
7260e735eaaSBruno Thomsen 		dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
7270e735eaaSBruno Thomsen 		return ret;
7280e735eaaSBruno Thomsen 	}
7290e735eaaSBruno Thomsen 
7305d78533aSUwe Kleine-König 	pcf2127_watchdog_init(dev, pcf2127);
7310e735eaaSBruno Thomsen 
73203623b4bSBruno Thomsen 	/*
73303623b4bSBruno Thomsen 	 * Disable battery low/switch-over timestamp and interrupts.
73403623b4bSBruno Thomsen 	 * Clear battery interrupt flags which can block new trigger events.
73503623b4bSBruno Thomsen 	 * Note: This is the default chip behaviour but added to ensure
73603623b4bSBruno Thomsen 	 * correct tamper timestamp and interrupt function.
73703623b4bSBruno Thomsen 	 */
73803623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
73903623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BTSE |
74003623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BIE |
74103623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL3_BLIE, 0);
74203623b4bSBruno Thomsen 	if (ret) {
74303623b4bSBruno Thomsen 		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
74403623b4bSBruno Thomsen 			__func__);
74503623b4bSBruno Thomsen 		return ret;
74603623b4bSBruno Thomsen 	}
74703623b4bSBruno Thomsen 
74803623b4bSBruno Thomsen 	/*
74903623b4bSBruno Thomsen 	 * Enable timestamp function and store timestamp of first trigger
7507b69b54aSHugo Villeneuve 	 * event until TSF1 and TSF2 interrupt flags are cleared.
75103623b4bSBruno Thomsen 	 */
75203623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL,
75303623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSOFF |
75403623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSM,
75503623b4bSBruno Thomsen 				 PCF2127_BIT_TS_CTRL_TSM);
75603623b4bSBruno Thomsen 	if (ret) {
75703623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n",
75803623b4bSBruno Thomsen 			__func__);
75903623b4bSBruno Thomsen 		return ret;
76003623b4bSBruno Thomsen 	}
76103623b4bSBruno Thomsen 
76203623b4bSBruno Thomsen 	/*
76303623b4bSBruno Thomsen 	 * Enable interrupt generation when TSF1 or TSF2 timestamp flags
76403623b4bSBruno Thomsen 	 * are set. Interrupt signal is an open-drain output and can be
76503623b4bSBruno Thomsen 	 * left floating if unused.
76603623b4bSBruno Thomsen 	 */
76703623b4bSBruno Thomsen 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2,
76803623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL2_TSIE,
76903623b4bSBruno Thomsen 				 PCF2127_BIT_CTRL2_TSIE);
77003623b4bSBruno Thomsen 	if (ret) {
77103623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper detection config (ctrl2) failed\n",
77203623b4bSBruno Thomsen 			__func__);
77303623b4bSBruno Thomsen 		return ret;
77403623b4bSBruno Thomsen 	}
77503623b4bSBruno Thomsen 
77603623b4bSBruno Thomsen 	ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group);
77703623b4bSBruno Thomsen 	if (ret) {
77803623b4bSBruno Thomsen 		dev_err(dev, "%s: tamper sysfs registering failed\n",
77903623b4bSBruno Thomsen 			__func__);
78003623b4bSBruno Thomsen 		return ret;
78103623b4bSBruno Thomsen 	}
78203623b4bSBruno Thomsen 
783fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(pcf2127->rtc);
78418cb6368SRenaud Cerrato }
78518cb6368SRenaud Cerrato 
78618cb6368SRenaud Cerrato #ifdef CONFIG_OF
78718cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
78818cb6368SRenaud Cerrato 	{ .compatible = "nxp,pcf2127" },
789cee2cc21SAkinobu Mita 	{ .compatible = "nxp,pcf2129" },
790985b30dbSLiam Beguin 	{ .compatible = "nxp,pca2129" },
79118cb6368SRenaud Cerrato 	{}
79218cb6368SRenaud Cerrato };
79318cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
79418cb6368SRenaud Cerrato #endif
79518cb6368SRenaud Cerrato 
7969408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C)
7979408ec1aSAkinobu Mita 
798907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count)
799907b3262SAkinobu Mita {
800907b3262SAkinobu Mita 	struct device *dev = context;
801907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
802907b3262SAkinobu Mita 	int ret;
803907b3262SAkinobu Mita 
804907b3262SAkinobu Mita 	ret = i2c_master_send(client, data, count);
805907b3262SAkinobu Mita 	if (ret != count)
806907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
807907b3262SAkinobu Mita 
808907b3262SAkinobu Mita 	return 0;
809907b3262SAkinobu Mita }
810907b3262SAkinobu Mita 
811907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context,
812907b3262SAkinobu Mita 				const void *reg, size_t reg_size,
813907b3262SAkinobu Mita 				const void *val, size_t val_size)
814907b3262SAkinobu Mita {
815907b3262SAkinobu Mita 	struct device *dev = context;
816907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
817907b3262SAkinobu Mita 	int ret;
818907b3262SAkinobu Mita 	void *buf;
819907b3262SAkinobu Mita 
820907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
821907b3262SAkinobu Mita 		return -EINVAL;
822907b3262SAkinobu Mita 
823907b3262SAkinobu Mita 	buf = kmalloc(val_size + 1, GFP_KERNEL);
824907b3262SAkinobu Mita 	if (!buf)
825907b3262SAkinobu Mita 		return -ENOMEM;
826907b3262SAkinobu Mita 
827907b3262SAkinobu Mita 	memcpy(buf, reg, 1);
828907b3262SAkinobu Mita 	memcpy(buf + 1, val, val_size);
829907b3262SAkinobu Mita 
830907b3262SAkinobu Mita 	ret = i2c_master_send(client, buf, val_size + 1);
8319bde0afbSXulin Sun 
8329bde0afbSXulin Sun 	kfree(buf);
8339bde0afbSXulin Sun 
834907b3262SAkinobu Mita 	if (ret != val_size + 1)
835907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
836907b3262SAkinobu Mita 
837907b3262SAkinobu Mita 	return 0;
838907b3262SAkinobu Mita }
839907b3262SAkinobu Mita 
840907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
841907b3262SAkinobu Mita 				void *val, size_t val_size)
842907b3262SAkinobu Mita {
843907b3262SAkinobu Mita 	struct device *dev = context;
844907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
845907b3262SAkinobu Mita 	int ret;
846907b3262SAkinobu Mita 
847907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
848907b3262SAkinobu Mita 		return -EINVAL;
849907b3262SAkinobu Mita 
850907b3262SAkinobu Mita 	ret = i2c_master_send(client, reg, 1);
851907b3262SAkinobu Mita 	if (ret != 1)
852907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
853907b3262SAkinobu Mita 
854907b3262SAkinobu Mita 	ret = i2c_master_recv(client, val, val_size);
855907b3262SAkinobu Mita 	if (ret != val_size)
856907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
857907b3262SAkinobu Mita 
858907b3262SAkinobu Mita 	return 0;
859907b3262SAkinobu Mita }
860907b3262SAkinobu Mita 
861907b3262SAkinobu Mita /*
862907b3262SAkinobu Mita  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
863907b3262SAkinobu Mita  * is that the STOP condition is required between set register address and
864907b3262SAkinobu Mita  * read register data when reading from registers.
865907b3262SAkinobu Mita  */
866907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = {
867907b3262SAkinobu Mita 	.write = pcf2127_i2c_write,
868907b3262SAkinobu Mita 	.gather_write = pcf2127_i2c_gather_write,
869907b3262SAkinobu Mita 	.read = pcf2127_i2c_read,
87018cb6368SRenaud Cerrato };
87118cb6368SRenaud Cerrato 
872907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver;
873907b3262SAkinobu Mita 
8745418e595SUwe Kleine-König static const struct i2c_device_id pcf2127_i2c_id[] = {
8755418e595SUwe Kleine-König 	{ "pcf2127", 1 },
8765418e595SUwe Kleine-König 	{ "pcf2129", 0 },
8775418e595SUwe Kleine-König 	{ "pca2129", 0 },
8785418e595SUwe Kleine-König 	{ }
8795418e595SUwe Kleine-König };
8805418e595SUwe Kleine-König MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
8815418e595SUwe Kleine-König 
8825418e595SUwe Kleine-König static int pcf2127_i2c_probe(struct i2c_client *client)
883907b3262SAkinobu Mita {
8845418e595SUwe Kleine-König 	const struct i2c_device_id *id = i2c_match_id(pcf2127_i2c_id, client);
885907b3262SAkinobu Mita 	struct regmap *regmap;
886907b3262SAkinobu Mita 	static const struct regmap_config config = {
887907b3262SAkinobu Mita 		.reg_bits = 8,
888907b3262SAkinobu Mita 		.val_bits = 8,
889040e6dc0SAlexandre Belloni 		.max_register = 0x1d,
890907b3262SAkinobu Mita 	};
891907b3262SAkinobu Mita 
892907b3262SAkinobu Mita 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
893907b3262SAkinobu Mita 		return -ENODEV;
894907b3262SAkinobu Mita 
895907b3262SAkinobu Mita 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
896907b3262SAkinobu Mita 					&client->dev, &config);
897907b3262SAkinobu Mita 	if (IS_ERR(regmap)) {
898907b3262SAkinobu Mita 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
899907b3262SAkinobu Mita 			__func__, PTR_ERR(regmap));
900907b3262SAkinobu Mita 		return PTR_ERR(regmap);
901907b3262SAkinobu Mita 	}
902907b3262SAkinobu Mita 
90327006416SAlexandre Belloni 	return pcf2127_probe(&client->dev, regmap, client->irq,
904d6c3029fSUwe Kleine-König 			     pcf2127_i2c_driver.driver.name, id->driver_data);
905907b3262SAkinobu Mita }
906907b3262SAkinobu Mita 
907907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = {
908907b3262SAkinobu Mita 	.driver		= {
909907b3262SAkinobu Mita 		.name	= "rtc-pcf2127-i2c",
910907b3262SAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
911907b3262SAkinobu Mita 	},
91231b0cecbSUwe Kleine-König 	.probe		= pcf2127_i2c_probe,
913907b3262SAkinobu Mita 	.id_table	= pcf2127_i2c_id,
914907b3262SAkinobu Mita };
9159408ec1aSAkinobu Mita 
9169408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
9179408ec1aSAkinobu Mita {
9189408ec1aSAkinobu Mita 	return i2c_add_driver(&pcf2127_i2c_driver);
9199408ec1aSAkinobu Mita }
9209408ec1aSAkinobu Mita 
9219408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
9229408ec1aSAkinobu Mita {
9239408ec1aSAkinobu Mita 	i2c_del_driver(&pcf2127_i2c_driver);
9249408ec1aSAkinobu Mita }
9259408ec1aSAkinobu Mita 
9269408ec1aSAkinobu Mita #else
9279408ec1aSAkinobu Mita 
9289408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
9299408ec1aSAkinobu Mita {
9309408ec1aSAkinobu Mita 	return 0;
9319408ec1aSAkinobu Mita }
9329408ec1aSAkinobu Mita 
9339408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
9349408ec1aSAkinobu Mita {
9359408ec1aSAkinobu Mita }
9369408ec1aSAkinobu Mita 
9379408ec1aSAkinobu Mita #endif
9389408ec1aSAkinobu Mita 
9399408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER)
9409408ec1aSAkinobu Mita 
9419408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver;
9429408ec1aSAkinobu Mita 
9439408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi)
9449408ec1aSAkinobu Mita {
9459408ec1aSAkinobu Mita 	static const struct regmap_config config = {
9469408ec1aSAkinobu Mita 		.reg_bits = 8,
9479408ec1aSAkinobu Mita 		.val_bits = 8,
9489408ec1aSAkinobu Mita 		.read_flag_mask = 0xa0,
9499408ec1aSAkinobu Mita 		.write_flag_mask = 0x20,
950040e6dc0SAlexandre Belloni 		.max_register = 0x1d,
9519408ec1aSAkinobu Mita 	};
9529408ec1aSAkinobu Mita 	struct regmap *regmap;
9539408ec1aSAkinobu Mita 
9549408ec1aSAkinobu Mita 	regmap = devm_regmap_init_spi(spi, &config);
9559408ec1aSAkinobu Mita 	if (IS_ERR(regmap)) {
9569408ec1aSAkinobu Mita 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
9579408ec1aSAkinobu Mita 			__func__, PTR_ERR(regmap));
9589408ec1aSAkinobu Mita 		return PTR_ERR(regmap);
9599408ec1aSAkinobu Mita 	}
9609408ec1aSAkinobu Mita 
96127006416SAlexandre Belloni 	return pcf2127_probe(&spi->dev, regmap, spi->irq,
96227006416SAlexandre Belloni 			     pcf2127_spi_driver.driver.name,
963d6c3029fSUwe Kleine-König 			     spi_get_device_id(spi)->driver_data);
9649408ec1aSAkinobu Mita }
9659408ec1aSAkinobu Mita 
9669408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = {
967d6c3029fSUwe Kleine-König 	{ "pcf2127", 1 },
968cee2cc21SAkinobu Mita 	{ "pcf2129", 0 },
969985b30dbSLiam Beguin 	{ "pca2129", 0 },
9709408ec1aSAkinobu Mita 	{ }
9719408ec1aSAkinobu Mita };
9729408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
9739408ec1aSAkinobu Mita 
9749408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = {
9759408ec1aSAkinobu Mita 	.driver		= {
9769408ec1aSAkinobu Mita 		.name	= "rtc-pcf2127-spi",
9779408ec1aSAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
9789408ec1aSAkinobu Mita 	},
9799408ec1aSAkinobu Mita 	.probe		= pcf2127_spi_probe,
9809408ec1aSAkinobu Mita 	.id_table	= pcf2127_spi_id,
9819408ec1aSAkinobu Mita };
9829408ec1aSAkinobu Mita 
9839408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
9849408ec1aSAkinobu Mita {
9859408ec1aSAkinobu Mita 	return spi_register_driver(&pcf2127_spi_driver);
9869408ec1aSAkinobu Mita }
9879408ec1aSAkinobu Mita 
9889408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
9899408ec1aSAkinobu Mita {
9909408ec1aSAkinobu Mita 	spi_unregister_driver(&pcf2127_spi_driver);
9919408ec1aSAkinobu Mita }
9929408ec1aSAkinobu Mita 
9939408ec1aSAkinobu Mita #else
9949408ec1aSAkinobu Mita 
9959408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
9969408ec1aSAkinobu Mita {
9979408ec1aSAkinobu Mita 	return 0;
9989408ec1aSAkinobu Mita }
9999408ec1aSAkinobu Mita 
10009408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
10019408ec1aSAkinobu Mita {
10029408ec1aSAkinobu Mita }
10039408ec1aSAkinobu Mita 
10049408ec1aSAkinobu Mita #endif
10059408ec1aSAkinobu Mita 
10069408ec1aSAkinobu Mita static int __init pcf2127_init(void)
10079408ec1aSAkinobu Mita {
10089408ec1aSAkinobu Mita 	int ret;
10099408ec1aSAkinobu Mita 
10109408ec1aSAkinobu Mita 	ret = pcf2127_i2c_register_driver();
10119408ec1aSAkinobu Mita 	if (ret) {
10129408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
10139408ec1aSAkinobu Mita 		return ret;
10149408ec1aSAkinobu Mita 	}
10159408ec1aSAkinobu Mita 
10169408ec1aSAkinobu Mita 	ret = pcf2127_spi_register_driver();
10179408ec1aSAkinobu Mita 	if (ret) {
10189408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
10199408ec1aSAkinobu Mita 		pcf2127_i2c_unregister_driver();
10209408ec1aSAkinobu Mita 	}
10219408ec1aSAkinobu Mita 
10229408ec1aSAkinobu Mita 	return ret;
10239408ec1aSAkinobu Mita }
10249408ec1aSAkinobu Mita module_init(pcf2127_init)
10259408ec1aSAkinobu Mita 
10269408ec1aSAkinobu Mita static void __exit pcf2127_exit(void)
10279408ec1aSAkinobu Mita {
10289408ec1aSAkinobu Mita 	pcf2127_spi_unregister_driver();
10299408ec1aSAkinobu Mita 	pcf2127_i2c_unregister_driver();
10309408ec1aSAkinobu Mita }
10319408ec1aSAkinobu Mita module_exit(pcf2127_exit)
103218cb6368SRenaud Cerrato 
103318cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
1034cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
10354d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
1036