xref: /openbmc/linux/drivers/rtc/rtc-ab-b5ze-s3.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
181880649SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0+
20b2f6228SArnaud Ebalard /*
30b2f6228SArnaud Ebalard  * rtc-ab-b5ze-s3 - Driver for Abracon AB-RTCMC-32.768Khz-B5ZE-S3
40b2f6228SArnaud Ebalard  *                  I2C RTC / Alarm chip
50b2f6228SArnaud Ebalard  *
60b2f6228SArnaud Ebalard  * Copyright (C) 2014, Arnaud EBALARD <arno@natisbad.org>
70b2f6228SArnaud Ebalard  *
80b2f6228SArnaud Ebalard  * Detailed datasheet of the chip is available here:
90b2f6228SArnaud Ebalard  *
103567d3d1SAlexander A. Klimov  *  https://www.abracon.com/realtimeclock/AB-RTCMC-32.768kHz-B5ZE-S3-Application-Manual.pdf
110b2f6228SArnaud Ebalard  *
120b2f6228SArnaud Ebalard  * This work is based on ISL12057 driver (drivers/rtc/rtc-isl12057.c).
130b2f6228SArnaud Ebalard  *
140b2f6228SArnaud Ebalard  */
150b2f6228SArnaud Ebalard 
160b2f6228SArnaud Ebalard #include <linux/module.h>
170b2f6228SArnaud Ebalard #include <linux/rtc.h>
180b2f6228SArnaud Ebalard #include <linux/i2c.h>
190b2f6228SArnaud Ebalard #include <linux/bcd.h>
200b2f6228SArnaud Ebalard #include <linux/of.h>
210b2f6228SArnaud Ebalard #include <linux/regmap.h>
220b2f6228SArnaud Ebalard #include <linux/interrupt.h>
230b2f6228SArnaud Ebalard 
240b2f6228SArnaud Ebalard #define DRV_NAME "rtc-ab-b5ze-s3"
250b2f6228SArnaud Ebalard 
260b2f6228SArnaud Ebalard /* Control section */
270b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1	   0x00	   /* Control 1 register */
280b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_CIE	   BIT(0)  /* Pulse interrupt enable */
290b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_AIE	   BIT(1)  /* Alarm interrupt enable */
300b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_SIE	   BIT(2)  /* Second interrupt enable */
310b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_PM	   BIT(3)  /* 24h/12h mode */
320b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_SR	   BIT(4)  /* Software reset */
330b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_STOP	   BIT(5)  /* RTC circuit enable */
340b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_CAP	   BIT(7)
350b2f6228SArnaud Ebalard 
360b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2	   0x01	   /* Control 2 register */
370b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTBIE   BIT(0)  /* Countdown timer B int. enable */
380b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTAIE   BIT(1)  /* Countdown timer A int. enable */
390b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_WTAIE   BIT(2)  /* Watchdog timer A int. enable */
400b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_AF	   BIT(3)  /* Alarm interrupt status */
410b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_SF	   BIT(4)  /* Second interrupt status */
420b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTBF	   BIT(5)  /* Countdown timer B int. status */
430b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTAF	   BIT(6)  /* Countdown timer A int. status */
440b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_WTAF	   BIT(7)  /* Watchdog timer A int. status */
450b2f6228SArnaud Ebalard 
460b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3	   0x02	   /* Control 3 register */
470b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM2	   BIT(7)  /* Power Management bit 2 */
480b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM1	   BIT(6)  /* Power Management bit 1 */
490b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM0	   BIT(5)  /* Power Management bit 0 */
500b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BSF	   BIT(3)  /* Battery switchover int. status */
510b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BLF	   BIT(2)  /* Battery low int. status */
520b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BSIE	   BIT(1)  /* Battery switchover int. enable */
530b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BLIE	   BIT(0)  /* Battery low int. enable */
540b2f6228SArnaud Ebalard 
550b2f6228SArnaud Ebalard #define ABB5ZES3_CTRL_SEC_LEN	   3
560b2f6228SArnaud Ebalard 
570b2f6228SArnaud Ebalard /* RTC section */
580b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_SC	   0x03	   /* RTC Seconds register */
590b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_SC_OSC	   BIT(7)  /* Clock integrity status */
600b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_MN	   0x04	   /* RTC Minutes register */
610b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_HR	   0x05	   /* RTC Hours register */
620b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_HR_PM	   BIT(5)  /* RTC Hours PM bit */
630b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_DT	   0x06	   /* RTC Date register */
640b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_DW	   0x07	   /* RTC Day of the week register */
650b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_MO	   0x08	   /* RTC Month register */
660b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_YR	   0x09	   /* RTC Year register */
670b2f6228SArnaud Ebalard 
680b2f6228SArnaud Ebalard #define ABB5ZES3_RTC_SEC_LEN	   7
690b2f6228SArnaud Ebalard 
700b2f6228SArnaud Ebalard /* Alarm section (enable bits are all active low) */
710b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_MN	   0x0A	   /* Alarm - minute register */
720b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_MN_AE	   BIT(7)  /* Minute enable */
730b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_HR	   0x0B	   /* Alarm - hours register */
740b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_HR_AE	   BIT(7)  /* Hour enable */
750b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DT	   0x0C	   /* Alarm - date register */
760b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DT_AE	   BIT(7)  /* Date (day of the month) enable */
770b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DW	   0x0D	   /* Alarm - day of the week reg. */
780b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DW_AE	   BIT(7)  /* Day of the week enable */
790b2f6228SArnaud Ebalard 
800b2f6228SArnaud Ebalard #define ABB5ZES3_ALRM_SEC_LEN	   4
810b2f6228SArnaud Ebalard 
820b2f6228SArnaud Ebalard /* Frequency offset section */
830b2f6228SArnaud Ebalard #define ABB5ZES3_REG_FREQ_OF	   0x0E	   /* Frequency offset register */
840b2f6228SArnaud Ebalard #define ABB5ZES3_REG_FREQ_OF_MODE  0x0E	   /* Offset mode: 2 hours / minute */
850b2f6228SArnaud Ebalard 
860b2f6228SArnaud Ebalard /* CLOCKOUT section */
870b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK	   0x0F	   /* Timer & Clockout register */
880b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAM   BIT(7)  /* Permanent/pulsed timer A/int. 2 */
890b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TBM   BIT(6)  /* Permanent/pulsed timer B */
900b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF2  BIT(5)  /* Clkout Freq bit 2 */
910b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF1  BIT(4)  /* Clkout Freq bit 1 */
920b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF0  BIT(3)  /* Clkout Freq bit 0 */
930b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAC1  BIT(2)  /* Timer A: - 01 : countdown */
940b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAC0  BIT(1)  /*	       - 10 : timer	*/
950b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TBC   BIT(0)  /* Timer B enable */
960b2f6228SArnaud Ebalard 
970b2f6228SArnaud Ebalard /* Timer A Section */
980b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK	   0x10	   /* Timer A clock register */
990b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ2 BIT(2)  /* Freq bit 2 */
1000b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ1 BIT(1)  /* Freq bit 1 */
1010b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ0 BIT(0)  /* Freq bit 0 */
1020b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA	   0x11	   /* Timer A register */
1030b2f6228SArnaud Ebalard 
1040b2f6228SArnaud Ebalard #define ABB5ZES3_TIMA_SEC_LEN	   2
1050b2f6228SArnaud Ebalard 
1060b2f6228SArnaud Ebalard /* Timer B Section */
1070b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK	   0x12	   /* Timer B clock register */
1080b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW2 BIT(6)
1090b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW1 BIT(5)
1100b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW0 BIT(4)
1110b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ2 BIT(2)
1120b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ1 BIT(1)
1130b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ0 BIT(0)
1140b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB	   0x13	   /* Timer B register */
1150b2f6228SArnaud Ebalard #define ABB5ZES3_TIMB_SEC_LEN	   2
1160b2f6228SArnaud Ebalard 
1170b2f6228SArnaud Ebalard #define ABB5ZES3_MEM_MAP_LEN	   0x14
1180b2f6228SArnaud Ebalard 
1190b2f6228SArnaud Ebalard struct abb5zes3_rtc_data {
1200b2f6228SArnaud Ebalard 	struct rtc_device *rtc;
1210b2f6228SArnaud Ebalard 	struct regmap *regmap;
1220b2f6228SArnaud Ebalard 
1230b2f6228SArnaud Ebalard 	int irq;
1240b2f6228SArnaud Ebalard 
1250b2f6228SArnaud Ebalard 	bool battery_low;
126c8a1d8a5SArnaud Ebalard 	bool timer_alarm; /* current alarm is via timer A */
1270b2f6228SArnaud Ebalard };
1280b2f6228SArnaud Ebalard 
1290b2f6228SArnaud Ebalard /*
1300b2f6228SArnaud Ebalard  * Try and match register bits w/ fixed null values to see whether we
131ac246738SAlexandre Belloni  * are dealing with an ABB5ZES3.
1320b2f6228SArnaud Ebalard  */
abb5zes3_i2c_validate_chip(struct regmap * regmap)1330b2f6228SArnaud Ebalard static int abb5zes3_i2c_validate_chip(struct regmap *regmap)
1340b2f6228SArnaud Ebalard {
1350b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_MEM_MAP_LEN];
1360b2f6228SArnaud Ebalard 	static const u8 mask[ABB5ZES3_MEM_MAP_LEN] = { 0x00, 0x00, 0x10, 0x00,
1370b2f6228SArnaud Ebalard 						       0x80, 0xc0, 0xc0, 0xf8,
1380b2f6228SArnaud Ebalard 						       0xe0, 0x00, 0x00, 0x40,
1390b2f6228SArnaud Ebalard 						       0x40, 0x78, 0x00, 0x00,
1400b2f6228SArnaud Ebalard 						       0xf8, 0x00, 0x88, 0x00 };
1410b2f6228SArnaud Ebalard 	int ret, i;
1420b2f6228SArnaud Ebalard 
1430b2f6228SArnaud Ebalard 	ret = regmap_bulk_read(regmap, 0, regs, ABB5ZES3_MEM_MAP_LEN);
1440b2f6228SArnaud Ebalard 	if (ret)
1450b2f6228SArnaud Ebalard 		return ret;
1460b2f6228SArnaud Ebalard 
1470b2f6228SArnaud Ebalard 	for (i = 0; i < ABB5ZES3_MEM_MAP_LEN; ++i) {
1480b2f6228SArnaud Ebalard 		if (regs[i] & mask[i]) /* check if bits are cleared */
1490b2f6228SArnaud Ebalard 			return -ENODEV;
1500b2f6228SArnaud Ebalard 	}
1510b2f6228SArnaud Ebalard 
1520b2f6228SArnaud Ebalard 	return 0;
1530b2f6228SArnaud Ebalard }
1540b2f6228SArnaud Ebalard 
1550b2f6228SArnaud Ebalard /* Clear alarm status bit. */
_abb5zes3_rtc_clear_alarm(struct device * dev)1560b2f6228SArnaud Ebalard static int _abb5zes3_rtc_clear_alarm(struct device *dev)
1570b2f6228SArnaud Ebalard {
1580b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
1590b2f6228SArnaud Ebalard 	int ret;
1600b2f6228SArnaud Ebalard 
1610b2f6228SArnaud Ebalard 	ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2,
1620b2f6228SArnaud Ebalard 				 ABB5ZES3_REG_CTRL2_AF, 0);
1630b2f6228SArnaud Ebalard 	if (ret)
1640b2f6228SArnaud Ebalard 		dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret);
1650b2f6228SArnaud Ebalard 
1660b2f6228SArnaud Ebalard 	return ret;
1670b2f6228SArnaud Ebalard }
1680b2f6228SArnaud Ebalard 
1690b2f6228SArnaud Ebalard /* Enable or disable alarm (i.e. alarm interrupt generation) */
_abb5zes3_rtc_update_alarm(struct device * dev,bool enable)1700b2f6228SArnaud Ebalard static int _abb5zes3_rtc_update_alarm(struct device *dev, bool enable)
1710b2f6228SArnaud Ebalard {
1720b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
1730b2f6228SArnaud Ebalard 	int ret;
1740b2f6228SArnaud Ebalard 
1750b2f6228SArnaud Ebalard 	ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL1,
1760b2f6228SArnaud Ebalard 				 ABB5ZES3_REG_CTRL1_AIE,
1770b2f6228SArnaud Ebalard 				 enable ? ABB5ZES3_REG_CTRL1_AIE : 0);
1780b2f6228SArnaud Ebalard 	if (ret)
1790b2f6228SArnaud Ebalard 		dev_err(dev, "%s: writing alarm INT failed (%d)\n",
1800b2f6228SArnaud Ebalard 			__func__, ret);
1810b2f6228SArnaud Ebalard 
1820b2f6228SArnaud Ebalard 	return ret;
1830b2f6228SArnaud Ebalard }
1840b2f6228SArnaud Ebalard 
185c8a1d8a5SArnaud Ebalard /* Enable or disable timer (watchdog timer A interrupt generation) */
_abb5zes3_rtc_update_timer(struct device * dev,bool enable)186c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_update_timer(struct device *dev, bool enable)
187c8a1d8a5SArnaud Ebalard {
188c8a1d8a5SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
189c8a1d8a5SArnaud Ebalard 	int ret;
190c8a1d8a5SArnaud Ebalard 
191c8a1d8a5SArnaud Ebalard 	ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2,
192c8a1d8a5SArnaud Ebalard 				 ABB5ZES3_REG_CTRL2_WTAIE,
193c8a1d8a5SArnaud Ebalard 				 enable ? ABB5ZES3_REG_CTRL2_WTAIE : 0);
194c8a1d8a5SArnaud Ebalard 	if (ret)
195c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: writing timer INT failed (%d)\n",
196c8a1d8a5SArnaud Ebalard 			__func__, ret);
197c8a1d8a5SArnaud Ebalard 
198c8a1d8a5SArnaud Ebalard 	return ret;
199c8a1d8a5SArnaud Ebalard }
200c8a1d8a5SArnaud Ebalard 
2010b2f6228SArnaud Ebalard /*
2020b2f6228SArnaud Ebalard  * Note: we only read, so regmap inner lock protection is sufficient, i.e.
2030b2f6228SArnaud Ebalard  * we do not need driver's main lock protection.
2040b2f6228SArnaud Ebalard  */
_abb5zes3_rtc_read_time(struct device * dev,struct rtc_time * tm)2050b2f6228SArnaud Ebalard static int _abb5zes3_rtc_read_time(struct device *dev, struct rtc_time *tm)
2060b2f6228SArnaud Ebalard {
2070b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
2080b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN];
209ce2e5a76SAlexandre Belloni 	int ret = 0;
2100b2f6228SArnaud Ebalard 
2110b2f6228SArnaud Ebalard 	/*
2120b2f6228SArnaud Ebalard 	 * As we need to read CTRL1 register anyway to access 24/12h
2130b2f6228SArnaud Ebalard 	 * mode bit, we do a single bulk read of both control and RTC
2140b2f6228SArnaud Ebalard 	 * sections (they are consecutive). This also ease indexing
2150b2f6228SArnaud Ebalard 	 * of register values after bulk read.
2160b2f6228SArnaud Ebalard 	 */
2170b2f6228SArnaud Ebalard 	ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_CTRL1, regs,
2180b2f6228SArnaud Ebalard 			       sizeof(regs));
2190b2f6228SArnaud Ebalard 	if (ret) {
2200b2f6228SArnaud Ebalard 		dev_err(dev, "%s: reading RTC time failed (%d)\n",
2210b2f6228SArnaud Ebalard 			__func__, ret);
2225d049837SAlexandre Belloni 		return ret;
2230b2f6228SArnaud Ebalard 	}
2240b2f6228SArnaud Ebalard 
2250b2f6228SArnaud Ebalard 	/* If clock integrity is not guaranteed, do not return a time value */
2265d049837SAlexandre Belloni 	if (regs[ABB5ZES3_REG_RTC_SC] & ABB5ZES3_REG_RTC_SC_OSC)
2275d049837SAlexandre Belloni 		return -ENODATA;
2280b2f6228SArnaud Ebalard 
2290b2f6228SArnaud Ebalard 	tm->tm_sec = bcd2bin(regs[ABB5ZES3_REG_RTC_SC] & 0x7F);
2300b2f6228SArnaud Ebalard 	tm->tm_min = bcd2bin(regs[ABB5ZES3_REG_RTC_MN]);
2310b2f6228SArnaud Ebalard 
2320b2f6228SArnaud Ebalard 	if (regs[ABB5ZES3_REG_CTRL1] & ABB5ZES3_REG_CTRL1_PM) { /* 12hr mode */
2330b2f6228SArnaud Ebalard 		tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR] & 0x1f);
2340b2f6228SArnaud Ebalard 		if (regs[ABB5ZES3_REG_RTC_HR] & ABB5ZES3_REG_RTC_HR_PM) /* PM */
2350b2f6228SArnaud Ebalard 			tm->tm_hour += 12;
2360b2f6228SArnaud Ebalard 	} else {						/* 24hr mode */
2370b2f6228SArnaud Ebalard 		tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR]);
2380b2f6228SArnaud Ebalard 	}
2390b2f6228SArnaud Ebalard 
2400b2f6228SArnaud Ebalard 	tm->tm_mday = bcd2bin(regs[ABB5ZES3_REG_RTC_DT]);
2410b2f6228SArnaud Ebalard 	tm->tm_wday = bcd2bin(regs[ABB5ZES3_REG_RTC_DW]);
2420b2f6228SArnaud Ebalard 	tm->tm_mon  = bcd2bin(regs[ABB5ZES3_REG_RTC_MO]) - 1; /* starts at 1 */
2430b2f6228SArnaud Ebalard 	tm->tm_year = bcd2bin(regs[ABB5ZES3_REG_RTC_YR]) + 100;
2440b2f6228SArnaud Ebalard 
2450b2f6228SArnaud Ebalard 	return ret;
2460b2f6228SArnaud Ebalard }
2470b2f6228SArnaud Ebalard 
abb5zes3_rtc_set_time(struct device * dev,struct rtc_time * tm)2480b2f6228SArnaud Ebalard static int abb5zes3_rtc_set_time(struct device *dev, struct rtc_time *tm)
2490b2f6228SArnaud Ebalard {
2500b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
2510b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN];
2520b2f6228SArnaud Ebalard 	int ret;
2530b2f6228SArnaud Ebalard 
2540b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_SC] = bin2bcd(tm->tm_sec); /* MSB=0 clears OSC */
2550b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_MN] = bin2bcd(tm->tm_min);
2560b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
2570b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_DT] = bin2bcd(tm->tm_mday);
2580b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_DW] = bin2bcd(tm->tm_wday);
2590b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1);
2600b2f6228SArnaud Ebalard 	regs[ABB5ZES3_REG_RTC_YR] = bin2bcd(tm->tm_year - 100);
2610b2f6228SArnaud Ebalard 
2620b2f6228SArnaud Ebalard 	ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_RTC_SC,
2630b2f6228SArnaud Ebalard 				regs + ABB5ZES3_REG_RTC_SC,
2640b2f6228SArnaud Ebalard 				ABB5ZES3_RTC_SEC_LEN);
2650b2f6228SArnaud Ebalard 
2660b2f6228SArnaud Ebalard 	return ret;
2670b2f6228SArnaud Ebalard }
2680b2f6228SArnaud Ebalard 
269c8a1d8a5SArnaud Ebalard /*
270c8a1d8a5SArnaud Ebalard  * Set provided TAQ and Timer A registers (TIMA_CLK and TIMA) based on
271c8a1d8a5SArnaud Ebalard  * given number of seconds.
272c8a1d8a5SArnaud Ebalard  */
sec_to_timer_a(u8 secs,u8 * taq,u8 * timer_a)273c8a1d8a5SArnaud Ebalard static inline void sec_to_timer_a(u8 secs, u8 *taq, u8 *timer_a)
274c8a1d8a5SArnaud Ebalard {
275c8a1d8a5SArnaud Ebalard 	*taq = ABB5ZES3_REG_TIMA_CLK_TAQ1; /* 1Hz */
276c8a1d8a5SArnaud Ebalard 	*timer_a = secs;
277c8a1d8a5SArnaud Ebalard }
278c8a1d8a5SArnaud Ebalard 
279c8a1d8a5SArnaud Ebalard /*
280c8a1d8a5SArnaud Ebalard  * Return current number of seconds in Timer A. As we only use
281c8a1d8a5SArnaud Ebalard  * timer A with a 1Hz freq, this is what we expect to have.
282c8a1d8a5SArnaud Ebalard  */
sec_from_timer_a(u8 * secs,u8 taq,u8 timer_a)283c8a1d8a5SArnaud Ebalard static inline int sec_from_timer_a(u8 *secs, u8 taq, u8 timer_a)
284c8a1d8a5SArnaud Ebalard {
285c8a1d8a5SArnaud Ebalard 	if (taq != ABB5ZES3_REG_TIMA_CLK_TAQ1) /* 1Hz */
286c8a1d8a5SArnaud Ebalard 		return -EINVAL;
287c8a1d8a5SArnaud Ebalard 
288c8a1d8a5SArnaud Ebalard 	*secs = timer_a;
289c8a1d8a5SArnaud Ebalard 
290c8a1d8a5SArnaud Ebalard 	return 0;
291c8a1d8a5SArnaud Ebalard }
292c8a1d8a5SArnaud Ebalard 
293c8a1d8a5SArnaud Ebalard /*
294c8a1d8a5SArnaud Ebalard  * Read alarm currently configured via a watchdog timer using timer A. This
295c8a1d8a5SArnaud Ebalard  * is done by reading current RTC time and adding remaining timer time.
296c8a1d8a5SArnaud Ebalard  */
_abb5zes3_rtc_read_timer(struct device * dev,struct rtc_wkalrm * alarm)297c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_read_timer(struct device *dev,
298c8a1d8a5SArnaud Ebalard 				    struct rtc_wkalrm *alarm)
299c8a1d8a5SArnaud Ebalard {
300c8a1d8a5SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
301c8a1d8a5SArnaud Ebalard 	struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
302c8a1d8a5SArnaud Ebalard 	u8 regs[ABB5ZES3_TIMA_SEC_LEN + 1];
303c8a1d8a5SArnaud Ebalard 	unsigned long rtc_secs;
304c8a1d8a5SArnaud Ebalard 	unsigned int reg;
305c8a1d8a5SArnaud Ebalard 	u8 timer_secs;
306c8a1d8a5SArnaud Ebalard 	int ret;
307c8a1d8a5SArnaud Ebalard 
308c8a1d8a5SArnaud Ebalard 	/*
309c8a1d8a5SArnaud Ebalard 	 * Instead of doing two separate calls, because they are consecutive,
310c8a1d8a5SArnaud Ebalard 	 * we grab both clockout register and Timer A section. The latter is
311c8a1d8a5SArnaud Ebalard 	 * used to decide if timer A is enabled (as a watchdog timer).
312c8a1d8a5SArnaud Ebalard 	 */
313c8a1d8a5SArnaud Ebalard 	ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_TIM_CLK, regs,
314c8a1d8a5SArnaud Ebalard 			       ABB5ZES3_TIMA_SEC_LEN + 1);
315c8a1d8a5SArnaud Ebalard 	if (ret) {
316c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: reading Timer A section failed (%d)\n",
317c8a1d8a5SArnaud Ebalard 			__func__, ret);
3185d049837SAlexandre Belloni 		return ret;
319c8a1d8a5SArnaud Ebalard 	}
320c8a1d8a5SArnaud Ebalard 
321c8a1d8a5SArnaud Ebalard 	/* get current time ... */
322c8a1d8a5SArnaud Ebalard 	ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
323c8a1d8a5SArnaud Ebalard 	if (ret)
3245d049837SAlexandre Belloni 		return ret;
325c8a1d8a5SArnaud Ebalard 
326c8a1d8a5SArnaud Ebalard 	/* ... convert to seconds ... */
3278a941124SAlexandre Belloni 	rtc_secs = rtc_tm_to_time64(&rtc_tm);
328c8a1d8a5SArnaud Ebalard 
329c8a1d8a5SArnaud Ebalard 	/* ... add remaining timer A time ... */
330c8a1d8a5SArnaud Ebalard 	ret = sec_from_timer_a(&timer_secs, regs[1], regs[2]);
331c8a1d8a5SArnaud Ebalard 	if (ret)
3325d049837SAlexandre Belloni 		return ret;
333c8a1d8a5SArnaud Ebalard 
334c8a1d8a5SArnaud Ebalard 	/* ... and convert back. */
3358a941124SAlexandre Belloni 	rtc_time64_to_tm(rtc_secs + timer_secs, alarm_tm);
336c8a1d8a5SArnaud Ebalard 
337c8a1d8a5SArnaud Ebalard 	ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL2, &reg);
338c8a1d8a5SArnaud Ebalard 	if (ret) {
339c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: reading ctrl reg failed (%d)\n",
340c8a1d8a5SArnaud Ebalard 			__func__, ret);
3415d049837SAlexandre Belloni 		return ret;
342c8a1d8a5SArnaud Ebalard 	}
343c8a1d8a5SArnaud Ebalard 
344c8a1d8a5SArnaud Ebalard 	alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL2_WTAIE);
345c8a1d8a5SArnaud Ebalard 
3465d049837SAlexandre Belloni 	return 0;
347c8a1d8a5SArnaud Ebalard }
348c8a1d8a5SArnaud Ebalard 
349c8a1d8a5SArnaud Ebalard /* Read alarm currently configured via a RTC alarm registers. */
_abb5zes3_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)350c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_read_alarm(struct device *dev,
351c8a1d8a5SArnaud Ebalard 				    struct rtc_wkalrm *alarm)
3520b2f6228SArnaud Ebalard {
3530b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
3540b2f6228SArnaud Ebalard 	struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
3550b2f6228SArnaud Ebalard 	unsigned long rtc_secs, alarm_secs;
3560b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_ALRM_SEC_LEN];
3570b2f6228SArnaud Ebalard 	unsigned int reg;
3580b2f6228SArnaud Ebalard 	int ret;
3590b2f6228SArnaud Ebalard 
3600b2f6228SArnaud Ebalard 	ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_ALRM_MN, regs,
3610b2f6228SArnaud Ebalard 			       ABB5ZES3_ALRM_SEC_LEN);
3620b2f6228SArnaud Ebalard 	if (ret) {
3630b2f6228SArnaud Ebalard 		dev_err(dev, "%s: reading alarm section failed (%d)\n",
3640b2f6228SArnaud Ebalard 			__func__, ret);
3655d049837SAlexandre Belloni 		return ret;
3660b2f6228SArnaud Ebalard 	}
3670b2f6228SArnaud Ebalard 
3680b2f6228SArnaud Ebalard 	alarm_tm->tm_sec  = 0;
3690b2f6228SArnaud Ebalard 	alarm_tm->tm_min  = bcd2bin(regs[0] & 0x7f);
3700b2f6228SArnaud Ebalard 	alarm_tm->tm_hour = bcd2bin(regs[1] & 0x3f);
3710b2f6228SArnaud Ebalard 	alarm_tm->tm_mday = bcd2bin(regs[2] & 0x3f);
3720b2f6228SArnaud Ebalard 	alarm_tm->tm_wday = -1;
3730b2f6228SArnaud Ebalard 
3740b2f6228SArnaud Ebalard 	/*
3750b2f6228SArnaud Ebalard 	 * The alarm section does not store year/month. We use the ones in rtc
3760b2f6228SArnaud Ebalard 	 * section as a basis and increment month and then year if needed to get
3770b2f6228SArnaud Ebalard 	 * alarm after current time.
3780b2f6228SArnaud Ebalard 	 */
3790b2f6228SArnaud Ebalard 	ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
3800b2f6228SArnaud Ebalard 	if (ret)
3815d049837SAlexandre Belloni 		return ret;
3820b2f6228SArnaud Ebalard 
3830b2f6228SArnaud Ebalard 	alarm_tm->tm_year = rtc_tm.tm_year;
3840b2f6228SArnaud Ebalard 	alarm_tm->tm_mon = rtc_tm.tm_mon;
3850b2f6228SArnaud Ebalard 
3868a941124SAlexandre Belloni 	rtc_secs = rtc_tm_to_time64(&rtc_tm);
3878a941124SAlexandre Belloni 	alarm_secs = rtc_tm_to_time64(alarm_tm);
3880b2f6228SArnaud Ebalard 
3890b2f6228SArnaud Ebalard 	if (alarm_secs < rtc_secs) {
3900b2f6228SArnaud Ebalard 		if (alarm_tm->tm_mon == 11) {
3910b2f6228SArnaud Ebalard 			alarm_tm->tm_mon = 0;
3920b2f6228SArnaud Ebalard 			alarm_tm->tm_year += 1;
3930b2f6228SArnaud Ebalard 		} else {
3940b2f6228SArnaud Ebalard 			alarm_tm->tm_mon += 1;
3950b2f6228SArnaud Ebalard 		}
3960b2f6228SArnaud Ebalard 	}
3970b2f6228SArnaud Ebalard 
3980b2f6228SArnaud Ebalard 	ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL1, &reg);
3990b2f6228SArnaud Ebalard 	if (ret) {
4000b2f6228SArnaud Ebalard 		dev_err(dev, "%s: reading ctrl reg failed (%d)\n",
4010b2f6228SArnaud Ebalard 			__func__, ret);
4025d049837SAlexandre Belloni 		return ret;
4030b2f6228SArnaud Ebalard 	}
4040b2f6228SArnaud Ebalard 
4050b2f6228SArnaud Ebalard 	alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL1_AIE);
4060b2f6228SArnaud Ebalard 
4075d049837SAlexandre Belloni 	return 0;
408c8a1d8a5SArnaud Ebalard }
409c8a1d8a5SArnaud Ebalard 
410c8a1d8a5SArnaud Ebalard /*
411c8a1d8a5SArnaud Ebalard  * As the Alarm mechanism supported by the chip is only accurate to the
412c8a1d8a5SArnaud Ebalard  * minute, we use the watchdog timer mechanism provided by timer A
413c8a1d8a5SArnaud Ebalard  * (up to 256 seconds w/ a second accuracy) for low alarm values (below
414c8a1d8a5SArnaud Ebalard  * 4 minutes). Otherwise, we use the common alarm mechanism provided
415c8a1d8a5SArnaud Ebalard  * by the chip. In order for that to work, we keep track of currently
416c8a1d8a5SArnaud Ebalard  * configured timer type via 'timer_alarm' flag in our private data
417c8a1d8a5SArnaud Ebalard  * structure.
418c8a1d8a5SArnaud Ebalard  */
abb5zes3_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)419c8a1d8a5SArnaud Ebalard static int abb5zes3_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
420c8a1d8a5SArnaud Ebalard {
421c8a1d8a5SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
422c8a1d8a5SArnaud Ebalard 	int ret;
423c8a1d8a5SArnaud Ebalard 
424c8a1d8a5SArnaud Ebalard 	if (data->timer_alarm)
425c8a1d8a5SArnaud Ebalard 		ret = _abb5zes3_rtc_read_timer(dev, alarm);
426c8a1d8a5SArnaud Ebalard 	else
427c8a1d8a5SArnaud Ebalard 		ret = _abb5zes3_rtc_read_alarm(dev, alarm);
4280b2f6228SArnaud Ebalard 
4290b2f6228SArnaud Ebalard 	return ret;
4300b2f6228SArnaud Ebalard }
4310b2f6228SArnaud Ebalard 
432c8a1d8a5SArnaud Ebalard /*
433c8a1d8a5SArnaud Ebalard  * Set alarm using chip alarm mechanism. It is only accurate to the
434c8a1d8a5SArnaud Ebalard  * minute (not the second). The function expects alarm interrupt to
435c8a1d8a5SArnaud Ebalard  * be disabled.
436c8a1d8a5SArnaud Ebalard  */
_abb5zes3_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)437c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
4380b2f6228SArnaud Ebalard {
4390b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
4400b2f6228SArnaud Ebalard 	struct rtc_time *alarm_tm = &alarm->time;
4410b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_ALRM_SEC_LEN];
4420b2f6228SArnaud Ebalard 	struct rtc_time rtc_tm;
4430b2f6228SArnaud Ebalard 	int ret, enable = 1;
4440b2f6228SArnaud Ebalard 
44533fee143SAlexandre Belloni 	if (!alarm->enabled) {
4460b2f6228SArnaud Ebalard 		enable = 0;
4470b2f6228SArnaud Ebalard 	} else {
44833fee143SAlexandre Belloni 		unsigned long rtc_secs, alarm_secs;
44933fee143SAlexandre Belloni 
4500b2f6228SArnaud Ebalard 		/*
4510b2f6228SArnaud Ebalard 		 * Chip only support alarms up to one month in the future. Let's
4520b2f6228SArnaud Ebalard 		 * return an error if we get something after that limit.
4530b2f6228SArnaud Ebalard 		 * Comparison is done by incrementing rtc_tm month field by one
4540b2f6228SArnaud Ebalard 		 * and checking alarm value is still below.
4550b2f6228SArnaud Ebalard 		 */
45633fee143SAlexandre Belloni 		ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
45733fee143SAlexandre Belloni 		if (ret)
45833fee143SAlexandre Belloni 			return ret;
45933fee143SAlexandre Belloni 
4600b2f6228SArnaud Ebalard 		if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
4610b2f6228SArnaud Ebalard 			rtc_tm.tm_mon = 0;
4620b2f6228SArnaud Ebalard 			rtc_tm.tm_year += 1;
4630b2f6228SArnaud Ebalard 		} else {
4640b2f6228SArnaud Ebalard 			rtc_tm.tm_mon += 1;
4650b2f6228SArnaud Ebalard 		}
4660b2f6228SArnaud Ebalard 
4678a941124SAlexandre Belloni 		rtc_secs = rtc_tm_to_time64(&rtc_tm);
46833fee143SAlexandre Belloni 		alarm_secs = rtc_tm_to_time64(alarm_tm);
4690b2f6228SArnaud Ebalard 
4700b2f6228SArnaud Ebalard 		if (alarm_secs > rtc_secs) {
4719c3ab855SAlexandre Belloni 			dev_err(dev, "%s: alarm maximum is one month in the future (%d)\n",
4729c3ab855SAlexandre Belloni 				__func__, ret);
4735d049837SAlexandre Belloni 			return -EINVAL;
4740b2f6228SArnaud Ebalard 		}
4750b2f6228SArnaud Ebalard 	}
4760b2f6228SArnaud Ebalard 
477c8a1d8a5SArnaud Ebalard 	/*
478c8a1d8a5SArnaud Ebalard 	 * Program all alarm registers but DW one. For each register, setting
479c8a1d8a5SArnaud Ebalard 	 * MSB to 0 enables associated alarm.
480c8a1d8a5SArnaud Ebalard 	 */
481c8a1d8a5SArnaud Ebalard 	regs[0] = bin2bcd(alarm_tm->tm_min) & 0x7f;
482c8a1d8a5SArnaud Ebalard 	regs[1] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
483c8a1d8a5SArnaud Ebalard 	regs[2] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
4840b2f6228SArnaud Ebalard 	regs[3] = ABB5ZES3_REG_ALRM_DW_AE; /* do not match day of the week */
4850b2f6228SArnaud Ebalard 
4860b2f6228SArnaud Ebalard 	ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_ALRM_MN, regs,
4870b2f6228SArnaud Ebalard 				ABB5ZES3_ALRM_SEC_LEN);
4880b2f6228SArnaud Ebalard 	if (ret < 0) {
4890b2f6228SArnaud Ebalard 		dev_err(dev, "%s: writing ALARM section failed (%d)\n",
4900b2f6228SArnaud Ebalard 			__func__, ret);
4915d049837SAlexandre Belloni 		return ret;
4920b2f6228SArnaud Ebalard 	}
4930b2f6228SArnaud Ebalard 
494c8a1d8a5SArnaud Ebalard 	/* Record currently configured alarm is not a timer */
495c8a1d8a5SArnaud Ebalard 	data->timer_alarm = 0;
496c8a1d8a5SArnaud Ebalard 
497c8a1d8a5SArnaud Ebalard 	/* Enable or disable alarm interrupt generation */
4985d049837SAlexandre Belloni 	return _abb5zes3_rtc_update_alarm(dev, enable);
499c8a1d8a5SArnaud Ebalard }
500c8a1d8a5SArnaud Ebalard 
501c8a1d8a5SArnaud Ebalard /*
502c8a1d8a5SArnaud Ebalard  * Set alarm using timer watchdog (via timer A) mechanism. The function expects
503c8a1d8a5SArnaud Ebalard  * timer A interrupt to be disabled.
504c8a1d8a5SArnaud Ebalard  */
_abb5zes3_rtc_set_timer(struct device * dev,struct rtc_wkalrm * alarm,u8 secs)505c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_set_timer(struct device *dev, struct rtc_wkalrm *alarm,
506c8a1d8a5SArnaud Ebalard 				   u8 secs)
507c8a1d8a5SArnaud Ebalard {
508c8a1d8a5SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
509c8a1d8a5SArnaud Ebalard 	u8 regs[ABB5ZES3_TIMA_SEC_LEN];
510c8a1d8a5SArnaud Ebalard 	u8 mask = ABB5ZES3_REG_TIM_CLK_TAC0 | ABB5ZES3_REG_TIM_CLK_TAC1;
511c8a1d8a5SArnaud Ebalard 	int ret = 0;
512c8a1d8a5SArnaud Ebalard 
513c8a1d8a5SArnaud Ebalard 	/* Program given number of seconds to Timer A registers */
514c8a1d8a5SArnaud Ebalard 	sec_to_timer_a(secs, &regs[0], &regs[1]);
515c8a1d8a5SArnaud Ebalard 	ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_TIMA_CLK, regs,
516c8a1d8a5SArnaud Ebalard 				ABB5ZES3_TIMA_SEC_LEN);
517c8a1d8a5SArnaud Ebalard 	if (ret < 0) {
518c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: writing timer section failed\n", __func__);
5195d049837SAlexandre Belloni 		return ret;
520c8a1d8a5SArnaud Ebalard 	}
521c8a1d8a5SArnaud Ebalard 
522c8a1d8a5SArnaud Ebalard 	/* Configure Timer A as a watchdog timer */
523c8a1d8a5SArnaud Ebalard 	ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_TIM_CLK,
524c8a1d8a5SArnaud Ebalard 				 mask, ABB5ZES3_REG_TIM_CLK_TAC1);
525c8a1d8a5SArnaud Ebalard 	if (ret)
526c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: failed to update timer\n", __func__);
527c8a1d8a5SArnaud Ebalard 
528c8a1d8a5SArnaud Ebalard 	/* Record currently configured alarm is a timer */
529c8a1d8a5SArnaud Ebalard 	data->timer_alarm = 1;
530c8a1d8a5SArnaud Ebalard 
531c8a1d8a5SArnaud Ebalard 	/* Enable or disable timer interrupt generation */
5325d049837SAlexandre Belloni 	return _abb5zes3_rtc_update_timer(dev, alarm->enabled);
533c8a1d8a5SArnaud Ebalard }
534c8a1d8a5SArnaud Ebalard 
535c8a1d8a5SArnaud Ebalard /*
536c8a1d8a5SArnaud Ebalard  * The chip has an alarm which is only accurate to the minute. In order to
537c8a1d8a5SArnaud Ebalard  * handle alarms below that limit, we use the watchdog timer function of
538c8a1d8a5SArnaud Ebalard  * timer A. More precisely, the timer method is used for alarms below 240
539c8a1d8a5SArnaud Ebalard  * seconds.
540c8a1d8a5SArnaud Ebalard  */
abb5zes3_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)541c8a1d8a5SArnaud Ebalard static int abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
542c8a1d8a5SArnaud Ebalard {
543c8a1d8a5SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
544c8a1d8a5SArnaud Ebalard 	struct rtc_time *alarm_tm = &alarm->time;
545c8a1d8a5SArnaud Ebalard 	unsigned long rtc_secs, alarm_secs;
546c8a1d8a5SArnaud Ebalard 	struct rtc_time rtc_tm;
547c8a1d8a5SArnaud Ebalard 	int ret;
548c8a1d8a5SArnaud Ebalard 
549c8a1d8a5SArnaud Ebalard 	ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
550c8a1d8a5SArnaud Ebalard 	if (ret)
5515d049837SAlexandre Belloni 		return ret;
552c8a1d8a5SArnaud Ebalard 
5538a941124SAlexandre Belloni 	rtc_secs = rtc_tm_to_time64(&rtc_tm);
5548a941124SAlexandre Belloni 	alarm_secs = rtc_tm_to_time64(alarm_tm);
555c8a1d8a5SArnaud Ebalard 
556c8a1d8a5SArnaud Ebalard 	/* Let's first disable both the alarm and the timer interrupts */
557c8a1d8a5SArnaud Ebalard 	ret = _abb5zes3_rtc_update_alarm(dev, false);
558c8a1d8a5SArnaud Ebalard 	if (ret < 0) {
559c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: unable to disable alarm (%d)\n", __func__,
560c8a1d8a5SArnaud Ebalard 			ret);
5615d049837SAlexandre Belloni 		return ret;
562c8a1d8a5SArnaud Ebalard 	}
563c8a1d8a5SArnaud Ebalard 	ret = _abb5zes3_rtc_update_timer(dev, false);
564c8a1d8a5SArnaud Ebalard 	if (ret < 0) {
565c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: unable to disable timer (%d)\n", __func__,
566c8a1d8a5SArnaud Ebalard 			ret);
5675d049837SAlexandre Belloni 		return ret;
568c8a1d8a5SArnaud Ebalard 	}
569c8a1d8a5SArnaud Ebalard 
570c8a1d8a5SArnaud Ebalard 	data->timer_alarm = 0;
571c8a1d8a5SArnaud Ebalard 
572c8a1d8a5SArnaud Ebalard 	/*
573c8a1d8a5SArnaud Ebalard 	 * Let's now configure the alarm; if we are expected to ring in
574c8a1d8a5SArnaud Ebalard 	 * more than 240s, then we setup an alarm. Otherwise, a timer.
575c8a1d8a5SArnaud Ebalard 	 */
576c8a1d8a5SArnaud Ebalard 	if ((alarm_secs > rtc_secs) && ((alarm_secs - rtc_secs) <= 240))
577c8a1d8a5SArnaud Ebalard 		ret = _abb5zes3_rtc_set_timer(dev, alarm,
578c8a1d8a5SArnaud Ebalard 					      alarm_secs - rtc_secs);
579c8a1d8a5SArnaud Ebalard 	else
580c8a1d8a5SArnaud Ebalard 		ret = _abb5zes3_rtc_set_alarm(dev, alarm);
581c8a1d8a5SArnaud Ebalard 
582c8a1d8a5SArnaud Ebalard 	if (ret)
583c8a1d8a5SArnaud Ebalard 		dev_err(dev, "%s: unable to configure alarm (%d)\n", __func__,
584c8a1d8a5SArnaud Ebalard 			ret);
585c8a1d8a5SArnaud Ebalard 
5860b2f6228SArnaud Ebalard 	return ret;
5870b2f6228SArnaud Ebalard }
5880b2f6228SArnaud Ebalard 
5890b2f6228SArnaud Ebalard /* Enable or disable battery low irq generation */
_abb5zes3_rtc_battery_low_irq_enable(struct regmap * regmap,bool enable)5900b2f6228SArnaud Ebalard static inline int _abb5zes3_rtc_battery_low_irq_enable(struct regmap *regmap,
5910b2f6228SArnaud Ebalard 						       bool enable)
5920b2f6228SArnaud Ebalard {
5930b2f6228SArnaud Ebalard 	return regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3,
5940b2f6228SArnaud Ebalard 				  ABB5ZES3_REG_CTRL3_BLIE,
5950b2f6228SArnaud Ebalard 				  enable ? ABB5ZES3_REG_CTRL3_BLIE : 0);
5960b2f6228SArnaud Ebalard }
5970b2f6228SArnaud Ebalard 
5980b2f6228SArnaud Ebalard /*
5990b2f6228SArnaud Ebalard  * Check current RTC status and enable/disable what needs to be. Return 0 if
600ac246738SAlexandre Belloni  * everything went ok and a negative value upon error.
6010b2f6228SArnaud Ebalard  */
abb5zes3_rtc_check_setup(struct device * dev)6020b2f6228SArnaud Ebalard static int abb5zes3_rtc_check_setup(struct device *dev)
6030b2f6228SArnaud Ebalard {
6040b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
6050b2f6228SArnaud Ebalard 	struct regmap *regmap = data->regmap;
6060b2f6228SArnaud Ebalard 	unsigned int reg;
6070b2f6228SArnaud Ebalard 	int ret;
6080b2f6228SArnaud Ebalard 	u8 mask;
6090b2f6228SArnaud Ebalard 
6100b2f6228SArnaud Ebalard 	/*
6110b2f6228SArnaud Ebalard 	 * By default, the devices generates a 32.768KHz signal on IRQ#1 pin. It
6120b2f6228SArnaud Ebalard 	 * is disabled here to prevent polluting the interrupt line and
6130b2f6228SArnaud Ebalard 	 * uselessly triggering the IRQ handler we install for alarm and battery
6140b2f6228SArnaud Ebalard 	 * low events. Note: this is done before clearing int. status below
6150b2f6228SArnaud Ebalard 	 * in this function.
6160b2f6228SArnaud Ebalard 	 * We also disable all timers and set timer interrupt to permanent (not
6170b2f6228SArnaud Ebalard 	 * pulsed).
6180b2f6228SArnaud Ebalard 	 */
6190b2f6228SArnaud Ebalard 	mask = (ABB5ZES3_REG_TIM_CLK_TBC | ABB5ZES3_REG_TIM_CLK_TAC0 |
6200b2f6228SArnaud Ebalard 		ABB5ZES3_REG_TIM_CLK_TAC1 | ABB5ZES3_REG_TIM_CLK_COF0 |
6210b2f6228SArnaud Ebalard 		ABB5ZES3_REG_TIM_CLK_COF1 | ABB5ZES3_REG_TIM_CLK_COF2 |
6220b2f6228SArnaud Ebalard 		ABB5ZES3_REG_TIM_CLK_TBM | ABB5ZES3_REG_TIM_CLK_TAM);
6230b2f6228SArnaud Ebalard 	ret = regmap_update_bits(regmap, ABB5ZES3_REG_TIM_CLK, mask,
6249c3ab855SAlexandre Belloni 				 ABB5ZES3_REG_TIM_CLK_COF0 |
6259c3ab855SAlexandre Belloni 				 ABB5ZES3_REG_TIM_CLK_COF1 |
6260b2f6228SArnaud Ebalard 				 ABB5ZES3_REG_TIM_CLK_COF2);
6270b2f6228SArnaud Ebalard 	if (ret < 0) {
6280b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to initialize clkout register (%d)\n",
6290b2f6228SArnaud Ebalard 			__func__, ret);
6300b2f6228SArnaud Ebalard 		return ret;
6310b2f6228SArnaud Ebalard 	}
6320b2f6228SArnaud Ebalard 
6330b2f6228SArnaud Ebalard 	/*
6340b2f6228SArnaud Ebalard 	 * Each component of the alarm (MN, HR, DT, DW) can be enabled/disabled
6350b2f6228SArnaud Ebalard 	 * individually by clearing/setting MSB of each associated register. So,
6360b2f6228SArnaud Ebalard 	 * we set all alarm enable bits to disable current alarm setting.
6370b2f6228SArnaud Ebalard 	 */
6380b2f6228SArnaud Ebalard 	mask = (ABB5ZES3_REG_ALRM_MN_AE | ABB5ZES3_REG_ALRM_HR_AE |
6390b2f6228SArnaud Ebalard 		ABB5ZES3_REG_ALRM_DT_AE | ABB5ZES3_REG_ALRM_DW_AE);
6400b2f6228SArnaud Ebalard 	ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, mask);
6410b2f6228SArnaud Ebalard 	if (ret < 0) {
6420b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to disable alarm setting (%d)\n",
6430b2f6228SArnaud Ebalard 			__func__, ret);
6440b2f6228SArnaud Ebalard 		return ret;
6450b2f6228SArnaud Ebalard 	}
6460b2f6228SArnaud Ebalard 
6470b2f6228SArnaud Ebalard 	/* Set Control 1 register (RTC enabled, 24hr mode, all int. disabled) */
6480b2f6228SArnaud Ebalard 	mask = (ABB5ZES3_REG_CTRL1_CIE | ABB5ZES3_REG_CTRL1_AIE |
6490b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL1_SIE | ABB5ZES3_REG_CTRL1_PM |
6500b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL1_CAP | ABB5ZES3_REG_CTRL1_STOP);
6510b2f6228SArnaud Ebalard 	ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL1, mask, 0);
6520b2f6228SArnaud Ebalard 	if (ret < 0) {
6530b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to initialize CTRL1 register (%d)\n",
6540b2f6228SArnaud Ebalard 			__func__, ret);
6550b2f6228SArnaud Ebalard 		return ret;
6560b2f6228SArnaud Ebalard 	}
6570b2f6228SArnaud Ebalard 
6580b2f6228SArnaud Ebalard 	/*
6590b2f6228SArnaud Ebalard 	 * Set Control 2 register (timer int. disabled, alarm status cleared).
6600b2f6228SArnaud Ebalard 	 * WTAF is read-only and cleared automatically by reading the register.
6610b2f6228SArnaud Ebalard 	 */
6620b2f6228SArnaud Ebalard 	mask = (ABB5ZES3_REG_CTRL2_CTBIE | ABB5ZES3_REG_CTRL2_CTAIE |
6630b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL2_WTAIE | ABB5ZES3_REG_CTRL2_AF |
6640b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL2_SF | ABB5ZES3_REG_CTRL2_CTBF |
6650b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL2_CTAF);
6660b2f6228SArnaud Ebalard 	ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, 0);
6670b2f6228SArnaud Ebalard 	if (ret < 0) {
6680b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to initialize CTRL2 register (%d)\n",
6690b2f6228SArnaud Ebalard 			__func__, ret);
6700b2f6228SArnaud Ebalard 		return ret;
6710b2f6228SArnaud Ebalard 	}
6720b2f6228SArnaud Ebalard 
6730b2f6228SArnaud Ebalard 	/*
6740b2f6228SArnaud Ebalard 	 * Enable battery low detection function and battery switchover function
6750b2f6228SArnaud Ebalard 	 * (standard mode). Disable associated interrupts. Clear battery
6760b2f6228SArnaud Ebalard 	 * switchover flag but not battery low flag. The latter is checked
6770b2f6228SArnaud Ebalard 	 * later below.
6780b2f6228SArnaud Ebalard 	 */
6790b2f6228SArnaud Ebalard 	mask = (ABB5ZES3_REG_CTRL3_PM0  | ABB5ZES3_REG_CTRL3_PM1 |
6800b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL3_PM2  | ABB5ZES3_REG_CTRL3_BLIE |
6810b2f6228SArnaud Ebalard 		ABB5ZES3_REG_CTRL3_BSIE | ABB5ZES3_REG_CTRL3_BSF);
6820b2f6228SArnaud Ebalard 	ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3, mask, 0);
6830b2f6228SArnaud Ebalard 	if (ret < 0) {
6840b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to initialize CTRL3 register (%d)\n",
6850b2f6228SArnaud Ebalard 			__func__, ret);
6860b2f6228SArnaud Ebalard 		return ret;
6870b2f6228SArnaud Ebalard 	}
6880b2f6228SArnaud Ebalard 
6890b2f6228SArnaud Ebalard 	/* Check oscillator integrity flag */
6900b2f6228SArnaud Ebalard 	ret = regmap_read(regmap, ABB5ZES3_REG_RTC_SC, &reg);
6910b2f6228SArnaud Ebalard 	if (ret < 0) {
6920b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to read osc. integrity flag (%d)\n",
6930b2f6228SArnaud Ebalard 			__func__, ret);
6940b2f6228SArnaud Ebalard 		return ret;
6950b2f6228SArnaud Ebalard 	}
6960b2f6228SArnaud Ebalard 
6970b2f6228SArnaud Ebalard 	if (reg & ABB5ZES3_REG_RTC_SC_OSC) {
6989c3ab855SAlexandre Belloni 		dev_err(dev, "clock integrity not guaranteed. Osc. has stopped or has been interrupted.\n");
6999c3ab855SAlexandre Belloni 		dev_err(dev, "change battery (if not already done) and then set time to reset osc. failure flag.\n");
7000b2f6228SArnaud Ebalard 	}
7010b2f6228SArnaud Ebalard 
7020b2f6228SArnaud Ebalard 	/*
7030b2f6228SArnaud Ebalard 	 * Check battery low flag at startup: this allows reporting battery
7040b2f6228SArnaud Ebalard 	 * is low at startup when IRQ line is not connected. Note: we record
7050b2f6228SArnaud Ebalard 	 * current status to avoid reenabling this interrupt later in probe
7060b2f6228SArnaud Ebalard 	 * function if battery is low.
7070b2f6228SArnaud Ebalard 	 */
7080b2f6228SArnaud Ebalard 	ret = regmap_read(regmap, ABB5ZES3_REG_CTRL3, &reg);
7090b2f6228SArnaud Ebalard 	if (ret < 0) {
7100b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to read battery low flag (%d)\n",
7110b2f6228SArnaud Ebalard 			__func__, ret);
7120b2f6228SArnaud Ebalard 		return ret;
7130b2f6228SArnaud Ebalard 	}
7140b2f6228SArnaud Ebalard 
7150b2f6228SArnaud Ebalard 	data->battery_low = reg & ABB5ZES3_REG_CTRL3_BLF;
7160b2f6228SArnaud Ebalard 	if (data->battery_low) {
7179c3ab855SAlexandre Belloni 		dev_err(dev, "RTC battery is low; please, consider changing it!\n");
7180b2f6228SArnaud Ebalard 
7190b2f6228SArnaud Ebalard 		ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, false);
7200b2f6228SArnaud Ebalard 		if (ret)
7219c3ab855SAlexandre Belloni 			dev_err(dev, "%s: disabling battery low interrupt generation failed (%d)\n",
7229c3ab855SAlexandre Belloni 				__func__, ret);
7230b2f6228SArnaud Ebalard 	}
7240b2f6228SArnaud Ebalard 
7250b2f6228SArnaud Ebalard 	return ret;
7260b2f6228SArnaud Ebalard }
7270b2f6228SArnaud Ebalard 
abb5zes3_rtc_alarm_irq_enable(struct device * dev,unsigned int enable)7280b2f6228SArnaud Ebalard static int abb5zes3_rtc_alarm_irq_enable(struct device *dev,
7290b2f6228SArnaud Ebalard 					 unsigned int enable)
7300b2f6228SArnaud Ebalard {
7310b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
7320b2f6228SArnaud Ebalard 	int ret = 0;
7330b2f6228SArnaud Ebalard 
7340b2f6228SArnaud Ebalard 	if (rtc_data->irq) {
735c8a1d8a5SArnaud Ebalard 		if (rtc_data->timer_alarm)
736c8a1d8a5SArnaud Ebalard 			ret = _abb5zes3_rtc_update_timer(dev, enable);
737c8a1d8a5SArnaud Ebalard 		else
7380b2f6228SArnaud Ebalard 			ret = _abb5zes3_rtc_update_alarm(dev, enable);
7390b2f6228SArnaud Ebalard 	}
7400b2f6228SArnaud Ebalard 
7410b2f6228SArnaud Ebalard 	return ret;
7420b2f6228SArnaud Ebalard }
7430b2f6228SArnaud Ebalard 
_abb5zes3_rtc_interrupt(int irq,void * data)7440b2f6228SArnaud Ebalard static irqreturn_t _abb5zes3_rtc_interrupt(int irq, void *data)
7450b2f6228SArnaud Ebalard {
7460b2f6228SArnaud Ebalard 	struct i2c_client *client = data;
7470b2f6228SArnaud Ebalard 	struct device *dev = &client->dev;
7480b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
7490b2f6228SArnaud Ebalard 	struct rtc_device *rtc = rtc_data->rtc;
7500b2f6228SArnaud Ebalard 	u8 regs[ABB5ZES3_CTRL_SEC_LEN];
7510b2f6228SArnaud Ebalard 	int ret, handled = IRQ_NONE;
7520b2f6228SArnaud Ebalard 
7530b2f6228SArnaud Ebalard 	ret = regmap_bulk_read(rtc_data->regmap, 0, regs,
7540b2f6228SArnaud Ebalard 			       ABB5ZES3_CTRL_SEC_LEN);
7550b2f6228SArnaud Ebalard 	if (ret) {
7560b2f6228SArnaud Ebalard 		dev_err(dev, "%s: unable to read control section (%d)!\n",
7570b2f6228SArnaud Ebalard 			__func__, ret);
7580b2f6228SArnaud Ebalard 		return handled;
7590b2f6228SArnaud Ebalard 	}
7600b2f6228SArnaud Ebalard 
7610b2f6228SArnaud Ebalard 	/*
7620b2f6228SArnaud Ebalard 	 * Check battery low detection flag and disable battery low interrupt
7630b2f6228SArnaud Ebalard 	 * generation if flag is set (interrupt can only be cleared when
7640b2f6228SArnaud Ebalard 	 * battery is replaced).
7650b2f6228SArnaud Ebalard 	 */
7660b2f6228SArnaud Ebalard 	if (regs[ABB5ZES3_REG_CTRL3] & ABB5ZES3_REG_CTRL3_BLF) {
7670b2f6228SArnaud Ebalard 		dev_err(dev, "RTC battery is low; please change it!\n");
7680b2f6228SArnaud Ebalard 
7690b2f6228SArnaud Ebalard 		_abb5zes3_rtc_battery_low_irq_enable(rtc_data->regmap, false);
7700b2f6228SArnaud Ebalard 
7710b2f6228SArnaud Ebalard 		handled = IRQ_HANDLED;
7720b2f6228SArnaud Ebalard 	}
7730b2f6228SArnaud Ebalard 
7740b2f6228SArnaud Ebalard 	/* Check alarm flag */
7750b2f6228SArnaud Ebalard 	if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_AF) {
7760b2f6228SArnaud Ebalard 		dev_dbg(dev, "RTC alarm!\n");
7770b2f6228SArnaud Ebalard 
7780b2f6228SArnaud Ebalard 		rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
7790b2f6228SArnaud Ebalard 
7800b2f6228SArnaud Ebalard 		/* Acknowledge and disable the alarm */
7810b2f6228SArnaud Ebalard 		_abb5zes3_rtc_clear_alarm(dev);
7820b2f6228SArnaud Ebalard 		_abb5zes3_rtc_update_alarm(dev, 0);
7830b2f6228SArnaud Ebalard 
7840b2f6228SArnaud Ebalard 		handled = IRQ_HANDLED;
7850b2f6228SArnaud Ebalard 	}
7860b2f6228SArnaud Ebalard 
787c8a1d8a5SArnaud Ebalard 	/* Check watchdog Timer A flag */
788c8a1d8a5SArnaud Ebalard 	if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_WTAF) {
789c8a1d8a5SArnaud Ebalard 		dev_dbg(dev, "RTC timer!\n");
790c8a1d8a5SArnaud Ebalard 
791c8a1d8a5SArnaud Ebalard 		rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
792c8a1d8a5SArnaud Ebalard 
793c8a1d8a5SArnaud Ebalard 		/*
794c8a1d8a5SArnaud Ebalard 		 * Acknowledge and disable the alarm. Note: WTAF
795c8a1d8a5SArnaud Ebalard 		 * flag had been cleared when reading CTRL2
796c8a1d8a5SArnaud Ebalard 		 */
797c8a1d8a5SArnaud Ebalard 		_abb5zes3_rtc_update_timer(dev, 0);
798c8a1d8a5SArnaud Ebalard 
799c8a1d8a5SArnaud Ebalard 		rtc_data->timer_alarm = 0;
800c8a1d8a5SArnaud Ebalard 
801c8a1d8a5SArnaud Ebalard 		handled = IRQ_HANDLED;
802c8a1d8a5SArnaud Ebalard 	}
803c8a1d8a5SArnaud Ebalard 
8040b2f6228SArnaud Ebalard 	return handled;
8050b2f6228SArnaud Ebalard }
8060b2f6228SArnaud Ebalard 
8070b2f6228SArnaud Ebalard static const struct rtc_class_ops rtc_ops = {
8080b2f6228SArnaud Ebalard 	.read_time = _abb5zes3_rtc_read_time,
8090b2f6228SArnaud Ebalard 	.set_time = abb5zes3_rtc_set_time,
8100b2f6228SArnaud Ebalard 	.read_alarm = abb5zes3_rtc_read_alarm,
8110b2f6228SArnaud Ebalard 	.set_alarm = abb5zes3_rtc_set_alarm,
8120b2f6228SArnaud Ebalard 	.alarm_irq_enable = abb5zes3_rtc_alarm_irq_enable,
8130b2f6228SArnaud Ebalard };
8140b2f6228SArnaud Ebalard 
815ac2a2726SKrzysztof Kozlowski static const struct regmap_config abb5zes3_rtc_regmap_config = {
8160b2f6228SArnaud Ebalard 	.reg_bits = 8,
8170b2f6228SArnaud Ebalard 	.val_bits = 8,
8180b2f6228SArnaud Ebalard };
8190b2f6228SArnaud Ebalard 
abb5zes3_probe(struct i2c_client * client)8203f4a3322SStephen Kitt static int abb5zes3_probe(struct i2c_client *client)
8210b2f6228SArnaud Ebalard {
8220b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *data = NULL;
8230b2f6228SArnaud Ebalard 	struct device *dev = &client->dev;
8240b2f6228SArnaud Ebalard 	struct regmap *regmap;
8250b2f6228SArnaud Ebalard 	int ret;
8260b2f6228SArnaud Ebalard 
8270b2f6228SArnaud Ebalard 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
8280b2f6228SArnaud Ebalard 				     I2C_FUNC_SMBUS_BYTE_DATA |
8295d049837SAlexandre Belloni 				     I2C_FUNC_SMBUS_I2C_BLOCK))
8305d049837SAlexandre Belloni 		return -ENODEV;
8310b2f6228SArnaud Ebalard 
8320b2f6228SArnaud Ebalard 	regmap = devm_regmap_init_i2c(client, &abb5zes3_rtc_regmap_config);
8330b2f6228SArnaud Ebalard 	if (IS_ERR(regmap)) {
8340b2f6228SArnaud Ebalard 		ret = PTR_ERR(regmap);
8350b2f6228SArnaud Ebalard 		dev_err(dev, "%s: regmap allocation failed: %d\n",
8360b2f6228SArnaud Ebalard 			__func__, ret);
8375d049837SAlexandre Belloni 		return ret;
8380b2f6228SArnaud Ebalard 	}
8390b2f6228SArnaud Ebalard 
8400b2f6228SArnaud Ebalard 	ret = abb5zes3_i2c_validate_chip(regmap);
8410b2f6228SArnaud Ebalard 	if (ret)
8425d049837SAlexandre Belloni 		return ret;
8430b2f6228SArnaud Ebalard 
8440b2f6228SArnaud Ebalard 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
8455d049837SAlexandre Belloni 	if (!data)
8465d049837SAlexandre Belloni 		return -ENOMEM;
8470b2f6228SArnaud Ebalard 
8480b2f6228SArnaud Ebalard 	data->regmap = regmap;
8490b2f6228SArnaud Ebalard 	dev_set_drvdata(dev, data);
8500b2f6228SArnaud Ebalard 
8510b2f6228SArnaud Ebalard 	ret = abb5zes3_rtc_check_setup(dev);
8520b2f6228SArnaud Ebalard 	if (ret)
8535d049837SAlexandre Belloni 		return ret;
8540b2f6228SArnaud Ebalard 
8558bde032bSAlexandre Belloni 	data->rtc = devm_rtc_allocate_device(dev);
8568bde032bSAlexandre Belloni 	ret = PTR_ERR_OR_ZERO(data->rtc);
8578bde032bSAlexandre Belloni 	if (ret) {
8588bde032bSAlexandre Belloni 		dev_err(dev, "%s: unable to allocate RTC device (%d)\n",
8598bde032bSAlexandre Belloni 			__func__, ret);
8605d049837SAlexandre Belloni 		return ret;
8618bde032bSAlexandre Belloni 	}
8628bde032bSAlexandre Belloni 
8630b2f6228SArnaud Ebalard 	if (client->irq > 0) {
8640b2f6228SArnaud Ebalard 		ret = devm_request_threaded_irq(dev, client->irq, NULL,
8650b2f6228SArnaud Ebalard 						_abb5zes3_rtc_interrupt,
8660b2f6228SArnaud Ebalard 						IRQF_SHARED | IRQF_ONESHOT,
8670b2f6228SArnaud Ebalard 						DRV_NAME, client);
8680b2f6228SArnaud Ebalard 		if (!ret) {
8690b2f6228SArnaud Ebalard 			device_init_wakeup(dev, true);
8700b2f6228SArnaud Ebalard 			data->irq = client->irq;
8710b2f6228SArnaud Ebalard 			dev_dbg(dev, "%s: irq %d used by RTC\n", __func__,
8720b2f6228SArnaud Ebalard 				client->irq);
8730b2f6228SArnaud Ebalard 		} else {
8740b2f6228SArnaud Ebalard 			dev_err(dev, "%s: irq %d unavailable (%d)\n",
8750b2f6228SArnaud Ebalard 				__func__, client->irq, ret);
8760b2f6228SArnaud Ebalard 			goto err;
8770b2f6228SArnaud Ebalard 		}
8780b2f6228SArnaud Ebalard 	}
8790b2f6228SArnaud Ebalard 
8808bde032bSAlexandre Belloni 	data->rtc->ops = &rtc_ops;
881c402f8eaSAlexandre Belloni 	data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
882c402f8eaSAlexandre Belloni 	data->rtc->range_max = RTC_TIMESTAMP_END_2099;
8830b2f6228SArnaud Ebalard 
8840b2f6228SArnaud Ebalard 	/* Enable battery low detection interrupt if battery not already low */
8850b2f6228SArnaud Ebalard 	if (!data->battery_low && data->irq) {
8860b2f6228SArnaud Ebalard 		ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, true);
8870b2f6228SArnaud Ebalard 		if (ret) {
8889c3ab855SAlexandre Belloni 			dev_err(dev, "%s: enabling battery low interrupt generation failed (%d)\n",
8899c3ab855SAlexandre Belloni 				__func__, ret);
8900b2f6228SArnaud Ebalard 			goto err;
8910b2f6228SArnaud Ebalard 		}
8920b2f6228SArnaud Ebalard 	}
8930b2f6228SArnaud Ebalard 
894fdcfd854SBartosz Golaszewski 	ret = devm_rtc_register_device(data->rtc);
8958bde032bSAlexandre Belloni 
8960b2f6228SArnaud Ebalard err:
8975d049837SAlexandre Belloni 	if (ret && data->irq)
8980b2f6228SArnaud Ebalard 		device_init_wakeup(dev, false);
8990b2f6228SArnaud Ebalard 	return ret;
9000b2f6228SArnaud Ebalard }
9010b2f6228SArnaud Ebalard 
9020b2f6228SArnaud Ebalard #ifdef CONFIG_PM_SLEEP
abb5zes3_rtc_suspend(struct device * dev)9030b2f6228SArnaud Ebalard static int abb5zes3_rtc_suspend(struct device *dev)
9040b2f6228SArnaud Ebalard {
9050b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
9060b2f6228SArnaud Ebalard 
9070b2f6228SArnaud Ebalard 	if (device_may_wakeup(dev))
9080b2f6228SArnaud Ebalard 		return enable_irq_wake(rtc_data->irq);
9090b2f6228SArnaud Ebalard 
9100b2f6228SArnaud Ebalard 	return 0;
9110b2f6228SArnaud Ebalard }
9120b2f6228SArnaud Ebalard 
abb5zes3_rtc_resume(struct device * dev)9130b2f6228SArnaud Ebalard static int abb5zes3_rtc_resume(struct device *dev)
9140b2f6228SArnaud Ebalard {
9150b2f6228SArnaud Ebalard 	struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
9160b2f6228SArnaud Ebalard 
9170b2f6228SArnaud Ebalard 	if (device_may_wakeup(dev))
9180b2f6228SArnaud Ebalard 		return disable_irq_wake(rtc_data->irq);
9190b2f6228SArnaud Ebalard 
9200b2f6228SArnaud Ebalard 	return 0;
9210b2f6228SArnaud Ebalard }
9220b2f6228SArnaud Ebalard #endif
9230b2f6228SArnaud Ebalard 
9240b2f6228SArnaud Ebalard static SIMPLE_DEV_PM_OPS(abb5zes3_rtc_pm_ops, abb5zes3_rtc_suspend,
9250b2f6228SArnaud Ebalard 			 abb5zes3_rtc_resume);
9260b2f6228SArnaud Ebalard 
9270b2f6228SArnaud Ebalard #ifdef CONFIG_OF
9280b2f6228SArnaud Ebalard static const struct of_device_id abb5zes3_dt_match[] = {
9290b2f6228SArnaud Ebalard 	{ .compatible = "abracon,abb5zes3" },
9300b2f6228SArnaud Ebalard 	{ },
9310b2f6228SArnaud Ebalard };
9321c4fc295SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, abb5zes3_dt_match);
9330b2f6228SArnaud Ebalard #endif
9340b2f6228SArnaud Ebalard 
9350b2f6228SArnaud Ebalard static const struct i2c_device_id abb5zes3_id[] = {
9360b2f6228SArnaud Ebalard 	{ "abb5zes3", 0 },
9370b2f6228SArnaud Ebalard 	{ }
9380b2f6228SArnaud Ebalard };
9390b2f6228SArnaud Ebalard MODULE_DEVICE_TABLE(i2c, abb5zes3_id);
9400b2f6228SArnaud Ebalard 
9410b2f6228SArnaud Ebalard static struct i2c_driver abb5zes3_driver = {
9420b2f6228SArnaud Ebalard 	.driver = {
9430b2f6228SArnaud Ebalard 		.name = DRV_NAME,
9440b2f6228SArnaud Ebalard 		.pm = &abb5zes3_rtc_pm_ops,
9450b2f6228SArnaud Ebalard 		.of_match_table = of_match_ptr(abb5zes3_dt_match),
9460b2f6228SArnaud Ebalard 	},
947*31b0cecbSUwe Kleine-König 	.probe = abb5zes3_probe,
9480b2f6228SArnaud Ebalard 	.id_table = abb5zes3_id,
9490b2f6228SArnaud Ebalard };
9500b2f6228SArnaud Ebalard module_i2c_driver(abb5zes3_driver);
9510b2f6228SArnaud Ebalard 
9520b2f6228SArnaud Ebalard MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
9530b2f6228SArnaud Ebalard MODULE_DESCRIPTION("Abracon AB-RTCMC-32.768kHz-B5ZE-S3 RTC/Alarm driver");
9540b2f6228SArnaud Ebalard MODULE_LICENSE("GPL");
955