18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * RTC driver for the Micro Crystal RV3032
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Micro Crystal SA
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Alexandre Belloni <alexandre.belloni@bootlin.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/clk.h>
128c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
138c2ecf20Sopenharmony_ci#include <linux/bcd.h>
148c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
158c2ecf20Sopenharmony_ci#include <linux/bitops.h>
168c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/log2.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/of_device.h>
238c2ecf20Sopenharmony_ci#include <linux/regmap.h>
248c2ecf20Sopenharmony_ci#include <linux/rtc.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define RV3032_SEC			0x01
278c2ecf20Sopenharmony_ci#define RV3032_MIN			0x02
288c2ecf20Sopenharmony_ci#define RV3032_HOUR			0x03
298c2ecf20Sopenharmony_ci#define RV3032_WDAY			0x04
308c2ecf20Sopenharmony_ci#define RV3032_DAY			0x05
318c2ecf20Sopenharmony_ci#define RV3032_MONTH			0x06
328c2ecf20Sopenharmony_ci#define RV3032_YEAR			0x07
338c2ecf20Sopenharmony_ci#define RV3032_ALARM_MIN		0x08
348c2ecf20Sopenharmony_ci#define RV3032_ALARM_HOUR		0x09
358c2ecf20Sopenharmony_ci#define RV3032_ALARM_DAY		0x0A
368c2ecf20Sopenharmony_ci#define RV3032_STATUS			0x0D
378c2ecf20Sopenharmony_ci#define RV3032_TLSB			0x0E
388c2ecf20Sopenharmony_ci#define RV3032_TMSB			0x0F
398c2ecf20Sopenharmony_ci#define RV3032_CTRL1			0x10
408c2ecf20Sopenharmony_ci#define RV3032_CTRL2			0x11
418c2ecf20Sopenharmony_ci#define RV3032_CTRL3			0x12
428c2ecf20Sopenharmony_ci#define RV3032_TS_CTRL			0x13
438c2ecf20Sopenharmony_ci#define RV3032_CLK_IRQ			0x14
448c2ecf20Sopenharmony_ci#define RV3032_EEPROM_ADDR		0x3D
458c2ecf20Sopenharmony_ci#define RV3032_EEPROM_DATA		0x3E
468c2ecf20Sopenharmony_ci#define RV3032_EEPROM_CMD		0x3F
478c2ecf20Sopenharmony_ci#define RV3032_RAM1			0x40
488c2ecf20Sopenharmony_ci#define RV3032_PMU			0xC0
498c2ecf20Sopenharmony_ci#define RV3032_OFFSET			0xC1
508c2ecf20Sopenharmony_ci#define RV3032_CLKOUT1			0xC2
518c2ecf20Sopenharmony_ci#define RV3032_CLKOUT2			0xC3
528c2ecf20Sopenharmony_ci#define RV3032_TREF0			0xC4
538c2ecf20Sopenharmony_ci#define RV3032_TREF1			0xC5
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define RV3032_STATUS_VLF		BIT(0)
568c2ecf20Sopenharmony_ci#define RV3032_STATUS_PORF		BIT(1)
578c2ecf20Sopenharmony_ci#define RV3032_STATUS_EVF		BIT(2)
588c2ecf20Sopenharmony_ci#define RV3032_STATUS_AF		BIT(3)
598c2ecf20Sopenharmony_ci#define RV3032_STATUS_TF		BIT(4)
608c2ecf20Sopenharmony_ci#define RV3032_STATUS_UF		BIT(5)
618c2ecf20Sopenharmony_ci#define RV3032_STATUS_TLF		BIT(6)
628c2ecf20Sopenharmony_ci#define RV3032_STATUS_THF		BIT(7)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define RV3032_TLSB_CLKF		BIT(1)
658c2ecf20Sopenharmony_ci#define RV3032_TLSB_EEBUSY		BIT(2)
668c2ecf20Sopenharmony_ci#define RV3032_TLSB_TEMP		GENMASK(7, 4)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define RV3032_CLKOUT2_HFD_MSK		GENMASK(4, 0)
698c2ecf20Sopenharmony_ci#define RV3032_CLKOUT2_FD_MSK		GENMASK(6, 5)
708c2ecf20Sopenharmony_ci#define RV3032_CLKOUT2_OS		BIT(7)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define RV3032_CTRL1_EERD		BIT(3)
738c2ecf20Sopenharmony_ci#define RV3032_CTRL1_WADA		BIT(5)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define RV3032_CTRL2_STOP		BIT(0)
768c2ecf20Sopenharmony_ci#define RV3032_CTRL2_EIE		BIT(2)
778c2ecf20Sopenharmony_ci#define RV3032_CTRL2_AIE		BIT(3)
788c2ecf20Sopenharmony_ci#define RV3032_CTRL2_TIE		BIT(4)
798c2ecf20Sopenharmony_ci#define RV3032_CTRL2_UIE		BIT(5)
808c2ecf20Sopenharmony_ci#define RV3032_CTRL2_CLKIE		BIT(6)
818c2ecf20Sopenharmony_ci#define RV3032_CTRL2_TSE		BIT(7)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define RV3032_PMU_TCM			GENMASK(1, 0)
848c2ecf20Sopenharmony_ci#define RV3032_PMU_TCR			GENMASK(3, 2)
858c2ecf20Sopenharmony_ci#define RV3032_PMU_BSM			GENMASK(5, 4)
868c2ecf20Sopenharmony_ci#define RV3032_PMU_NCLKE		BIT(6)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define RV3032_PMU_BSM_DSM		1
898c2ecf20Sopenharmony_ci#define RV3032_PMU_BSM_LSM		2
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define RV3032_OFFSET_MSK		GENMASK(5, 0)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define RV3032_EVT_CTRL_TSR		BIT(2)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define RV3032_EEPROM_CMD_UPDATE	0x11
968c2ecf20Sopenharmony_ci#define RV3032_EEPROM_CMD_WRITE		0x21
978c2ecf20Sopenharmony_ci#define RV3032_EEPROM_CMD_READ		0x22
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define RV3032_EEPROM_USER		0xCB
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define RV3032_EEBUSY_POLL		10000
1028c2ecf20Sopenharmony_ci#define RV3032_EEBUSY_TIMEOUT		100000
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define OFFSET_STEP_PPT			238419
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistruct rv3032_data {
1078c2ecf20Sopenharmony_ci	struct regmap *regmap;
1088c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
1098c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
1108c2ecf20Sopenharmony_ci	struct clk_hw clkout_hw;
1118c2ecf20Sopenharmony_ci#endif
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic u16 rv3032_trickle_resistors[] = {1000, 2000, 7000, 11000};
1158c2ecf20Sopenharmony_cistatic u16 rv3032_trickle_voltages[] = {0, 1750, 3000, 4400};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int rv3032_exit_eerd(struct rv3032_data *rv3032, u32 eerd)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	if (eerd)
1208c2ecf20Sopenharmony_ci		return 0;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	return regmap_update_bits(rv3032->regmap, RV3032_CTRL1, RV3032_CTRL1_EERD, 0);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int rv3032_enter_eerd(struct rv3032_data *rv3032, u32 *eerd)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u32 ctrl1, status;
1288c2ecf20Sopenharmony_ci	int ret;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_CTRL1, &ctrl1);
1318c2ecf20Sopenharmony_ci	if (ret)
1328c2ecf20Sopenharmony_ci		return ret;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	*eerd = ctrl1 & RV3032_CTRL1_EERD;
1358c2ecf20Sopenharmony_ci	if (*eerd)
1368c2ecf20Sopenharmony_ci		return 0;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1,
1398c2ecf20Sopenharmony_ci				 RV3032_CTRL1_EERD, RV3032_CTRL1_EERD);
1408c2ecf20Sopenharmony_ci	if (ret)
1418c2ecf20Sopenharmony_ci		return ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status,
1448c2ecf20Sopenharmony_ci				       !(status & RV3032_TLSB_EEBUSY),
1458c2ecf20Sopenharmony_ci				       RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
1468c2ecf20Sopenharmony_ci	if (ret) {
1478c2ecf20Sopenharmony_ci		rv3032_exit_eerd(rv3032, *eerd);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		return ret;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int rv3032_update_cfg(struct rv3032_data *rv3032, unsigned int reg,
1568c2ecf20Sopenharmony_ci			     unsigned int mask, unsigned int val)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	u32 status, eerd;
1598c2ecf20Sopenharmony_ci	int ret;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ret = rv3032_enter_eerd(rv3032, &eerd);
1628c2ecf20Sopenharmony_ci	if (ret)
1638c2ecf20Sopenharmony_ci		return ret;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, reg, mask, val);
1668c2ecf20Sopenharmony_ci	if (ret)
1678c2ecf20Sopenharmony_ci		goto exit_eerd;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE);
1708c2ecf20Sopenharmony_ci	if (ret)
1718c2ecf20Sopenharmony_ci		goto exit_eerd;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	usleep_range(46000, RV3032_EEBUSY_TIMEOUT);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status,
1768c2ecf20Sopenharmony_ci				       !(status & RV3032_TLSB_EEBUSY),
1778c2ecf20Sopenharmony_ci				       RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciexit_eerd:
1808c2ecf20Sopenharmony_ci	rv3032_exit_eerd(rv3032, eerd);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return ret;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic irqreturn_t rv3032_handle_irq(int irq, void *dev_id)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_id;
1888c2ecf20Sopenharmony_ci	unsigned long events = 0;
1898c2ecf20Sopenharmony_ci	u32 status = 0, ctrl = 0;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (regmap_read(rv3032->regmap, RV3032_STATUS, &status) < 0 ||
1928c2ecf20Sopenharmony_ci	    status == 0) {
1938c2ecf20Sopenharmony_ci		return IRQ_NONE;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (status & RV3032_STATUS_TF) {
1978c2ecf20Sopenharmony_ci		status |= RV3032_STATUS_TF;
1988c2ecf20Sopenharmony_ci		ctrl |= RV3032_CTRL2_TIE;
1998c2ecf20Sopenharmony_ci		events |= RTC_PF;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (status & RV3032_STATUS_AF) {
2038c2ecf20Sopenharmony_ci		status |= RV3032_STATUS_AF;
2048c2ecf20Sopenharmony_ci		ctrl |= RV3032_CTRL2_AIE;
2058c2ecf20Sopenharmony_ci		events |= RTC_AF;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (status & RV3032_STATUS_UF) {
2098c2ecf20Sopenharmony_ci		status |= RV3032_STATUS_UF;
2108c2ecf20Sopenharmony_ci		ctrl |= RV3032_CTRL2_UIE;
2118c2ecf20Sopenharmony_ci		events |= RTC_UF;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	if (events) {
2158c2ecf20Sopenharmony_ci		rtc_update_irq(rv3032->rtc, 1, events);
2168c2ecf20Sopenharmony_ci		regmap_update_bits(rv3032->regmap, RV3032_STATUS, status, 0);
2178c2ecf20Sopenharmony_ci		regmap_update_bits(rv3032->regmap, RV3032_CTRL2, ctrl, 0);
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int rv3032_get_time(struct device *dev, struct rtc_time *tm)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
2268c2ecf20Sopenharmony_ci	u8 date[7];
2278c2ecf20Sopenharmony_ci	int ret, status;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status);
2308c2ecf20Sopenharmony_ci	if (ret < 0)
2318c2ecf20Sopenharmony_ci		return ret;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF))
2348c2ecf20Sopenharmony_ci		return -EINVAL;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3032->regmap, RV3032_SEC, date, sizeof(date));
2378c2ecf20Sopenharmony_ci	if (ret)
2388c2ecf20Sopenharmony_ci		return ret;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	tm->tm_sec  = bcd2bin(date[0] & 0x7f);
2418c2ecf20Sopenharmony_ci	tm->tm_min  = bcd2bin(date[1] & 0x7f);
2428c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(date[2] & 0x3f);
2438c2ecf20Sopenharmony_ci	tm->tm_wday = date[3] & 0x7;
2448c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(date[4] & 0x3f);
2458c2ecf20Sopenharmony_ci	tm->tm_mon  = bcd2bin(date[5] & 0x1f) - 1;
2468c2ecf20Sopenharmony_ci	tm->tm_year = bcd2bin(date[6]) + 100;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	return 0;
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic int rv3032_set_time(struct device *dev, struct rtc_time *tm)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
2548c2ecf20Sopenharmony_ci	u8 date[7];
2558c2ecf20Sopenharmony_ci	int ret;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	date[0] = bin2bcd(tm->tm_sec);
2588c2ecf20Sopenharmony_ci	date[1] = bin2bcd(tm->tm_min);
2598c2ecf20Sopenharmony_ci	date[2] = bin2bcd(tm->tm_hour);
2608c2ecf20Sopenharmony_ci	date[3] = tm->tm_wday;
2618c2ecf20Sopenharmony_ci	date[4] = bin2bcd(tm->tm_mday);
2628c2ecf20Sopenharmony_ci	date[5] = bin2bcd(tm->tm_mon + 1);
2638c2ecf20Sopenharmony_ci	date[6] = bin2bcd(tm->tm_year - 100);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(rv3032->regmap, RV3032_SEC, date,
2668c2ecf20Sopenharmony_ci				sizeof(date));
2678c2ecf20Sopenharmony_ci	if (ret)
2688c2ecf20Sopenharmony_ci		return ret;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS,
2718c2ecf20Sopenharmony_ci				 RV3032_STATUS_PORF | RV3032_STATUS_VLF, 0);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return ret;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic int rv3032_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
2798c2ecf20Sopenharmony_ci	u8 alarmvals[3];
2808c2ecf20Sopenharmony_ci	int status, ctrl, ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3032->regmap, RV3032_ALARM_MIN, alarmvals,
2838c2ecf20Sopenharmony_ci			       sizeof(alarmvals));
2848c2ecf20Sopenharmony_ci	if (ret)
2858c2ecf20Sopenharmony_ci		return ret;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status);
2888c2ecf20Sopenharmony_ci	if (ret < 0)
2898c2ecf20Sopenharmony_ci		return ret;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_CTRL2, &ctrl);
2928c2ecf20Sopenharmony_ci	if (ret < 0)
2938c2ecf20Sopenharmony_ci		return ret;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	alrm->time.tm_sec  = 0;
2968c2ecf20Sopenharmony_ci	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
2978c2ecf20Sopenharmony_ci	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
2988c2ecf20Sopenharmony_ci	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	alrm->enabled = !!(ctrl & RV3032_CTRL2_AIE);
3018c2ecf20Sopenharmony_ci	alrm->pending = (status & RV3032_STATUS_AF) && alrm->enabled;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return 0;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int rv3032_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
3098c2ecf20Sopenharmony_ci	u8 alarmvals[3];
3108c2ecf20Sopenharmony_ci	u8 ctrl = 0;
3118c2ecf20Sopenharmony_ci	int ret;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* The alarm has no seconds, round up to nearest minute */
3148c2ecf20Sopenharmony_ci	if (alrm->time.tm_sec) {
3158c2ecf20Sopenharmony_ci		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		alarm_time += 60 - alrm->time.tm_sec;
3188c2ecf20Sopenharmony_ci		rtc_time64_to_tm(alarm_time, &alrm->time);
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2,
3228c2ecf20Sopenharmony_ci				 RV3032_CTRL2_AIE | RV3032_CTRL2_UIE, 0);
3238c2ecf20Sopenharmony_ci	if (ret)
3248c2ecf20Sopenharmony_ci		return ret;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	alarmvals[0] = bin2bcd(alrm->time.tm_min);
3278c2ecf20Sopenharmony_ci	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
3288c2ecf20Sopenharmony_ci	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS,
3318c2ecf20Sopenharmony_ci				 RV3032_STATUS_AF, 0);
3328c2ecf20Sopenharmony_ci	if (ret)
3338c2ecf20Sopenharmony_ci		return ret;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(rv3032->regmap, RV3032_ALARM_MIN, alarmvals,
3368c2ecf20Sopenharmony_ci				sizeof(alarmvals));
3378c2ecf20Sopenharmony_ci	if (ret)
3388c2ecf20Sopenharmony_ci		return ret;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if (alrm->enabled) {
3418c2ecf20Sopenharmony_ci		if (rv3032->rtc->uie_rtctimer.enabled)
3428c2ecf20Sopenharmony_ci			ctrl |= RV3032_CTRL2_UIE;
3438c2ecf20Sopenharmony_ci		if (rv3032->rtc->aie_timer.enabled)
3448c2ecf20Sopenharmony_ci			ctrl |= RV3032_CTRL2_AIE;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2,
3488c2ecf20Sopenharmony_ci				 RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	return ret;
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic int rv3032_alarm_irq_enable(struct device *dev, unsigned int enabled)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
3568c2ecf20Sopenharmony_ci	int ctrl = 0, ret;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (enabled) {
3598c2ecf20Sopenharmony_ci		if (rv3032->rtc->uie_rtctimer.enabled)
3608c2ecf20Sopenharmony_ci			ctrl |= RV3032_CTRL2_UIE;
3618c2ecf20Sopenharmony_ci		if (rv3032->rtc->aie_timer.enabled)
3628c2ecf20Sopenharmony_ci			ctrl |= RV3032_CTRL2_AIE;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS,
3668c2ecf20Sopenharmony_ci				 RV3032_STATUS_AF | RV3032_STATUS_UF, 0);
3678c2ecf20Sopenharmony_ci	if (ret)
3688c2ecf20Sopenharmony_ci		return ret;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2,
3718c2ecf20Sopenharmony_ci				 RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl);
3728c2ecf20Sopenharmony_ci	if (ret)
3738c2ecf20Sopenharmony_ci		return ret;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic int rv3032_read_offset(struct device *dev, long *offset)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
3818c2ecf20Sopenharmony_ci	int ret, value, steps;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_OFFSET, &value);
3848c2ecf20Sopenharmony_ci	if (ret < 0)
3858c2ecf20Sopenharmony_ci		return ret;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	steps = sign_extend32(FIELD_GET(RV3032_OFFSET_MSK, value), 5);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	*offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return 0;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic int rv3032_set_offset(struct device *dev, long offset)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	offset = clamp(offset, -7629L, 7391L) * 1000;
3998c2ecf20Sopenharmony_ci	offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	return rv3032_update_cfg(rv3032, RV3032_OFFSET, RV3032_OFFSET_MSK,
4028c2ecf20Sopenharmony_ci				 FIELD_PREP(RV3032_OFFSET_MSK, offset));
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic int rv3032_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
4088c2ecf20Sopenharmony_ci	int status, val = 0, ret = 0;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	switch (cmd) {
4118c2ecf20Sopenharmony_ci	case RTC_VL_READ:
4128c2ecf20Sopenharmony_ci		ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status);
4138c2ecf20Sopenharmony_ci		if (ret < 0)
4148c2ecf20Sopenharmony_ci			return ret;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF))
4178c2ecf20Sopenharmony_ci			val = RTC_VL_DATA_INVALID;
4188c2ecf20Sopenharmony_ci		return put_user(val, (unsigned int __user *)arg);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	default:
4218c2ecf20Sopenharmony_ci		return -ENOIOCTLCMD;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic int rv3032_nvram_write(void *priv, unsigned int offset, void *val, size_t bytes)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	return regmap_bulk_write(priv, RV3032_RAM1 + offset, val, bytes);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int rv3032_nvram_read(void *priv, unsigned int offset, void *val, size_t bytes)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	return regmap_bulk_read(priv, RV3032_RAM1 + offset, val, bytes);
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic int rv3032_eeprom_write(void *priv, unsigned int offset, void *val, size_t bytes)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = priv;
4388c2ecf20Sopenharmony_ci	u32 status, eerd;
4398c2ecf20Sopenharmony_ci	int i, ret;
4408c2ecf20Sopenharmony_ci	u8 *buf = val;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	ret = rv3032_enter_eerd(rv3032, &eerd);
4438c2ecf20Sopenharmony_ci	if (ret)
4448c2ecf20Sopenharmony_ci		return ret;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i++) {
4478c2ecf20Sopenharmony_ci		ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR,
4488c2ecf20Sopenharmony_ci				   RV3032_EEPROM_USER + offset + i);
4498c2ecf20Sopenharmony_ci		if (ret)
4508c2ecf20Sopenharmony_ci			goto exit_eerd;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		ret = regmap_write(rv3032->regmap, RV3032_EEPROM_DATA, buf[i]);
4538c2ecf20Sopenharmony_ci		if (ret)
4548c2ecf20Sopenharmony_ci			goto exit_eerd;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci		ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD,
4578c2ecf20Sopenharmony_ci				   RV3032_EEPROM_CMD_WRITE);
4588c2ecf20Sopenharmony_ci		if (ret)
4598c2ecf20Sopenharmony_ci			goto exit_eerd;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci		usleep_range(RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status,
4648c2ecf20Sopenharmony_ci					       !(status & RV3032_TLSB_EEBUSY),
4658c2ecf20Sopenharmony_ci					       RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
4668c2ecf20Sopenharmony_ci		if (ret)
4678c2ecf20Sopenharmony_ci			goto exit_eerd;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ciexit_eerd:
4718c2ecf20Sopenharmony_ci	rv3032_exit_eerd(rv3032, eerd);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return ret;
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic int rv3032_eeprom_read(void *priv, unsigned int offset, void *val, size_t bytes)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = priv;
4798c2ecf20Sopenharmony_ci	u32 status, eerd, data;
4808c2ecf20Sopenharmony_ci	int i, ret;
4818c2ecf20Sopenharmony_ci	u8 *buf = val;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	ret = rv3032_enter_eerd(rv3032, &eerd);
4848c2ecf20Sopenharmony_ci	if (ret)
4858c2ecf20Sopenharmony_ci		return ret;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i++) {
4888c2ecf20Sopenharmony_ci		ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR,
4898c2ecf20Sopenharmony_ci				   RV3032_EEPROM_USER + offset + i);
4908c2ecf20Sopenharmony_ci		if (ret)
4918c2ecf20Sopenharmony_ci			goto exit_eerd;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD,
4948c2ecf20Sopenharmony_ci				   RV3032_EEPROM_CMD_READ);
4958c2ecf20Sopenharmony_ci		if (ret)
4968c2ecf20Sopenharmony_ci			goto exit_eerd;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status,
4998c2ecf20Sopenharmony_ci					       !(status & RV3032_TLSB_EEBUSY),
5008c2ecf20Sopenharmony_ci					       RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
5018c2ecf20Sopenharmony_ci		if (ret)
5028c2ecf20Sopenharmony_ci			goto exit_eerd;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		ret = regmap_read(rv3032->regmap, RV3032_EEPROM_DATA, &data);
5058c2ecf20Sopenharmony_ci		if (ret)
5068c2ecf20Sopenharmony_ci			goto exit_eerd;
5078c2ecf20Sopenharmony_ci		buf[i] = data;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ciexit_eerd:
5118c2ecf20Sopenharmony_ci	rv3032_exit_eerd(rv3032, eerd);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	return ret;
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic int rv3032_trickle_charger_setup(struct device *dev, struct rv3032_data *rv3032)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	u32 val, ohms, voltage;
5198c2ecf20Sopenharmony_ci	int i;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	val = FIELD_PREP(RV3032_PMU_TCM, 1) | FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_DSM);
5228c2ecf20Sopenharmony_ci	if (!device_property_read_u32(dev, "trickle-voltage-millivolt", &voltage)) {
5238c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(rv3032_trickle_voltages); i++)
5248c2ecf20Sopenharmony_ci			if (voltage == rv3032_trickle_voltages[i])
5258c2ecf20Sopenharmony_ci				break;
5268c2ecf20Sopenharmony_ci		if (i < ARRAY_SIZE(rv3032_trickle_voltages))
5278c2ecf20Sopenharmony_ci			val = FIELD_PREP(RV3032_PMU_TCM, i) |
5288c2ecf20Sopenharmony_ci			      FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_LSM);
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	if (device_property_read_u32(dev, "trickle-resistor-ohms", &ohms))
5328c2ecf20Sopenharmony_ci		return 0;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rv3032_trickle_resistors); i++)
5358c2ecf20Sopenharmony_ci		if (ohms == rv3032_trickle_resistors[i])
5368c2ecf20Sopenharmony_ci			break;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (i >= ARRAY_SIZE(rv3032_trickle_resistors)) {
5398c2ecf20Sopenharmony_ci		dev_warn(dev, "invalid trickle resistor value\n");
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		return 0;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	return rv3032_update_cfg(rv3032, RV3032_PMU,
5458c2ecf20Sopenharmony_ci				 RV3032_PMU_TCR | RV3032_PMU_TCM | RV3032_PMU_BSM,
5468c2ecf20Sopenharmony_ci				 val | FIELD_PREP(RV3032_PMU_TCR, i));
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
5508c2ecf20Sopenharmony_ci#define clkout_hw_to_rv3032(hw) container_of(hw, struct rv3032_data, clkout_hw)
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic int clkout_xtal_rates[] = {
5538c2ecf20Sopenharmony_ci	32768,
5548c2ecf20Sopenharmony_ci	1024,
5558c2ecf20Sopenharmony_ci	64,
5568c2ecf20Sopenharmony_ci	1,
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci#define RV3032_HFD_STEP 8192
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic unsigned long rv3032_clkout_recalc_rate(struct clk_hw *hw,
5628c2ecf20Sopenharmony_ci					       unsigned long parent_rate)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	int clkout, ret;
5658c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_CLKOUT2, &clkout);
5688c2ecf20Sopenharmony_ci	if (ret < 0)
5698c2ecf20Sopenharmony_ci		return 0;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (clkout & RV3032_CLKOUT2_OS) {
5728c2ecf20Sopenharmony_ci		unsigned long rate = FIELD_GET(RV3032_CLKOUT2_HFD_MSK, clkout) << 8;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		ret = regmap_read(rv3032->regmap, RV3032_CLKOUT1, &clkout);
5758c2ecf20Sopenharmony_ci		if (ret < 0)
5768c2ecf20Sopenharmony_ci			return 0;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		rate += clkout + 1;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci		return rate * RV3032_HFD_STEP;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	return clkout_xtal_rates[FIELD_GET(RV3032_CLKOUT2_FD_MSK, clkout)];
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic long rv3032_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
5878c2ecf20Sopenharmony_ci				     unsigned long *prate)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	int i, hfd;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	if (rate < RV3032_HFD_STEP)
5928c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++)
5938c2ecf20Sopenharmony_ci			if (clkout_xtal_rates[i] <= rate)
5948c2ecf20Sopenharmony_ci				return clkout_xtal_rates[i];
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return RV3032_HFD_STEP * clamp(hfd, 0, 8192);
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic int rv3032_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
6028c2ecf20Sopenharmony_ci				  unsigned long parent_rate)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw);
6058c2ecf20Sopenharmony_ci	u32 status, eerd;
6068c2ecf20Sopenharmony_ci	int i, hfd, ret;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++) {
6098c2ecf20Sopenharmony_ci		if (clkout_xtal_rates[i] == rate) {
6108c2ecf20Sopenharmony_ci			return rv3032_update_cfg(rv3032, RV3032_CLKOUT2, 0xff,
6118c2ecf20Sopenharmony_ci						 FIELD_PREP(RV3032_CLKOUT2_FD_MSK, i));
6128c2ecf20Sopenharmony_ci		}
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP);
6168c2ecf20Sopenharmony_ci	hfd = clamp(hfd, 1, 8192) - 1;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	ret = rv3032_enter_eerd(rv3032, &eerd);
6198c2ecf20Sopenharmony_ci	if (ret)
6208c2ecf20Sopenharmony_ci		return ret;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	ret = regmap_write(rv3032->regmap, RV3032_CLKOUT1, hfd & 0xff);
6238c2ecf20Sopenharmony_ci	if (ret)
6248c2ecf20Sopenharmony_ci		goto exit_eerd;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ret = regmap_write(rv3032->regmap, RV3032_CLKOUT2, RV3032_CLKOUT2_OS |
6278c2ecf20Sopenharmony_ci			    FIELD_PREP(RV3032_CLKOUT2_HFD_MSK, hfd >> 8));
6288c2ecf20Sopenharmony_ci	if (ret)
6298c2ecf20Sopenharmony_ci		goto exit_eerd;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE);
6328c2ecf20Sopenharmony_ci	if (ret)
6338c2ecf20Sopenharmony_ci		goto exit_eerd;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	usleep_range(46000, RV3032_EEBUSY_TIMEOUT);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status,
6388c2ecf20Sopenharmony_ci				       !(status & RV3032_TLSB_EEBUSY),
6398c2ecf20Sopenharmony_ci				       RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ciexit_eerd:
6428c2ecf20Sopenharmony_ci	rv3032_exit_eerd(rv3032, eerd);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return ret;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int rv3032_clkout_prepare(struct clk_hw *hw)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, 0);
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic void rv3032_clkout_unprepare(struct clk_hw *hw)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, RV3032_PMU_NCLKE);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int rv3032_clkout_is_prepared(struct clk_hw *hw)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	int val, ret;
6648c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_PMU, &val);
6678c2ecf20Sopenharmony_ci	if (ret < 0)
6688c2ecf20Sopenharmony_ci		return ret;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	return !(val & RV3032_PMU_NCLKE);
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic const struct clk_ops rv3032_clkout_ops = {
6748c2ecf20Sopenharmony_ci	.prepare = rv3032_clkout_prepare,
6758c2ecf20Sopenharmony_ci	.unprepare = rv3032_clkout_unprepare,
6768c2ecf20Sopenharmony_ci	.is_prepared = rv3032_clkout_is_prepared,
6778c2ecf20Sopenharmony_ci	.recalc_rate = rv3032_clkout_recalc_rate,
6788c2ecf20Sopenharmony_ci	.round_rate = rv3032_clkout_round_rate,
6798c2ecf20Sopenharmony_ci	.set_rate = rv3032_clkout_set_rate,
6808c2ecf20Sopenharmony_ci};
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic int rv3032_clkout_register_clk(struct rv3032_data *rv3032,
6838c2ecf20Sopenharmony_ci				      struct i2c_client *client)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	int ret;
6868c2ecf20Sopenharmony_ci	struct clk *clk;
6878c2ecf20Sopenharmony_ci	struct clk_init_data init;
6888c2ecf20Sopenharmony_ci	struct device_node *node = client->dev.of_node;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_TLSB, RV3032_TLSB_CLKF, 0);
6918c2ecf20Sopenharmony_ci	if (ret < 0)
6928c2ecf20Sopenharmony_ci		return ret;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, RV3032_CTRL2_CLKIE, 0);
6958c2ecf20Sopenharmony_ci	if (ret < 0)
6968c2ecf20Sopenharmony_ci		return ret;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	ret = regmap_write(rv3032->regmap, RV3032_CLK_IRQ, 0);
6998c2ecf20Sopenharmony_ci	if (ret < 0)
7008c2ecf20Sopenharmony_ci		return ret;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	init.name = "rv3032-clkout";
7038c2ecf20Sopenharmony_ci	init.ops = &rv3032_clkout_ops;
7048c2ecf20Sopenharmony_ci	init.flags = 0;
7058c2ecf20Sopenharmony_ci	init.parent_names = NULL;
7068c2ecf20Sopenharmony_ci	init.num_parents = 0;
7078c2ecf20Sopenharmony_ci	rv3032->clkout_hw.init = &init;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	of_property_read_string(node, "clock-output-names", &init.name);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	clk = devm_clk_register(&client->dev, &rv3032->clkout_hw);
7128c2ecf20Sopenharmony_ci	if (!IS_ERR(clk))
7138c2ecf20Sopenharmony_ci		of_clk_add_provider(node, of_clk_src_simple_get, clk);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	return 0;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci#endif
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic int rv3032_hwmon_read_temp(struct device *dev, long *mC)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
7228c2ecf20Sopenharmony_ci	u8 buf[2];
7238c2ecf20Sopenharmony_ci	int temp, prev = 0;
7248c2ecf20Sopenharmony_ci	int ret;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf));
7278c2ecf20Sopenharmony_ci	if (ret)
7288c2ecf20Sopenharmony_ci		return ret;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	temp = sign_extend32(buf[1], 7) << 4;
7318c2ecf20Sopenharmony_ci	temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	/* No blocking or shadowing on RV3032_TLSB and RV3032_TMSB */
7348c2ecf20Sopenharmony_ci	do {
7358c2ecf20Sopenharmony_ci		prev = temp;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf));
7388c2ecf20Sopenharmony_ci		if (ret)
7398c2ecf20Sopenharmony_ci			return ret;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci		temp = sign_extend32(buf[1], 7) << 4;
7428c2ecf20Sopenharmony_ci		temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]);
7438c2ecf20Sopenharmony_ci	} while (temp != prev);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	*mC = (temp * 1000) / 16;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	return 0;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic umode_t rv3032_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
7518c2ecf20Sopenharmony_ci				       u32 attr, int channel)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	if (type != hwmon_temp)
7548c2ecf20Sopenharmony_ci		return 0;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	switch (attr) {
7578c2ecf20Sopenharmony_ci	case hwmon_temp_input:
7588c2ecf20Sopenharmony_ci		return 0444;
7598c2ecf20Sopenharmony_ci	default:
7608c2ecf20Sopenharmony_ci		return 0;
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic int rv3032_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
7658c2ecf20Sopenharmony_ci			     u32 attr, int channel, long *temp)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	int err;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	switch (attr) {
7708c2ecf20Sopenharmony_ci	case hwmon_temp_input:
7718c2ecf20Sopenharmony_ci		err = rv3032_hwmon_read_temp(dev, temp);
7728c2ecf20Sopenharmony_ci		break;
7738c2ecf20Sopenharmony_ci	default:
7748c2ecf20Sopenharmony_ci		err = -EOPNOTSUPP;
7758c2ecf20Sopenharmony_ci		break;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	return err;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic const struct hwmon_channel_info *rv3032_hwmon_info[] = {
7828c2ecf20Sopenharmony_ci	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
7838c2ecf20Sopenharmony_ci	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
7848c2ecf20Sopenharmony_ci	NULL
7858c2ecf20Sopenharmony_ci};
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cistatic const struct hwmon_ops rv3032_hwmon_hwmon_ops = {
7888c2ecf20Sopenharmony_ci	.is_visible = rv3032_hwmon_is_visible,
7898c2ecf20Sopenharmony_ci	.read = rv3032_hwmon_read,
7908c2ecf20Sopenharmony_ci};
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic const struct hwmon_chip_info rv3032_hwmon_chip_info = {
7938c2ecf20Sopenharmony_ci	.ops = &rv3032_hwmon_hwmon_ops,
7948c2ecf20Sopenharmony_ci	.info = rv3032_hwmon_info,
7958c2ecf20Sopenharmony_ci};
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic void rv3032_hwmon_register(struct device *dev)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032 = dev_get_drvdata(dev);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (!IS_REACHABLE(CONFIG_HWMON))
8028c2ecf20Sopenharmony_ci		return;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	devm_hwmon_device_register_with_info(dev, "rv3032", rv3032, &rv3032_hwmon_chip_info, NULL);
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic struct rtc_class_ops rv3032_rtc_ops = {
8088c2ecf20Sopenharmony_ci	.read_time = rv3032_get_time,
8098c2ecf20Sopenharmony_ci	.set_time = rv3032_set_time,
8108c2ecf20Sopenharmony_ci	.read_offset = rv3032_read_offset,
8118c2ecf20Sopenharmony_ci	.set_offset = rv3032_set_offset,
8128c2ecf20Sopenharmony_ci	.ioctl = rv3032_ioctl,
8138c2ecf20Sopenharmony_ci};
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic const struct regmap_config regmap_config = {
8168c2ecf20Sopenharmony_ci	.reg_bits = 8,
8178c2ecf20Sopenharmony_ci	.val_bits = 8,
8188c2ecf20Sopenharmony_ci	.max_register = 0xCA,
8198c2ecf20Sopenharmony_ci};
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_cistatic int rv3032_probe(struct i2c_client *client)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	struct rv3032_data *rv3032;
8248c2ecf20Sopenharmony_ci	int ret, status;
8258c2ecf20Sopenharmony_ci	struct nvmem_config nvmem_cfg = {
8268c2ecf20Sopenharmony_ci		.name = "rv3032_nvram",
8278c2ecf20Sopenharmony_ci		.word_size = 1,
8288c2ecf20Sopenharmony_ci		.stride = 1,
8298c2ecf20Sopenharmony_ci		.size = 16,
8308c2ecf20Sopenharmony_ci		.type = NVMEM_TYPE_BATTERY_BACKED,
8318c2ecf20Sopenharmony_ci		.reg_read = rv3032_nvram_read,
8328c2ecf20Sopenharmony_ci		.reg_write = rv3032_nvram_write,
8338c2ecf20Sopenharmony_ci	};
8348c2ecf20Sopenharmony_ci	struct nvmem_config eeprom_cfg = {
8358c2ecf20Sopenharmony_ci		.name = "rv3032_eeprom",
8368c2ecf20Sopenharmony_ci		.word_size = 1,
8378c2ecf20Sopenharmony_ci		.stride = 1,
8388c2ecf20Sopenharmony_ci		.size = 32,
8398c2ecf20Sopenharmony_ci		.type = NVMEM_TYPE_EEPROM,
8408c2ecf20Sopenharmony_ci		.reg_read = rv3032_eeprom_read,
8418c2ecf20Sopenharmony_ci		.reg_write = rv3032_eeprom_write,
8428c2ecf20Sopenharmony_ci	};
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	rv3032 = devm_kzalloc(&client->dev, sizeof(struct rv3032_data),
8458c2ecf20Sopenharmony_ci			      GFP_KERNEL);
8468c2ecf20Sopenharmony_ci	if (!rv3032)
8478c2ecf20Sopenharmony_ci		return -ENOMEM;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	rv3032->regmap = devm_regmap_init_i2c(client, &regmap_config);
8508c2ecf20Sopenharmony_ci	if (IS_ERR(rv3032->regmap))
8518c2ecf20Sopenharmony_ci		return PTR_ERR(rv3032->regmap);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, rv3032);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status);
8568c2ecf20Sopenharmony_ci	if (ret < 0)
8578c2ecf20Sopenharmony_ci		return ret;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	rv3032->rtc = devm_rtc_allocate_device(&client->dev);
8608c2ecf20Sopenharmony_ci	if (IS_ERR(rv3032->rtc))
8618c2ecf20Sopenharmony_ci		return PTR_ERR(rv3032->rtc);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	if (client->irq > 0) {
8648c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(&client->dev, client->irq,
8658c2ecf20Sopenharmony_ci						NULL, rv3032_handle_irq,
8668c2ecf20Sopenharmony_ci						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
8678c2ecf20Sopenharmony_ci						"rv3032", rv3032);
8688c2ecf20Sopenharmony_ci		if (ret) {
8698c2ecf20Sopenharmony_ci			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
8708c2ecf20Sopenharmony_ci			client->irq = 0;
8718c2ecf20Sopenharmony_ci		} else {
8728c2ecf20Sopenharmony_ci			rv3032_rtc_ops.read_alarm = rv3032_get_alarm;
8738c2ecf20Sopenharmony_ci			rv3032_rtc_ops.set_alarm = rv3032_set_alarm;
8748c2ecf20Sopenharmony_ci			rv3032_rtc_ops.alarm_irq_enable = rv3032_alarm_irq_enable;
8758c2ecf20Sopenharmony_ci		}
8768c2ecf20Sopenharmony_ci	}
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1,
8798c2ecf20Sopenharmony_ci				 RV3032_CTRL1_WADA, RV3032_CTRL1_WADA);
8808c2ecf20Sopenharmony_ci	if (ret)
8818c2ecf20Sopenharmony_ci		return ret;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	rv3032_trickle_charger_setup(&client->dev, rv3032);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	rv3032->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
8868c2ecf20Sopenharmony_ci	rv3032->rtc->range_max = RTC_TIMESTAMP_END_2099;
8878c2ecf20Sopenharmony_ci	rv3032->rtc->ops = &rv3032_rtc_ops;
8888c2ecf20Sopenharmony_ci	ret = rtc_register_device(rv3032->rtc);
8898c2ecf20Sopenharmony_ci	if (ret)
8908c2ecf20Sopenharmony_ci		return ret;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	nvmem_cfg.priv = rv3032;
8938c2ecf20Sopenharmony_ci	rtc_nvmem_register(rv3032->rtc, &nvmem_cfg);
8948c2ecf20Sopenharmony_ci	eeprom_cfg.priv = rv3032;
8958c2ecf20Sopenharmony_ci	rtc_nvmem_register(rv3032->rtc, &eeprom_cfg);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	rv3032->rtc->max_user_freq = 1;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
9008c2ecf20Sopenharmony_ci	rv3032_clkout_register_clk(rv3032, client);
9018c2ecf20Sopenharmony_ci#endif
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	rv3032_hwmon_register(&client->dev);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	return 0;
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic const struct of_device_id rv3032_of_match[] = {
9098c2ecf20Sopenharmony_ci	{ .compatible = "microcrystal,rv3032", },
9108c2ecf20Sopenharmony_ci	{ }
9118c2ecf20Sopenharmony_ci};
9128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rv3032_of_match);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic struct i2c_driver rv3032_driver = {
9158c2ecf20Sopenharmony_ci	.driver = {
9168c2ecf20Sopenharmony_ci		.name = "rtc-rv3032",
9178c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(rv3032_of_match),
9188c2ecf20Sopenharmony_ci	},
9198c2ecf20Sopenharmony_ci	.probe_new	= rv3032_probe,
9208c2ecf20Sopenharmony_ci};
9218c2ecf20Sopenharmony_cimodule_i2c_driver(rv3032_driver);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
9248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Micro Crystal RV3032 RTC driver");
9258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
926