xref: /openbmc/linux/drivers/rtc/rtc-rv3028.c (revision de0ad60e79e1ce563223b3fddf03fc19f606c6d4)
1e6e7376cSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
2e6e7376cSAlexandre Belloni /*
3e6e7376cSAlexandre Belloni  * RTC driver for the Micro Crystal RV3028
4e6e7376cSAlexandre Belloni  *
5e6e7376cSAlexandre Belloni  * Copyright (C) 2019 Micro Crystal SA
6e6e7376cSAlexandre Belloni  *
7e6e7376cSAlexandre Belloni  * Alexandre Belloni <alexandre.belloni@bootlin.com>
8e6e7376cSAlexandre Belloni  *
9e6e7376cSAlexandre Belloni  */
10e6e7376cSAlexandre Belloni 
11f583c341SParthiban Nallathambi #include <linux/clk-provider.h>
12e6e7376cSAlexandre Belloni #include <linux/bcd.h>
13e6e7376cSAlexandre Belloni #include <linux/bitops.h>
14e6e7376cSAlexandre Belloni #include <linux/i2c.h>
15e6e7376cSAlexandre Belloni #include <linux/interrupt.h>
16e6e7376cSAlexandre Belloni #include <linux/kernel.h>
17e6e7376cSAlexandre Belloni #include <linux/log2.h>
18e6e7376cSAlexandre Belloni #include <linux/module.h>
19e6e7376cSAlexandre Belloni #include <linux/of_device.h>
20e6e7376cSAlexandre Belloni #include <linux/regmap.h>
21e6e7376cSAlexandre Belloni #include <linux/rtc.h>
22e6e7376cSAlexandre Belloni 
23e6e7376cSAlexandre Belloni #define RV3028_SEC			0x00
24e6e7376cSAlexandre Belloni #define RV3028_MIN			0x01
25e6e7376cSAlexandre Belloni #define RV3028_HOUR			0x02
26e6e7376cSAlexandre Belloni #define RV3028_WDAY			0x03
27e6e7376cSAlexandre Belloni #define RV3028_DAY			0x04
28e6e7376cSAlexandre Belloni #define RV3028_MONTH			0x05
29e6e7376cSAlexandre Belloni #define RV3028_YEAR			0x06
30e6e7376cSAlexandre Belloni #define RV3028_ALARM_MIN		0x07
31e6e7376cSAlexandre Belloni #define RV3028_ALARM_HOUR		0x08
32e6e7376cSAlexandre Belloni #define RV3028_ALARM_DAY		0x09
33e6e7376cSAlexandre Belloni #define RV3028_STATUS			0x0E
34e6e7376cSAlexandre Belloni #define RV3028_CTRL1			0x0F
35e6e7376cSAlexandre Belloni #define RV3028_CTRL2			0x10
36e6e7376cSAlexandre Belloni #define RV3028_EVT_CTRL			0x13
37e6e7376cSAlexandre Belloni #define RV3028_TS_COUNT			0x14
38e6e7376cSAlexandre Belloni #define RV3028_TS_SEC			0x15
39e6e7376cSAlexandre Belloni #define RV3028_RAM1			0x1F
40e6e7376cSAlexandre Belloni #define RV3028_EEPROM_ADDR		0x25
41e6e7376cSAlexandre Belloni #define RV3028_EEPROM_DATA		0x26
42e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD		0x27
43e6e7376cSAlexandre Belloni #define RV3028_CLKOUT			0x35
44e6e7376cSAlexandre Belloni #define RV3028_OFFSET			0x36
45e6e7376cSAlexandre Belloni #define RV3028_BACKUP			0x37
46e6e7376cSAlexandre Belloni 
47e6e7376cSAlexandre Belloni #define RV3028_STATUS_PORF		BIT(0)
48e6e7376cSAlexandre Belloni #define RV3028_STATUS_EVF		BIT(1)
49e6e7376cSAlexandre Belloni #define RV3028_STATUS_AF		BIT(2)
50e6e7376cSAlexandre Belloni #define RV3028_STATUS_TF		BIT(3)
51e6e7376cSAlexandre Belloni #define RV3028_STATUS_UF		BIT(4)
52e6e7376cSAlexandre Belloni #define RV3028_STATUS_BSF		BIT(5)
53e6e7376cSAlexandre Belloni #define RV3028_STATUS_CLKF		BIT(6)
54e6e7376cSAlexandre Belloni #define RV3028_STATUS_EEBUSY		BIT(7)
55e6e7376cSAlexandre Belloni 
56f583c341SParthiban Nallathambi #define RV3028_CLKOUT_FD_MASK		GENMASK(2, 0)
57f583c341SParthiban Nallathambi #define RV3028_CLKOUT_PORIE		BIT(3)
58f583c341SParthiban Nallathambi #define RV3028_CLKOUT_CLKSY		BIT(6)
59f583c341SParthiban Nallathambi #define RV3028_CLKOUT_CLKOE		BIT(7)
60f583c341SParthiban Nallathambi 
61e6e7376cSAlexandre Belloni #define RV3028_CTRL1_EERD		BIT(3)
62e6e7376cSAlexandre Belloni #define RV3028_CTRL1_WADA		BIT(5)
63e6e7376cSAlexandre Belloni 
64e6e7376cSAlexandre Belloni #define RV3028_CTRL2_RESET		BIT(0)
65e6e7376cSAlexandre Belloni #define RV3028_CTRL2_12_24		BIT(1)
66e6e7376cSAlexandre Belloni #define RV3028_CTRL2_EIE		BIT(2)
67e6e7376cSAlexandre Belloni #define RV3028_CTRL2_AIE		BIT(3)
68e6e7376cSAlexandre Belloni #define RV3028_CTRL2_TIE		BIT(4)
69e6e7376cSAlexandre Belloni #define RV3028_CTRL2_UIE		BIT(5)
70e6e7376cSAlexandre Belloni #define RV3028_CTRL2_TSE		BIT(7)
71e6e7376cSAlexandre Belloni 
72e6e7376cSAlexandre Belloni #define RV3028_EVT_CTRL_TSR		BIT(2)
73e6e7376cSAlexandre Belloni 
74e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD_WRITE		0x21
75e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD_READ		0x22
76e6e7376cSAlexandre Belloni 
77e6e7376cSAlexandre Belloni #define RV3028_EEBUSY_POLL		10000
78e6e7376cSAlexandre Belloni #define RV3028_EEBUSY_TIMEOUT		100000
79e6e7376cSAlexandre Belloni 
80e6e7376cSAlexandre Belloni #define RV3028_BACKUP_TCE		BIT(5)
81e6e7376cSAlexandre Belloni #define RV3028_BACKUP_TCR_MASK		GENMASK(1,0)
82e6e7376cSAlexandre Belloni 
83e6e7376cSAlexandre Belloni #define OFFSET_STEP_PPT			953674
84e6e7376cSAlexandre Belloni 
85e6e7376cSAlexandre Belloni enum rv3028_type {
86e6e7376cSAlexandre Belloni 	rv_3028,
87e6e7376cSAlexandre Belloni };
88e6e7376cSAlexandre Belloni 
89e6e7376cSAlexandre Belloni struct rv3028_data {
90e6e7376cSAlexandre Belloni 	struct regmap *regmap;
91e6e7376cSAlexandre Belloni 	struct rtc_device *rtc;
92e6e7376cSAlexandre Belloni 	enum rv3028_type type;
93f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK
94f583c341SParthiban Nallathambi 	struct clk_hw clkout_hw;
95f583c341SParthiban Nallathambi #endif
96e6e7376cSAlexandre Belloni };
97e6e7376cSAlexandre Belloni 
98c1efae14SAlexandre Belloni static u16 rv3028_trickle_resistors[] = {3000, 5000, 9000, 15000};
99e6e7376cSAlexandre Belloni 
100e6e7376cSAlexandre Belloni static ssize_t timestamp0_store(struct device *dev,
101e6e7376cSAlexandre Belloni 				struct device_attribute *attr,
102e6e7376cSAlexandre Belloni 				const char *buf, size_t count)
103e6e7376cSAlexandre Belloni {
104e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
105e6e7376cSAlexandre Belloni 
106e6e7376cSAlexandre Belloni 	regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
107e6e7376cSAlexandre Belloni 			   RV3028_EVT_CTRL_TSR);
108e6e7376cSAlexandre Belloni 
109e6e7376cSAlexandre Belloni 	return count;
110e6e7376cSAlexandre Belloni };
111e6e7376cSAlexandre Belloni 
112e6e7376cSAlexandre Belloni static ssize_t timestamp0_show(struct device *dev,
113e6e7376cSAlexandre Belloni 			       struct device_attribute *attr, char *buf)
114e6e7376cSAlexandre Belloni {
115e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
116e6e7376cSAlexandre Belloni 	struct rtc_time tm;
117e6e7376cSAlexandre Belloni 	int ret, count;
118e6e7376cSAlexandre Belloni 	u8 date[6];
119e6e7376cSAlexandre Belloni 
120e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
121e6e7376cSAlexandre Belloni 	if (ret)
122e6e7376cSAlexandre Belloni 		return ret;
123e6e7376cSAlexandre Belloni 
124e6e7376cSAlexandre Belloni 	if (!count)
125e6e7376cSAlexandre Belloni 		return 0;
126e6e7376cSAlexandre Belloni 
127e6e7376cSAlexandre Belloni 	ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
128e6e7376cSAlexandre Belloni 			       sizeof(date));
129e6e7376cSAlexandre Belloni 	if (ret)
130e6e7376cSAlexandre Belloni 		return ret;
131e6e7376cSAlexandre Belloni 
132e6e7376cSAlexandre Belloni 	tm.tm_sec = bcd2bin(date[0]);
133e6e7376cSAlexandre Belloni 	tm.tm_min = bcd2bin(date[1]);
134e6e7376cSAlexandre Belloni 	tm.tm_hour = bcd2bin(date[2]);
135e6e7376cSAlexandre Belloni 	tm.tm_mday = bcd2bin(date[3]);
136e6e7376cSAlexandre Belloni 	tm.tm_mon = bcd2bin(date[4]) - 1;
137e6e7376cSAlexandre Belloni 	tm.tm_year = bcd2bin(date[5]) + 100;
138e6e7376cSAlexandre Belloni 
139e6e7376cSAlexandre Belloni 	ret = rtc_valid_tm(&tm);
140e6e7376cSAlexandre Belloni 	if (ret)
141e6e7376cSAlexandre Belloni 		return ret;
142e6e7376cSAlexandre Belloni 
143e6e7376cSAlexandre Belloni 	return sprintf(buf, "%llu\n",
144e6e7376cSAlexandre Belloni 		       (unsigned long long)rtc_tm_to_time64(&tm));
145e6e7376cSAlexandre Belloni };
146e6e7376cSAlexandre Belloni 
147e6e7376cSAlexandre Belloni static DEVICE_ATTR_RW(timestamp0);
148e6e7376cSAlexandre Belloni 
149e6e7376cSAlexandre Belloni static ssize_t timestamp0_count_show(struct device *dev,
150e6e7376cSAlexandre Belloni 				     struct device_attribute *attr, char *buf)
151e6e7376cSAlexandre Belloni {
152e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
153e6e7376cSAlexandre Belloni 	int ret, count;
154e6e7376cSAlexandre Belloni 
155e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
156e6e7376cSAlexandre Belloni 	if (ret)
157e6e7376cSAlexandre Belloni 		return ret;
158e6e7376cSAlexandre Belloni 
159e6e7376cSAlexandre Belloni 	return sprintf(buf, "%u\n", count);
160e6e7376cSAlexandre Belloni };
161e6e7376cSAlexandre Belloni 
162e6e7376cSAlexandre Belloni static DEVICE_ATTR_RO(timestamp0_count);
163e6e7376cSAlexandre Belloni 
164e6e7376cSAlexandre Belloni static struct attribute *rv3028_attrs[] = {
165e6e7376cSAlexandre Belloni 	&dev_attr_timestamp0.attr,
166e6e7376cSAlexandre Belloni 	&dev_attr_timestamp0_count.attr,
167e6e7376cSAlexandre Belloni 	NULL
168e6e7376cSAlexandre Belloni };
169e6e7376cSAlexandre Belloni 
170e6e7376cSAlexandre Belloni static const struct attribute_group rv3028_attr_group = {
171e6e7376cSAlexandre Belloni 	.attrs	= rv3028_attrs,
172e6e7376cSAlexandre Belloni };
173e6e7376cSAlexandre Belloni 
174*de0ad60eSAlexandre Belloni static int rv3028_exit_eerd(struct rv3028_data *rv3028, u32 eerd)
175*de0ad60eSAlexandre Belloni {
176*de0ad60eSAlexandre Belloni 	if (eerd)
177*de0ad60eSAlexandre Belloni 		return 0;
178*de0ad60eSAlexandre Belloni 
179*de0ad60eSAlexandre Belloni 	return regmap_update_bits(rv3028->regmap, RV3028_CTRL1, RV3028_CTRL1_EERD, 0);
180*de0ad60eSAlexandre Belloni }
181*de0ad60eSAlexandre Belloni 
182*de0ad60eSAlexandre Belloni static int rv3028_enter_eerd(struct rv3028_data *rv3028, u32 *eerd)
183*de0ad60eSAlexandre Belloni {
184*de0ad60eSAlexandre Belloni 	u32 ctrl1, status;
185*de0ad60eSAlexandre Belloni 	int ret;
186*de0ad60eSAlexandre Belloni 
187*de0ad60eSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_CTRL1, &ctrl1);
188*de0ad60eSAlexandre Belloni 	if (ret)
189*de0ad60eSAlexandre Belloni 		return ret;
190*de0ad60eSAlexandre Belloni 
191*de0ad60eSAlexandre Belloni 	*eerd = ctrl1 & RV3028_CTRL1_EERD;
192*de0ad60eSAlexandre Belloni 	if (*eerd)
193*de0ad60eSAlexandre Belloni 		return 0;
194*de0ad60eSAlexandre Belloni 
195*de0ad60eSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
196*de0ad60eSAlexandre Belloni 				 RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
197*de0ad60eSAlexandre Belloni 	if (ret)
198*de0ad60eSAlexandre Belloni 		return ret;
199*de0ad60eSAlexandre Belloni 
200*de0ad60eSAlexandre Belloni 	ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
201*de0ad60eSAlexandre Belloni 				       !(status & RV3028_STATUS_EEBUSY),
202*de0ad60eSAlexandre Belloni 				       RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
203*de0ad60eSAlexandre Belloni 	if (ret) {
204*de0ad60eSAlexandre Belloni 		rv3028_exit_eerd(rv3028, *eerd);
205*de0ad60eSAlexandre Belloni 
206*de0ad60eSAlexandre Belloni 		return ret;
207*de0ad60eSAlexandre Belloni 	}
208*de0ad60eSAlexandre Belloni 
209*de0ad60eSAlexandre Belloni 	return 0;
210*de0ad60eSAlexandre Belloni }
211*de0ad60eSAlexandre Belloni 
212e6e7376cSAlexandre Belloni static irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
213e6e7376cSAlexandre Belloni {
214e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_id;
215e6e7376cSAlexandre Belloni 	unsigned long events = 0;
216e6e7376cSAlexandre Belloni 	u32 status = 0, ctrl = 0;
217e6e7376cSAlexandre Belloni 
218e6e7376cSAlexandre Belloni 	if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
219e6e7376cSAlexandre Belloni 	   status == 0) {
220e6e7376cSAlexandre Belloni 		return IRQ_NONE;
221e6e7376cSAlexandre Belloni 	}
222e6e7376cSAlexandre Belloni 
223e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_PORF)
224e6e7376cSAlexandre Belloni 		dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
225e6e7376cSAlexandre Belloni 
226e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_TF) {
227e6e7376cSAlexandre Belloni 		status |= RV3028_STATUS_TF;
228e6e7376cSAlexandre Belloni 		ctrl |= RV3028_CTRL2_TIE;
229e6e7376cSAlexandre Belloni 		events |= RTC_PF;
230e6e7376cSAlexandre Belloni 	}
231e6e7376cSAlexandre Belloni 
232e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_AF) {
233e6e7376cSAlexandre Belloni 		status |= RV3028_STATUS_AF;
234e6e7376cSAlexandre Belloni 		ctrl |= RV3028_CTRL2_AIE;
235e6e7376cSAlexandre Belloni 		events |= RTC_AF;
236e6e7376cSAlexandre Belloni 	}
237e6e7376cSAlexandre Belloni 
238e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_UF) {
239e6e7376cSAlexandre Belloni 		status |= RV3028_STATUS_UF;
240e6e7376cSAlexandre Belloni 		ctrl |= RV3028_CTRL2_UIE;
241e6e7376cSAlexandre Belloni 		events |= RTC_UF;
242e6e7376cSAlexandre Belloni 	}
243e6e7376cSAlexandre Belloni 
244e6e7376cSAlexandre Belloni 	if (events) {
245e6e7376cSAlexandre Belloni 		rtc_update_irq(rv3028->rtc, 1, events);
246e6e7376cSAlexandre Belloni 		regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0);
247e6e7376cSAlexandre Belloni 		regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0);
248e6e7376cSAlexandre Belloni 	}
249e6e7376cSAlexandre Belloni 
250e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_EVF) {
251e6e7376cSAlexandre Belloni 		sysfs_notify(&rv3028->rtc->dev.kobj, NULL,
252e6e7376cSAlexandre Belloni 			     dev_attr_timestamp0.attr.name);
253e6e7376cSAlexandre Belloni 		dev_warn(&rv3028->rtc->dev, "event detected");
254e6e7376cSAlexandre Belloni 	}
255e6e7376cSAlexandre Belloni 
256e6e7376cSAlexandre Belloni 	return IRQ_HANDLED;
257e6e7376cSAlexandre Belloni }
258e6e7376cSAlexandre Belloni 
259e6e7376cSAlexandre Belloni static int rv3028_get_time(struct device *dev, struct rtc_time *tm)
260e6e7376cSAlexandre Belloni {
261e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
262e6e7376cSAlexandre Belloni 	u8 date[7];
263e6e7376cSAlexandre Belloni 	int ret, status;
264e6e7376cSAlexandre Belloni 
265e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
266e6e7376cSAlexandre Belloni 	if (ret < 0)
267e6e7376cSAlexandre Belloni 		return ret;
268e6e7376cSAlexandre Belloni 
269e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_PORF) {
270e6e7376cSAlexandre Belloni 		dev_warn(dev, "Voltage low, data is invalid.\n");
271e6e7376cSAlexandre Belloni 		return -EINVAL;
272e6e7376cSAlexandre Belloni 	}
273e6e7376cSAlexandre Belloni 
274e6e7376cSAlexandre Belloni 	ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
275e6e7376cSAlexandre Belloni 	if (ret)
276e6e7376cSAlexandre Belloni 		return ret;
277e6e7376cSAlexandre Belloni 
278e6e7376cSAlexandre Belloni 	tm->tm_sec  = bcd2bin(date[RV3028_SEC] & 0x7f);
279e6e7376cSAlexandre Belloni 	tm->tm_min  = bcd2bin(date[RV3028_MIN] & 0x7f);
280e6e7376cSAlexandre Belloni 	tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f);
281e6e7376cSAlexandre Belloni 	tm->tm_wday = ilog2(date[RV3028_WDAY] & 0x7f);
282e6e7376cSAlexandre Belloni 	tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f);
283e6e7376cSAlexandre Belloni 	tm->tm_mon  = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1;
284e6e7376cSAlexandre Belloni 	tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100;
285e6e7376cSAlexandre Belloni 
286e6e7376cSAlexandre Belloni 	return 0;
287e6e7376cSAlexandre Belloni }
288e6e7376cSAlexandre Belloni 
289e6e7376cSAlexandre Belloni static int rv3028_set_time(struct device *dev, struct rtc_time *tm)
290e6e7376cSAlexandre Belloni {
291e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
292e6e7376cSAlexandre Belloni 	u8 date[7];
293e6e7376cSAlexandre Belloni 	int ret;
294e6e7376cSAlexandre Belloni 
295e6e7376cSAlexandre Belloni 	date[RV3028_SEC]   = bin2bcd(tm->tm_sec);
296e6e7376cSAlexandre Belloni 	date[RV3028_MIN]   = bin2bcd(tm->tm_min);
297e6e7376cSAlexandre Belloni 	date[RV3028_HOUR]  = bin2bcd(tm->tm_hour);
298e6e7376cSAlexandre Belloni 	date[RV3028_WDAY]  = 1 << (tm->tm_wday);
299e6e7376cSAlexandre Belloni 	date[RV3028_DAY]   = bin2bcd(tm->tm_mday);
300e6e7376cSAlexandre Belloni 	date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1);
301e6e7376cSAlexandre Belloni 	date[RV3028_YEAR]  = bin2bcd(tm->tm_year - 100);
302e6e7376cSAlexandre Belloni 
303e6e7376cSAlexandre Belloni 	/*
304e6e7376cSAlexandre Belloni 	 * Writing to the Seconds register has the same effect as setting RESET
305e6e7376cSAlexandre Belloni 	 * bit to 1
306e6e7376cSAlexandre Belloni 	 */
307e6e7376cSAlexandre Belloni 	ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
308e6e7376cSAlexandre Belloni 				sizeof(date));
309e6e7376cSAlexandre Belloni 	if (ret)
310e6e7376cSAlexandre Belloni 		return ret;
311e6e7376cSAlexandre Belloni 
312e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
313e6e7376cSAlexandre Belloni 				 RV3028_STATUS_PORF, 0);
314e6e7376cSAlexandre Belloni 
315e6e7376cSAlexandre Belloni 	return ret;
316e6e7376cSAlexandre Belloni }
317e6e7376cSAlexandre Belloni 
318e6e7376cSAlexandre Belloni static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
319e6e7376cSAlexandre Belloni {
320e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
321e6e7376cSAlexandre Belloni 	u8 alarmvals[3];
322e6e7376cSAlexandre Belloni 	int status, ctrl, ret;
323e6e7376cSAlexandre Belloni 
324e6e7376cSAlexandre Belloni 	ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
325e6e7376cSAlexandre Belloni 			       sizeof(alarmvals));
326e6e7376cSAlexandre Belloni 	if (ret)
327e6e7376cSAlexandre Belloni 		return ret;
328e6e7376cSAlexandre Belloni 
329e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
330e6e7376cSAlexandre Belloni 	if (ret < 0)
331e6e7376cSAlexandre Belloni 		return ret;
332e6e7376cSAlexandre Belloni 
333e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
334e6e7376cSAlexandre Belloni 	if (ret < 0)
335e6e7376cSAlexandre Belloni 		return ret;
336e6e7376cSAlexandre Belloni 
337e6e7376cSAlexandre Belloni 	alrm->time.tm_sec  = 0;
338e6e7376cSAlexandre Belloni 	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
339e6e7376cSAlexandre Belloni 	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
340e6e7376cSAlexandre Belloni 	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
341e6e7376cSAlexandre Belloni 
342e6e7376cSAlexandre Belloni 	alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
343e6e7376cSAlexandre Belloni 	alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
344e6e7376cSAlexandre Belloni 
345e6e7376cSAlexandre Belloni 	return 0;
346e6e7376cSAlexandre Belloni }
347e6e7376cSAlexandre Belloni 
348e6e7376cSAlexandre Belloni static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
349e6e7376cSAlexandre Belloni {
350e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
351e6e7376cSAlexandre Belloni 	u8 alarmvals[3];
352e6e7376cSAlexandre Belloni 	u8 ctrl = 0;
353e6e7376cSAlexandre Belloni 	int ret;
354e6e7376cSAlexandre Belloni 
355e6e7376cSAlexandre Belloni 	/* The alarm has no seconds, round up to nearest minute */
356e6e7376cSAlexandre Belloni 	if (alrm->time.tm_sec) {
357e6e7376cSAlexandre Belloni 		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
358e6e7376cSAlexandre Belloni 
359e6e7376cSAlexandre Belloni 		alarm_time += 60 - alrm->time.tm_sec;
360e6e7376cSAlexandre Belloni 		rtc_time64_to_tm(alarm_time, &alrm->time);
361e6e7376cSAlexandre Belloni 	}
362e6e7376cSAlexandre Belloni 
363e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
364e6e7376cSAlexandre Belloni 				 RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
365e6e7376cSAlexandre Belloni 	if (ret)
366e6e7376cSAlexandre Belloni 		return ret;
367e6e7376cSAlexandre Belloni 
368e6e7376cSAlexandre Belloni 	alarmvals[0] = bin2bcd(alrm->time.tm_min);
369e6e7376cSAlexandre Belloni 	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
370e6e7376cSAlexandre Belloni 	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
371e6e7376cSAlexandre Belloni 
372e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
373e6e7376cSAlexandre Belloni 				 RV3028_STATUS_AF, 0);
374e6e7376cSAlexandre Belloni 	if (ret)
375e6e7376cSAlexandre Belloni 		return ret;
376e6e7376cSAlexandre Belloni 
377e6e7376cSAlexandre Belloni 	ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
378e6e7376cSAlexandre Belloni 				sizeof(alarmvals));
379e6e7376cSAlexandre Belloni 	if (ret)
380e6e7376cSAlexandre Belloni 		return ret;
381e6e7376cSAlexandre Belloni 
382e6e7376cSAlexandre Belloni 	if (alrm->enabled) {
383e6e7376cSAlexandre Belloni 		if (rv3028->rtc->uie_rtctimer.enabled)
384e6e7376cSAlexandre Belloni 			ctrl |= RV3028_CTRL2_UIE;
385e6e7376cSAlexandre Belloni 		if (rv3028->rtc->aie_timer.enabled)
386e6e7376cSAlexandre Belloni 			ctrl |= RV3028_CTRL2_AIE;
387e6e7376cSAlexandre Belloni 	}
388e6e7376cSAlexandre Belloni 
389e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
390e6e7376cSAlexandre Belloni 				 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
391e6e7376cSAlexandre Belloni 
392e6e7376cSAlexandre Belloni 	return ret;
393e6e7376cSAlexandre Belloni }
394e6e7376cSAlexandre Belloni 
395e6e7376cSAlexandre Belloni static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
396e6e7376cSAlexandre Belloni {
397e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
398e6e7376cSAlexandre Belloni 	int ctrl = 0, ret;
399e6e7376cSAlexandre Belloni 
400e6e7376cSAlexandre Belloni 	if (enabled) {
401e6e7376cSAlexandre Belloni 		if (rv3028->rtc->uie_rtctimer.enabled)
402e6e7376cSAlexandre Belloni 			ctrl |= RV3028_CTRL2_UIE;
403e6e7376cSAlexandre Belloni 		if (rv3028->rtc->aie_timer.enabled)
404e6e7376cSAlexandre Belloni 			ctrl |= RV3028_CTRL2_AIE;
405e6e7376cSAlexandre Belloni 	}
406e6e7376cSAlexandre Belloni 
407e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
408e6e7376cSAlexandre Belloni 				 RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
409e6e7376cSAlexandre Belloni 	if (ret)
410e6e7376cSAlexandre Belloni 		return ret;
411e6e7376cSAlexandre Belloni 
412e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
413e6e7376cSAlexandre Belloni 				 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
414e6e7376cSAlexandre Belloni 	if (ret)
415e6e7376cSAlexandre Belloni 		return ret;
416e6e7376cSAlexandre Belloni 
417e6e7376cSAlexandre Belloni 	return 0;
418e6e7376cSAlexandre Belloni }
419e6e7376cSAlexandre Belloni 
420e6e7376cSAlexandre Belloni static int rv3028_read_offset(struct device *dev, long *offset)
421e6e7376cSAlexandre Belloni {
422e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
423e6e7376cSAlexandre Belloni 	int ret, value, steps;
424e6e7376cSAlexandre Belloni 
425e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
426e6e7376cSAlexandre Belloni 	if (ret < 0)
427e6e7376cSAlexandre Belloni 		return ret;
428e6e7376cSAlexandre Belloni 
429e6e7376cSAlexandre Belloni 	steps = sign_extend32(value << 1, 8);
430e6e7376cSAlexandre Belloni 
431e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
432e6e7376cSAlexandre Belloni 	if (ret < 0)
433e6e7376cSAlexandre Belloni 		return ret;
434e6e7376cSAlexandre Belloni 
435e6e7376cSAlexandre Belloni 	steps += value >> 7;
436e6e7376cSAlexandre Belloni 
437e6e7376cSAlexandre Belloni 	*offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
438e6e7376cSAlexandre Belloni 
439e6e7376cSAlexandre Belloni 	return 0;
440e6e7376cSAlexandre Belloni }
441e6e7376cSAlexandre Belloni 
442e6e7376cSAlexandre Belloni static int rv3028_set_offset(struct device *dev, long offset)
443e6e7376cSAlexandre Belloni {
444e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
445e6e7376cSAlexandre Belloni 	int ret;
446e6e7376cSAlexandre Belloni 
447e6e7376cSAlexandre Belloni 	offset = clamp(offset, -244141L, 243187L) * 1000;
448e6e7376cSAlexandre Belloni 	offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
449e6e7376cSAlexandre Belloni 
450e6e7376cSAlexandre Belloni 	ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
451e6e7376cSAlexandre Belloni 	if (ret < 0)
452e6e7376cSAlexandre Belloni 		return ret;
453e6e7376cSAlexandre Belloni 
454e6e7376cSAlexandre Belloni 	return regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
455e6e7376cSAlexandre Belloni 				  offset << 7);
456e6e7376cSAlexandre Belloni }
457e6e7376cSAlexandre Belloni 
458e6e7376cSAlexandre Belloni static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
459e6e7376cSAlexandre Belloni {
460e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028 = dev_get_drvdata(dev);
461e6e7376cSAlexandre Belloni 	int status, ret = 0;
462e6e7376cSAlexandre Belloni 
463e6e7376cSAlexandre Belloni 	switch (cmd) {
464e6e7376cSAlexandre Belloni 	case RTC_VL_READ:
465e6e7376cSAlexandre Belloni 		ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
466e6e7376cSAlexandre Belloni 		if (ret < 0)
467e6e7376cSAlexandre Belloni 			return ret;
468e6e7376cSAlexandre Belloni 
46986e655f9SAlexandre Belloni 		status = status & RV3028_STATUS_PORF ? RTC_VL_DATA_INVALID : 0;
47086e655f9SAlexandre Belloni 		return put_user(status, (unsigned int __user *)arg);
471e6e7376cSAlexandre Belloni 
472e6e7376cSAlexandre Belloni 	default:
473e6e7376cSAlexandre Belloni 		return -ENOIOCTLCMD;
474e6e7376cSAlexandre Belloni 	}
475e6e7376cSAlexandre Belloni }
476e6e7376cSAlexandre Belloni 
477e6e7376cSAlexandre Belloni static int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
478e6e7376cSAlexandre Belloni 			      size_t bytes)
479e6e7376cSAlexandre Belloni {
480e6e7376cSAlexandre Belloni 	return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
481e6e7376cSAlexandre Belloni }
482e6e7376cSAlexandre Belloni 
483e6e7376cSAlexandre Belloni static int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
484e6e7376cSAlexandre Belloni 			     size_t bytes)
485e6e7376cSAlexandre Belloni {
486e6e7376cSAlexandre Belloni 	return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
487e6e7376cSAlexandre Belloni }
488e6e7376cSAlexandre Belloni 
489e6e7376cSAlexandre Belloni static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
490e6e7376cSAlexandre Belloni 			       size_t bytes)
491e6e7376cSAlexandre Belloni {
492*de0ad60eSAlexandre Belloni 	struct rv3028_data *rv3028 = priv;
493*de0ad60eSAlexandre Belloni 	u32 status, eerd;
494*de0ad60eSAlexandre Belloni 	int i, ret;
495e6e7376cSAlexandre Belloni 	u8 *buf = val;
496e6e7376cSAlexandre Belloni 
497*de0ad60eSAlexandre Belloni 	ret = rv3028_enter_eerd(rv3028, &eerd);
498e6e7376cSAlexandre Belloni 	if (ret)
499e6e7376cSAlexandre Belloni 		return ret;
500e6e7376cSAlexandre Belloni 
501e6e7376cSAlexandre Belloni 	for (i = 0; i < bytes; i++) {
502*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
503e6e7376cSAlexandre Belloni 		if (ret)
504e6e7376cSAlexandre Belloni 			goto restore_eerd;
505e6e7376cSAlexandre Belloni 
506*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_DATA, buf[i]);
507e6e7376cSAlexandre Belloni 		if (ret)
508e6e7376cSAlexandre Belloni 			goto restore_eerd;
509e6e7376cSAlexandre Belloni 
510*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
511e6e7376cSAlexandre Belloni 		if (ret)
512e6e7376cSAlexandre Belloni 			goto restore_eerd;
513e6e7376cSAlexandre Belloni 
514*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
515e6e7376cSAlexandre Belloni 				   RV3028_EEPROM_CMD_WRITE);
516e6e7376cSAlexandre Belloni 		if (ret)
517e6e7376cSAlexandre Belloni 			goto restore_eerd;
518e6e7376cSAlexandre Belloni 
519e6e7376cSAlexandre Belloni 		usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
520e6e7376cSAlexandre Belloni 
521*de0ad60eSAlexandre Belloni 		ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
522e6e7376cSAlexandre Belloni 					       !(status & RV3028_STATUS_EEBUSY),
523e6e7376cSAlexandre Belloni 					       RV3028_EEBUSY_POLL,
524e6e7376cSAlexandre Belloni 					       RV3028_EEBUSY_TIMEOUT);
525e6e7376cSAlexandre Belloni 		if (ret)
526e6e7376cSAlexandre Belloni 			goto restore_eerd;
527e6e7376cSAlexandre Belloni 	}
528e6e7376cSAlexandre Belloni 
529e6e7376cSAlexandre Belloni restore_eerd:
530*de0ad60eSAlexandre Belloni 	rv3028_exit_eerd(rv3028, eerd);
531e6e7376cSAlexandre Belloni 
532e6e7376cSAlexandre Belloni 	return ret;
533e6e7376cSAlexandre Belloni }
534e6e7376cSAlexandre Belloni 
535e6e7376cSAlexandre Belloni static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
536e6e7376cSAlexandre Belloni 			      size_t bytes)
537e6e7376cSAlexandre Belloni {
538*de0ad60eSAlexandre Belloni 	struct rv3028_data *rv3028 = priv;
539*de0ad60eSAlexandre Belloni 	u32 status, eerd, data;
540*de0ad60eSAlexandre Belloni 	int i, ret;
541e6e7376cSAlexandre Belloni 	u8 *buf = val;
542e6e7376cSAlexandre Belloni 
543*de0ad60eSAlexandre Belloni 	ret = rv3028_enter_eerd(rv3028, &eerd);
544e6e7376cSAlexandre Belloni 	if (ret)
545e6e7376cSAlexandre Belloni 		return ret;
546e6e7376cSAlexandre Belloni 
547e6e7376cSAlexandre Belloni 	for (i = 0; i < bytes; i++) {
548*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
549e6e7376cSAlexandre Belloni 		if (ret)
550e6e7376cSAlexandre Belloni 			goto restore_eerd;
551e6e7376cSAlexandre Belloni 
552*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
553e6e7376cSAlexandre Belloni 		if (ret)
554e6e7376cSAlexandre Belloni 			goto restore_eerd;
555e6e7376cSAlexandre Belloni 
556*de0ad60eSAlexandre Belloni 		ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
557e6e7376cSAlexandre Belloni 				   RV3028_EEPROM_CMD_READ);
558e6e7376cSAlexandre Belloni 		if (ret)
559e6e7376cSAlexandre Belloni 			goto restore_eerd;
560e6e7376cSAlexandre Belloni 
561*de0ad60eSAlexandre Belloni 		ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
562e6e7376cSAlexandre Belloni 					       !(status & RV3028_STATUS_EEBUSY),
563e6e7376cSAlexandre Belloni 					       RV3028_EEBUSY_POLL,
564e6e7376cSAlexandre Belloni 					       RV3028_EEBUSY_TIMEOUT);
565e6e7376cSAlexandre Belloni 		if (ret)
566e6e7376cSAlexandre Belloni 			goto restore_eerd;
567e6e7376cSAlexandre Belloni 
568*de0ad60eSAlexandre Belloni 		ret = regmap_read(rv3028->regmap, RV3028_EEPROM_DATA, &data);
569e6e7376cSAlexandre Belloni 		if (ret)
570e6e7376cSAlexandre Belloni 			goto restore_eerd;
571e6e7376cSAlexandre Belloni 		buf[i] = data;
572e6e7376cSAlexandre Belloni 	}
573e6e7376cSAlexandre Belloni 
574e6e7376cSAlexandre Belloni restore_eerd:
575*de0ad60eSAlexandre Belloni 	rv3028_exit_eerd(rv3028, eerd);
576e6e7376cSAlexandre Belloni 
577e6e7376cSAlexandre Belloni 	return ret;
578e6e7376cSAlexandre Belloni }
579e6e7376cSAlexandre Belloni 
580f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK
581f583c341SParthiban Nallathambi #define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw)
582f583c341SParthiban Nallathambi 
583f583c341SParthiban Nallathambi static int clkout_rates[] = {
584f583c341SParthiban Nallathambi 	32768,
585f583c341SParthiban Nallathambi 	8192,
586f583c341SParthiban Nallathambi 	1024,
587f583c341SParthiban Nallathambi 	64,
588f583c341SParthiban Nallathambi 	32,
589f583c341SParthiban Nallathambi 	1,
590f583c341SParthiban Nallathambi };
591f583c341SParthiban Nallathambi 
592f583c341SParthiban Nallathambi static unsigned long rv3028_clkout_recalc_rate(struct clk_hw *hw,
593f583c341SParthiban Nallathambi 					       unsigned long parent_rate)
594f583c341SParthiban Nallathambi {
595f583c341SParthiban Nallathambi 	int clkout, ret;
596f583c341SParthiban Nallathambi 	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
597f583c341SParthiban Nallathambi 
598f583c341SParthiban Nallathambi 	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
599f583c341SParthiban Nallathambi 	if (ret < 0)
600f583c341SParthiban Nallathambi 		return 0;
601f583c341SParthiban Nallathambi 
602f583c341SParthiban Nallathambi 	clkout &= RV3028_CLKOUT_FD_MASK;
603f583c341SParthiban Nallathambi 	return clkout_rates[clkout];
604f583c341SParthiban Nallathambi }
605f583c341SParthiban Nallathambi 
606f583c341SParthiban Nallathambi static long rv3028_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
607f583c341SParthiban Nallathambi 				     unsigned long *prate)
608f583c341SParthiban Nallathambi {
609f583c341SParthiban Nallathambi 	int i;
610f583c341SParthiban Nallathambi 
611f583c341SParthiban Nallathambi 	for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
612f583c341SParthiban Nallathambi 		if (clkout_rates[i] <= rate)
613f583c341SParthiban Nallathambi 			return clkout_rates[i];
614f583c341SParthiban Nallathambi 
615f583c341SParthiban Nallathambi 	return 0;
616f583c341SParthiban Nallathambi }
617f583c341SParthiban Nallathambi 
618f583c341SParthiban Nallathambi static int rv3028_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
619f583c341SParthiban Nallathambi 				  unsigned long parent_rate)
620f583c341SParthiban Nallathambi {
621f583c341SParthiban Nallathambi 	int i, ret;
62200e8e87fSAlexandre Belloni 	u32 enabled;
623f583c341SParthiban Nallathambi 	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
624f583c341SParthiban Nallathambi 
62500e8e87fSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &enabled);
62600e8e87fSAlexandre Belloni 	if (ret < 0)
62700e8e87fSAlexandre Belloni 		return ret;
62800e8e87fSAlexandre Belloni 
629f583c341SParthiban Nallathambi 	ret = regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
630f583c341SParthiban Nallathambi 	if (ret < 0)
631f583c341SParthiban Nallathambi 		return ret;
632f583c341SParthiban Nallathambi 
63300e8e87fSAlexandre Belloni 	enabled &= RV3028_CLKOUT_CLKOE;
634f583c341SParthiban Nallathambi 
63500e8e87fSAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
63600e8e87fSAlexandre Belloni 		if (clkout_rates[i] == rate)
637f583c341SParthiban Nallathambi 			return regmap_write(rv3028->regmap, RV3028_CLKOUT,
63800e8e87fSAlexandre Belloni 					    RV3028_CLKOUT_CLKSY | enabled | i);
639f583c341SParthiban Nallathambi 
640f583c341SParthiban Nallathambi 	return -EINVAL;
641f583c341SParthiban Nallathambi }
642f583c341SParthiban Nallathambi 
643f583c341SParthiban Nallathambi static int rv3028_clkout_prepare(struct clk_hw *hw)
644f583c341SParthiban Nallathambi {
645f583c341SParthiban Nallathambi 	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
646f583c341SParthiban Nallathambi 
647f583c341SParthiban Nallathambi 	return regmap_write(rv3028->regmap, RV3028_CLKOUT,
648f583c341SParthiban Nallathambi 			    RV3028_CLKOUT_CLKSY | RV3028_CLKOUT_CLKOE);
649f583c341SParthiban Nallathambi }
650f583c341SParthiban Nallathambi 
651f583c341SParthiban Nallathambi static void rv3028_clkout_unprepare(struct clk_hw *hw)
652f583c341SParthiban Nallathambi {
653f583c341SParthiban Nallathambi 	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
654f583c341SParthiban Nallathambi 
655f583c341SParthiban Nallathambi 	regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
656f583c341SParthiban Nallathambi 	regmap_update_bits(rv3028->regmap, RV3028_STATUS,
657f583c341SParthiban Nallathambi 			   RV3028_STATUS_CLKF, 0);
658f583c341SParthiban Nallathambi }
659f583c341SParthiban Nallathambi 
660f583c341SParthiban Nallathambi static int rv3028_clkout_is_prepared(struct clk_hw *hw)
661f583c341SParthiban Nallathambi {
662f583c341SParthiban Nallathambi 	int clkout, ret;
663f583c341SParthiban Nallathambi 	struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
664f583c341SParthiban Nallathambi 
665f583c341SParthiban Nallathambi 	ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
666f583c341SParthiban Nallathambi 	if (ret < 0)
667f583c341SParthiban Nallathambi 		return ret;
668f583c341SParthiban Nallathambi 
669f583c341SParthiban Nallathambi 	return !!(clkout & RV3028_CLKOUT_CLKOE);
670f583c341SParthiban Nallathambi }
671f583c341SParthiban Nallathambi 
672f583c341SParthiban Nallathambi static const struct clk_ops rv3028_clkout_ops = {
673f583c341SParthiban Nallathambi 	.prepare = rv3028_clkout_prepare,
674f583c341SParthiban Nallathambi 	.unprepare = rv3028_clkout_unprepare,
675f583c341SParthiban Nallathambi 	.is_prepared = rv3028_clkout_is_prepared,
676f583c341SParthiban Nallathambi 	.recalc_rate = rv3028_clkout_recalc_rate,
677f583c341SParthiban Nallathambi 	.round_rate = rv3028_clkout_round_rate,
678f583c341SParthiban Nallathambi 	.set_rate = rv3028_clkout_set_rate,
679f583c341SParthiban Nallathambi };
680f583c341SParthiban Nallathambi 
681f583c341SParthiban Nallathambi static int rv3028_clkout_register_clk(struct rv3028_data *rv3028,
682f583c341SParthiban Nallathambi 				      struct i2c_client *client)
683f583c341SParthiban Nallathambi {
684f583c341SParthiban Nallathambi 	int ret;
685f583c341SParthiban Nallathambi 	struct clk *clk;
686f583c341SParthiban Nallathambi 	struct clk_init_data init;
687f583c341SParthiban Nallathambi 	struct device_node *node = client->dev.of_node;
688f583c341SParthiban Nallathambi 
689f583c341SParthiban Nallathambi 	ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
690f583c341SParthiban Nallathambi 				 RV3028_STATUS_CLKF, 0);
691f583c341SParthiban Nallathambi 	if (ret < 0)
692f583c341SParthiban Nallathambi 		return ret;
693f583c341SParthiban Nallathambi 
694f583c341SParthiban Nallathambi 	init.name = "rv3028-clkout";
695f583c341SParthiban Nallathambi 	init.ops = &rv3028_clkout_ops;
696f583c341SParthiban Nallathambi 	init.flags = 0;
697f583c341SParthiban Nallathambi 	init.parent_names = NULL;
698f583c341SParthiban Nallathambi 	init.num_parents = 0;
699f583c341SParthiban Nallathambi 	rv3028->clkout_hw.init = &init;
700f583c341SParthiban Nallathambi 
701f583c341SParthiban Nallathambi 	/* optional override of the clockname */
702f583c341SParthiban Nallathambi 	of_property_read_string(node, "clock-output-names", &init.name);
703f583c341SParthiban Nallathambi 
704f583c341SParthiban Nallathambi 	/* register the clock */
705f583c341SParthiban Nallathambi 	clk = devm_clk_register(&client->dev, &rv3028->clkout_hw);
706f583c341SParthiban Nallathambi 	if (!IS_ERR(clk))
707f583c341SParthiban Nallathambi 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
708f583c341SParthiban Nallathambi 
709f583c341SParthiban Nallathambi 	return 0;
710f583c341SParthiban Nallathambi }
711f583c341SParthiban Nallathambi #endif
712f583c341SParthiban Nallathambi 
713e6e7376cSAlexandre Belloni static struct rtc_class_ops rv3028_rtc_ops = {
714e6e7376cSAlexandre Belloni 	.read_time = rv3028_get_time,
715e6e7376cSAlexandre Belloni 	.set_time = rv3028_set_time,
716e6e7376cSAlexandre Belloni 	.read_offset = rv3028_read_offset,
717e6e7376cSAlexandre Belloni 	.set_offset = rv3028_set_offset,
718e6e7376cSAlexandre Belloni 	.ioctl = rv3028_ioctl,
719e6e7376cSAlexandre Belloni };
720e6e7376cSAlexandre Belloni 
721e6e7376cSAlexandre Belloni static const struct regmap_config regmap_config = {
722e6e7376cSAlexandre Belloni         .reg_bits = 8,
723e6e7376cSAlexandre Belloni         .val_bits = 8,
724e6e7376cSAlexandre Belloni         .max_register = 0x37,
725e6e7376cSAlexandre Belloni };
726e6e7376cSAlexandre Belloni 
727e6e7376cSAlexandre Belloni static int rv3028_probe(struct i2c_client *client)
728e6e7376cSAlexandre Belloni {
729e6e7376cSAlexandre Belloni 	struct rv3028_data *rv3028;
730e6e7376cSAlexandre Belloni 	int ret, status;
731e6e7376cSAlexandre Belloni 	u32 ohms;
732e6e7376cSAlexandre Belloni 	struct nvmem_config nvmem_cfg = {
733e6e7376cSAlexandre Belloni 		.name = "rv3028_nvram",
734e6e7376cSAlexandre Belloni 		.word_size = 1,
735e6e7376cSAlexandre Belloni 		.stride = 1,
736e6e7376cSAlexandre Belloni 		.size = 2,
737e6e7376cSAlexandre Belloni 		.type = NVMEM_TYPE_BATTERY_BACKED,
738e6e7376cSAlexandre Belloni 		.reg_read = rv3028_nvram_read,
739e6e7376cSAlexandre Belloni 		.reg_write = rv3028_nvram_write,
740e6e7376cSAlexandre Belloni 	};
741e6e7376cSAlexandre Belloni 	struct nvmem_config eeprom_cfg = {
742e6e7376cSAlexandre Belloni 		.name = "rv3028_eeprom",
743e6e7376cSAlexandre Belloni 		.word_size = 1,
744e6e7376cSAlexandre Belloni 		.stride = 1,
745e6e7376cSAlexandre Belloni 		.size = 43,
746e6e7376cSAlexandre Belloni 		.type = NVMEM_TYPE_EEPROM,
747e6e7376cSAlexandre Belloni 		.reg_read = rv3028_eeprom_read,
748e6e7376cSAlexandre Belloni 		.reg_write = rv3028_eeprom_write,
749e6e7376cSAlexandre Belloni 	};
750e6e7376cSAlexandre Belloni 
751e6e7376cSAlexandre Belloni 	rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
752e6e7376cSAlexandre Belloni 			      GFP_KERNEL);
753e6e7376cSAlexandre Belloni 	if (!rv3028)
754e6e7376cSAlexandre Belloni 		return -ENOMEM;
755e6e7376cSAlexandre Belloni 
756e6e7376cSAlexandre Belloni 	rv3028->regmap = devm_regmap_init_i2c(client, &regmap_config);
757c3b29bf6SChuhong Yuan 	if (IS_ERR(rv3028->regmap))
758c3b29bf6SChuhong Yuan 		return PTR_ERR(rv3028->regmap);
759e6e7376cSAlexandre Belloni 
760e6e7376cSAlexandre Belloni 	i2c_set_clientdata(client, rv3028);
761e6e7376cSAlexandre Belloni 
762e6e7376cSAlexandre Belloni 	ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
763e6e7376cSAlexandre Belloni 	if (ret < 0)
764e6e7376cSAlexandre Belloni 		return ret;
765e6e7376cSAlexandre Belloni 
766e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_PORF)
767e6e7376cSAlexandre Belloni 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
768e6e7376cSAlexandre Belloni 
769e6e7376cSAlexandre Belloni 	if (status & RV3028_STATUS_AF)
770e6e7376cSAlexandre Belloni 		dev_warn(&client->dev, "An alarm may have been missed.\n");
771e6e7376cSAlexandre Belloni 
772e6e7376cSAlexandre Belloni 	rv3028->rtc = devm_rtc_allocate_device(&client->dev);
77344c638ceSAlexandre Belloni 	if (IS_ERR(rv3028->rtc))
774e6e7376cSAlexandre Belloni 		return PTR_ERR(rv3028->rtc);
775e6e7376cSAlexandre Belloni 
776e6e7376cSAlexandre Belloni 	if (client->irq > 0) {
777e6e7376cSAlexandre Belloni 		ret = devm_request_threaded_irq(&client->dev, client->irq,
778e6e7376cSAlexandre Belloni 						NULL, rv3028_handle_irq,
779e6e7376cSAlexandre Belloni 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
780e6e7376cSAlexandre Belloni 						"rv3028", rv3028);
781e6e7376cSAlexandre Belloni 		if (ret) {
782e6e7376cSAlexandre Belloni 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
783e6e7376cSAlexandre Belloni 			client->irq = 0;
784e6e7376cSAlexandre Belloni 		} else {
785e6e7376cSAlexandre Belloni 			rv3028_rtc_ops.read_alarm = rv3028_get_alarm;
786e6e7376cSAlexandre Belloni 			rv3028_rtc_ops.set_alarm = rv3028_set_alarm;
787e6e7376cSAlexandre Belloni 			rv3028_rtc_ops.alarm_irq_enable = rv3028_alarm_irq_enable;
788e6e7376cSAlexandre Belloni 		}
789e6e7376cSAlexandre Belloni 	}
790e6e7376cSAlexandre Belloni 
791e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
792e6e7376cSAlexandre Belloni 				 RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
793e6e7376cSAlexandre Belloni 	if (ret)
794e6e7376cSAlexandre Belloni 		return ret;
795e6e7376cSAlexandre Belloni 
796e6e7376cSAlexandre Belloni 	/* setup timestamping */
797e6e7376cSAlexandre Belloni 	ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
798e6e7376cSAlexandre Belloni 				 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE,
799e6e7376cSAlexandre Belloni 				 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE);
800e6e7376cSAlexandre Belloni 	if (ret)
801e6e7376cSAlexandre Belloni 		return ret;
802e6e7376cSAlexandre Belloni 
803e6e7376cSAlexandre Belloni 	/* setup trickle charger */
804e6e7376cSAlexandre Belloni 	if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
805e6e7376cSAlexandre Belloni 				      &ohms)) {
806e6e7376cSAlexandre Belloni 		int i;
807e6e7376cSAlexandre Belloni 
808e6e7376cSAlexandre Belloni 		for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
809e6e7376cSAlexandre Belloni 			if (ohms == rv3028_trickle_resistors[i])
810e6e7376cSAlexandre Belloni 				break;
811e6e7376cSAlexandre Belloni 
812e6e7376cSAlexandre Belloni 		if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
813e6e7376cSAlexandre Belloni 			ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
814e6e7376cSAlexandre Belloni 						 RV3028_BACKUP_TCE |
815e6e7376cSAlexandre Belloni 						 RV3028_BACKUP_TCR_MASK,
816e6e7376cSAlexandre Belloni 						 RV3028_BACKUP_TCE | i);
817e6e7376cSAlexandre Belloni 			if (ret)
818e6e7376cSAlexandre Belloni 				return ret;
819e6e7376cSAlexandre Belloni 		} else {
820e6e7376cSAlexandre Belloni 			dev_warn(&client->dev, "invalid trickle resistor value\n");
821e6e7376cSAlexandre Belloni 		}
822e6e7376cSAlexandre Belloni 	}
823e6e7376cSAlexandre Belloni 
824e6e7376cSAlexandre Belloni 	ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
825e6e7376cSAlexandre Belloni 	if (ret)
826e6e7376cSAlexandre Belloni 		return ret;
827e6e7376cSAlexandre Belloni 
828e6e7376cSAlexandre Belloni 	rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
829e6e7376cSAlexandre Belloni 	rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099;
830e6e7376cSAlexandre Belloni 	rv3028->rtc->ops = &rv3028_rtc_ops;
831e6e7376cSAlexandre Belloni 	ret = rtc_register_device(rv3028->rtc);
832e6e7376cSAlexandre Belloni 	if (ret)
833e6e7376cSAlexandre Belloni 		return ret;
834e6e7376cSAlexandre Belloni 
835e6e7376cSAlexandre Belloni 	nvmem_cfg.priv = rv3028->regmap;
836e6e7376cSAlexandre Belloni 	rtc_nvmem_register(rv3028->rtc, &nvmem_cfg);
837*de0ad60eSAlexandre Belloni 	eeprom_cfg.priv = rv3028;
838e6e7376cSAlexandre Belloni 	rtc_nvmem_register(rv3028->rtc, &eeprom_cfg);
839e6e7376cSAlexandre Belloni 
840e6e7376cSAlexandre Belloni 	rv3028->rtc->max_user_freq = 1;
841e6e7376cSAlexandre Belloni 
842f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK
843f583c341SParthiban Nallathambi 	rv3028_clkout_register_clk(rv3028, client);
844f583c341SParthiban Nallathambi #endif
845e6e7376cSAlexandre Belloni 	return 0;
846e6e7376cSAlexandre Belloni }
847e6e7376cSAlexandre Belloni 
848e6e7376cSAlexandre Belloni static const struct of_device_id rv3028_of_match[] = {
849e6e7376cSAlexandre Belloni 	{ .compatible = "microcrystal,rv3028", },
850e6e7376cSAlexandre Belloni 	{ }
851e6e7376cSAlexandre Belloni };
852e6e7376cSAlexandre Belloni MODULE_DEVICE_TABLE(of, rv3028_of_match);
853e6e7376cSAlexandre Belloni 
854e6e7376cSAlexandre Belloni static struct i2c_driver rv3028_driver = {
855e6e7376cSAlexandre Belloni 	.driver = {
856e6e7376cSAlexandre Belloni 		.name = "rtc-rv3028",
857e6e7376cSAlexandre Belloni 		.of_match_table = of_match_ptr(rv3028_of_match),
858e6e7376cSAlexandre Belloni 	},
859e6e7376cSAlexandre Belloni 	.probe_new	= rv3028_probe,
860e6e7376cSAlexandre Belloni };
861e6e7376cSAlexandre Belloni module_i2c_driver(rv3028_driver);
862e6e7376cSAlexandre Belloni 
863e6e7376cSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
864e6e7376cSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
865e6e7376cSAlexandre Belloni MODULE_LICENSE("GPL v2");
866