18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * RTC driver for the Micro Crystal RV3028
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Micro Crystal SA
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Alexandre Belloni <alexandre.belloni@bootlin.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
128c2ecf20Sopenharmony_ci#include <linux/bcd.h>
138c2ecf20Sopenharmony_ci#include <linux/bitops.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/log2.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/of_device.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci#include <linux/rtc.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define RV3028_SEC			0x00
248c2ecf20Sopenharmony_ci#define RV3028_MIN			0x01
258c2ecf20Sopenharmony_ci#define RV3028_HOUR			0x02
268c2ecf20Sopenharmony_ci#define RV3028_WDAY			0x03
278c2ecf20Sopenharmony_ci#define RV3028_DAY			0x04
288c2ecf20Sopenharmony_ci#define RV3028_MONTH			0x05
298c2ecf20Sopenharmony_ci#define RV3028_YEAR			0x06
308c2ecf20Sopenharmony_ci#define RV3028_ALARM_MIN		0x07
318c2ecf20Sopenharmony_ci#define RV3028_ALARM_HOUR		0x08
328c2ecf20Sopenharmony_ci#define RV3028_ALARM_DAY		0x09
338c2ecf20Sopenharmony_ci#define RV3028_STATUS			0x0E
348c2ecf20Sopenharmony_ci#define RV3028_CTRL1			0x0F
358c2ecf20Sopenharmony_ci#define RV3028_CTRL2			0x10
368c2ecf20Sopenharmony_ci#define RV3028_EVT_CTRL			0x13
378c2ecf20Sopenharmony_ci#define RV3028_TS_COUNT			0x14
388c2ecf20Sopenharmony_ci#define RV3028_TS_SEC			0x15
398c2ecf20Sopenharmony_ci#define RV3028_RAM1			0x1F
408c2ecf20Sopenharmony_ci#define RV3028_EEPROM_ADDR		0x25
418c2ecf20Sopenharmony_ci#define RV3028_EEPROM_DATA		0x26
428c2ecf20Sopenharmony_ci#define RV3028_EEPROM_CMD		0x27
438c2ecf20Sopenharmony_ci#define RV3028_CLKOUT			0x35
448c2ecf20Sopenharmony_ci#define RV3028_OFFSET			0x36
458c2ecf20Sopenharmony_ci#define RV3028_BACKUP			0x37
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define RV3028_STATUS_PORF		BIT(0)
488c2ecf20Sopenharmony_ci#define RV3028_STATUS_EVF		BIT(1)
498c2ecf20Sopenharmony_ci#define RV3028_STATUS_AF		BIT(2)
508c2ecf20Sopenharmony_ci#define RV3028_STATUS_TF		BIT(3)
518c2ecf20Sopenharmony_ci#define RV3028_STATUS_UF		BIT(4)
528c2ecf20Sopenharmony_ci#define RV3028_STATUS_BSF		BIT(5)
538c2ecf20Sopenharmony_ci#define RV3028_STATUS_CLKF		BIT(6)
548c2ecf20Sopenharmony_ci#define RV3028_STATUS_EEBUSY		BIT(7)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define RV3028_CLKOUT_FD_MASK		GENMASK(2, 0)
578c2ecf20Sopenharmony_ci#define RV3028_CLKOUT_PORIE		BIT(3)
588c2ecf20Sopenharmony_ci#define RV3028_CLKOUT_CLKSY		BIT(6)
598c2ecf20Sopenharmony_ci#define RV3028_CLKOUT_CLKOE		BIT(7)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define RV3028_CTRL1_EERD		BIT(3)
628c2ecf20Sopenharmony_ci#define RV3028_CTRL1_WADA		BIT(5)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define RV3028_CTRL2_RESET		BIT(0)
658c2ecf20Sopenharmony_ci#define RV3028_CTRL2_12_24		BIT(1)
668c2ecf20Sopenharmony_ci#define RV3028_CTRL2_EIE		BIT(2)
678c2ecf20Sopenharmony_ci#define RV3028_CTRL2_AIE		BIT(3)
688c2ecf20Sopenharmony_ci#define RV3028_CTRL2_TIE		BIT(4)
698c2ecf20Sopenharmony_ci#define RV3028_CTRL2_UIE		BIT(5)
708c2ecf20Sopenharmony_ci#define RV3028_CTRL2_TSE		BIT(7)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define RV3028_EVT_CTRL_TSR		BIT(2)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define RV3028_EEPROM_CMD_UPDATE	0x11
758c2ecf20Sopenharmony_ci#define RV3028_EEPROM_CMD_WRITE		0x21
768c2ecf20Sopenharmony_ci#define RV3028_EEPROM_CMD_READ		0x22
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define RV3028_EEBUSY_POLL		10000
798c2ecf20Sopenharmony_ci#define RV3028_EEBUSY_TIMEOUT		100000
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define RV3028_BACKUP_TCE		BIT(5)
828c2ecf20Sopenharmony_ci#define RV3028_BACKUP_TCR_MASK		GENMASK(1,0)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define OFFSET_STEP_PPT			953674
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cienum rv3028_type {
878c2ecf20Sopenharmony_ci	rv_3028,
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct rv3028_data {
918c2ecf20Sopenharmony_ci	struct regmap *regmap;
928c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
938c2ecf20Sopenharmony_ci	enum rv3028_type type;
948c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
958c2ecf20Sopenharmony_ci	struct clk_hw clkout_hw;
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic u16 rv3028_trickle_resistors[] = {3000, 5000, 9000, 15000};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic ssize_t timestamp0_store(struct device *dev,
1028c2ecf20Sopenharmony_ci				struct device_attribute *attr,
1038c2ecf20Sopenharmony_ci				const char *buf, size_t count)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
1088c2ecf20Sopenharmony_ci			   RV3028_EVT_CTRL_TSR);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return count;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic ssize_t timestamp0_show(struct device *dev,
1148c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
1178c2ecf20Sopenharmony_ci	struct rtc_time tm;
1188c2ecf20Sopenharmony_ci	int ret, count;
1198c2ecf20Sopenharmony_ci	u8 date[6];
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
1228c2ecf20Sopenharmony_ci	if (ret)
1238c2ecf20Sopenharmony_ci		return ret;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (!count)
1268c2ecf20Sopenharmony_ci		return 0;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
1298c2ecf20Sopenharmony_ci			       sizeof(date));
1308c2ecf20Sopenharmony_ci	if (ret)
1318c2ecf20Sopenharmony_ci		return ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	tm.tm_sec = bcd2bin(date[0]);
1348c2ecf20Sopenharmony_ci	tm.tm_min = bcd2bin(date[1]);
1358c2ecf20Sopenharmony_ci	tm.tm_hour = bcd2bin(date[2]);
1368c2ecf20Sopenharmony_ci	tm.tm_mday = bcd2bin(date[3]);
1378c2ecf20Sopenharmony_ci	tm.tm_mon = bcd2bin(date[4]) - 1;
1388c2ecf20Sopenharmony_ci	tm.tm_year = bcd2bin(date[5]) + 100;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	ret = rtc_valid_tm(&tm);
1418c2ecf20Sopenharmony_ci	if (ret)
1428c2ecf20Sopenharmony_ci		return ret;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n",
1458c2ecf20Sopenharmony_ci		       (unsigned long long)rtc_tm_to_time64(&tm));
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(timestamp0);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic ssize_t timestamp0_count_show(struct device *dev,
1518c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
1548c2ecf20Sopenharmony_ci	int ret, count;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
1578c2ecf20Sopenharmony_ci	if (ret)
1588c2ecf20Sopenharmony_ci		return ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", count);
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(timestamp0_count);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic struct attribute *rv3028_attrs[] = {
1668c2ecf20Sopenharmony_ci	&dev_attr_timestamp0.attr,
1678c2ecf20Sopenharmony_ci	&dev_attr_timestamp0_count.attr,
1688c2ecf20Sopenharmony_ci	NULL
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic const struct attribute_group rv3028_attr_group = {
1728c2ecf20Sopenharmony_ci	.attrs	= rv3028_attrs,
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic int rv3028_exit_eerd(struct rv3028_data *rv3028, u32 eerd)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	if (eerd)
1788c2ecf20Sopenharmony_ci		return 0;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return regmap_update_bits(rv3028->regmap, RV3028_CTRL1, RV3028_CTRL1_EERD, 0);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int rv3028_enter_eerd(struct rv3028_data *rv3028, u32 *eerd)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	u32 ctrl1, status;
1868c2ecf20Sopenharmony_ci	int ret;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_CTRL1, &ctrl1);
1898c2ecf20Sopenharmony_ci	if (ret)
1908c2ecf20Sopenharmony_ci		return ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	*eerd = ctrl1 & RV3028_CTRL1_EERD;
1938c2ecf20Sopenharmony_ci	if (*eerd)
1948c2ecf20Sopenharmony_ci		return 0;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
1978c2ecf20Sopenharmony_ci				 RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
1988c2ecf20Sopenharmony_ci	if (ret)
1998c2ecf20Sopenharmony_ci		return ret;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
2028c2ecf20Sopenharmony_ci				       !(status & RV3028_STATUS_EEBUSY),
2038c2ecf20Sopenharmony_ci				       RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
2048c2ecf20Sopenharmony_ci	if (ret) {
2058c2ecf20Sopenharmony_ci		rv3028_exit_eerd(rv3028, *eerd);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		return ret;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int rv3028_update_eeprom(struct rv3028_data *rv3028, u32 eerd)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	u32 status;
2168c2ecf20Sopenharmony_ci	int ret;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
2198c2ecf20Sopenharmony_ci	if (ret)
2208c2ecf20Sopenharmony_ci		goto exit_eerd;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, RV3028_EEPROM_CMD_UPDATE);
2238c2ecf20Sopenharmony_ci	if (ret)
2248c2ecf20Sopenharmony_ci		goto exit_eerd;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	usleep_range(63000, RV3028_EEBUSY_TIMEOUT);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
2298c2ecf20Sopenharmony_ci				       !(status & RV3028_STATUS_EEBUSY),
2308c2ecf20Sopenharmony_ci				       RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciexit_eerd:
2338c2ecf20Sopenharmony_ci	rv3028_exit_eerd(rv3028, eerd);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return ret;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int rv3028_update_cfg(struct rv3028_data *rv3028, unsigned int reg,
2398c2ecf20Sopenharmony_ci			     unsigned int mask, unsigned int val)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	u32 eerd;
2428c2ecf20Sopenharmony_ci	int ret;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	ret = rv3028_enter_eerd(rv3028, &eerd);
2458c2ecf20Sopenharmony_ci	if (ret)
2468c2ecf20Sopenharmony_ci		return ret;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, reg, mask, val);
2498c2ecf20Sopenharmony_ci	if (ret) {
2508c2ecf20Sopenharmony_ci		rv3028_exit_eerd(rv3028, eerd);
2518c2ecf20Sopenharmony_ci		return ret;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	return rv3028_update_eeprom(rv3028, eerd);
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_id;
2608c2ecf20Sopenharmony_ci	unsigned long events = 0;
2618c2ecf20Sopenharmony_ci	u32 status = 0, ctrl = 0;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
2648c2ecf20Sopenharmony_ci	   status == 0) {
2658c2ecf20Sopenharmony_ci		return IRQ_NONE;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_PORF)
2698c2ecf20Sopenharmony_ci		dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_TF) {
2728c2ecf20Sopenharmony_ci		status |= RV3028_STATUS_TF;
2738c2ecf20Sopenharmony_ci		ctrl |= RV3028_CTRL2_TIE;
2748c2ecf20Sopenharmony_ci		events |= RTC_PF;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_AF) {
2788c2ecf20Sopenharmony_ci		status |= RV3028_STATUS_AF;
2798c2ecf20Sopenharmony_ci		ctrl |= RV3028_CTRL2_AIE;
2808c2ecf20Sopenharmony_ci		events |= RTC_AF;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_UF) {
2848c2ecf20Sopenharmony_ci		status |= RV3028_STATUS_UF;
2858c2ecf20Sopenharmony_ci		ctrl |= RV3028_CTRL2_UIE;
2868c2ecf20Sopenharmony_ci		events |= RTC_UF;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (events) {
2908c2ecf20Sopenharmony_ci		rtc_update_irq(rv3028->rtc, 1, events);
2918c2ecf20Sopenharmony_ci		regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0);
2928c2ecf20Sopenharmony_ci		regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0);
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_EVF) {
2968c2ecf20Sopenharmony_ci		sysfs_notify(&rv3028->rtc->dev.kobj, NULL,
2978c2ecf20Sopenharmony_ci			     dev_attr_timestamp0.attr.name);
2988c2ecf20Sopenharmony_ci		dev_warn(&rv3028->rtc->dev, "event detected");
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic int rv3028_get_time(struct device *dev, struct rtc_time *tm)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
3078c2ecf20Sopenharmony_ci	u8 date[7];
3088c2ecf20Sopenharmony_ci	int ret, status;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
3118c2ecf20Sopenharmony_ci	if (ret < 0)
3128c2ecf20Sopenharmony_ci		return ret;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_PORF) {
3158c2ecf20Sopenharmony_ci		dev_warn(dev, "Voltage low, data is invalid.\n");
3168c2ecf20Sopenharmony_ci		return -EINVAL;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
3208c2ecf20Sopenharmony_ci	if (ret)
3218c2ecf20Sopenharmony_ci		return ret;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	tm->tm_sec  = bcd2bin(date[RV3028_SEC] & 0x7f);
3248c2ecf20Sopenharmony_ci	tm->tm_min  = bcd2bin(date[RV3028_MIN] & 0x7f);
3258c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f);
3268c2ecf20Sopenharmony_ci	tm->tm_wday = ilog2(date[RV3028_WDAY] & 0x7f);
3278c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f);
3288c2ecf20Sopenharmony_ci	tm->tm_mon  = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1;
3298c2ecf20Sopenharmony_ci	tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	return 0;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic int rv3028_set_time(struct device *dev, struct rtc_time *tm)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
3378c2ecf20Sopenharmony_ci	u8 date[7];
3388c2ecf20Sopenharmony_ci	int ret;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	date[RV3028_SEC]   = bin2bcd(tm->tm_sec);
3418c2ecf20Sopenharmony_ci	date[RV3028_MIN]   = bin2bcd(tm->tm_min);
3428c2ecf20Sopenharmony_ci	date[RV3028_HOUR]  = bin2bcd(tm->tm_hour);
3438c2ecf20Sopenharmony_ci	date[RV3028_WDAY]  = 1 << (tm->tm_wday);
3448c2ecf20Sopenharmony_ci	date[RV3028_DAY]   = bin2bcd(tm->tm_mday);
3458c2ecf20Sopenharmony_ci	date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1);
3468c2ecf20Sopenharmony_ci	date[RV3028_YEAR]  = bin2bcd(tm->tm_year - 100);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * Writing to the Seconds register has the same effect as setting RESET
3508c2ecf20Sopenharmony_ci	 * bit to 1
3518c2ecf20Sopenharmony_ci	 */
3528c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
3538c2ecf20Sopenharmony_ci				sizeof(date));
3548c2ecf20Sopenharmony_ci	if (ret)
3558c2ecf20Sopenharmony_ci		return ret;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
3588c2ecf20Sopenharmony_ci				 RV3028_STATUS_PORF, 0);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return ret;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
3668c2ecf20Sopenharmony_ci	u8 alarmvals[3];
3678c2ecf20Sopenharmony_ci	int status, ctrl, ret;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
3708c2ecf20Sopenharmony_ci			       sizeof(alarmvals));
3718c2ecf20Sopenharmony_ci	if (ret)
3728c2ecf20Sopenharmony_ci		return ret;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
3758c2ecf20Sopenharmony_ci	if (ret < 0)
3768c2ecf20Sopenharmony_ci		return ret;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
3798c2ecf20Sopenharmony_ci	if (ret < 0)
3808c2ecf20Sopenharmony_ci		return ret;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	alrm->time.tm_sec  = 0;
3838c2ecf20Sopenharmony_ci	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
3848c2ecf20Sopenharmony_ci	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
3858c2ecf20Sopenharmony_ci	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
3888c2ecf20Sopenharmony_ci	alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	return 0;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
3968c2ecf20Sopenharmony_ci	u8 alarmvals[3];
3978c2ecf20Sopenharmony_ci	u8 ctrl = 0;
3988c2ecf20Sopenharmony_ci	int ret;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* The alarm has no seconds, round up to nearest minute */
4018c2ecf20Sopenharmony_ci	if (alrm->time.tm_sec) {
4028c2ecf20Sopenharmony_ci		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		alarm_time += 60 - alrm->time.tm_sec;
4058c2ecf20Sopenharmony_ci		rtc_time64_to_tm(alarm_time, &alrm->time);
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
4098c2ecf20Sopenharmony_ci				 RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
4108c2ecf20Sopenharmony_ci	if (ret)
4118c2ecf20Sopenharmony_ci		return ret;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	alarmvals[0] = bin2bcd(alrm->time.tm_min);
4148c2ecf20Sopenharmony_ci	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
4158c2ecf20Sopenharmony_ci	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
4188c2ecf20Sopenharmony_ci				 RV3028_STATUS_AF, 0);
4198c2ecf20Sopenharmony_ci	if (ret)
4208c2ecf20Sopenharmony_ci		return ret;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
4238c2ecf20Sopenharmony_ci				sizeof(alarmvals));
4248c2ecf20Sopenharmony_ci	if (ret)
4258c2ecf20Sopenharmony_ci		return ret;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	if (alrm->enabled) {
4288c2ecf20Sopenharmony_ci		if (rv3028->rtc->uie_rtctimer.enabled)
4298c2ecf20Sopenharmony_ci			ctrl |= RV3028_CTRL2_UIE;
4308c2ecf20Sopenharmony_ci		if (rv3028->rtc->aie_timer.enabled)
4318c2ecf20Sopenharmony_ci			ctrl |= RV3028_CTRL2_AIE;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
4358c2ecf20Sopenharmony_ci				 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	return ret;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
4438c2ecf20Sopenharmony_ci	int ctrl = 0, ret;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (enabled) {
4468c2ecf20Sopenharmony_ci		if (rv3028->rtc->uie_rtctimer.enabled)
4478c2ecf20Sopenharmony_ci			ctrl |= RV3028_CTRL2_UIE;
4488c2ecf20Sopenharmony_ci		if (rv3028->rtc->aie_timer.enabled)
4498c2ecf20Sopenharmony_ci			ctrl |= RV3028_CTRL2_AIE;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
4538c2ecf20Sopenharmony_ci				 RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
4548c2ecf20Sopenharmony_ci	if (ret)
4558c2ecf20Sopenharmony_ci		return ret;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
4588c2ecf20Sopenharmony_ci				 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
4598c2ecf20Sopenharmony_ci	if (ret)
4608c2ecf20Sopenharmony_ci		return ret;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	return 0;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic int rv3028_read_offset(struct device *dev, long *offset)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
4688c2ecf20Sopenharmony_ci	int ret, value, steps;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
4718c2ecf20Sopenharmony_ci	if (ret < 0)
4728c2ecf20Sopenharmony_ci		return ret;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	steps = sign_extend32(value << 1, 8);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
4778c2ecf20Sopenharmony_ci	if (ret < 0)
4788c2ecf20Sopenharmony_ci		return ret;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	steps += value >> 7;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	*offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	return 0;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int rv3028_set_offset(struct device *dev, long offset)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
4908c2ecf20Sopenharmony_ci	u32 eerd;
4918c2ecf20Sopenharmony_ci	int ret;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	offset = clamp(offset, -244141L, 243187L) * 1000;
4948c2ecf20Sopenharmony_ci	offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	ret = rv3028_enter_eerd(rv3028, &eerd);
4978c2ecf20Sopenharmony_ci	if (ret)
4988c2ecf20Sopenharmony_ci		return ret;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
5018c2ecf20Sopenharmony_ci	if (ret < 0)
5028c2ecf20Sopenharmony_ci		goto exit_eerd;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
5058c2ecf20Sopenharmony_ci				 offset << 7);
5068c2ecf20Sopenharmony_ci	if (ret < 0)
5078c2ecf20Sopenharmony_ci		goto exit_eerd;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	return rv3028_update_eeprom(rv3028, eerd);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ciexit_eerd:
5128c2ecf20Sopenharmony_ci	rv3028_exit_eerd(rv3028, eerd);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	return ret;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
5218c2ecf20Sopenharmony_ci	int status, ret = 0;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	switch (cmd) {
5248c2ecf20Sopenharmony_ci	case RTC_VL_READ:
5258c2ecf20Sopenharmony_ci		ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
5268c2ecf20Sopenharmony_ci		if (ret < 0)
5278c2ecf20Sopenharmony_ci			return ret;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		status = status & RV3028_STATUS_PORF ? RTC_VL_DATA_INVALID : 0;
5308c2ecf20Sopenharmony_ci		return put_user(status, (unsigned int __user *)arg);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	default:
5338c2ecf20Sopenharmony_ci		return -ENOIOCTLCMD;
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
5388c2ecf20Sopenharmony_ci			      size_t bytes)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
5448c2ecf20Sopenharmony_ci			     size_t bytes)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
5508c2ecf20Sopenharmony_ci			       size_t bytes)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = priv;
5538c2ecf20Sopenharmony_ci	u32 status, eerd;
5548c2ecf20Sopenharmony_ci	int i, ret;
5558c2ecf20Sopenharmony_ci	u8 *buf = val;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	ret = rv3028_enter_eerd(rv3028, &eerd);
5588c2ecf20Sopenharmony_ci	if (ret)
5598c2ecf20Sopenharmony_ci		return ret;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i++) {
5628c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
5638c2ecf20Sopenharmony_ci		if (ret)
5648c2ecf20Sopenharmony_ci			goto restore_eerd;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_DATA, buf[i]);
5678c2ecf20Sopenharmony_ci		if (ret)
5688c2ecf20Sopenharmony_ci			goto restore_eerd;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
5718c2ecf20Sopenharmony_ci		if (ret)
5728c2ecf20Sopenharmony_ci			goto restore_eerd;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
5758c2ecf20Sopenharmony_ci				   RV3028_EEPROM_CMD_WRITE);
5768c2ecf20Sopenharmony_ci		if (ret)
5778c2ecf20Sopenharmony_ci			goto restore_eerd;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci		ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
5828c2ecf20Sopenharmony_ci					       !(status & RV3028_STATUS_EEBUSY),
5838c2ecf20Sopenharmony_ci					       RV3028_EEBUSY_POLL,
5848c2ecf20Sopenharmony_ci					       RV3028_EEBUSY_TIMEOUT);
5858c2ecf20Sopenharmony_ci		if (ret)
5868c2ecf20Sopenharmony_ci			goto restore_eerd;
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_cirestore_eerd:
5908c2ecf20Sopenharmony_ci	rv3028_exit_eerd(rv3028, eerd);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	return ret;
5938c2ecf20Sopenharmony_ci}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
5968c2ecf20Sopenharmony_ci			      size_t bytes)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = priv;
5998c2ecf20Sopenharmony_ci	u32 status, eerd, data;
6008c2ecf20Sopenharmony_ci	int i, ret;
6018c2ecf20Sopenharmony_ci	u8 *buf = val;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	ret = rv3028_enter_eerd(rv3028, &eerd);
6048c2ecf20Sopenharmony_ci	if (ret)
6058c2ecf20Sopenharmony_ci		return ret;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i++) {
6088c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
6098c2ecf20Sopenharmony_ci		if (ret)
6108c2ecf20Sopenharmony_ci			goto restore_eerd;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
6138c2ecf20Sopenharmony_ci		if (ret)
6148c2ecf20Sopenharmony_ci			goto restore_eerd;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
6178c2ecf20Sopenharmony_ci				   RV3028_EEPROM_CMD_READ);
6188c2ecf20Sopenharmony_ci		if (ret)
6198c2ecf20Sopenharmony_ci			goto restore_eerd;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci		ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
6228c2ecf20Sopenharmony_ci					       !(status & RV3028_STATUS_EEBUSY),
6238c2ecf20Sopenharmony_ci					       RV3028_EEBUSY_POLL,
6248c2ecf20Sopenharmony_ci					       RV3028_EEBUSY_TIMEOUT);
6258c2ecf20Sopenharmony_ci		if (ret)
6268c2ecf20Sopenharmony_ci			goto restore_eerd;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		ret = regmap_read(rv3028->regmap, RV3028_EEPROM_DATA, &data);
6298c2ecf20Sopenharmony_ci		if (ret)
6308c2ecf20Sopenharmony_ci			goto restore_eerd;
6318c2ecf20Sopenharmony_ci		buf[i] = data;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cirestore_eerd:
6358c2ecf20Sopenharmony_ci	rv3028_exit_eerd(rv3028, eerd);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	return ret;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
6418c2ecf20Sopenharmony_ci#define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw)
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic int clkout_rates[] = {
6448c2ecf20Sopenharmony_ci	32768,
6458c2ecf20Sopenharmony_ci	8192,
6468c2ecf20Sopenharmony_ci	1024,
6478c2ecf20Sopenharmony_ci	64,
6488c2ecf20Sopenharmony_ci	32,
6498c2ecf20Sopenharmony_ci	1,
6508c2ecf20Sopenharmony_ci};
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic unsigned long rv3028_clkout_recalc_rate(struct clk_hw *hw,
6538c2ecf20Sopenharmony_ci					       unsigned long parent_rate)
6548c2ecf20Sopenharmony_ci{
6558c2ecf20Sopenharmony_ci	int clkout, ret;
6568c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
6598c2ecf20Sopenharmony_ci	if (ret < 0)
6608c2ecf20Sopenharmony_ci		return 0;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	clkout &= RV3028_CLKOUT_FD_MASK;
6638c2ecf20Sopenharmony_ci	return clkout_rates[clkout];
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic long rv3028_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
6678c2ecf20Sopenharmony_ci				     unsigned long *prate)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	int i;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
6728c2ecf20Sopenharmony_ci		if (clkout_rates[i] <= rate)
6738c2ecf20Sopenharmony_ci			return clkout_rates[i];
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	return 0;
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_cistatic int rv3028_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
6798c2ecf20Sopenharmony_ci				  unsigned long parent_rate)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	int i, ret;
6828c2ecf20Sopenharmony_ci	u32 enabled;
6838c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &enabled);
6868c2ecf20Sopenharmony_ci	if (ret < 0)
6878c2ecf20Sopenharmony_ci		return ret;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	ret = regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
6908c2ecf20Sopenharmony_ci	if (ret < 0)
6918c2ecf20Sopenharmony_ci		return ret;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	enabled &= RV3028_CLKOUT_CLKOE;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
6968c2ecf20Sopenharmony_ci		if (clkout_rates[i] == rate)
6978c2ecf20Sopenharmony_ci			return rv3028_update_cfg(rv3028, RV3028_CLKOUT, 0xff,
6988c2ecf20Sopenharmony_ci						 RV3028_CLKOUT_CLKSY | enabled | i);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	return -EINVAL;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic int rv3028_clkout_prepare(struct clk_hw *hw)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	return regmap_write(rv3028->regmap, RV3028_CLKOUT,
7088c2ecf20Sopenharmony_ci			    RV3028_CLKOUT_CLKSY | RV3028_CLKOUT_CLKOE);
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic void rv3028_clkout_unprepare(struct clk_hw *hw)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
7168c2ecf20Sopenharmony_ci	regmap_update_bits(rv3028->regmap, RV3028_STATUS,
7178c2ecf20Sopenharmony_ci			   RV3028_STATUS_CLKF, 0);
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic int rv3028_clkout_is_prepared(struct clk_hw *hw)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	int clkout, ret;
7238c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
7268c2ecf20Sopenharmony_ci	if (ret < 0)
7278c2ecf20Sopenharmony_ci		return ret;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	return !!(clkout & RV3028_CLKOUT_CLKOE);
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic const struct clk_ops rv3028_clkout_ops = {
7338c2ecf20Sopenharmony_ci	.prepare = rv3028_clkout_prepare,
7348c2ecf20Sopenharmony_ci	.unprepare = rv3028_clkout_unprepare,
7358c2ecf20Sopenharmony_ci	.is_prepared = rv3028_clkout_is_prepared,
7368c2ecf20Sopenharmony_ci	.recalc_rate = rv3028_clkout_recalc_rate,
7378c2ecf20Sopenharmony_ci	.round_rate = rv3028_clkout_round_rate,
7388c2ecf20Sopenharmony_ci	.set_rate = rv3028_clkout_set_rate,
7398c2ecf20Sopenharmony_ci};
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic int rv3028_clkout_register_clk(struct rv3028_data *rv3028,
7428c2ecf20Sopenharmony_ci				      struct i2c_client *client)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	int ret;
7458c2ecf20Sopenharmony_ci	struct clk *clk;
7468c2ecf20Sopenharmony_ci	struct clk_init_data init;
7478c2ecf20Sopenharmony_ci	struct device_node *node = client->dev.of_node;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
7508c2ecf20Sopenharmony_ci				 RV3028_STATUS_CLKF, 0);
7518c2ecf20Sopenharmony_ci	if (ret < 0)
7528c2ecf20Sopenharmony_ci		return ret;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	init.name = "rv3028-clkout";
7558c2ecf20Sopenharmony_ci	init.ops = &rv3028_clkout_ops;
7568c2ecf20Sopenharmony_ci	init.flags = 0;
7578c2ecf20Sopenharmony_ci	init.parent_names = NULL;
7588c2ecf20Sopenharmony_ci	init.num_parents = 0;
7598c2ecf20Sopenharmony_ci	rv3028->clkout_hw.init = &init;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* optional override of the clockname */
7628c2ecf20Sopenharmony_ci	of_property_read_string(node, "clock-output-names", &init.name);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* register the clock */
7658c2ecf20Sopenharmony_ci	clk = devm_clk_register(&client->dev, &rv3028->clkout_hw);
7668c2ecf20Sopenharmony_ci	if (!IS_ERR(clk))
7678c2ecf20Sopenharmony_ci		of_clk_add_provider(node, of_clk_src_simple_get, clk);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	return 0;
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ci#endif
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_cistatic struct rtc_class_ops rv3028_rtc_ops = {
7748c2ecf20Sopenharmony_ci	.read_time = rv3028_get_time,
7758c2ecf20Sopenharmony_ci	.set_time = rv3028_set_time,
7768c2ecf20Sopenharmony_ci	.read_offset = rv3028_read_offset,
7778c2ecf20Sopenharmony_ci	.set_offset = rv3028_set_offset,
7788c2ecf20Sopenharmony_ci	.ioctl = rv3028_ioctl,
7798c2ecf20Sopenharmony_ci};
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic const struct regmap_config regmap_config = {
7828c2ecf20Sopenharmony_ci        .reg_bits = 8,
7838c2ecf20Sopenharmony_ci        .val_bits = 8,
7848c2ecf20Sopenharmony_ci        .max_register = 0x37,
7858c2ecf20Sopenharmony_ci};
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cistatic int rv3028_probe(struct i2c_client *client)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	struct rv3028_data *rv3028;
7908c2ecf20Sopenharmony_ci	int ret, status;
7918c2ecf20Sopenharmony_ci	u32 ohms;
7928c2ecf20Sopenharmony_ci	struct nvmem_config nvmem_cfg = {
7938c2ecf20Sopenharmony_ci		.name = "rv3028_nvram",
7948c2ecf20Sopenharmony_ci		.word_size = 1,
7958c2ecf20Sopenharmony_ci		.stride = 1,
7968c2ecf20Sopenharmony_ci		.size = 2,
7978c2ecf20Sopenharmony_ci		.type = NVMEM_TYPE_BATTERY_BACKED,
7988c2ecf20Sopenharmony_ci		.reg_read = rv3028_nvram_read,
7998c2ecf20Sopenharmony_ci		.reg_write = rv3028_nvram_write,
8008c2ecf20Sopenharmony_ci	};
8018c2ecf20Sopenharmony_ci	struct nvmem_config eeprom_cfg = {
8028c2ecf20Sopenharmony_ci		.name = "rv3028_eeprom",
8038c2ecf20Sopenharmony_ci		.word_size = 1,
8048c2ecf20Sopenharmony_ci		.stride = 1,
8058c2ecf20Sopenharmony_ci		.size = 43,
8068c2ecf20Sopenharmony_ci		.type = NVMEM_TYPE_EEPROM,
8078c2ecf20Sopenharmony_ci		.reg_read = rv3028_eeprom_read,
8088c2ecf20Sopenharmony_ci		.reg_write = rv3028_eeprom_write,
8098c2ecf20Sopenharmony_ci	};
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
8128c2ecf20Sopenharmony_ci			      GFP_KERNEL);
8138c2ecf20Sopenharmony_ci	if (!rv3028)
8148c2ecf20Sopenharmony_ci		return -ENOMEM;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	rv3028->regmap = devm_regmap_init_i2c(client, &regmap_config);
8178c2ecf20Sopenharmony_ci	if (IS_ERR(rv3028->regmap))
8188c2ecf20Sopenharmony_ci		return PTR_ERR(rv3028->regmap);
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, rv3028);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
8238c2ecf20Sopenharmony_ci	if (ret < 0)
8248c2ecf20Sopenharmony_ci		return ret;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_PORF)
8278c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if (status & RV3028_STATUS_AF)
8308c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "An alarm may have been missed.\n");
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	rv3028->rtc = devm_rtc_allocate_device(&client->dev);
8338c2ecf20Sopenharmony_ci	if (IS_ERR(rv3028->rtc))
8348c2ecf20Sopenharmony_ci		return PTR_ERR(rv3028->rtc);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	if (client->irq > 0) {
8378c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(&client->dev, client->irq,
8388c2ecf20Sopenharmony_ci						NULL, rv3028_handle_irq,
8398c2ecf20Sopenharmony_ci						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
8408c2ecf20Sopenharmony_ci						"rv3028", rv3028);
8418c2ecf20Sopenharmony_ci		if (ret) {
8428c2ecf20Sopenharmony_ci			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
8438c2ecf20Sopenharmony_ci			client->irq = 0;
8448c2ecf20Sopenharmony_ci		} else {
8458c2ecf20Sopenharmony_ci			rv3028_rtc_ops.read_alarm = rv3028_get_alarm;
8468c2ecf20Sopenharmony_ci			rv3028_rtc_ops.set_alarm = rv3028_set_alarm;
8478c2ecf20Sopenharmony_ci			rv3028_rtc_ops.alarm_irq_enable = rv3028_alarm_irq_enable;
8488c2ecf20Sopenharmony_ci		}
8498c2ecf20Sopenharmony_ci	}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
8528c2ecf20Sopenharmony_ci				 RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
8538c2ecf20Sopenharmony_ci	if (ret)
8548c2ecf20Sopenharmony_ci		return ret;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	/* setup timestamping */
8578c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
8588c2ecf20Sopenharmony_ci				 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE,
8598c2ecf20Sopenharmony_ci				 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE);
8608c2ecf20Sopenharmony_ci	if (ret)
8618c2ecf20Sopenharmony_ci		return ret;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	/* setup trickle charger */
8648c2ecf20Sopenharmony_ci	if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
8658c2ecf20Sopenharmony_ci				      &ohms)) {
8668c2ecf20Sopenharmony_ci		int i;
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
8698c2ecf20Sopenharmony_ci			if (ohms == rv3028_trickle_resistors[i])
8708c2ecf20Sopenharmony_ci				break;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci		if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
8738c2ecf20Sopenharmony_ci			ret = rv3028_update_cfg(rv3028, RV3028_BACKUP, RV3028_BACKUP_TCE |
8748c2ecf20Sopenharmony_ci						 RV3028_BACKUP_TCR_MASK, RV3028_BACKUP_TCE | i);
8758c2ecf20Sopenharmony_ci			if (ret)
8768c2ecf20Sopenharmony_ci				return ret;
8778c2ecf20Sopenharmony_ci		} else {
8788c2ecf20Sopenharmony_ci			dev_warn(&client->dev, "invalid trickle resistor value\n");
8798c2ecf20Sopenharmony_ci		}
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
8838c2ecf20Sopenharmony_ci	if (ret)
8848c2ecf20Sopenharmony_ci		return ret;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
8878c2ecf20Sopenharmony_ci	rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099;
8888c2ecf20Sopenharmony_ci	rv3028->rtc->ops = &rv3028_rtc_ops;
8898c2ecf20Sopenharmony_ci	ret = rtc_register_device(rv3028->rtc);
8908c2ecf20Sopenharmony_ci	if (ret)
8918c2ecf20Sopenharmony_ci		return ret;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	nvmem_cfg.priv = rv3028->regmap;
8948c2ecf20Sopenharmony_ci	rtc_nvmem_register(rv3028->rtc, &nvmem_cfg);
8958c2ecf20Sopenharmony_ci	eeprom_cfg.priv = rv3028;
8968c2ecf20Sopenharmony_ci	rtc_nvmem_register(rv3028->rtc, &eeprom_cfg);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	rv3028->rtc->max_user_freq = 1;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
9018c2ecf20Sopenharmony_ci	rv3028_clkout_register_clk(rv3028, client);
9028c2ecf20Sopenharmony_ci#endif
9038c2ecf20Sopenharmony_ci	return 0;
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic const struct of_device_id rv3028_of_match[] = {
9078c2ecf20Sopenharmony_ci	{ .compatible = "microcrystal,rv3028", },
9088c2ecf20Sopenharmony_ci	{ }
9098c2ecf20Sopenharmony_ci};
9108c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rv3028_of_match);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_cistatic struct i2c_driver rv3028_driver = {
9138c2ecf20Sopenharmony_ci	.driver = {
9148c2ecf20Sopenharmony_ci		.name = "rtc-rv3028",
9158c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(rv3028_of_match),
9168c2ecf20Sopenharmony_ci	},
9178c2ecf20Sopenharmony_ci	.probe_new	= rv3028_probe,
9188c2ecf20Sopenharmony_ci};
9198c2ecf20Sopenharmony_cimodule_i2c_driver(rv3028_driver);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
9228c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
9238c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
924