xref: /linux/drivers/rtc/rtc-rv3029c2.c (revision 23c48a124b469cee2eb0c75e6d22d366d1caa118)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Micro Crystal RV-3029 / RV-3049 rtc class driver
4  *
5  * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
6  *         Michael Buesch <m@bues.ch>
7  *
8  * based on previously existing rtc class drivers
9  */
10 
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/spi/spi.h>
14 #include <linux/bcd.h>
15 #include <linux/rtc.h>
16 #include <linux/delay.h>
17 #include <linux/of.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/regmap.h>
21 
22 /* Register map */
23 /* control section */
24 #define RV3029_ONOFF_CTRL		0x00
25 #define RV3029_ONOFF_CTRL_WE		BIT(0)
26 #define RV3029_ONOFF_CTRL_TE		BIT(1)
27 #define RV3029_ONOFF_CTRL_TAR		BIT(2)
28 #define RV3029_ONOFF_CTRL_EERE		BIT(3)
29 #define RV3029_ONOFF_CTRL_SRON		BIT(4)
30 #define RV3029_ONOFF_CTRL_TD0		BIT(5)
31 #define RV3029_ONOFF_CTRL_TD1		BIT(6)
32 #define RV3029_ONOFF_CTRL_CLKINT	BIT(7)
33 #define RV3029_IRQ_CTRL			0x01
34 #define RV3029_IRQ_CTRL_AIE		BIT(0)
35 #define RV3029_IRQ_CTRL_TIE		BIT(1)
36 #define RV3029_IRQ_CTRL_V1IE		BIT(2)
37 #define RV3029_IRQ_CTRL_V2IE		BIT(3)
38 #define RV3029_IRQ_CTRL_SRIE		BIT(4)
39 #define RV3029_IRQ_FLAGS		0x02
40 #define RV3029_IRQ_FLAGS_AF		BIT(0)
41 #define RV3029_IRQ_FLAGS_TF		BIT(1)
42 #define RV3029_IRQ_FLAGS_V1IF		BIT(2)
43 #define RV3029_IRQ_FLAGS_V2IF		BIT(3)
44 #define RV3029_IRQ_FLAGS_SRF		BIT(4)
45 #define RV3029_STATUS			0x03
46 #define RV3029_STATUS_VLOW1		BIT(2)
47 #define RV3029_STATUS_VLOW2		BIT(3)
48 #define RV3029_STATUS_SR		BIT(4)
49 #define RV3029_STATUS_PON		BIT(5)
50 #define RV3029_STATUS_EEBUSY		BIT(7)
51 #define RV3029_RST_CTRL			0x04
52 #define RV3029_RST_CTRL_SYSR		BIT(4)
53 #define RV3029_CONTROL_SECTION_LEN	0x05
54 
55 /* watch section */
56 #define RV3029_W_SEC			0x08
57 #define RV3029_W_MINUTES		0x09
58 #define RV3029_W_HOURS			0x0A
59 #define RV3029_REG_HR_12_24		BIT(6) /* 24h/12h mode */
60 #define RV3029_REG_HR_PM		BIT(5) /* PM/AM bit in 12h mode */
61 #define RV3029_W_DATE			0x0B
62 #define RV3029_W_DAYS			0x0C
63 #define RV3029_W_MONTHS			0x0D
64 #define RV3029_W_YEARS			0x0E
65 #define RV3029_WATCH_SECTION_LEN	0x07
66 
67 /* alarm section */
68 #define RV3029_A_SC			0x10
69 #define RV3029_A_MN			0x11
70 #define RV3029_A_HR			0x12
71 #define RV3029_A_DT			0x13
72 #define RV3029_A_DW			0x14
73 #define RV3029_A_MO			0x15
74 #define RV3029_A_YR			0x16
75 #define RV3029_A_AE_X			BIT(7)
76 #define RV3029_ALARM_SECTION_LEN	0x07
77 
78 /* timer section */
79 #define RV3029_TIMER_LOW		0x18
80 #define RV3029_TIMER_HIGH		0x19
81 
82 /* temperature section */
83 #define RV3029_TEMP_PAGE		0x20
84 
85 /* eeprom data section */
86 #define RV3029_E2P_EEDATA1		0x28
87 #define RV3029_E2P_EEDATA2		0x29
88 #define RV3029_E2PDATA_SECTION_LEN	0x02
89 
90 /* eeprom control section */
91 #define RV3029_CONTROL_E2P_EECTRL	0x30
92 #define RV3029_EECTRL_THP		BIT(0) /* temp scan interval */
93 #define RV3029_EECTRL_THE		BIT(1) /* thermometer enable */
94 #define RV3029_EECTRL_FD0		BIT(2) /* CLKOUT */
95 #define RV3029_EECTRL_FD1		BIT(3) /* CLKOUT */
96 #define RV3029_TRICKLE_1K		BIT(4) /* 1.5K resistance */
97 #define RV3029_TRICKLE_5K		BIT(5) /* 5K   resistance */
98 #define RV3029_TRICKLE_20K		BIT(6) /* 20K  resistance */
99 #define RV3029_TRICKLE_80K		BIT(7) /* 80K  resistance */
100 #define RV3029_TRICKLE_MASK		(RV3029_TRICKLE_1K |\
101 					 RV3029_TRICKLE_5K |\
102 					 RV3029_TRICKLE_20K |\
103 					 RV3029_TRICKLE_80K)
104 #define RV3029_TRICKLE_SHIFT		4
105 #define RV3029_CONTROL_E2P_XOFFS	0x31 /* XTAL offset */
106 #define RV3029_CONTROL_E2P_XOFFS_SIGN	BIT(7) /* Sign: 1->pos, 0->neg */
107 #define RV3029_CONTROL_E2P_QCOEF	0x32 /* XTAL temp drift coef */
108 #define RV3029_CONTROL_E2P_TURNOVER	0x33 /* XTAL turnover temp (in *C) */
109 #define RV3029_CONTROL_E2P_TOV_MASK	0x3F /* XTAL turnover temp mask */
110 
111 /* user ram section */
112 #define RV3029_RAM_PAGE			0x38
113 #define RV3029_RAM_SECTION_LEN		8
114 
115 struct rv3029_data {
116 	struct device		*dev;
117 	struct rtc_device	*rtc;
118 	struct regmap		*regmap;
119 	int irq;
120 };
121 
122 static int rv3029_eeprom_busywait(struct rv3029_data *rv3029)
123 {
124 	unsigned int sr;
125 	int i, ret;
126 
127 	for (i = 100; i > 0; i--) {
128 		ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
129 		if (ret < 0)
130 			break;
131 		if (!(sr & RV3029_STATUS_EEBUSY))
132 			break;
133 		usleep_range(1000, 10000);
134 	}
135 	if (i <= 0) {
136 		dev_err(rv3029->dev, "EEPROM busy wait timeout.\n");
137 		return -ETIMEDOUT;
138 	}
139 
140 	return ret;
141 }
142 
143 static int rv3029_eeprom_exit(struct rv3029_data *rv3029)
144 {
145 	/* Re-enable eeprom refresh */
146 	return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
147 				  RV3029_ONOFF_CTRL_EERE,
148 				  RV3029_ONOFF_CTRL_EERE);
149 }
150 
151 static int rv3029_eeprom_enter(struct rv3029_data *rv3029)
152 {
153 	unsigned int sr;
154 	int ret;
155 
156 	/* Check whether we are in the allowed voltage range. */
157 	ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
158 	if (ret < 0)
159 		return ret;
160 	if (sr & RV3029_STATUS_VLOW2)
161 		return -ENODEV;
162 	if (sr & RV3029_STATUS_VLOW1) {
163 		/* We clear the bits and retry once just in case
164 		 * we had a brown out in early startup.
165 		 */
166 		ret = regmap_update_bits(rv3029->regmap, RV3029_STATUS,
167 					 RV3029_STATUS_VLOW1, 0);
168 		if (ret < 0)
169 			return ret;
170 		usleep_range(1000, 10000);
171 		ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
172 		if (ret < 0)
173 			return ret;
174 		if (sr & RV3029_STATUS_VLOW1) {
175 			dev_err(rv3029->dev,
176 				"Supply voltage is too low to safely access the EEPROM.\n");
177 			return -ENODEV;
178 		}
179 	}
180 
181 	/* Disable eeprom refresh. */
182 	ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
183 				 RV3029_ONOFF_CTRL_EERE, 0);
184 	if (ret < 0)
185 		return ret;
186 
187 	/* Wait for any previous eeprom accesses to finish. */
188 	ret = rv3029_eeprom_busywait(rv3029);
189 	if (ret < 0)
190 		rv3029_eeprom_exit(rv3029);
191 
192 	return ret;
193 }
194 
195 static int rv3029_eeprom_read(struct rv3029_data *rv3029, u8 reg,
196 			      u8 buf[], size_t len)
197 {
198 	int ret, err;
199 
200 	err = rv3029_eeprom_enter(rv3029);
201 	if (err < 0)
202 		return err;
203 
204 	ret = regmap_bulk_read(rv3029->regmap, reg, buf, len);
205 
206 	err = rv3029_eeprom_exit(rv3029);
207 	if (err < 0)
208 		return err;
209 
210 	return ret;
211 }
212 
213 static int rv3029_eeprom_write(struct rv3029_data *rv3029, u8 reg,
214 			       u8 const buf[], size_t len)
215 {
216 	unsigned int tmp;
217 	int ret, err;
218 	size_t i;
219 
220 	err = rv3029_eeprom_enter(rv3029);
221 	if (err < 0)
222 		return err;
223 
224 	for (i = 0; i < len; i++, reg++) {
225 		ret = regmap_read(rv3029->regmap, reg, &tmp);
226 		if (ret < 0)
227 			break;
228 		if (tmp != buf[i]) {
229 			tmp = buf[i];
230 			ret = regmap_write(rv3029->regmap, reg, tmp);
231 			if (ret < 0)
232 				break;
233 		}
234 		ret = rv3029_eeprom_busywait(rv3029);
235 		if (ret < 0)
236 			break;
237 	}
238 
239 	err = rv3029_eeprom_exit(rv3029);
240 	if (err < 0)
241 		return err;
242 
243 	return ret;
244 }
245 
246 static int rv3029_eeprom_update_bits(struct rv3029_data *rv3029,
247 				     u8 reg, u8 mask, u8 set)
248 {
249 	u8 buf;
250 	int ret;
251 
252 	ret = rv3029_eeprom_read(rv3029, reg, &buf, 1);
253 	if (ret < 0)
254 		return ret;
255 	buf &= ~mask;
256 	buf |= set & mask;
257 	ret = rv3029_eeprom_write(rv3029, reg, &buf, 1);
258 	if (ret < 0)
259 		return ret;
260 
261 	return 0;
262 }
263 
264 static irqreturn_t rv3029_handle_irq(int irq, void *dev_id)
265 {
266 	struct device *dev = dev_id;
267 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
268 	unsigned int flags, controls;
269 	unsigned long events = 0;
270 	int ret;
271 
272 	rtc_lock(rv3029->rtc);
273 
274 	ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
275 	if (ret) {
276 		dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
277 		rtc_unlock(rv3029->rtc);
278 		return IRQ_NONE;
279 	}
280 
281 	ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
282 	if (ret) {
283 		dev_warn(dev, "Read IRQ Flags Register error %d\n", ret);
284 		rtc_unlock(rv3029->rtc);
285 		return IRQ_NONE;
286 	}
287 
288 	if (flags & RV3029_IRQ_FLAGS_AF) {
289 		flags &= ~RV3029_IRQ_FLAGS_AF;
290 		controls &= ~RV3029_IRQ_CTRL_AIE;
291 		events |= RTC_AF;
292 	}
293 
294 	if (events) {
295 		rtc_update_irq(rv3029->rtc, 1, events);
296 		regmap_write(rv3029->regmap, RV3029_IRQ_FLAGS, flags);
297 		regmap_write(rv3029->regmap, RV3029_IRQ_CTRL, controls);
298 	}
299 	rtc_unlock(rv3029->rtc);
300 
301 	return IRQ_HANDLED;
302 }
303 
304 static int rv3029_read_time(struct device *dev, struct rtc_time *tm)
305 {
306 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
307 	unsigned int sr;
308 	int ret;
309 	u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
310 
311 	ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
312 	if (ret < 0)
313 		return ret;
314 
315 	if (sr & (RV3029_STATUS_VLOW2 | RV3029_STATUS_PON))
316 		return -EINVAL;
317 
318 	ret = regmap_bulk_read(rv3029->regmap, RV3029_W_SEC, regs,
319 			       RV3029_WATCH_SECTION_LEN);
320 	if (ret < 0)
321 		return ret;
322 
323 	tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
324 	tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
325 
326 	/* HR field has a more complex interpretation */
327 	{
328 		const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
329 
330 		if (_hr & RV3029_REG_HR_12_24) {
331 			/* 12h format */
332 			tm->tm_hour = bcd2bin(_hr & 0x1f);
333 			if (_hr & RV3029_REG_HR_PM)	/* PM flag set */
334 				tm->tm_hour += 12;
335 		} else /* 24h format */
336 			tm->tm_hour = bcd2bin(_hr & 0x3f);
337 	}
338 
339 	tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
340 	tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
341 	tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100;
342 	tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
343 
344 	return 0;
345 }
346 
347 static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
348 {
349 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
350 	struct rtc_time *const tm = &alarm->time;
351 	unsigned int controls, flags;
352 	int ret;
353 	u8 regs[8];
354 
355 	ret = regmap_bulk_read(rv3029->regmap, RV3029_A_SC, regs,
356 			       RV3029_ALARM_SECTION_LEN);
357 	if (ret < 0)
358 		return ret;
359 
360 	ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
361 	if (ret)
362 		return ret;
363 
364 	ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
365 	if (ret < 0)
366 		return ret;
367 
368 	tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f);
369 	tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f);
370 	tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f);
371 	tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f);
372 	tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1;
373 	tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100;
374 	tm->tm_wday = bcd2bin(regs[RV3029_A_DW - RV3029_A_SC] & 0x07) - 1;
375 
376 	alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE);
377 	alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled;
378 
379 	return 0;
380 }
381 
382 static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable)
383 {
384 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
385 
386 	return regmap_update_bits(rv3029->regmap, RV3029_IRQ_CTRL,
387 				  RV3029_IRQ_CTRL_AIE,
388 				  enable ? RV3029_IRQ_CTRL_AIE : 0);
389 }
390 
391 static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
392 {
393 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
394 	struct rtc_time *const tm = &alarm->time;
395 	int ret;
396 	u8 regs[8];
397 
398 	/* Activate all the alarms with AE_x bit */
399 	regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X;
400 	regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X;
401 	regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f)
402 		| RV3029_A_AE_X;
403 	regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f)
404 		| RV3029_A_AE_X;
405 	regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f)
406 		| RV3029_A_AE_X;
407 	regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7)
408 		| RV3029_A_AE_X;
409 	regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100))
410 		| RV3029_A_AE_X;
411 
412 	/* Write the alarm */
413 	ret = regmap_bulk_write(rv3029->regmap, RV3029_A_SC, regs,
414 				RV3029_ALARM_SECTION_LEN);
415 	if (ret < 0)
416 		return ret;
417 
418 	return rv3029_alarm_irq_enable(dev, alarm->enabled);
419 }
420 
421 static int rv3029_set_time(struct device *dev, struct rtc_time *tm)
422 {
423 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
424 	u8 regs[8];
425 	int ret;
426 
427 	regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
428 	regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
429 	regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
430 	regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
431 	regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
432 	regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
433 	regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
434 
435 	ret = regmap_bulk_write(rv3029->regmap, RV3029_W_SEC, regs,
436 				RV3029_WATCH_SECTION_LEN);
437 	if (ret < 0)
438 		return ret;
439 
440 	/* clear PON and VLOW2 bits */
441 	return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
442 				  RV3029_STATUS_PON | RV3029_STATUS_VLOW2, 0);
443 }
444 
445 static int rv3029_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
446 {
447 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
448 	unsigned long vl = 0;
449 	int sr, ret = 0;
450 
451 	switch (cmd) {
452 	case RTC_VL_READ:
453 		ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
454 		if (ret < 0)
455 			return ret;
456 
457 		if (sr & RV3029_STATUS_VLOW1)
458 			vl = RTC_VL_ACCURACY_LOW;
459 
460 		if (sr & (RV3029_STATUS_VLOW2 | RV3029_STATUS_PON))
461 			vl |= RTC_VL_DATA_INVALID;
462 
463 		return put_user(vl, (unsigned int __user *)arg);
464 
465 	case RTC_VL_CLR:
466 		return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
467 					  RV3029_STATUS_VLOW1, 0);
468 
469 	default:
470 		return -ENOIOCTLCMD;
471 	}
472 }
473 
474 static int rv3029_nvram_write(void *priv, unsigned int offset, void *val,
475 			      size_t bytes)
476 {
477 	return regmap_bulk_write(priv, RV3029_RAM_PAGE + offset, val, bytes);
478 }
479 
480 static int rv3029_nvram_read(void *priv, unsigned int offset, void *val,
481 			     size_t bytes)
482 {
483 	return regmap_bulk_read(priv, RV3029_RAM_PAGE + offset, val, bytes);
484 }
485 
486 static const struct rv3029_trickle_tab_elem {
487 	u32 r;		/* resistance in ohms */
488 	u8 conf;	/* trickle config bits */
489 } rv3029_trickle_tab[] = {
490 	{
491 		.r	= 1076,
492 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
493 			  RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
494 	}, {
495 		.r	= 1091,
496 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
497 			  RV3029_TRICKLE_20K,
498 	}, {
499 		.r	= 1137,
500 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
501 			  RV3029_TRICKLE_80K,
502 	}, {
503 		.r	= 1154,
504 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
505 	}, {
506 		.r	= 1371,
507 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
508 			  RV3029_TRICKLE_80K,
509 	}, {
510 		.r	= 1395,
511 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
512 	}, {
513 		.r	= 1472,
514 		.conf	= RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
515 	}, {
516 		.r	= 1500,
517 		.conf	= RV3029_TRICKLE_1K,
518 	}, {
519 		.r	= 3810,
520 		.conf	= RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
521 			  RV3029_TRICKLE_80K,
522 	}, {
523 		.r	= 4000,
524 		.conf	= RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
525 	}, {
526 		.r	= 4706,
527 		.conf	= RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
528 	}, {
529 		.r	= 5000,
530 		.conf	= RV3029_TRICKLE_5K,
531 	}, {
532 		.r	= 16000,
533 		.conf	= RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
534 	}, {
535 		.r	= 20000,
536 		.conf	= RV3029_TRICKLE_20K,
537 	}, {
538 		.r	= 80000,
539 		.conf	= RV3029_TRICKLE_80K,
540 	},
541 };
542 
543 static void rv3029_trickle_config(struct device *dev)
544 {
545 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
546 	struct device_node *of_node = dev->of_node;
547 	const struct rv3029_trickle_tab_elem *elem;
548 	int i, err;
549 	u32 ohms;
550 	u8 trickle_set_bits;
551 
552 	if (!of_node)
553 		return;
554 
555 	/* Configure the trickle charger. */
556 	err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
557 	if (err) {
558 		/* Disable trickle charger. */
559 		trickle_set_bits = 0;
560 	} else {
561 		/* Enable trickle charger. */
562 		for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
563 			elem = &rv3029_trickle_tab[i];
564 			if (elem->r >= ohms)
565 				break;
566 		}
567 		trickle_set_bits = elem->conf;
568 		dev_info(dev,
569 			 "Trickle charger enabled at %d ohms resistance.\n",
570 			 elem->r);
571 	}
572 	err = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
573 					RV3029_TRICKLE_MASK,
574 					trickle_set_bits);
575 	if (err < 0)
576 		dev_err(dev, "Failed to update trickle charger config\n");
577 }
578 
579 #ifdef CONFIG_RTC_DRV_RV3029_HWMON
580 
581 static int rv3029_read_temp(struct rv3029_data *rv3029, int *temp_mC)
582 {
583 	unsigned int temp;
584 	int ret;
585 
586 	ret = regmap_read(rv3029->regmap, RV3029_TEMP_PAGE, &temp);
587 	if (ret < 0)
588 		return ret;
589 
590 	*temp_mC = ((int)temp - 60) * 1000;
591 
592 	return 0;
593 }
594 
595 static ssize_t rv3029_hwmon_show_temp(struct device *dev,
596 				      struct device_attribute *attr,
597 				      char *buf)
598 {
599 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
600 	int ret, temp_mC;
601 
602 	ret = rv3029_read_temp(rv3029, &temp_mC);
603 	if (ret < 0)
604 		return ret;
605 
606 	return sprintf(buf, "%d\n", temp_mC);
607 }
608 
609 static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
610 						struct device_attribute *attr,
611 						const char *buf,
612 						size_t count)
613 {
614 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
615 	unsigned int th_set_bits = 0;
616 	unsigned long interval_ms;
617 	int ret;
618 
619 	ret = kstrtoul(buf, 10, &interval_ms);
620 	if (ret < 0)
621 		return ret;
622 
623 	if (interval_ms != 0) {
624 		th_set_bits |= RV3029_EECTRL_THE;
625 		if (interval_ms >= 16000)
626 			th_set_bits |= RV3029_EECTRL_THP;
627 	}
628 	ret = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
629 					RV3029_EECTRL_THE | RV3029_EECTRL_THP,
630 					th_set_bits);
631 	if (ret < 0)
632 		return ret;
633 
634 	return count;
635 }
636 
637 static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
638 						 struct device_attribute *attr,
639 						 char *buf)
640 {
641 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
642 	int ret, interval_ms;
643 	u8 eectrl;
644 
645 	ret = rv3029_eeprom_read(rv3029, RV3029_CONTROL_E2P_EECTRL,
646 				 &eectrl, 1);
647 	if (ret < 0)
648 		return ret;
649 
650 	if (eectrl & RV3029_EECTRL_THE) {
651 		if (eectrl & RV3029_EECTRL_THP)
652 			interval_ms = 16000;
653 		else
654 			interval_ms = 1000;
655 	} else {
656 		interval_ms = 0;
657 	}
658 
659 	return sprintf(buf, "%d\n", interval_ms);
660 }
661 
662 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
663 			  NULL, 0);
664 static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
665 			  rv3029_hwmon_show_update_interval,
666 			  rv3029_hwmon_set_update_interval, 0);
667 
668 static struct attribute *rv3029_hwmon_attrs[] = {
669 	&sensor_dev_attr_temp1_input.dev_attr.attr,
670 	&sensor_dev_attr_update_interval.dev_attr.attr,
671 	NULL,
672 };
673 ATTRIBUTE_GROUPS(rv3029_hwmon);
674 
675 static void rv3029_hwmon_register(struct device *dev, const char *name)
676 {
677 	struct rv3029_data *rv3029 = dev_get_drvdata(dev);
678 	struct device *hwmon_dev;
679 
680 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029,
681 							   rv3029_hwmon_groups);
682 	if (IS_ERR(hwmon_dev)) {
683 		dev_warn(dev, "unable to register hwmon device %ld\n",
684 			 PTR_ERR(hwmon_dev));
685 	}
686 }
687 
688 #else /* CONFIG_RTC_DRV_RV3029_HWMON */
689 
690 static void rv3029_hwmon_register(struct device *dev, const char *name)
691 {
692 }
693 
694 #endif /* CONFIG_RTC_DRV_RV3029_HWMON */
695 
696 static const struct rtc_class_ops rv3029_rtc_ops = {
697 	.read_time	= rv3029_read_time,
698 	.set_time	= rv3029_set_time,
699 	.ioctl		= rv3029_ioctl,
700 	.read_alarm	= rv3029_read_alarm,
701 	.set_alarm	= rv3029_set_alarm,
702 	.alarm_irq_enable = rv3029_alarm_irq_enable,
703 };
704 
705 static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq,
706 			const char *name)
707 {
708 	struct rv3029_data *rv3029;
709 	struct nvmem_config nvmem_cfg = {
710 		.name = "rv3029_nvram",
711 		.word_size = 1,
712 		.stride = 1,
713 		.size = RV3029_RAM_SECTION_LEN,
714 		.type = NVMEM_TYPE_BATTERY_BACKED,
715 		.reg_read = rv3029_nvram_read,
716 		.reg_write = rv3029_nvram_write,
717 	};
718 	int rc = 0;
719 
720 	rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL);
721 	if (!rv3029)
722 		return -ENOMEM;
723 
724 	rv3029->regmap = regmap;
725 	rv3029->irq = irq;
726 	rv3029->dev = dev;
727 	dev_set_drvdata(dev, rv3029);
728 
729 	rv3029_trickle_config(dev);
730 	rv3029_hwmon_register(dev, name);
731 
732 	rv3029->rtc = devm_rtc_allocate_device(dev);
733 	if (IS_ERR(rv3029->rtc))
734 		return PTR_ERR(rv3029->rtc);
735 
736 	if (rv3029->irq > 0) {
737 		rc = devm_request_threaded_irq(dev, rv3029->irq,
738 					       NULL, rv3029_handle_irq,
739 					       IRQF_TRIGGER_LOW | IRQF_ONESHOT,
740 					       "rv3029", dev);
741 		if (rc) {
742 			dev_warn(dev, "unable to request IRQ, alarms disabled\n");
743 			rv3029->irq = 0;
744 		}
745 	}
746 	if (!rv3029->irq)
747 		clear_bit(RTC_FEATURE_ALARM, rv3029->rtc->features);
748 
749 	rv3029->rtc->ops = &rv3029_rtc_ops;
750 	rv3029->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
751 	rv3029->rtc->range_max = RTC_TIMESTAMP_END_2079;
752 
753 	rc = devm_rtc_register_device(rv3029->rtc);
754 	if (rc)
755 		return rc;
756 
757 	nvmem_cfg.priv = rv3029->regmap;
758 	devm_rtc_nvmem_register(rv3029->rtc, &nvmem_cfg);
759 
760 	return 0;
761 }
762 
763 static const struct regmap_range rv3029_holes_range[] = {
764 	regmap_reg_range(0x05, 0x07),
765 	regmap_reg_range(0x0f, 0x0f),
766 	regmap_reg_range(0x17, 0x17),
767 	regmap_reg_range(0x1a, 0x1f),
768 	regmap_reg_range(0x21, 0x27),
769 	regmap_reg_range(0x34, 0x37),
770 };
771 
772 static const struct regmap_access_table rv3029_regs = {
773 	.no_ranges =	rv3029_holes_range,
774 	.n_no_ranges =	ARRAY_SIZE(rv3029_holes_range),
775 };
776 
777 static const struct regmap_config config = {
778 	.reg_bits = 8,
779 	.val_bits = 8,
780 	.rd_table = &rv3029_regs,
781 	.wr_table = &rv3029_regs,
782 	.max_register = 0x3f,
783 };
784 
785 #if IS_ENABLED(CONFIG_I2C)
786 
787 static int rv3029_i2c_probe(struct i2c_client *client)
788 {
789 	struct regmap *regmap;
790 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
791 				     I2C_FUNC_SMBUS_BYTE)) {
792 		dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n");
793 		return -ENODEV;
794 	}
795 
796 	regmap = devm_regmap_init_i2c(client, &config);
797 	if (IS_ERR(regmap))
798 		return PTR_ERR(regmap);
799 
800 	return rv3029_probe(&client->dev, regmap, client->irq, client->name);
801 }
802 
803 static const struct i2c_device_id rv3029_id[] = {
804 	{ "rv3029", 0 },
805 	{ "rv3029c2", 0 },
806 	{ }
807 };
808 MODULE_DEVICE_TABLE(i2c, rv3029_id);
809 
810 static const __maybe_unused struct of_device_id rv3029_of_match[] = {
811 	{ .compatible = "microcrystal,rv3029" },
812 	{ }
813 };
814 MODULE_DEVICE_TABLE(of, rv3029_of_match);
815 
816 static struct i2c_driver rv3029_driver = {
817 	.driver = {
818 		.name = "rv3029",
819 		.of_match_table = of_match_ptr(rv3029_of_match),
820 	},
821 	.probe_new	= rv3029_i2c_probe,
822 	.id_table	= rv3029_id,
823 };
824 
825 static int __init rv3029_register_driver(void)
826 {
827 	return i2c_add_driver(&rv3029_driver);
828 }
829 
830 static void rv3029_unregister_driver(void)
831 {
832 	i2c_del_driver(&rv3029_driver);
833 }
834 
835 #else
836 
837 static int __init rv3029_register_driver(void)
838 {
839 	return 0;
840 }
841 
842 static void rv3029_unregister_driver(void)
843 {
844 }
845 
846 #endif
847 
848 #if IS_ENABLED(CONFIG_SPI_MASTER)
849 
850 static int rv3049_probe(struct spi_device *spi)
851 {
852 	struct regmap *regmap;
853 
854 	regmap = devm_regmap_init_spi(spi, &config);
855 	if (IS_ERR(regmap))
856 		return PTR_ERR(regmap);
857 
858 	return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049");
859 }
860 
861 static struct spi_driver rv3049_driver = {
862 	.driver = {
863 		.name    = "rv3049",
864 	},
865 	.probe   = rv3049_probe,
866 };
867 
868 static int __init rv3049_register_driver(void)
869 {
870 	return spi_register_driver(&rv3049_driver);
871 }
872 
873 static void __exit rv3049_unregister_driver(void)
874 {
875 	spi_unregister_driver(&rv3049_driver);
876 }
877 
878 #else
879 
880 static int __init rv3049_register_driver(void)
881 {
882 	return 0;
883 }
884 
885 static void __exit rv3049_unregister_driver(void)
886 {
887 }
888 
889 #endif
890 
891 static int __init rv30x9_init(void)
892 {
893 	int ret;
894 
895 	ret = rv3029_register_driver();
896 	if (ret)
897 		return ret;
898 
899 	ret = rv3049_register_driver();
900 	if (ret)
901 		rv3029_unregister_driver();
902 
903 	return ret;
904 }
905 module_init(rv30x9_init)
906 
907 static void __exit rv30x9_exit(void)
908 {
909 	rv3049_unregister_driver();
910 	rv3029_unregister_driver();
911 }
912 module_exit(rv30x9_exit)
913 
914 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
915 MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
916 MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver");
917 MODULE_LICENSE("GPL");
918 MODULE_ALIAS("spi:rv3049");
919