xref: /openbmc/linux/drivers/rtc/rtc-abx80x.c (revision 2a12187d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A driver for the I2C members of the Abracon AB x8xx RTC family,
4  * and compatible: AB 1805 and AB 0805
5  *
6  * Copyright 2014-2015 Macq S.A.
7  *
8  * Author: Philippe De Muyter <phdm@macqel.be>
9  * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
10  *
11  */
12 
13 #include <linux/bcd.h>
14 #include <linux/i2c.h>
15 #include <linux/kstrtox.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/rtc.h>
19 #include <linux/watchdog.h>
20 
21 #define ABX8XX_REG_HTH		0x00
22 #define ABX8XX_REG_SC		0x01
23 #define ABX8XX_REG_MN		0x02
24 #define ABX8XX_REG_HR		0x03
25 #define ABX8XX_REG_DA		0x04
26 #define ABX8XX_REG_MO		0x05
27 #define ABX8XX_REG_YR		0x06
28 #define ABX8XX_REG_WD		0x07
29 
30 #define ABX8XX_REG_AHTH		0x08
31 #define ABX8XX_REG_ASC		0x09
32 #define ABX8XX_REG_AMN		0x0a
33 #define ABX8XX_REG_AHR		0x0b
34 #define ABX8XX_REG_ADA		0x0c
35 #define ABX8XX_REG_AMO		0x0d
36 #define ABX8XX_REG_AWD		0x0e
37 
38 #define ABX8XX_REG_STATUS	0x0f
39 #define ABX8XX_STATUS_AF	BIT(2)
40 #define ABX8XX_STATUS_BLF	BIT(4)
41 #define ABX8XX_STATUS_WDT	BIT(6)
42 
43 #define ABX8XX_REG_CTRL1	0x10
44 #define ABX8XX_CTRL_WRITE	BIT(0)
45 #define ABX8XX_CTRL_ARST	BIT(2)
46 #define ABX8XX_CTRL_12_24	BIT(6)
47 
48 #define ABX8XX_REG_CTRL2	0x11
49 #define ABX8XX_CTRL2_RSVD	BIT(5)
50 
51 #define ABX8XX_REG_IRQ		0x12
52 #define ABX8XX_IRQ_AIE		BIT(2)
53 #define ABX8XX_IRQ_IM_1_4	(0x3 << 5)
54 
55 #define ABX8XX_REG_CD_TIMER_CTL	0x18
56 
57 #define ABX8XX_REG_OSC		0x1c
58 #define ABX8XX_OSC_FOS		BIT(3)
59 #define ABX8XX_OSC_BOS		BIT(4)
60 #define ABX8XX_OSC_ACAL_512	BIT(5)
61 #define ABX8XX_OSC_ACAL_1024	BIT(6)
62 
63 #define ABX8XX_OSC_OSEL		BIT(7)
64 
65 #define ABX8XX_REG_OSS		0x1d
66 #define ABX8XX_OSS_OF		BIT(1)
67 #define ABX8XX_OSS_OMODE	BIT(4)
68 
69 #define ABX8XX_REG_WDT		0x1b
70 #define ABX8XX_WDT_WDS		BIT(7)
71 #define ABX8XX_WDT_BMB_MASK	0x7c
72 #define ABX8XX_WDT_BMB_SHIFT	2
73 #define ABX8XX_WDT_MAX_TIME	(ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT)
74 #define ABX8XX_WDT_WRB_MASK	0x03
75 #define ABX8XX_WDT_WRB_1HZ	0x02
76 
77 #define ABX8XX_REG_CFG_KEY	0x1f
78 #define ABX8XX_CFG_KEY_OSC	0xa1
79 #define ABX8XX_CFG_KEY_MISC	0x9d
80 
81 #define ABX8XX_REG_ID0		0x28
82 
83 #define ABX8XX_REG_OUT_CTRL	0x30
84 #define ABX8XX_OUT_CTRL_EXDS	BIT(4)
85 
86 #define ABX8XX_REG_TRICKLE	0x20
87 #define ABX8XX_TRICKLE_CHARGE_ENABLE	0xa0
88 #define ABX8XX_TRICKLE_STANDARD_DIODE	0x8
89 #define ABX8XX_TRICKLE_SCHOTTKY_DIODE	0x4
90 
91 static u8 trickle_resistors[] = {0, 3, 6, 11};
92 
93 enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
94 	AB1801, AB1803, AB1804, AB1805, RV1805, ABX80X};
95 
96 struct abx80x_cap {
97 	u16 pn;
98 	bool has_tc;
99 	bool has_wdog;
100 };
101 
102 static struct abx80x_cap abx80x_caps[] = {
103 	[AB0801] = {.pn = 0x0801},
104 	[AB0803] = {.pn = 0x0803},
105 	[AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true},
106 	[AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true},
107 	[AB1801] = {.pn = 0x1801},
108 	[AB1803] = {.pn = 0x1803},
109 	[AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true},
110 	[AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
111 	[RV1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
112 	[ABX80X] = {.pn = 0}
113 };
114 
115 struct abx80x_priv {
116 	struct rtc_device *rtc;
117 	struct i2c_client *client;
118 	struct watchdog_device wdog;
119 };
120 
121 static int abx80x_write_config_key(struct i2c_client *client, u8 key)
122 {
123 	if (i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, key) < 0) {
124 		dev_err(&client->dev, "Unable to write configuration key\n");
125 		return -EIO;
126 	}
127 
128 	return 0;
129 }
130 
131 static int abx80x_is_rc_mode(struct i2c_client *client)
132 {
133 	int flags = 0;
134 
135 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
136 	if (flags < 0) {
137 		dev_err(&client->dev,
138 			"Failed to read autocalibration attribute\n");
139 		return flags;
140 	}
141 
142 	return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
143 }
144 
145 static int abx80x_enable_trickle_charger(struct i2c_client *client,
146 					 u8 trickle_cfg)
147 {
148 	int err;
149 
150 	/*
151 	 * Write the configuration key register to enable access to the Trickle
152 	 * register
153 	 */
154 	if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
155 		return -EIO;
156 
157 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
158 					ABX8XX_TRICKLE_CHARGE_ENABLE |
159 					trickle_cfg);
160 	if (err < 0) {
161 		dev_err(&client->dev, "Unable to write trickle register\n");
162 		return -EIO;
163 	}
164 
165 	return 0;
166 }
167 
168 static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
169 {
170 	struct i2c_client *client = to_i2c_client(dev);
171 	unsigned char buf[8];
172 	int err, flags, rc_mode = 0;
173 
174 	/* Read the Oscillator Failure only in XT mode */
175 	rc_mode = abx80x_is_rc_mode(client);
176 	if (rc_mode < 0)
177 		return rc_mode;
178 
179 	if (!rc_mode) {
180 		flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
181 		if (flags < 0)
182 			return flags;
183 
184 		if (flags & ABX8XX_OSS_OF) {
185 			dev_err(dev, "Oscillator failure, data is invalid.\n");
186 			return -EINVAL;
187 		}
188 	}
189 
190 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
191 					    sizeof(buf), buf);
192 	if (err < 0) {
193 		dev_err(&client->dev, "Unable to read date\n");
194 		return -EIO;
195 	}
196 
197 	tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
198 	tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
199 	tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
200 	tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
201 	tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
202 	tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
203 	tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
204 
205 	return 0;
206 }
207 
208 static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
209 {
210 	struct i2c_client *client = to_i2c_client(dev);
211 	unsigned char buf[8];
212 	int err, flags;
213 
214 	if (tm->tm_year < 100)
215 		return -EINVAL;
216 
217 	buf[ABX8XX_REG_HTH] = 0;
218 	buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
219 	buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
220 	buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
221 	buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
222 	buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
223 	buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
224 	buf[ABX8XX_REG_WD] = tm->tm_wday;
225 
226 	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
227 					     sizeof(buf), buf);
228 	if (err < 0) {
229 		dev_err(&client->dev, "Unable to write to date registers\n");
230 		return -EIO;
231 	}
232 
233 	/* Clear the OF bit of Oscillator Status Register */
234 	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
235 	if (flags < 0)
236 		return flags;
237 
238 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
239 					flags & ~ABX8XX_OSS_OF);
240 	if (err < 0) {
241 		dev_err(&client->dev, "Unable to write oscillator status register\n");
242 		return err;
243 	}
244 
245 	return 0;
246 }
247 
248 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
249 {
250 	struct i2c_client *client = dev_id;
251 	struct abx80x_priv *priv = i2c_get_clientdata(client);
252 	struct rtc_device *rtc = priv->rtc;
253 	int status;
254 
255 	status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
256 	if (status < 0)
257 		return IRQ_NONE;
258 
259 	if (status & ABX8XX_STATUS_AF)
260 		rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
261 
262 	/*
263 	 * It is unclear if we'll get an interrupt before the external
264 	 * reset kicks in.
265 	 */
266 	if (status & ABX8XX_STATUS_WDT)
267 		dev_alert(&client->dev, "watchdog timeout interrupt.\n");
268 
269 	i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
270 
271 	return IRQ_HANDLED;
272 }
273 
274 static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
275 {
276 	struct i2c_client *client = to_i2c_client(dev);
277 	unsigned char buf[7];
278 
279 	int irq_mask, err;
280 
281 	if (client->irq <= 0)
282 		return -EINVAL;
283 
284 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
285 					    sizeof(buf), buf);
286 	if (err)
287 		return err;
288 
289 	irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
290 	if (irq_mask < 0)
291 		return irq_mask;
292 
293 	t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
294 	t->time.tm_min = bcd2bin(buf[1] & 0x7F);
295 	t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
296 	t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
297 	t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
298 	t->time.tm_wday = buf[5] & 0x7;
299 
300 	t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
301 	t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
302 
303 	return err;
304 }
305 
306 static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
307 {
308 	struct i2c_client *client = to_i2c_client(dev);
309 	u8 alarm[6];
310 	int err;
311 
312 	if (client->irq <= 0)
313 		return -EINVAL;
314 
315 	alarm[0] = 0x0;
316 	alarm[1] = bin2bcd(t->time.tm_sec);
317 	alarm[2] = bin2bcd(t->time.tm_min);
318 	alarm[3] = bin2bcd(t->time.tm_hour);
319 	alarm[4] = bin2bcd(t->time.tm_mday);
320 	alarm[5] = bin2bcd(t->time.tm_mon + 1);
321 
322 	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
323 					     sizeof(alarm), alarm);
324 	if (err < 0) {
325 		dev_err(&client->dev, "Unable to write alarm registers\n");
326 		return -EIO;
327 	}
328 
329 	if (t->enabled) {
330 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
331 						(ABX8XX_IRQ_IM_1_4 |
332 						 ABX8XX_IRQ_AIE));
333 		if (err)
334 			return err;
335 	}
336 
337 	return 0;
338 }
339 
340 static int abx80x_rtc_set_autocalibration(struct device *dev,
341 					  int autocalibration)
342 {
343 	struct i2c_client *client = to_i2c_client(dev);
344 	int retval, flags = 0;
345 
346 	if ((autocalibration != 0) && (autocalibration != 1024) &&
347 	    (autocalibration != 512)) {
348 		dev_err(dev, "autocalibration value outside permitted range\n");
349 		return -EINVAL;
350 	}
351 
352 	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
353 	if (flags < 0)
354 		return flags;
355 
356 	if (autocalibration == 0) {
357 		flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
358 	} else if (autocalibration == 1024) {
359 		/* 1024 autocalibration is 0x10 */
360 		flags |= ABX8XX_OSC_ACAL_1024;
361 		flags &= ~(ABX8XX_OSC_ACAL_512);
362 	} else {
363 		/* 512 autocalibration is 0x11 */
364 		flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
365 	}
366 
367 	/* Unlock write access to Oscillator Control Register */
368 	if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
369 		return -EIO;
370 
371 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
372 
373 	return retval;
374 }
375 
376 static int abx80x_rtc_get_autocalibration(struct device *dev)
377 {
378 	struct i2c_client *client = to_i2c_client(dev);
379 	int flags = 0, autocalibration;
380 
381 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
382 	if (flags < 0)
383 		return flags;
384 
385 	if (flags & ABX8XX_OSC_ACAL_512)
386 		autocalibration = 512;
387 	else if (flags & ABX8XX_OSC_ACAL_1024)
388 		autocalibration = 1024;
389 	else
390 		autocalibration = 0;
391 
392 	return autocalibration;
393 }
394 
395 static ssize_t autocalibration_store(struct device *dev,
396 				     struct device_attribute *attr,
397 				     const char *buf, size_t count)
398 {
399 	int retval;
400 	unsigned long autocalibration = 0;
401 
402 	retval = kstrtoul(buf, 10, &autocalibration);
403 	if (retval < 0) {
404 		dev_err(dev, "Failed to store RTC autocalibration attribute\n");
405 		return -EINVAL;
406 	}
407 
408 	retval = abx80x_rtc_set_autocalibration(dev->parent, autocalibration);
409 
410 	return retval ? retval : count;
411 }
412 
413 static ssize_t autocalibration_show(struct device *dev,
414 				    struct device_attribute *attr, char *buf)
415 {
416 	int autocalibration = 0;
417 
418 	autocalibration = abx80x_rtc_get_autocalibration(dev->parent);
419 	if (autocalibration < 0) {
420 		dev_err(dev, "Failed to read RTC autocalibration\n");
421 		sprintf(buf, "0\n");
422 		return autocalibration;
423 	}
424 
425 	return sprintf(buf, "%d\n", autocalibration);
426 }
427 
428 static DEVICE_ATTR_RW(autocalibration);
429 
430 static ssize_t oscillator_store(struct device *dev,
431 				struct device_attribute *attr,
432 				const char *buf, size_t count)
433 {
434 	struct i2c_client *client = to_i2c_client(dev->parent);
435 	int retval, flags, rc_mode = 0;
436 
437 	if (strncmp(buf, "rc", 2) == 0) {
438 		rc_mode = 1;
439 	} else if (strncmp(buf, "xtal", 4) == 0) {
440 		rc_mode = 0;
441 	} else {
442 		dev_err(dev, "Oscillator selection value outside permitted ones\n");
443 		return -EINVAL;
444 	}
445 
446 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
447 	if (flags < 0)
448 		return flags;
449 
450 	if (rc_mode == 0)
451 		flags &= ~(ABX8XX_OSC_OSEL);
452 	else
453 		flags |= (ABX8XX_OSC_OSEL);
454 
455 	/* Unlock write access on Oscillator Control register */
456 	if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
457 		return -EIO;
458 
459 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
460 	if (retval < 0) {
461 		dev_err(dev, "Failed to write Oscillator Control register\n");
462 		return retval;
463 	}
464 
465 	return retval ? retval : count;
466 }
467 
468 static ssize_t oscillator_show(struct device *dev,
469 			       struct device_attribute *attr, char *buf)
470 {
471 	int rc_mode = 0;
472 	struct i2c_client *client = to_i2c_client(dev->parent);
473 
474 	rc_mode = abx80x_is_rc_mode(client);
475 
476 	if (rc_mode < 0) {
477 		dev_err(dev, "Failed to read RTC oscillator selection\n");
478 		sprintf(buf, "\n");
479 		return rc_mode;
480 	}
481 
482 	if (rc_mode)
483 		return sprintf(buf, "rc\n");
484 	else
485 		return sprintf(buf, "xtal\n");
486 }
487 
488 static DEVICE_ATTR_RW(oscillator);
489 
490 static struct attribute *rtc_calib_attrs[] = {
491 	&dev_attr_autocalibration.attr,
492 	&dev_attr_oscillator.attr,
493 	NULL,
494 };
495 
496 static const struct attribute_group rtc_calib_attr_group = {
497 	.attrs		= rtc_calib_attrs,
498 };
499 
500 static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
501 {
502 	struct i2c_client *client = to_i2c_client(dev);
503 	int err;
504 
505 	if (enabled)
506 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
507 						(ABX8XX_IRQ_IM_1_4 |
508 						 ABX8XX_IRQ_AIE));
509 	else
510 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
511 						ABX8XX_IRQ_IM_1_4);
512 	return err;
513 }
514 
515 static int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
516 {
517 	struct i2c_client *client = to_i2c_client(dev);
518 	int status, tmp;
519 
520 	switch (cmd) {
521 	case RTC_VL_READ:
522 		status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
523 		if (status < 0)
524 			return status;
525 
526 		tmp = status & ABX8XX_STATUS_BLF ? RTC_VL_BACKUP_LOW : 0;
527 
528 		return put_user(tmp, (unsigned int __user *)arg);
529 
530 	case RTC_VL_CLR:
531 		status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
532 		if (status < 0)
533 			return status;
534 
535 		status &= ~ABX8XX_STATUS_BLF;
536 
537 		tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
538 		if (tmp < 0)
539 			return tmp;
540 
541 		return 0;
542 
543 	default:
544 		return -ENOIOCTLCMD;
545 	}
546 }
547 
548 static const struct rtc_class_ops abx80x_rtc_ops = {
549 	.read_time	= abx80x_rtc_read_time,
550 	.set_time	= abx80x_rtc_set_time,
551 	.read_alarm	= abx80x_read_alarm,
552 	.set_alarm	= abx80x_set_alarm,
553 	.alarm_irq_enable = abx80x_alarm_irq_enable,
554 	.ioctl		= abx80x_ioctl,
555 };
556 
557 static int abx80x_dt_trickle_cfg(struct i2c_client *client)
558 {
559 	struct device_node *np = client->dev.of_node;
560 	const char *diode;
561 	int trickle_cfg = 0;
562 	int i, ret;
563 	u32 tmp;
564 
565 	ret = of_property_read_string(np, "abracon,tc-diode", &diode);
566 	if (ret)
567 		return ret;
568 
569 	if (!strcmp(diode, "standard")) {
570 		trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
571 	} else if (!strcmp(diode, "schottky")) {
572 		trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
573 	} else {
574 		dev_dbg(&client->dev, "Invalid tc-diode value: %s\n", diode);
575 		return -EINVAL;
576 	}
577 
578 	ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
579 	if (ret)
580 		return ret;
581 
582 	for (i = 0; i < sizeof(trickle_resistors); i++)
583 		if (trickle_resistors[i] == tmp)
584 			break;
585 
586 	if (i == sizeof(trickle_resistors)) {
587 		dev_dbg(&client->dev, "Invalid tc-resistor value: %u\n", tmp);
588 		return -EINVAL;
589 	}
590 
591 	return (trickle_cfg | i);
592 }
593 
594 #ifdef CONFIG_WATCHDOG
595 
596 static inline u8 timeout_bits(unsigned int timeout)
597 {
598 	return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) |
599 		 ABX8XX_WDT_WRB_1HZ;
600 }
601 
602 static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog,
603 				     unsigned int timeout)
604 {
605 	struct abx80x_priv *priv = watchdog_get_drvdata(wdog);
606 	u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout);
607 
608 	/*
609 	 * Writing any timeout to the WDT register resets the watchdog timer.
610 	 * Writing 0 disables it.
611 	 */
612 	return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val);
613 }
614 
615 static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
616 				   unsigned int new_timeout)
617 {
618 	int err = 0;
619 
620 	if (watchdog_hw_running(wdog))
621 		err = __abx80x_wdog_set_timeout(wdog, new_timeout);
622 
623 	if (err == 0)
624 		wdog->timeout = new_timeout;
625 
626 	return err;
627 }
628 
629 static int abx80x_wdog_ping(struct watchdog_device *wdog)
630 {
631 	return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
632 }
633 
634 static int abx80x_wdog_start(struct watchdog_device *wdog)
635 {
636 	return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
637 }
638 
639 static int abx80x_wdog_stop(struct watchdog_device *wdog)
640 {
641 	return __abx80x_wdog_set_timeout(wdog, 0);
642 }
643 
644 static const struct watchdog_info abx80x_wdog_info = {
645 	.identity = "abx80x watchdog",
646 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
647 };
648 
649 static const struct watchdog_ops abx80x_wdog_ops = {
650 	.owner = THIS_MODULE,
651 	.start = abx80x_wdog_start,
652 	.stop = abx80x_wdog_stop,
653 	.ping = abx80x_wdog_ping,
654 	.set_timeout = abx80x_wdog_set_timeout,
655 };
656 
657 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
658 {
659 	priv->wdog.parent = &priv->client->dev;
660 	priv->wdog.ops = &abx80x_wdog_ops;
661 	priv->wdog.info = &abx80x_wdog_info;
662 	priv->wdog.min_timeout = 1;
663 	priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME;
664 	priv->wdog.timeout = ABX8XX_WDT_MAX_TIME;
665 
666 	watchdog_set_drvdata(&priv->wdog, priv);
667 
668 	return devm_watchdog_register_device(&priv->client->dev, &priv->wdog);
669 }
670 #else
671 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
672 {
673 	return 0;
674 }
675 #endif
676 
677 static const struct i2c_device_id abx80x_id[] = {
678 	{ "abx80x", ABX80X },
679 	{ "ab0801", AB0801 },
680 	{ "ab0803", AB0803 },
681 	{ "ab0804", AB0804 },
682 	{ "ab0805", AB0805 },
683 	{ "ab1801", AB1801 },
684 	{ "ab1803", AB1803 },
685 	{ "ab1804", AB1804 },
686 	{ "ab1805", AB1805 },
687 	{ "rv1805", RV1805 },
688 	{ }
689 };
690 MODULE_DEVICE_TABLE(i2c, abx80x_id);
691 
692 static int abx80x_probe(struct i2c_client *client)
693 {
694 	struct device_node *np = client->dev.of_node;
695 	struct abx80x_priv *priv;
696 	int i, data, err, trickle_cfg = -EINVAL;
697 	char buf[7];
698 	const struct i2c_device_id *id = i2c_match_id(abx80x_id, client);
699 	unsigned int part = id->driver_data;
700 	unsigned int partnumber;
701 	unsigned int majrev, minrev;
702 	unsigned int lot;
703 	unsigned int wafer;
704 	unsigned int uid;
705 
706 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
707 		return -ENODEV;
708 
709 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
710 					    sizeof(buf), buf);
711 	if (err < 0) {
712 		dev_err(&client->dev, "Unable to read partnumber\n");
713 		return -EIO;
714 	}
715 
716 	partnumber = (buf[0] << 8) | buf[1];
717 	majrev = buf[2] >> 3;
718 	minrev = buf[2] & 0x7;
719 	lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
720 	uid = ((buf[4] & 0x7f) << 8) | buf[5];
721 	wafer = (buf[6] & 0x7c) >> 2;
722 	dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
723 		 partnumber, majrev, minrev, lot, wafer, uid);
724 
725 	data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
726 	if (data < 0) {
727 		dev_err(&client->dev, "Unable to read control register\n");
728 		return -EIO;
729 	}
730 
731 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
732 					((data & ~(ABX8XX_CTRL_12_24 |
733 						   ABX8XX_CTRL_ARST)) |
734 					 ABX8XX_CTRL_WRITE));
735 	if (err < 0) {
736 		dev_err(&client->dev, "Unable to write control register\n");
737 		return -EIO;
738 	}
739 
740 	/* Configure RV1805 specifics */
741 	if (part == RV1805) {
742 		/*
743 		 * Avoid accidentally entering test mode. This can happen
744 		 * on the RV1805 in case the reserved bit 5 in control2
745 		 * register is set. RV-1805-C3 datasheet indicates that
746 		 * the bit should be cleared in section 11h - Control2.
747 		 */
748 		data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL2);
749 		if (data < 0) {
750 			dev_err(&client->dev,
751 				"Unable to read control2 register\n");
752 			return -EIO;
753 		}
754 
755 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL2,
756 						data & ~ABX8XX_CTRL2_RSVD);
757 		if (err < 0) {
758 			dev_err(&client->dev,
759 				"Unable to write control2 register\n");
760 			return -EIO;
761 		}
762 
763 		/*
764 		 * Avoid extra power leakage. The RV1805 uses smaller
765 		 * 10pin package and the EXTI input is not present.
766 		 * Disable it to avoid leakage.
767 		 */
768 		data = i2c_smbus_read_byte_data(client, ABX8XX_REG_OUT_CTRL);
769 		if (data < 0) {
770 			dev_err(&client->dev,
771 				"Unable to read output control register\n");
772 			return -EIO;
773 		}
774 
775 		/*
776 		 * Write the configuration key register to enable access to
777 		 * the config2 register
778 		 */
779 		if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
780 			return -EIO;
781 
782 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OUT_CTRL,
783 						data | ABX8XX_OUT_CTRL_EXDS);
784 		if (err < 0) {
785 			dev_err(&client->dev,
786 				"Unable to write output control register\n");
787 			return -EIO;
788 		}
789 	}
790 
791 	/* part autodetection */
792 	if (part == ABX80X) {
793 		for (i = 0; abx80x_caps[i].pn; i++)
794 			if (partnumber == abx80x_caps[i].pn)
795 				break;
796 		if (abx80x_caps[i].pn == 0) {
797 			dev_err(&client->dev, "Unknown part: %04x\n",
798 				partnumber);
799 			return -EINVAL;
800 		}
801 		part = i;
802 	}
803 
804 	if (partnumber != abx80x_caps[part].pn) {
805 		dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
806 			partnumber, abx80x_caps[part].pn);
807 		return -EINVAL;
808 	}
809 
810 	if (np && abx80x_caps[part].has_tc)
811 		trickle_cfg = abx80x_dt_trickle_cfg(client);
812 
813 	if (trickle_cfg > 0) {
814 		dev_info(&client->dev, "Enabling trickle charger: %02x\n",
815 			 trickle_cfg);
816 		abx80x_enable_trickle_charger(client, trickle_cfg);
817 	}
818 
819 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
820 					BIT(2));
821 	if (err)
822 		return err;
823 
824 	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
825 	if (priv == NULL)
826 		return -ENOMEM;
827 
828 	priv->rtc = devm_rtc_allocate_device(&client->dev);
829 	if (IS_ERR(priv->rtc))
830 		return PTR_ERR(priv->rtc);
831 
832 	priv->rtc->ops = &abx80x_rtc_ops;
833 	priv->client = client;
834 
835 	i2c_set_clientdata(client, priv);
836 
837 	if (abx80x_caps[part].has_wdog) {
838 		err = abx80x_setup_watchdog(priv);
839 		if (err)
840 			return err;
841 	}
842 
843 	if (client->irq > 0) {
844 		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
845 		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
846 						abx80x_handle_irq,
847 						IRQF_SHARED | IRQF_ONESHOT,
848 						"abx8xx",
849 						client);
850 		if (err) {
851 			dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
852 			client->irq = 0;
853 		}
854 	}
855 
856 	err = rtc_add_group(priv->rtc, &rtc_calib_attr_group);
857 	if (err) {
858 		dev_err(&client->dev, "Failed to create sysfs group: %d\n",
859 			err);
860 		return err;
861 	}
862 
863 	return devm_rtc_register_device(priv->rtc);
864 }
865 
866 #ifdef CONFIG_OF
867 static const struct of_device_id abx80x_of_match[] = {
868 	{
869 		.compatible = "abracon,abx80x",
870 		.data = (void *)ABX80X
871 	},
872 	{
873 		.compatible = "abracon,ab0801",
874 		.data = (void *)AB0801
875 	},
876 	{
877 		.compatible = "abracon,ab0803",
878 		.data = (void *)AB0803
879 	},
880 	{
881 		.compatible = "abracon,ab0804",
882 		.data = (void *)AB0804
883 	},
884 	{
885 		.compatible = "abracon,ab0805",
886 		.data = (void *)AB0805
887 	},
888 	{
889 		.compatible = "abracon,ab1801",
890 		.data = (void *)AB1801
891 	},
892 	{
893 		.compatible = "abracon,ab1803",
894 		.data = (void *)AB1803
895 	},
896 	{
897 		.compatible = "abracon,ab1804",
898 		.data = (void *)AB1804
899 	},
900 	{
901 		.compatible = "abracon,ab1805",
902 		.data = (void *)AB1805
903 	},
904 	{
905 		.compatible = "microcrystal,rv1805",
906 		.data = (void *)RV1805
907 	},
908 	{ }
909 };
910 MODULE_DEVICE_TABLE(of, abx80x_of_match);
911 #endif
912 
913 static struct i2c_driver abx80x_driver = {
914 	.driver		= {
915 		.name	= "rtc-abx80x",
916 		.of_match_table = of_match_ptr(abx80x_of_match),
917 	},
918 	.probe_new	= abx80x_probe,
919 	.id_table	= abx80x_id,
920 };
921 
922 module_i2c_driver(abx80x_driver);
923 
924 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
925 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
926 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
927 MODULE_LICENSE("GPL v2");
928