xref: /linux/drivers/rtc/rtc-rv8803.c (revision 16d70a78b45a65c88a3c614ebadbf2589f6f8efe)
11e3929efSAlexandre Belloni /*
21e3929efSAlexandre Belloni  * RTC driver for the Micro Crystal RV8803
31e3929efSAlexandre Belloni  *
41e3929efSAlexandre Belloni  * Copyright (C) 2015 Micro Crystal SA
51e3929efSAlexandre Belloni  *
61e3929efSAlexandre Belloni  * Alexandre Belloni <alexandre.belloni@free-electrons.com>
71e3929efSAlexandre Belloni  *
81e3929efSAlexandre Belloni  * This program is free software; you can redistribute it and/or modify
91e3929efSAlexandre Belloni  * it under the terms of the GNU General Public License version 2 as
101e3929efSAlexandre Belloni  * published by the Free Software Foundation.
111e3929efSAlexandre Belloni  *
121e3929efSAlexandre Belloni  */
131e3929efSAlexandre Belloni 
141e3929efSAlexandre Belloni #include <linux/bcd.h>
151e3929efSAlexandre Belloni #include <linux/bitops.h>
16a1e98e09SBenoît Thébaudeau #include <linux/log2.h>
171e3929efSAlexandre Belloni #include <linux/i2c.h>
181e3929efSAlexandre Belloni #include <linux/interrupt.h>
191e3929efSAlexandre Belloni #include <linux/kernel.h>
201e3929efSAlexandre Belloni #include <linux/module.h>
21740ad8f4SJavier Martinez Canillas #include <linux/of_device.h>
221e3929efSAlexandre Belloni #include <linux/rtc.h>
231e3929efSAlexandre Belloni 
24d522649eSBenoît Thébaudeau #define RV8803_I2C_TRY_COUNT		4
25d522649eSBenoît Thébaudeau 
261e3929efSAlexandre Belloni #define RV8803_SEC			0x00
271e3929efSAlexandre Belloni #define RV8803_MIN			0x01
281e3929efSAlexandre Belloni #define RV8803_HOUR			0x02
291e3929efSAlexandre Belloni #define RV8803_WEEK			0x03
301e3929efSAlexandre Belloni #define RV8803_DAY			0x04
311e3929efSAlexandre Belloni #define RV8803_MONTH			0x05
321e3929efSAlexandre Belloni #define RV8803_YEAR			0x06
331e3929efSAlexandre Belloni #define RV8803_RAM			0x07
341e3929efSAlexandre Belloni #define RV8803_ALARM_MIN		0x08
351e3929efSAlexandre Belloni #define RV8803_ALARM_HOUR		0x09
361e3929efSAlexandre Belloni #define RV8803_ALARM_WEEK_OR_DAY	0x0A
371e3929efSAlexandre Belloni #define RV8803_EXT			0x0D
381e3929efSAlexandre Belloni #define RV8803_FLAG			0x0E
391e3929efSAlexandre Belloni #define RV8803_CTRL			0x0F
401e3929efSAlexandre Belloni 
411e3929efSAlexandre Belloni #define RV8803_EXT_WADA			BIT(6)
421e3929efSAlexandre Belloni 
431e3929efSAlexandre Belloni #define RV8803_FLAG_V1F			BIT(0)
441e3929efSAlexandre Belloni #define RV8803_FLAG_V2F			BIT(1)
451e3929efSAlexandre Belloni #define RV8803_FLAG_AF			BIT(3)
461e3929efSAlexandre Belloni #define RV8803_FLAG_TF			BIT(4)
471e3929efSAlexandre Belloni #define RV8803_FLAG_UF			BIT(5)
481e3929efSAlexandre Belloni 
491e3929efSAlexandre Belloni #define RV8803_CTRL_RESET		BIT(0)
501e3929efSAlexandre Belloni 
511e3929efSAlexandre Belloni #define RV8803_CTRL_EIE			BIT(2)
521e3929efSAlexandre Belloni #define RV8803_CTRL_AIE			BIT(3)
531e3929efSAlexandre Belloni #define RV8803_CTRL_TIE			BIT(4)
541e3929efSAlexandre Belloni #define RV8803_CTRL_UIE			BIT(5)
551e3929efSAlexandre Belloni 
561cd71376SOleksij Rempel #define RX8900_BACKUP_CTRL		0x18
571cd71376SOleksij Rempel #define RX8900_FLAG_SWOFF		BIT(2)
581cd71376SOleksij Rempel #define RX8900_FLAG_VDETOFF		BIT(3)
591cd71376SOleksij Rempel 
601cd71376SOleksij Rempel enum rv8803_type {
611cd71376SOleksij Rempel 	rv_8803,
621cd71376SOleksij Rempel 	rx_8900
631cd71376SOleksij Rempel };
641cd71376SOleksij Rempel 
651e3929efSAlexandre Belloni struct rv8803_data {
661e3929efSAlexandre Belloni 	struct i2c_client *client;
671e3929efSAlexandre Belloni 	struct rtc_device *rtc;
689d1fa4c3SOleksij Rempel 	struct mutex flags_lock;
691e3929efSAlexandre Belloni 	u8 ctrl;
701cd71376SOleksij Rempel 	enum rv8803_type type;
71*16d70a78SAlexandre Belloni 	struct nvmem_config nvmem_cfg;
721e3929efSAlexandre Belloni };
731e3929efSAlexandre Belloni 
74d522649eSBenoît Thébaudeau static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
75d522649eSBenoît Thébaudeau {
76d522649eSBenoît Thébaudeau 	int try = RV8803_I2C_TRY_COUNT;
77d522649eSBenoît Thébaudeau 	s32 ret;
78d522649eSBenoît Thébaudeau 
79d522649eSBenoît Thébaudeau 	/*
80d522649eSBenoît Thébaudeau 	 * There is a 61µs window during which the RTC does not acknowledge I2C
81d522649eSBenoît Thébaudeau 	 * transfers. In that case, ensure that there are multiple attempts.
82d522649eSBenoît Thébaudeau 	 */
83d522649eSBenoît Thébaudeau 	do
84d522649eSBenoît Thébaudeau 		ret = i2c_smbus_read_byte_data(client, reg);
85d522649eSBenoît Thébaudeau 	while ((ret == -ENXIO || ret == -EIO) && --try);
86d522649eSBenoît Thébaudeau 	if (ret < 0)
87d522649eSBenoît Thébaudeau 		dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
88d522649eSBenoît Thébaudeau 
89d522649eSBenoît Thébaudeau 	return ret;
90d522649eSBenoît Thébaudeau }
91d522649eSBenoît Thébaudeau 
92d522649eSBenoît Thébaudeau static int rv8803_read_regs(const struct i2c_client *client,
93d522649eSBenoît Thébaudeau 			    u8 reg, u8 count, u8 *values)
94d522649eSBenoît Thébaudeau {
95d522649eSBenoît Thébaudeau 	int try = RV8803_I2C_TRY_COUNT;
96d522649eSBenoît Thébaudeau 	s32 ret;
97d522649eSBenoît Thébaudeau 
98d522649eSBenoît Thébaudeau 	do
99d522649eSBenoît Thébaudeau 		ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
100d522649eSBenoît Thébaudeau 	while ((ret == -ENXIO || ret == -EIO) && --try);
101d522649eSBenoît Thébaudeau 	if (ret != count) {
102d522649eSBenoît Thébaudeau 		dev_err(&client->dev,
103d522649eSBenoît Thébaudeau 			"Unable to read registers 0x%02x..0x%02x\n",
104d522649eSBenoît Thébaudeau 			reg, reg + count - 1);
105d522649eSBenoît Thébaudeau 		return ret < 0 ? ret : -EIO;
106d522649eSBenoît Thébaudeau 	}
107d522649eSBenoît Thébaudeau 
108d522649eSBenoît Thébaudeau 	return 0;
109d522649eSBenoît Thébaudeau }
110d522649eSBenoît Thébaudeau 
111d522649eSBenoît Thébaudeau static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
112d522649eSBenoît Thébaudeau {
113d522649eSBenoît Thébaudeau 	int try = RV8803_I2C_TRY_COUNT;
114d522649eSBenoît Thébaudeau 	s32 ret;
115d522649eSBenoît Thébaudeau 
116d522649eSBenoît Thébaudeau 	do
117d522649eSBenoît Thébaudeau 		ret = i2c_smbus_write_byte_data(client, reg, value);
118d522649eSBenoît Thébaudeau 	while ((ret == -ENXIO || ret == -EIO) && --try);
119d522649eSBenoît Thébaudeau 	if (ret)
120d522649eSBenoît Thébaudeau 		dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
121d522649eSBenoît Thébaudeau 
122d522649eSBenoît Thébaudeau 	return ret;
123d522649eSBenoît Thébaudeau }
124d522649eSBenoît Thébaudeau 
125d522649eSBenoît Thébaudeau static int rv8803_write_regs(const struct i2c_client *client,
126d522649eSBenoît Thébaudeau 			     u8 reg, u8 count, const u8 *values)
127d522649eSBenoît Thébaudeau {
128d522649eSBenoît Thébaudeau 	int try = RV8803_I2C_TRY_COUNT;
129d522649eSBenoît Thébaudeau 	s32 ret;
130d522649eSBenoît Thébaudeau 
131d522649eSBenoît Thébaudeau 	do
132d522649eSBenoît Thébaudeau 		ret = i2c_smbus_write_i2c_block_data(client, reg, count,
133d522649eSBenoît Thébaudeau 						     values);
134d522649eSBenoît Thébaudeau 	while ((ret == -ENXIO || ret == -EIO) && --try);
135d522649eSBenoît Thébaudeau 	if (ret)
136d522649eSBenoît Thébaudeau 		dev_err(&client->dev,
137d522649eSBenoît Thébaudeau 			"Unable to write registers 0x%02x..0x%02x\n",
138d522649eSBenoît Thébaudeau 			reg, reg + count - 1);
139d522649eSBenoît Thébaudeau 
140d522649eSBenoît Thébaudeau 	return ret;
141d522649eSBenoît Thébaudeau }
142d522649eSBenoît Thébaudeau 
1431e3929efSAlexandre Belloni static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
1441e3929efSAlexandre Belloni {
1451e3929efSAlexandre Belloni 	struct i2c_client *client = dev_id;
1461e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = i2c_get_clientdata(client);
1471e3929efSAlexandre Belloni 	unsigned long events = 0;
148d522649eSBenoît Thébaudeau 	int flags;
1491e3929efSAlexandre Belloni 
1509d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
1511e3929efSAlexandre Belloni 
152d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(client, RV8803_FLAG);
1531e3929efSAlexandre Belloni 	if (flags <= 0) {
1549d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
1551e3929efSAlexandre Belloni 		return IRQ_NONE;
1561e3929efSAlexandre Belloni 	}
1571e3929efSAlexandre Belloni 
1581e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V1F)
1591e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
1601e3929efSAlexandre Belloni 
1611e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F)
1621e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
1631e3929efSAlexandre Belloni 
1641e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_TF) {
1651e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_TF;
1661e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_TIE;
1671e3929efSAlexandre Belloni 		events |= RTC_PF;
1681e3929efSAlexandre Belloni 	}
1691e3929efSAlexandre Belloni 
1701e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_AF) {
1711e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_AF;
1721e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_AIE;
1731e3929efSAlexandre Belloni 		events |= RTC_AF;
1741e3929efSAlexandre Belloni 	}
1751e3929efSAlexandre Belloni 
1761e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_UF) {
1771e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_UF;
1781e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_UIE;
1791e3929efSAlexandre Belloni 		events |= RTC_UF;
1801e3929efSAlexandre Belloni 	}
1811e3929efSAlexandre Belloni 
1821e3929efSAlexandre Belloni 	if (events) {
1831e3929efSAlexandre Belloni 		rtc_update_irq(rv8803->rtc, 1, events);
184d522649eSBenoît Thébaudeau 		rv8803_write_reg(client, RV8803_FLAG, flags);
185d522649eSBenoît Thébaudeau 		rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
1861e3929efSAlexandre Belloni 	}
1871e3929efSAlexandre Belloni 
1889d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
1891e3929efSAlexandre Belloni 
1901e3929efSAlexandre Belloni 	return IRQ_HANDLED;
1911e3929efSAlexandre Belloni }
1921e3929efSAlexandre Belloni 
1931e3929efSAlexandre Belloni static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
1941e3929efSAlexandre Belloni {
1951e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
1961e3929efSAlexandre Belloni 	u8 date1[7];
1971e3929efSAlexandre Belloni 	u8 date2[7];
1981e3929efSAlexandre Belloni 	u8 *date = date1;
1991e3929efSAlexandre Belloni 	int ret, flags;
2001e3929efSAlexandre Belloni 
201d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
2021e3929efSAlexandre Belloni 	if (flags < 0)
2031e3929efSAlexandre Belloni 		return flags;
2041e3929efSAlexandre Belloni 
2051e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F) {
2061e3929efSAlexandre Belloni 		dev_warn(dev, "Voltage low, data is invalid.\n");
2071e3929efSAlexandre Belloni 		return -EINVAL;
2081e3929efSAlexandre Belloni 	}
2091e3929efSAlexandre Belloni 
210d522649eSBenoît Thébaudeau 	ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
211d522649eSBenoît Thébaudeau 	if (ret)
212d522649eSBenoît Thébaudeau 		return ret;
2131e3929efSAlexandre Belloni 
2141e3929efSAlexandre Belloni 	if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
215d522649eSBenoît Thébaudeau 		ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
216d522649eSBenoît Thébaudeau 		if (ret)
217d522649eSBenoît Thébaudeau 			return ret;
2181e3929efSAlexandre Belloni 
2191e3929efSAlexandre Belloni 		if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
2201e3929efSAlexandre Belloni 			date = date2;
2211e3929efSAlexandre Belloni 	}
2221e3929efSAlexandre Belloni 
2231e3929efSAlexandre Belloni 	tm->tm_sec  = bcd2bin(date[RV8803_SEC] & 0x7f);
2241e3929efSAlexandre Belloni 	tm->tm_min  = bcd2bin(date[RV8803_MIN] & 0x7f);
2251e3929efSAlexandre Belloni 	tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
226a1e98e09SBenoît Thébaudeau 	tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
2271e3929efSAlexandre Belloni 	tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
2281e3929efSAlexandre Belloni 	tm->tm_mon  = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
2291e3929efSAlexandre Belloni 	tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
2301e3929efSAlexandre Belloni 
23196acb25cSBenoît Thébaudeau 	return 0;
2321e3929efSAlexandre Belloni }
2331e3929efSAlexandre Belloni 
2341e3929efSAlexandre Belloni static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
2351e3929efSAlexandre Belloni {
2361e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
2371e3929efSAlexandre Belloni 	u8 date[7];
238d3700b6bSBenoît Thébaudeau 	int ctrl, flags, ret;
2391e3929efSAlexandre Belloni 
2401e3929efSAlexandre Belloni 	if ((tm->tm_year < 100) || (tm->tm_year > 199))
2411e3929efSAlexandre Belloni 		return -EINVAL;
2421e3929efSAlexandre Belloni 
243d3700b6bSBenoît Thébaudeau 	ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
244d3700b6bSBenoît Thébaudeau 	if (ctrl < 0)
245d3700b6bSBenoît Thébaudeau 		return ctrl;
246d3700b6bSBenoît Thébaudeau 
247d3700b6bSBenoît Thébaudeau 	/* Stop the clock */
248d3700b6bSBenoît Thébaudeau 	ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
249d3700b6bSBenoît Thébaudeau 			       ctrl | RV8803_CTRL_RESET);
250d3700b6bSBenoît Thébaudeau 	if (ret)
251d3700b6bSBenoît Thébaudeau 		return ret;
252d3700b6bSBenoît Thébaudeau 
2531e3929efSAlexandre Belloni 	date[RV8803_SEC]   = bin2bcd(tm->tm_sec);
2541e3929efSAlexandre Belloni 	date[RV8803_MIN]   = bin2bcd(tm->tm_min);
2551e3929efSAlexandre Belloni 	date[RV8803_HOUR]  = bin2bcd(tm->tm_hour);
2561e3929efSAlexandre Belloni 	date[RV8803_WEEK]  = 1 << (tm->tm_wday);
2571e3929efSAlexandre Belloni 	date[RV8803_DAY]   = bin2bcd(tm->tm_mday);
2581e3929efSAlexandre Belloni 	date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
2591e3929efSAlexandre Belloni 	date[RV8803_YEAR]  = bin2bcd(tm->tm_year - 100);
2601e3929efSAlexandre Belloni 
261d522649eSBenoît Thébaudeau 	ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
262d522649eSBenoît Thébaudeau 	if (ret)
2631e3929efSAlexandre Belloni 		return ret;
2641e3929efSAlexandre Belloni 
265d3700b6bSBenoît Thébaudeau 	/* Restart the clock */
266d3700b6bSBenoît Thébaudeau 	ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
267d3700b6bSBenoît Thébaudeau 			       ctrl & ~RV8803_CTRL_RESET);
268d3700b6bSBenoît Thébaudeau 	if (ret)
269d3700b6bSBenoît Thébaudeau 		return ret;
270d3700b6bSBenoît Thébaudeau 
2719d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
2721e3929efSAlexandre Belloni 
273d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
2741e3929efSAlexandre Belloni 	if (flags < 0) {
2759d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
2761e3929efSAlexandre Belloni 		return flags;
2771e3929efSAlexandre Belloni 	}
2781e3929efSAlexandre Belloni 
279d522649eSBenoît Thébaudeau 	ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
2806f367788SBenoît Thébaudeau 			       flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
2811e3929efSAlexandre Belloni 
2829d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
2831e3929efSAlexandre Belloni 
2841e3929efSAlexandre Belloni 	return ret;
2851e3929efSAlexandre Belloni }
2861e3929efSAlexandre Belloni 
2871e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2881e3929efSAlexandre Belloni {
2891e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
2901e3929efSAlexandre Belloni 	struct i2c_client *client = rv8803->client;
2911e3929efSAlexandre Belloni 	u8 alarmvals[3];
2921e3929efSAlexandre Belloni 	int flags, ret;
2931e3929efSAlexandre Belloni 
294d522649eSBenoît Thébaudeau 	ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
295d522649eSBenoît Thébaudeau 	if (ret)
296d522649eSBenoît Thébaudeau 		return ret;
2971e3929efSAlexandre Belloni 
298d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(client, RV8803_FLAG);
2991e3929efSAlexandre Belloni 	if (flags < 0)
3001e3929efSAlexandre Belloni 		return flags;
3011e3929efSAlexandre Belloni 
3021e3929efSAlexandre Belloni 	alrm->time.tm_sec  = 0;
3031e3929efSAlexandre Belloni 	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
3041e3929efSAlexandre Belloni 	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
3051e3929efSAlexandre Belloni 	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
3061e3929efSAlexandre Belloni 
3071e3929efSAlexandre Belloni 	alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
3081e3929efSAlexandre Belloni 	alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
3091e3929efSAlexandre Belloni 
3101e3929efSAlexandre Belloni 	return 0;
3111e3929efSAlexandre Belloni }
3121e3929efSAlexandre Belloni 
3131e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3141e3929efSAlexandre Belloni {
3151e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
3161e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
3171e3929efSAlexandre Belloni 	u8 alarmvals[3];
3181e3929efSAlexandre Belloni 	u8 ctrl[2];
3191e3929efSAlexandre Belloni 	int ret, err;
3201e3929efSAlexandre Belloni 
3211e3929efSAlexandre Belloni 	/* The alarm has no seconds, round up to nearest minute */
3221e3929efSAlexandre Belloni 	if (alrm->time.tm_sec) {
3231e3929efSAlexandre Belloni 		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
3241e3929efSAlexandre Belloni 
3251e3929efSAlexandre Belloni 		alarm_time += 60 - alrm->time.tm_sec;
3261e3929efSAlexandre Belloni 		rtc_time64_to_tm(alarm_time, &alrm->time);
3271e3929efSAlexandre Belloni 	}
3281e3929efSAlexandre Belloni 
3299d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
3301e3929efSAlexandre Belloni 
331d522649eSBenoît Thébaudeau 	ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
332d522649eSBenoît Thébaudeau 	if (ret) {
3339d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
334d522649eSBenoît Thébaudeau 		return ret;
3351e3929efSAlexandre Belloni 	}
3361e3929efSAlexandre Belloni 
3371e3929efSAlexandre Belloni 	alarmvals[0] = bin2bcd(alrm->time.tm_min);
3381e3929efSAlexandre Belloni 	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
3391e3929efSAlexandre Belloni 	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
3401e3929efSAlexandre Belloni 
3411e3929efSAlexandre Belloni 	if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
3421e3929efSAlexandre Belloni 		rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
343d522649eSBenoît Thébaudeau 		err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
3441e3929efSAlexandre Belloni 				       rv8803->ctrl);
3451e3929efSAlexandre Belloni 		if (err) {
3469d1fa4c3SOleksij Rempel 			mutex_unlock(&rv8803->flags_lock);
3471e3929efSAlexandre Belloni 			return err;
3481e3929efSAlexandre Belloni 		}
3491e3929efSAlexandre Belloni 	}
3501e3929efSAlexandre Belloni 
3511e3929efSAlexandre Belloni 	ctrl[1] &= ~RV8803_FLAG_AF;
352d522649eSBenoît Thébaudeau 	err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]);
3539d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
3541e3929efSAlexandre Belloni 	if (err)
3551e3929efSAlexandre Belloni 		return err;
3561e3929efSAlexandre Belloni 
357d522649eSBenoît Thébaudeau 	err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
3581e3929efSAlexandre Belloni 	if (err)
3591e3929efSAlexandre Belloni 		return err;
3601e3929efSAlexandre Belloni 
3611e3929efSAlexandre Belloni 	if (alrm->enabled) {
3621e3929efSAlexandre Belloni 		if (rv8803->rtc->uie_rtctimer.enabled)
3631e3929efSAlexandre Belloni 			rv8803->ctrl |= RV8803_CTRL_UIE;
3641e3929efSAlexandre Belloni 		if (rv8803->rtc->aie_timer.enabled)
3651e3929efSAlexandre Belloni 			rv8803->ctrl |= RV8803_CTRL_AIE;
3661e3929efSAlexandre Belloni 
367d522649eSBenoît Thébaudeau 		err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
3681e3929efSAlexandre Belloni 				       rv8803->ctrl);
3691e3929efSAlexandre Belloni 		if (err)
3701e3929efSAlexandre Belloni 			return err;
3711e3929efSAlexandre Belloni 	}
3721e3929efSAlexandre Belloni 
3731e3929efSAlexandre Belloni 	return 0;
3741e3929efSAlexandre Belloni }
3751e3929efSAlexandre Belloni 
3761e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
3771e3929efSAlexandre Belloni {
3781e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
3791e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
3801e3929efSAlexandre Belloni 	int ctrl, flags, err;
3811e3929efSAlexandre Belloni 
3821e3929efSAlexandre Belloni 	ctrl = rv8803->ctrl;
3831e3929efSAlexandre Belloni 
3841e3929efSAlexandre Belloni 	if (enabled) {
3851e3929efSAlexandre Belloni 		if (rv8803->rtc->uie_rtctimer.enabled)
3861e3929efSAlexandre Belloni 			ctrl |= RV8803_CTRL_UIE;
3871e3929efSAlexandre Belloni 		if (rv8803->rtc->aie_timer.enabled)
3881e3929efSAlexandre Belloni 			ctrl |= RV8803_CTRL_AIE;
3891e3929efSAlexandre Belloni 	} else {
3901e3929efSAlexandre Belloni 		if (!rv8803->rtc->uie_rtctimer.enabled)
3911e3929efSAlexandre Belloni 			ctrl &= ~RV8803_CTRL_UIE;
3921e3929efSAlexandre Belloni 		if (!rv8803->rtc->aie_timer.enabled)
3931e3929efSAlexandre Belloni 			ctrl &= ~RV8803_CTRL_AIE;
3941e3929efSAlexandre Belloni 	}
3951e3929efSAlexandre Belloni 
3969d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
397d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(client, RV8803_FLAG);
3981e3929efSAlexandre Belloni 	if (flags < 0) {
3999d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
4001e3929efSAlexandre Belloni 		return flags;
4011e3929efSAlexandre Belloni 	}
4021e3929efSAlexandre Belloni 	flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
403d522649eSBenoît Thébaudeau 	err = rv8803_write_reg(client, RV8803_FLAG, flags);
4049d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
4051e3929efSAlexandre Belloni 	if (err)
4061e3929efSAlexandre Belloni 		return err;
4071e3929efSAlexandre Belloni 
4081e3929efSAlexandre Belloni 	if (ctrl != rv8803->ctrl) {
4091e3929efSAlexandre Belloni 		rv8803->ctrl = ctrl;
410d522649eSBenoît Thébaudeau 		err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
4111e3929efSAlexandre Belloni 		if (err)
4121e3929efSAlexandre Belloni 			return err;
4131e3929efSAlexandre Belloni 	}
4141e3929efSAlexandre Belloni 
4151e3929efSAlexandre Belloni 	return 0;
4161e3929efSAlexandre Belloni }
4171e3929efSAlexandre Belloni 
4181e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
4191e3929efSAlexandre Belloni {
4201e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
4211e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
4221e3929efSAlexandre Belloni 	int flags, ret = 0;
4231e3929efSAlexandre Belloni 
4241e3929efSAlexandre Belloni 	switch (cmd) {
4251e3929efSAlexandre Belloni 	case RTC_VL_READ:
426d522649eSBenoît Thébaudeau 		flags = rv8803_read_reg(client, RV8803_FLAG);
4271e3929efSAlexandre Belloni 		if (flags < 0)
4281e3929efSAlexandre Belloni 			return flags;
4291e3929efSAlexandre Belloni 
4301e3929efSAlexandre Belloni 		if (flags & RV8803_FLAG_V1F)
4311e3929efSAlexandre Belloni 			dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
4321e3929efSAlexandre Belloni 
4331e3929efSAlexandre Belloni 		if (flags & RV8803_FLAG_V2F)
4341e3929efSAlexandre Belloni 			dev_warn(&client->dev, "Voltage low, data loss detected.\n");
4351e3929efSAlexandre Belloni 
4361e3929efSAlexandre Belloni 		flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
4371e3929efSAlexandre Belloni 
4381e3929efSAlexandre Belloni 		if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
4391e3929efSAlexandre Belloni 			return -EFAULT;
4401e3929efSAlexandre Belloni 
4411e3929efSAlexandre Belloni 		return 0;
4421e3929efSAlexandre Belloni 
4431e3929efSAlexandre Belloni 	case RTC_VL_CLR:
4449d1fa4c3SOleksij Rempel 		mutex_lock(&rv8803->flags_lock);
445d522649eSBenoît Thébaudeau 		flags = rv8803_read_reg(client, RV8803_FLAG);
4461e3929efSAlexandre Belloni 		if (flags < 0) {
4479d1fa4c3SOleksij Rempel 			mutex_unlock(&rv8803->flags_lock);
4481e3929efSAlexandre Belloni 			return flags;
4491e3929efSAlexandre Belloni 		}
4501e3929efSAlexandre Belloni 
4511e3929efSAlexandre Belloni 		flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
452d522649eSBenoît Thébaudeau 		ret = rv8803_write_reg(client, RV8803_FLAG, flags);
4539d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
454d522649eSBenoît Thébaudeau 		if (ret)
4551e3929efSAlexandre Belloni 			return ret;
4561e3929efSAlexandre Belloni 
4571e3929efSAlexandre Belloni 		return 0;
4581e3929efSAlexandre Belloni 
4591e3929efSAlexandre Belloni 	default:
4601e3929efSAlexandre Belloni 		return -ENOIOCTLCMD;
4611e3929efSAlexandre Belloni 	}
4621e3929efSAlexandre Belloni }
4631e3929efSAlexandre Belloni 
464*16d70a78SAlexandre Belloni static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
465*16d70a78SAlexandre Belloni 			      size_t bytes)
4661e3929efSAlexandre Belloni {
4671e3929efSAlexandre Belloni 	int ret;
4681e3929efSAlexandre Belloni 
469*16d70a78SAlexandre Belloni 	ret = rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
470d522649eSBenoît Thébaudeau 	if (ret)
4711e3929efSAlexandre Belloni 		return ret;
4721e3929efSAlexandre Belloni 
473*16d70a78SAlexandre Belloni 	return 0;
4741e3929efSAlexandre Belloni }
4751e3929efSAlexandre Belloni 
476*16d70a78SAlexandre Belloni static int rv8803_nvram_read(void *priv, unsigned int offset,
477*16d70a78SAlexandre Belloni 			     void *val, size_t bytes)
4781e3929efSAlexandre Belloni {
4791e3929efSAlexandre Belloni 	int ret;
4801e3929efSAlexandre Belloni 
481*16d70a78SAlexandre Belloni 	ret = rv8803_read_reg(priv, RV8803_RAM);
4821e3929efSAlexandre Belloni 	if (ret < 0)
4831e3929efSAlexandre Belloni 		return ret;
4841e3929efSAlexandre Belloni 
485*16d70a78SAlexandre Belloni 	*(u8 *)val = ret;
4861e3929efSAlexandre Belloni 
487*16d70a78SAlexandre Belloni 	return 0;
4881e3929efSAlexandre Belloni }
4891e3929efSAlexandre Belloni 
4901e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = {
4911e3929efSAlexandre Belloni 	.read_time = rv8803_get_time,
4921e3929efSAlexandre Belloni 	.set_time = rv8803_set_time,
4931e3929efSAlexandre Belloni 	.ioctl = rv8803_ioctl,
4941e3929efSAlexandre Belloni };
4951e3929efSAlexandre Belloni 
4961cd71376SOleksij Rempel static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
4971cd71376SOleksij Rempel {
4981cd71376SOleksij Rempel 	struct i2c_client *client = rv8803->client;
4991cd71376SOleksij Rempel 	struct device_node *node = client->dev.of_node;
5001cd71376SOleksij Rempel 	int err;
5011cd71376SOleksij Rempel 	u8 flags;
5021cd71376SOleksij Rempel 
5031cd71376SOleksij Rempel 	if (!node)
5041cd71376SOleksij Rempel 		return 0;
5051cd71376SOleksij Rempel 
5061cd71376SOleksij Rempel 	if (rv8803->type != rx_8900)
5071cd71376SOleksij Rempel 		return 0;
5081cd71376SOleksij Rempel 
5091cd71376SOleksij Rempel 	err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
5101cd71376SOleksij Rempel 	if (err < 0)
5111cd71376SOleksij Rempel 		return err;
5121cd71376SOleksij Rempel 
5131cd71376SOleksij Rempel 	flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
5141cd71376SOleksij Rempel 
5151cd71376SOleksij Rempel 	if (of_property_read_bool(node, "epson,vdet-disable"))
5161cd71376SOleksij Rempel 		flags |= RX8900_FLAG_VDETOFF;
5171cd71376SOleksij Rempel 
5181cd71376SOleksij Rempel 	if (of_property_read_bool(node, "trickle-diode-disable"))
5191cd71376SOleksij Rempel 		flags |= RX8900_FLAG_SWOFF;
5201cd71376SOleksij Rempel 
5211cd71376SOleksij Rempel 	return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
5221cd71376SOleksij Rempel 					 flags);
5231cd71376SOleksij Rempel }
5241cd71376SOleksij Rempel 
5251e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client,
5261e3929efSAlexandre Belloni 			const struct i2c_device_id *id)
5271e3929efSAlexandre Belloni {
5281e3929efSAlexandre Belloni 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5291e3929efSAlexandre Belloni 	struct rv8803_data *rv8803;
530d522649eSBenoît Thébaudeau 	int err, flags;
5311e3929efSAlexandre Belloni 
5321e3929efSAlexandre Belloni 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
5331e3929efSAlexandre Belloni 				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
5341e3929efSAlexandre Belloni 		dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
5351e3929efSAlexandre Belloni 		return -EIO;
5361e3929efSAlexandre Belloni 	}
5371e3929efSAlexandre Belloni 
5381e3929efSAlexandre Belloni 	rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
5391e3929efSAlexandre Belloni 			      GFP_KERNEL);
5401e3929efSAlexandre Belloni 	if (!rv8803)
5411e3929efSAlexandre Belloni 		return -ENOMEM;
5421e3929efSAlexandre Belloni 
5439d1fa4c3SOleksij Rempel 	mutex_init(&rv8803->flags_lock);
5441e3929efSAlexandre Belloni 	rv8803->client = client;
545740ad8f4SJavier Martinez Canillas 	if (client->dev.of_node)
546740ad8f4SJavier Martinez Canillas 		rv8803->type = (enum rv8803_type)
547740ad8f4SJavier Martinez Canillas 			of_device_get_match_data(&client->dev);
548740ad8f4SJavier Martinez Canillas 	else
5491cd71376SOleksij Rempel 		rv8803->type = id->driver_data;
5501e3929efSAlexandre Belloni 	i2c_set_clientdata(client, rv8803);
5511e3929efSAlexandre Belloni 
552d522649eSBenoît Thébaudeau 	flags = rv8803_read_reg(client, RV8803_FLAG);
5531e3929efSAlexandre Belloni 	if (flags < 0)
5541e3929efSAlexandre Belloni 		return flags;
5551e3929efSAlexandre Belloni 
5561e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V1F)
5571e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
5581e3929efSAlexandre Belloni 
5591e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F)
5601e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
5611e3929efSAlexandre Belloni 
5621e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_AF)
5631e3929efSAlexandre Belloni 		dev_warn(&client->dev, "An alarm maybe have been missed.\n");
5641e3929efSAlexandre Belloni 
5657133eca1SAlexandre Belloni 	rv8803->rtc = devm_rtc_allocate_device(&client->dev);
5667133eca1SAlexandre Belloni 	if (IS_ERR(rv8803->rtc)) {
5677133eca1SAlexandre Belloni 		return PTR_ERR(rv8803->rtc);
5687133eca1SAlexandre Belloni 	}
5697133eca1SAlexandre Belloni 
5701e3929efSAlexandre Belloni 	if (client->irq > 0) {
5711e3929efSAlexandre Belloni 		err = devm_request_threaded_irq(&client->dev, client->irq,
5721e3929efSAlexandre Belloni 						NULL, rv8803_handle_irq,
5731e3929efSAlexandre Belloni 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
5741e3929efSAlexandre Belloni 						"rv8803", client);
5751e3929efSAlexandre Belloni 		if (err) {
5761e3929efSAlexandre Belloni 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
5771e3929efSAlexandre Belloni 			client->irq = 0;
5781e3929efSAlexandre Belloni 		} else {
5791e3929efSAlexandre Belloni 			rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
5801e3929efSAlexandre Belloni 			rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
5811e3929efSAlexandre Belloni 			rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
5821e3929efSAlexandre Belloni 		}
5831e3929efSAlexandre Belloni 	}
5841e3929efSAlexandre Belloni 
585*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.name = "rv8803_nvram",
586*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.word_size = 1,
587*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.stride = 1,
588*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.size = 1,
589*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.reg_read = rv8803_nvram_read,
590*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.reg_write = rv8803_nvram_write,
591*16d70a78SAlexandre Belloni 	rv8803->nvmem_cfg.priv = client;
592*16d70a78SAlexandre Belloni 
5937133eca1SAlexandre Belloni 	rv8803->rtc->ops = &rv8803_rtc_ops;
594*16d70a78SAlexandre Belloni 	rv8803->rtc->nvmem_config = &rv8803->nvmem_cfg;
595*16d70a78SAlexandre Belloni 	rv8803->rtc->nvram_old_abi = true;
5967133eca1SAlexandre Belloni 	err = rtc_register_device(rv8803->rtc);
5977133eca1SAlexandre Belloni 	if (err)
5987133eca1SAlexandre Belloni 		return err;
5991e3929efSAlexandre Belloni 
600d522649eSBenoît Thébaudeau 	err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
6011e3929efSAlexandre Belloni 	if (err)
6021e3929efSAlexandre Belloni 		return err;
6031e3929efSAlexandre Belloni 
6041cd71376SOleksij Rempel 	err = rx8900_trickle_charger_init(rv8803);
6051cd71376SOleksij Rempel 	if (err) {
6061cd71376SOleksij Rempel 		dev_err(&client->dev, "failed to init charger\n");
6071cd71376SOleksij Rempel 		return err;
6081cd71376SOleksij Rempel 	}
6091cd71376SOleksij Rempel 
6101e3929efSAlexandre Belloni 	rv8803->rtc->max_user_freq = 1;
6111e3929efSAlexandre Belloni 
6121e3929efSAlexandre Belloni 	return 0;
6131e3929efSAlexandre Belloni }
6141e3929efSAlexandre Belloni 
6151e3929efSAlexandre Belloni static int rv8803_remove(struct i2c_client *client)
6161e3929efSAlexandre Belloni {
6171e3929efSAlexandre Belloni 	return 0;
6181e3929efSAlexandre Belloni }
6191e3929efSAlexandre Belloni 
6201e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = {
6211cd71376SOleksij Rempel 	{ "rv8803", rv_8803 },
6221cd71376SOleksij Rempel 	{ "rx8900", rx_8900 },
6231e3929efSAlexandre Belloni 	{ }
6241e3929efSAlexandre Belloni };
6251e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id);
6261e3929efSAlexandre Belloni 
627740ad8f4SJavier Martinez Canillas static const struct of_device_id rv8803_of_match[] = {
628740ad8f4SJavier Martinez Canillas 	{
629740ad8f4SJavier Martinez Canillas 		.compatible = "microcrystal,rv8803",
630740ad8f4SJavier Martinez Canillas 		.data = (void *)rx_8900
631740ad8f4SJavier Martinez Canillas 	},
632740ad8f4SJavier Martinez Canillas 	{
633740ad8f4SJavier Martinez Canillas 		.compatible = "epson,rx8900",
634740ad8f4SJavier Martinez Canillas 		.data = (void *)rx_8900
635740ad8f4SJavier Martinez Canillas 	},
636740ad8f4SJavier Martinez Canillas 	{ }
637740ad8f4SJavier Martinez Canillas };
638740ad8f4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, rv8803_of_match);
639740ad8f4SJavier Martinez Canillas 
6401e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = {
6411e3929efSAlexandre Belloni 	.driver = {
6421e3929efSAlexandre Belloni 		.name = "rtc-rv8803",
643740ad8f4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(rv8803_of_match),
6441e3929efSAlexandre Belloni 	},
6451e3929efSAlexandre Belloni 	.probe		= rv8803_probe,
6461e3929efSAlexandre Belloni 	.remove		= rv8803_remove,
6471e3929efSAlexandre Belloni 	.id_table	= rv8803_id,
6481e3929efSAlexandre Belloni };
6491e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver);
6501e3929efSAlexandre Belloni 
6511e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
6521e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
6531e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2");
654