xref: /linux/drivers/rtc/rtc-rv8803.c (revision 96acb25c50e77355673b89765b96bd764abf487d)
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>
161e3929efSAlexandre Belloni #include <linux/i2c.h>
171e3929efSAlexandre Belloni #include <linux/interrupt.h>
181e3929efSAlexandre Belloni #include <linux/kernel.h>
191e3929efSAlexandre Belloni #include <linux/module.h>
201e3929efSAlexandre Belloni #include <linux/rtc.h>
211e3929efSAlexandre Belloni 
221e3929efSAlexandre Belloni #define RV8803_SEC			0x00
231e3929efSAlexandre Belloni #define RV8803_MIN			0x01
241e3929efSAlexandre Belloni #define RV8803_HOUR			0x02
251e3929efSAlexandre Belloni #define RV8803_WEEK			0x03
261e3929efSAlexandre Belloni #define RV8803_DAY			0x04
271e3929efSAlexandre Belloni #define RV8803_MONTH			0x05
281e3929efSAlexandre Belloni #define RV8803_YEAR			0x06
291e3929efSAlexandre Belloni #define RV8803_RAM			0x07
301e3929efSAlexandre Belloni #define RV8803_ALARM_MIN		0x08
311e3929efSAlexandre Belloni #define RV8803_ALARM_HOUR		0x09
321e3929efSAlexandre Belloni #define RV8803_ALARM_WEEK_OR_DAY	0x0A
331e3929efSAlexandre Belloni #define RV8803_EXT			0x0D
341e3929efSAlexandre Belloni #define RV8803_FLAG			0x0E
351e3929efSAlexandre Belloni #define RV8803_CTRL			0x0F
361e3929efSAlexandre Belloni 
371e3929efSAlexandre Belloni #define RV8803_EXT_WADA			BIT(6)
381e3929efSAlexandre Belloni 
391e3929efSAlexandre Belloni #define RV8803_FLAG_V1F			BIT(0)
401e3929efSAlexandre Belloni #define RV8803_FLAG_V2F			BIT(1)
411e3929efSAlexandre Belloni #define RV8803_FLAG_AF			BIT(3)
421e3929efSAlexandre Belloni #define RV8803_FLAG_TF			BIT(4)
431e3929efSAlexandre Belloni #define RV8803_FLAG_UF			BIT(5)
441e3929efSAlexandre Belloni 
451e3929efSAlexandre Belloni #define RV8803_CTRL_RESET		BIT(0)
461e3929efSAlexandre Belloni 
471e3929efSAlexandre Belloni #define RV8803_CTRL_EIE			BIT(2)
481e3929efSAlexandre Belloni #define RV8803_CTRL_AIE			BIT(3)
491e3929efSAlexandre Belloni #define RV8803_CTRL_TIE			BIT(4)
501e3929efSAlexandre Belloni #define RV8803_CTRL_UIE			BIT(5)
511e3929efSAlexandre Belloni 
521e3929efSAlexandre Belloni struct rv8803_data {
531e3929efSAlexandre Belloni 	struct i2c_client *client;
541e3929efSAlexandre Belloni 	struct rtc_device *rtc;
559d1fa4c3SOleksij Rempel 	struct mutex flags_lock;
561e3929efSAlexandre Belloni 	u8 ctrl;
571e3929efSAlexandre Belloni };
581e3929efSAlexandre Belloni 
591e3929efSAlexandre Belloni static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
601e3929efSAlexandre Belloni {
611e3929efSAlexandre Belloni 	struct i2c_client *client = dev_id;
621e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = i2c_get_clientdata(client);
631e3929efSAlexandre Belloni 	unsigned long events = 0;
6485062c9bSAlexandre Belloni 	int flags, try = 0;
651e3929efSAlexandre Belloni 
669d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
671e3929efSAlexandre Belloni 
6885062c9bSAlexandre Belloni 	do {
691e3929efSAlexandre Belloni 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
7085062c9bSAlexandre Belloni 		try++;
71cde0fe2aSAlexandre Belloni 	} while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
721e3929efSAlexandre Belloni 	if (flags <= 0) {
739d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
741e3929efSAlexandre Belloni 		return IRQ_NONE;
751e3929efSAlexandre Belloni 	}
761e3929efSAlexandre Belloni 
771e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V1F)
781e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
791e3929efSAlexandre Belloni 
801e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F)
811e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
821e3929efSAlexandre Belloni 
831e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_TF) {
841e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_TF;
851e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_TIE;
861e3929efSAlexandre Belloni 		events |= RTC_PF;
871e3929efSAlexandre Belloni 	}
881e3929efSAlexandre Belloni 
891e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_AF) {
901e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_AF;
911e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_AIE;
921e3929efSAlexandre Belloni 		events |= RTC_AF;
931e3929efSAlexandre Belloni 	}
941e3929efSAlexandre Belloni 
951e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_UF) {
961e3929efSAlexandre Belloni 		flags &= ~RV8803_FLAG_UF;
971e3929efSAlexandre Belloni 		rv8803->ctrl &= ~RV8803_CTRL_UIE;
981e3929efSAlexandre Belloni 		events |= RTC_UF;
991e3929efSAlexandre Belloni 	}
1001e3929efSAlexandre Belloni 
1011e3929efSAlexandre Belloni 	if (events) {
1021e3929efSAlexandre Belloni 		rtc_update_irq(rv8803->rtc, 1, events);
1031e3929efSAlexandre Belloni 		i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
1041e3929efSAlexandre Belloni 		i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
1051e3929efSAlexandre Belloni 					  rv8803->ctrl);
1061e3929efSAlexandre Belloni 	}
1071e3929efSAlexandre Belloni 
1089d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
1091e3929efSAlexandre Belloni 
1101e3929efSAlexandre Belloni 	return IRQ_HANDLED;
1111e3929efSAlexandre Belloni }
1121e3929efSAlexandre Belloni 
1131e3929efSAlexandre Belloni static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
1141e3929efSAlexandre Belloni {
1151e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
1161e3929efSAlexandre Belloni 	u8 date1[7];
1171e3929efSAlexandre Belloni 	u8 date2[7];
1181e3929efSAlexandre Belloni 	u8 *date = date1;
1191e3929efSAlexandre Belloni 	int ret, flags;
1201e3929efSAlexandre Belloni 
1211e3929efSAlexandre Belloni 	flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
1221e3929efSAlexandre Belloni 	if (flags < 0)
1231e3929efSAlexandre Belloni 		return flags;
1241e3929efSAlexandre Belloni 
1251e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F) {
1261e3929efSAlexandre Belloni 		dev_warn(dev, "Voltage low, data is invalid.\n");
1271e3929efSAlexandre Belloni 		return -EINVAL;
1281e3929efSAlexandre Belloni 	}
1291e3929efSAlexandre Belloni 
1301e3929efSAlexandre Belloni 	ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
1311e3929efSAlexandre Belloni 					    7, date);
1321e3929efSAlexandre Belloni 	if (ret != 7)
1331e3929efSAlexandre Belloni 		return ret < 0 ? ret : -EIO;
1341e3929efSAlexandre Belloni 
1351e3929efSAlexandre Belloni 	if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
1361e3929efSAlexandre Belloni 		ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
1371e3929efSAlexandre Belloni 						    7, date2);
1381e3929efSAlexandre Belloni 		if (ret != 7)
1391e3929efSAlexandre Belloni 			return ret < 0 ? ret : -EIO;
1401e3929efSAlexandre Belloni 
1411e3929efSAlexandre Belloni 		if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
1421e3929efSAlexandre Belloni 			date = date2;
1431e3929efSAlexandre Belloni 	}
1441e3929efSAlexandre Belloni 
1451e3929efSAlexandre Belloni 	tm->tm_sec  = bcd2bin(date[RV8803_SEC] & 0x7f);
1461e3929efSAlexandre Belloni 	tm->tm_min  = bcd2bin(date[RV8803_MIN] & 0x7f);
1471e3929efSAlexandre Belloni 	tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
1481e3929efSAlexandre Belloni 	tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
1491e3929efSAlexandre Belloni 	tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
1501e3929efSAlexandre Belloni 	tm->tm_mon  = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
1511e3929efSAlexandre Belloni 	tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
1521e3929efSAlexandre Belloni 
153*96acb25cSBenoît Thébaudeau 	return 0;
1541e3929efSAlexandre Belloni }
1551e3929efSAlexandre Belloni 
1561e3929efSAlexandre Belloni static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
1571e3929efSAlexandre Belloni {
1581e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
1591e3929efSAlexandre Belloni 	u8 date[7];
1601e3929efSAlexandre Belloni 	int flags, ret;
1611e3929efSAlexandre Belloni 
1621e3929efSAlexandre Belloni 	if ((tm->tm_year < 100) || (tm->tm_year > 199))
1631e3929efSAlexandre Belloni 		return -EINVAL;
1641e3929efSAlexandre Belloni 
1651e3929efSAlexandre Belloni 	date[RV8803_SEC]   = bin2bcd(tm->tm_sec);
1661e3929efSAlexandre Belloni 	date[RV8803_MIN]   = bin2bcd(tm->tm_min);
1671e3929efSAlexandre Belloni 	date[RV8803_HOUR]  = bin2bcd(tm->tm_hour);
1681e3929efSAlexandre Belloni 	date[RV8803_WEEK]  = 1 << (tm->tm_wday);
1691e3929efSAlexandre Belloni 	date[RV8803_DAY]   = bin2bcd(tm->tm_mday);
1701e3929efSAlexandre Belloni 	date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
1711e3929efSAlexandre Belloni 	date[RV8803_YEAR]  = bin2bcd(tm->tm_year - 100);
1721e3929efSAlexandre Belloni 
1731e3929efSAlexandre Belloni 	ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
1741e3929efSAlexandre Belloni 					     7, date);
1751e3929efSAlexandre Belloni 	if (ret < 0)
1761e3929efSAlexandre Belloni 		return ret;
1771e3929efSAlexandre Belloni 
1789d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
1791e3929efSAlexandre Belloni 
1801e3929efSAlexandre Belloni 	flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
1811e3929efSAlexandre Belloni 	if (flags < 0) {
1829d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
1831e3929efSAlexandre Belloni 		return flags;
1841e3929efSAlexandre Belloni 	}
1851e3929efSAlexandre Belloni 
1861e3929efSAlexandre Belloni 	ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
1871e3929efSAlexandre Belloni 					flags & ~RV8803_FLAG_V2F);
1881e3929efSAlexandre Belloni 
1899d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
1901e3929efSAlexandre Belloni 
1911e3929efSAlexandre Belloni 	return ret;
1921e3929efSAlexandre Belloni }
1931e3929efSAlexandre Belloni 
1941e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1951e3929efSAlexandre Belloni {
1961e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
1971e3929efSAlexandre Belloni 	struct i2c_client *client = rv8803->client;
1981e3929efSAlexandre Belloni 	u8 alarmvals[3];
1991e3929efSAlexandre Belloni 	int flags, ret;
2001e3929efSAlexandre Belloni 
2011e3929efSAlexandre Belloni 	ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
2021e3929efSAlexandre Belloni 					    3, alarmvals);
2031e3929efSAlexandre Belloni 	if (ret != 3)
2041e3929efSAlexandre Belloni 		return ret < 0 ? ret : -EIO;
2051e3929efSAlexandre Belloni 
2061e3929efSAlexandre Belloni 	flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
2071e3929efSAlexandre Belloni 	if (flags < 0)
2081e3929efSAlexandre Belloni 		return flags;
2091e3929efSAlexandre Belloni 
2101e3929efSAlexandre Belloni 	alrm->time.tm_sec  = 0;
2111e3929efSAlexandre Belloni 	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
2121e3929efSAlexandre Belloni 	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
2131e3929efSAlexandre Belloni 	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
2141e3929efSAlexandre Belloni 
2151e3929efSAlexandre Belloni 	alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
2161e3929efSAlexandre Belloni 	alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
2171e3929efSAlexandre Belloni 
2181e3929efSAlexandre Belloni 	return 0;
2191e3929efSAlexandre Belloni }
2201e3929efSAlexandre Belloni 
2211e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2221e3929efSAlexandre Belloni {
2231e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
2241e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
2251e3929efSAlexandre Belloni 	u8 alarmvals[3];
2261e3929efSAlexandre Belloni 	u8 ctrl[2];
2271e3929efSAlexandre Belloni 	int ret, err;
2281e3929efSAlexandre Belloni 
2291e3929efSAlexandre Belloni 	/* The alarm has no seconds, round up to nearest minute */
2301e3929efSAlexandre Belloni 	if (alrm->time.tm_sec) {
2311e3929efSAlexandre Belloni 		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
2321e3929efSAlexandre Belloni 
2331e3929efSAlexandre Belloni 		alarm_time += 60 - alrm->time.tm_sec;
2341e3929efSAlexandre Belloni 		rtc_time64_to_tm(alarm_time, &alrm->time);
2351e3929efSAlexandre Belloni 	}
2361e3929efSAlexandre Belloni 
2379d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
2381e3929efSAlexandre Belloni 
2391e3929efSAlexandre Belloni 	ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
2401e3929efSAlexandre Belloni 	if (ret != 2) {
2419d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
2421e3929efSAlexandre Belloni 		return ret < 0 ? ret : -EIO;
2431e3929efSAlexandre Belloni 	}
2441e3929efSAlexandre Belloni 
2451e3929efSAlexandre Belloni 	alarmvals[0] = bin2bcd(alrm->time.tm_min);
2461e3929efSAlexandre Belloni 	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
2471e3929efSAlexandre Belloni 	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
2481e3929efSAlexandre Belloni 
2491e3929efSAlexandre Belloni 	if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
2501e3929efSAlexandre Belloni 		rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
2511e3929efSAlexandre Belloni 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
2521e3929efSAlexandre Belloni 						rv8803->ctrl);
2531e3929efSAlexandre Belloni 		if (err) {
2549d1fa4c3SOleksij Rempel 			mutex_unlock(&rv8803->flags_lock);
2551e3929efSAlexandre Belloni 			return err;
2561e3929efSAlexandre Belloni 		}
2571e3929efSAlexandre Belloni 	}
2581e3929efSAlexandre Belloni 
2591e3929efSAlexandre Belloni 	ctrl[1] &= ~RV8803_FLAG_AF;
2601e3929efSAlexandre Belloni 	err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
2619d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
2621e3929efSAlexandre Belloni 	if (err)
2631e3929efSAlexandre Belloni 		return err;
2641e3929efSAlexandre Belloni 
2651e3929efSAlexandre Belloni 	err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
2661e3929efSAlexandre Belloni 					     3, alarmvals);
2671e3929efSAlexandre Belloni 	if (err)
2681e3929efSAlexandre Belloni 		return err;
2691e3929efSAlexandre Belloni 
2701e3929efSAlexandre Belloni 	if (alrm->enabled) {
2711e3929efSAlexandre Belloni 		if (rv8803->rtc->uie_rtctimer.enabled)
2721e3929efSAlexandre Belloni 			rv8803->ctrl |= RV8803_CTRL_UIE;
2731e3929efSAlexandre Belloni 		if (rv8803->rtc->aie_timer.enabled)
2741e3929efSAlexandre Belloni 			rv8803->ctrl |= RV8803_CTRL_AIE;
2751e3929efSAlexandre Belloni 
2761e3929efSAlexandre Belloni 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
2771e3929efSAlexandre Belloni 						rv8803->ctrl);
2781e3929efSAlexandre Belloni 		if (err)
2791e3929efSAlexandre Belloni 			return err;
2801e3929efSAlexandre Belloni 	}
2811e3929efSAlexandre Belloni 
2821e3929efSAlexandre Belloni 	return 0;
2831e3929efSAlexandre Belloni }
2841e3929efSAlexandre Belloni 
2851e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
2861e3929efSAlexandre Belloni {
2871e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
2881e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
2891e3929efSAlexandre Belloni 	int ctrl, flags, err;
2901e3929efSAlexandre Belloni 
2911e3929efSAlexandre Belloni 	ctrl = rv8803->ctrl;
2921e3929efSAlexandre Belloni 
2931e3929efSAlexandre Belloni 	if (enabled) {
2941e3929efSAlexandre Belloni 		if (rv8803->rtc->uie_rtctimer.enabled)
2951e3929efSAlexandre Belloni 			ctrl |= RV8803_CTRL_UIE;
2961e3929efSAlexandre Belloni 		if (rv8803->rtc->aie_timer.enabled)
2971e3929efSAlexandre Belloni 			ctrl |= RV8803_CTRL_AIE;
2981e3929efSAlexandre Belloni 	} else {
2991e3929efSAlexandre Belloni 		if (!rv8803->rtc->uie_rtctimer.enabled)
3001e3929efSAlexandre Belloni 			ctrl &= ~RV8803_CTRL_UIE;
3011e3929efSAlexandre Belloni 		if (!rv8803->rtc->aie_timer.enabled)
3021e3929efSAlexandre Belloni 			ctrl &= ~RV8803_CTRL_AIE;
3031e3929efSAlexandre Belloni 	}
3041e3929efSAlexandre Belloni 
3059d1fa4c3SOleksij Rempel 	mutex_lock(&rv8803->flags_lock);
3061e3929efSAlexandre Belloni 	flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
3071e3929efSAlexandre Belloni 	if (flags < 0) {
3089d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
3091e3929efSAlexandre Belloni 		return flags;
3101e3929efSAlexandre Belloni 	}
3111e3929efSAlexandre Belloni 	flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
3121e3929efSAlexandre Belloni 	err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
3139d1fa4c3SOleksij Rempel 	mutex_unlock(&rv8803->flags_lock);
3141e3929efSAlexandre Belloni 	if (err)
3151e3929efSAlexandre Belloni 		return err;
3161e3929efSAlexandre Belloni 
3171e3929efSAlexandre Belloni 	if (ctrl != rv8803->ctrl) {
3181e3929efSAlexandre Belloni 		rv8803->ctrl = ctrl;
3191e3929efSAlexandre Belloni 		err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
3201e3929efSAlexandre Belloni 						rv8803->ctrl);
3211e3929efSAlexandre Belloni 		if (err)
3221e3929efSAlexandre Belloni 			return err;
3231e3929efSAlexandre Belloni 	}
3241e3929efSAlexandre Belloni 
3251e3929efSAlexandre Belloni 	return 0;
3261e3929efSAlexandre Belloni }
3271e3929efSAlexandre Belloni 
3281e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
3291e3929efSAlexandre Belloni {
3301e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
3311e3929efSAlexandre Belloni 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
3321e3929efSAlexandre Belloni 	int flags, ret = 0;
3331e3929efSAlexandre Belloni 
3341e3929efSAlexandre Belloni 	switch (cmd) {
3351e3929efSAlexandre Belloni 	case RTC_VL_READ:
3361e3929efSAlexandre Belloni 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
3371e3929efSAlexandre Belloni 		if (flags < 0)
3381e3929efSAlexandre Belloni 			return flags;
3391e3929efSAlexandre Belloni 
3401e3929efSAlexandre Belloni 		if (flags & RV8803_FLAG_V1F)
3411e3929efSAlexandre Belloni 			dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
3421e3929efSAlexandre Belloni 
3431e3929efSAlexandre Belloni 		if (flags & RV8803_FLAG_V2F)
3441e3929efSAlexandre Belloni 			dev_warn(&client->dev, "Voltage low, data loss detected.\n");
3451e3929efSAlexandre Belloni 
3461e3929efSAlexandre Belloni 		flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
3471e3929efSAlexandre Belloni 
3481e3929efSAlexandre Belloni 		if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
3491e3929efSAlexandre Belloni 			return -EFAULT;
3501e3929efSAlexandre Belloni 
3511e3929efSAlexandre Belloni 		return 0;
3521e3929efSAlexandre Belloni 
3531e3929efSAlexandre Belloni 	case RTC_VL_CLR:
3549d1fa4c3SOleksij Rempel 		mutex_lock(&rv8803->flags_lock);
3551e3929efSAlexandre Belloni 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
3561e3929efSAlexandre Belloni 		if (flags < 0) {
3579d1fa4c3SOleksij Rempel 			mutex_unlock(&rv8803->flags_lock);
3581e3929efSAlexandre Belloni 			return flags;
3591e3929efSAlexandre Belloni 		}
3601e3929efSAlexandre Belloni 
3611e3929efSAlexandre Belloni 		flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
3621e3929efSAlexandre Belloni 		ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
3639d1fa4c3SOleksij Rempel 		mutex_unlock(&rv8803->flags_lock);
3641e3929efSAlexandre Belloni 		if (ret < 0)
3651e3929efSAlexandre Belloni 			return ret;
3661e3929efSAlexandre Belloni 
3671e3929efSAlexandre Belloni 		return 0;
3681e3929efSAlexandre Belloni 
3691e3929efSAlexandre Belloni 	default:
3701e3929efSAlexandre Belloni 		return -ENOIOCTLCMD;
3711e3929efSAlexandre Belloni 	}
3721e3929efSAlexandre Belloni }
3731e3929efSAlexandre Belloni 
3741e3929efSAlexandre Belloni static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
3751e3929efSAlexandre Belloni 				  struct bin_attribute *attr,
3761e3929efSAlexandre Belloni 				  char *buf, loff_t off, size_t count)
3771e3929efSAlexandre Belloni {
3781e3929efSAlexandre Belloni 	struct device *dev = kobj_to_dev(kobj);
3791e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
3801e3929efSAlexandre Belloni 	int ret;
3811e3929efSAlexandre Belloni 
3821e3929efSAlexandre Belloni 	ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
3831e3929efSAlexandre Belloni 	if (ret < 0)
3841e3929efSAlexandre Belloni 		return ret;
3851e3929efSAlexandre Belloni 
3861e3929efSAlexandre Belloni 	return 1;
3871e3929efSAlexandre Belloni }
3881e3929efSAlexandre Belloni 
3891e3929efSAlexandre Belloni static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
3901e3929efSAlexandre Belloni 				 struct bin_attribute *attr,
3911e3929efSAlexandre Belloni 				 char *buf, loff_t off, size_t count)
3921e3929efSAlexandre Belloni {
3931e3929efSAlexandre Belloni 	struct device *dev = kobj_to_dev(kobj);
3941e3929efSAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
3951e3929efSAlexandre Belloni 	int ret;
3961e3929efSAlexandre Belloni 
3971e3929efSAlexandre Belloni 	ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
3981e3929efSAlexandre Belloni 	if (ret < 0)
3991e3929efSAlexandre Belloni 		return ret;
4001e3929efSAlexandre Belloni 
4011e3929efSAlexandre Belloni 	buf[0] = ret;
4021e3929efSAlexandre Belloni 
4031e3929efSAlexandre Belloni 	return 1;
4041e3929efSAlexandre Belloni }
4051e3929efSAlexandre Belloni 
4061e3929efSAlexandre Belloni static struct bin_attribute rv8803_nvram_attr = {
4071e3929efSAlexandre Belloni 	.attr = {
4081e3929efSAlexandre Belloni 		.name = "nvram",
4091e3929efSAlexandre Belloni 		.mode = S_IRUGO | S_IWUSR,
4101e3929efSAlexandre Belloni 	},
4111e3929efSAlexandre Belloni 	.size = 1,
4121e3929efSAlexandre Belloni 	.read = rv8803_nvram_read,
4131e3929efSAlexandre Belloni 	.write = rv8803_nvram_write,
4141e3929efSAlexandre Belloni };
4151e3929efSAlexandre Belloni 
4161e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = {
4171e3929efSAlexandre Belloni 	.read_time = rv8803_get_time,
4181e3929efSAlexandre Belloni 	.set_time = rv8803_set_time,
4191e3929efSAlexandre Belloni 	.ioctl = rv8803_ioctl,
4201e3929efSAlexandre Belloni };
4211e3929efSAlexandre Belloni 
4221e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client,
4231e3929efSAlexandre Belloni 			const struct i2c_device_id *id)
4241e3929efSAlexandre Belloni {
4251e3929efSAlexandre Belloni 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4261e3929efSAlexandre Belloni 	struct rv8803_data *rv8803;
42785062c9bSAlexandre Belloni 	int err, flags, try = 0;
4281e3929efSAlexandre Belloni 
4291e3929efSAlexandre Belloni 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
4301e3929efSAlexandre Belloni 				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
4311e3929efSAlexandre Belloni 		dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
4321e3929efSAlexandre Belloni 		return -EIO;
4331e3929efSAlexandre Belloni 	}
4341e3929efSAlexandre Belloni 
4351e3929efSAlexandre Belloni 	rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
4361e3929efSAlexandre Belloni 			      GFP_KERNEL);
4371e3929efSAlexandre Belloni 	if (!rv8803)
4381e3929efSAlexandre Belloni 		return -ENOMEM;
4391e3929efSAlexandre Belloni 
4409d1fa4c3SOleksij Rempel 	mutex_init(&rv8803->flags_lock);
4411e3929efSAlexandre Belloni 	rv8803->client = client;
4421e3929efSAlexandre Belloni 	i2c_set_clientdata(client, rv8803);
4431e3929efSAlexandre Belloni 
44485062c9bSAlexandre Belloni 	/*
44585062c9bSAlexandre Belloni 	 * There is a 60µs window where the RTC may not reply on the i2c bus in
44685062c9bSAlexandre Belloni 	 * that case, the transfer is not ACKed. In that case, ensure there are
44785062c9bSAlexandre Belloni 	 * multiple attempts.
44885062c9bSAlexandre Belloni 	 */
44985062c9bSAlexandre Belloni 	do {
4501e3929efSAlexandre Belloni 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
45185062c9bSAlexandre Belloni 		try++;
452cde0fe2aSAlexandre Belloni 	} while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
45385062c9bSAlexandre Belloni 
4541e3929efSAlexandre Belloni 	if (flags < 0)
4551e3929efSAlexandre Belloni 		return flags;
4561e3929efSAlexandre Belloni 
4571e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V1F)
4581e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
4591e3929efSAlexandre Belloni 
4601e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_V2F)
4611e3929efSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
4621e3929efSAlexandre Belloni 
4631e3929efSAlexandre Belloni 	if (flags & RV8803_FLAG_AF)
4641e3929efSAlexandre Belloni 		dev_warn(&client->dev, "An alarm maybe have been missed.\n");
4651e3929efSAlexandre Belloni 
4661e3929efSAlexandre Belloni 	if (client->irq > 0) {
4671e3929efSAlexandre Belloni 		err = devm_request_threaded_irq(&client->dev, client->irq,
4681e3929efSAlexandre Belloni 						NULL, rv8803_handle_irq,
4691e3929efSAlexandre Belloni 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
4701e3929efSAlexandre Belloni 						"rv8803", client);
4711e3929efSAlexandre Belloni 		if (err) {
4721e3929efSAlexandre Belloni 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
4731e3929efSAlexandre Belloni 			client->irq = 0;
4741e3929efSAlexandre Belloni 		} else {
4751e3929efSAlexandre Belloni 			rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
4761e3929efSAlexandre Belloni 			rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
4771e3929efSAlexandre Belloni 			rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
4781e3929efSAlexandre Belloni 		}
4791e3929efSAlexandre Belloni 	}
4801e3929efSAlexandre Belloni 
4811e3929efSAlexandre Belloni 	rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
4821e3929efSAlexandre Belloni 					       &rv8803_rtc_ops, THIS_MODULE);
4831e3929efSAlexandre Belloni 	if (IS_ERR(rv8803->rtc)) {
4841e3929efSAlexandre Belloni 		dev_err(&client->dev, "unable to register the class device\n");
4851e3929efSAlexandre Belloni 		return PTR_ERR(rv8803->rtc);
4861e3929efSAlexandre Belloni 	}
4871e3929efSAlexandre Belloni 
48885062c9bSAlexandre Belloni 	try = 0;
48985062c9bSAlexandre Belloni 	do {
4901e3929efSAlexandre Belloni 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
4911e3929efSAlexandre Belloni 						RV8803_EXT_WADA);
49285062c9bSAlexandre Belloni 		try++;
493cde0fe2aSAlexandre Belloni 	} while (((err == -ENXIO) || (flags == -EIO)) && (try < 4));
4941e3929efSAlexandre Belloni 	if (err)
4951e3929efSAlexandre Belloni 		return err;
4961e3929efSAlexandre Belloni 
4971e3929efSAlexandre Belloni 	err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
4981e3929efSAlexandre Belloni 	if (err)
4991e3929efSAlexandre Belloni 		return err;
5001e3929efSAlexandre Belloni 
5011e3929efSAlexandre Belloni 	rv8803->rtc->max_user_freq = 1;
5021e3929efSAlexandre Belloni 
5031e3929efSAlexandre Belloni 	return 0;
5041e3929efSAlexandre Belloni }
5051e3929efSAlexandre Belloni 
5061e3929efSAlexandre Belloni static int rv8803_remove(struct i2c_client *client)
5071e3929efSAlexandre Belloni {
5081e3929efSAlexandre Belloni 	device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
5091e3929efSAlexandre Belloni 
5101e3929efSAlexandre Belloni 	return 0;
5111e3929efSAlexandre Belloni }
5121e3929efSAlexandre Belloni 
5131e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = {
5141e3929efSAlexandre Belloni 	{ "rv8803", 0 },
51578ef5f2dSGregory CLEMENT 	{ "rx8900", 0 },
5161e3929efSAlexandre Belloni 	{ }
5171e3929efSAlexandre Belloni };
5181e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id);
5191e3929efSAlexandre Belloni 
5201e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = {
5211e3929efSAlexandre Belloni 	.driver = {
5221e3929efSAlexandre Belloni 		.name = "rtc-rv8803",
5231e3929efSAlexandre Belloni 	},
5241e3929efSAlexandre Belloni 	.probe		= rv8803_probe,
5251e3929efSAlexandre Belloni 	.remove		= rv8803_remove,
5261e3929efSAlexandre Belloni 	.id_table	= rv8803_id,
5271e3929efSAlexandre Belloni };
5281e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver);
5291e3929efSAlexandre Belloni 
5301e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
5311e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
5321e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2");
533