1 /*
2  * A driver for the I2C members of the Abracon AB x8xx RTC family,
3  * and compatible: AB 1805 and AB 0805
4  *
5  * Copyright 2014-2015 Macq S.A.
6  *
7  * Author: Philippe De Muyter <phdm@macqel.be>
8  * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 
16 #include <linux/bcd.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/rtc.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 
41 #define ABX8XX_REG_CTRL1	0x10
42 #define ABX8XX_CTRL_WRITE	BIT(0)
43 #define ABX8XX_CTRL_ARST	BIT(2)
44 #define ABX8XX_CTRL_12_24	BIT(6)
45 
46 #define ABX8XX_REG_IRQ		0x12
47 #define ABX8XX_IRQ_AIE		BIT(2)
48 #define ABX8XX_IRQ_IM_1_4	(0x3 << 5)
49 
50 #define ABX8XX_REG_CD_TIMER_CTL	0x18
51 
52 #define ABX8XX_REG_OSC		0x1c
53 #define ABX8XX_OSC_FOS		BIT(3)
54 #define ABX8XX_OSC_BOS		BIT(4)
55 #define ABX8XX_OSC_ACAL_512	BIT(5)
56 #define ABX8XX_OSC_ACAL_1024	BIT(6)
57 
58 #define ABX8XX_OSC_OSEL		BIT(7)
59 
60 #define ABX8XX_REG_OSS		0x1d
61 #define ABX8XX_OSS_OF		BIT(1)
62 #define ABX8XX_OSS_OMODE	BIT(4)
63 
64 #define ABX8XX_REG_CFG_KEY	0x1f
65 #define ABX8XX_CFG_KEY_OSC	0xa1
66 #define ABX8XX_CFG_KEY_MISC	0x9d
67 
68 #define ABX8XX_REG_ID0		0x28
69 
70 #define ABX8XX_REG_TRICKLE	0x20
71 #define ABX8XX_TRICKLE_CHARGE_ENABLE	0xa0
72 #define ABX8XX_TRICKLE_STANDARD_DIODE	0x8
73 #define ABX8XX_TRICKLE_SCHOTTKY_DIODE	0x4
74 
75 static u8 trickle_resistors[] = {0, 3, 6, 11};
76 
77 enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
78 	AB1801, AB1803, AB1804, AB1805, ABX80X};
79 
80 struct abx80x_cap {
81 	u16 pn;
82 	bool has_tc;
83 };
84 
85 static struct abx80x_cap abx80x_caps[] = {
86 	[AB0801] = {.pn = 0x0801},
87 	[AB0803] = {.pn = 0x0803},
88 	[AB0804] = {.pn = 0x0804, .has_tc = true},
89 	[AB0805] = {.pn = 0x0805, .has_tc = true},
90 	[AB1801] = {.pn = 0x1801},
91 	[AB1803] = {.pn = 0x1803},
92 	[AB1804] = {.pn = 0x1804, .has_tc = true},
93 	[AB1805] = {.pn = 0x1805, .has_tc = true},
94 	[ABX80X] = {.pn = 0}
95 };
96 
abx80x_is_rc_mode(struct i2c_client * client)97 static int abx80x_is_rc_mode(struct i2c_client *client)
98 {
99 	int flags = 0;
100 
101 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
102 	if (flags < 0) {
103 		dev_err(&client->dev,
104 			"Failed to read autocalibration attribute\n");
105 		return flags;
106 	}
107 
108 	return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
109 }
110 
abx80x_enable_trickle_charger(struct i2c_client * client,u8 trickle_cfg)111 static int abx80x_enable_trickle_charger(struct i2c_client *client,
112 					 u8 trickle_cfg)
113 {
114 	int err;
115 
116 	/*
117 	 * Write the configuration key register to enable access to the Trickle
118 	 * register
119 	 */
120 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
121 					ABX8XX_CFG_KEY_MISC);
122 	if (err < 0) {
123 		dev_err(&client->dev, "Unable to write configuration key\n");
124 		return -EIO;
125 	}
126 
127 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
128 					ABX8XX_TRICKLE_CHARGE_ENABLE |
129 					trickle_cfg);
130 	if (err < 0) {
131 		dev_err(&client->dev, "Unable to write trickle register\n");
132 		return -EIO;
133 	}
134 
135 	return 0;
136 }
137 
abx80x_rtc_read_time(struct device * dev,struct rtc_time * tm)138 static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
139 {
140 	struct i2c_client *client = to_i2c_client(dev);
141 	unsigned char buf[8];
142 	int err, flags, rc_mode = 0;
143 
144 	/* Read the Oscillator Failure only in XT mode */
145 	rc_mode = abx80x_is_rc_mode(client);
146 	if (rc_mode < 0)
147 		return rc_mode;
148 
149 	if (!rc_mode) {
150 		flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
151 		if (flags < 0)
152 			return flags;
153 
154 		if (flags & ABX8XX_OSS_OF) {
155 			dev_err(dev, "Oscillator failure, data is invalid.\n");
156 			return -EINVAL;
157 		}
158 	}
159 
160 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
161 					    sizeof(buf), buf);
162 	if (err < 0) {
163 		dev_err(&client->dev, "Unable to read date\n");
164 		return -EIO;
165 	}
166 
167 	tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
168 	tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
169 	tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
170 	tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
171 	tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
172 	tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
173 	tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
174 
175 	return 0;
176 }
177 
abx80x_rtc_set_time(struct device * dev,struct rtc_time * tm)178 static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
179 {
180 	struct i2c_client *client = to_i2c_client(dev);
181 	unsigned char buf[8];
182 	int err, flags;
183 
184 	if (tm->tm_year < 100)
185 		return -EINVAL;
186 
187 	buf[ABX8XX_REG_HTH] = 0;
188 	buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
189 	buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
190 	buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
191 	buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
192 	buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
193 	buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
194 	buf[ABX8XX_REG_WD] = tm->tm_wday;
195 
196 	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
197 					     sizeof(buf), buf);
198 	if (err < 0) {
199 		dev_err(&client->dev, "Unable to write to date registers\n");
200 		return -EIO;
201 	}
202 
203 	/* Clear the OF bit of Oscillator Status Register */
204 	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
205 	if (flags < 0)
206 		return flags;
207 
208 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
209 					flags & ~ABX8XX_OSS_OF);
210 	if (err < 0) {
211 		dev_err(&client->dev, "Unable to write oscillator status register\n");
212 		return err;
213 	}
214 
215 	return 0;
216 }
217 
abx80x_handle_irq(int irq,void * dev_id)218 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
219 {
220 	struct i2c_client *client = dev_id;
221 	struct rtc_device *rtc = i2c_get_clientdata(client);
222 	int status;
223 
224 	status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
225 	if (status < 0)
226 		return IRQ_NONE;
227 
228 	if (status & ABX8XX_STATUS_AF)
229 		rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
230 
231 	i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
232 
233 	return IRQ_HANDLED;
234 }
235 
abx80x_read_alarm(struct device * dev,struct rtc_wkalrm * t)236 static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
237 {
238 	struct i2c_client *client = to_i2c_client(dev);
239 	unsigned char buf[7];
240 
241 	int irq_mask, err;
242 
243 	if (client->irq <= 0)
244 		return -EINVAL;
245 
246 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
247 					    sizeof(buf), buf);
248 	if (err)
249 		return err;
250 
251 	irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
252 	if (irq_mask < 0)
253 		return irq_mask;
254 
255 	t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
256 	t->time.tm_min = bcd2bin(buf[1] & 0x7F);
257 	t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
258 	t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
259 	t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
260 	t->time.tm_wday = buf[5] & 0x7;
261 
262 	t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
263 	t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
264 
265 	return err;
266 }
267 
abx80x_set_alarm(struct device * dev,struct rtc_wkalrm * t)268 static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
269 {
270 	struct i2c_client *client = to_i2c_client(dev);
271 	u8 alarm[6];
272 	int err;
273 
274 	if (client->irq <= 0)
275 		return -EINVAL;
276 
277 	alarm[0] = 0x0;
278 	alarm[1] = bin2bcd(t->time.tm_sec);
279 	alarm[2] = bin2bcd(t->time.tm_min);
280 	alarm[3] = bin2bcd(t->time.tm_hour);
281 	alarm[4] = bin2bcd(t->time.tm_mday);
282 	alarm[5] = bin2bcd(t->time.tm_mon + 1);
283 
284 	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
285 					     sizeof(alarm), alarm);
286 	if (err < 0) {
287 		dev_err(&client->dev, "Unable to write alarm registers\n");
288 		return -EIO;
289 	}
290 
291 	if (t->enabled) {
292 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
293 						(ABX8XX_IRQ_IM_1_4 |
294 						 ABX8XX_IRQ_AIE));
295 		if (err)
296 			return err;
297 	}
298 
299 	return 0;
300 }
301 
abx80x_rtc_set_autocalibration(struct device * dev,int autocalibration)302 static int abx80x_rtc_set_autocalibration(struct device *dev,
303 					  int autocalibration)
304 {
305 	struct i2c_client *client = to_i2c_client(dev);
306 	int retval, flags = 0;
307 
308 	if ((autocalibration != 0) && (autocalibration != 1024) &&
309 	    (autocalibration != 512)) {
310 		dev_err(dev, "autocalibration value outside permitted range\n");
311 		return -EINVAL;
312 	}
313 
314 	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
315 	if (flags < 0)
316 		return flags;
317 
318 	if (autocalibration == 0) {
319 		flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
320 	} else if (autocalibration == 1024) {
321 		/* 1024 autocalibration is 0x10 */
322 		flags |= ABX8XX_OSC_ACAL_1024;
323 		flags &= ~(ABX8XX_OSC_ACAL_512);
324 	} else {
325 		/* 512 autocalibration is 0x11 */
326 		flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
327 	}
328 
329 	/* Unlock write access to Oscillator Control Register */
330 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
331 					   ABX8XX_CFG_KEY_OSC);
332 	if (retval < 0) {
333 		dev_err(dev, "Failed to write CONFIG_KEY register\n");
334 		return retval;
335 	}
336 
337 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
338 
339 	return retval;
340 }
341 
abx80x_rtc_get_autocalibration(struct device * dev)342 static int abx80x_rtc_get_autocalibration(struct device *dev)
343 {
344 	struct i2c_client *client = to_i2c_client(dev);
345 	int flags = 0, autocalibration;
346 
347 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
348 	if (flags < 0)
349 		return flags;
350 
351 	if (flags & ABX8XX_OSC_ACAL_512)
352 		autocalibration = 512;
353 	else if (flags & ABX8XX_OSC_ACAL_1024)
354 		autocalibration = 1024;
355 	else
356 		autocalibration = 0;
357 
358 	return autocalibration;
359 }
360 
autocalibration_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)361 static ssize_t autocalibration_store(struct device *dev,
362 				     struct device_attribute *attr,
363 				     const char *buf, size_t count)
364 {
365 	int retval;
366 	unsigned long autocalibration = 0;
367 
368 	retval = kstrtoul(buf, 10, &autocalibration);
369 	if (retval < 0) {
370 		dev_err(dev, "Failed to store RTC autocalibration attribute\n");
371 		return -EINVAL;
372 	}
373 
374 	retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
375 
376 	return retval ? retval : count;
377 }
378 
autocalibration_show(struct device * dev,struct device_attribute * attr,char * buf)379 static ssize_t autocalibration_show(struct device *dev,
380 				    struct device_attribute *attr, char *buf)
381 {
382 	int autocalibration = 0;
383 
384 	autocalibration = abx80x_rtc_get_autocalibration(dev);
385 	if (autocalibration < 0) {
386 		dev_err(dev, "Failed to read RTC autocalibration\n");
387 		sprintf(buf, "0\n");
388 		return autocalibration;
389 	}
390 
391 	return sprintf(buf, "%d\n", autocalibration);
392 }
393 
394 static DEVICE_ATTR_RW(autocalibration);
395 
oscillator_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)396 static ssize_t oscillator_store(struct device *dev,
397 				struct device_attribute *attr,
398 				const char *buf, size_t count)
399 {
400 	struct i2c_client *client = to_i2c_client(dev);
401 	int retval, flags, rc_mode = 0;
402 
403 	if (strncmp(buf, "rc", 2) == 0) {
404 		rc_mode = 1;
405 	} else if (strncmp(buf, "xtal", 4) == 0) {
406 		rc_mode = 0;
407 	} else {
408 		dev_err(dev, "Oscillator selection value outside permitted ones\n");
409 		return -EINVAL;
410 	}
411 
412 	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
413 	if (flags < 0)
414 		return flags;
415 
416 	if (rc_mode == 0)
417 		flags &= ~(ABX8XX_OSC_OSEL);
418 	else
419 		flags |= (ABX8XX_OSC_OSEL);
420 
421 	/* Unlock write access on Oscillator Control register */
422 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
423 					   ABX8XX_CFG_KEY_OSC);
424 	if (retval < 0) {
425 		dev_err(dev, "Failed to write CONFIG_KEY register\n");
426 		return retval;
427 	}
428 
429 	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
430 	if (retval < 0) {
431 		dev_err(dev, "Failed to write Oscillator Control register\n");
432 		return retval;
433 	}
434 
435 	return retval ? retval : count;
436 }
437 
oscillator_show(struct device * dev,struct device_attribute * attr,char * buf)438 static ssize_t oscillator_show(struct device *dev,
439 			       struct device_attribute *attr, char *buf)
440 {
441 	int rc_mode = 0;
442 	struct i2c_client *client = to_i2c_client(dev);
443 
444 	rc_mode = abx80x_is_rc_mode(client);
445 
446 	if (rc_mode < 0) {
447 		dev_err(dev, "Failed to read RTC oscillator selection\n");
448 		sprintf(buf, "\n");
449 		return rc_mode;
450 	}
451 
452 	if (rc_mode)
453 		return sprintf(buf, "rc\n");
454 	else
455 		return sprintf(buf, "xtal\n");
456 }
457 
458 static DEVICE_ATTR_RW(oscillator);
459 
460 static struct attribute *rtc_calib_attrs[] = {
461 	&dev_attr_autocalibration.attr,
462 	&dev_attr_oscillator.attr,
463 	NULL,
464 };
465 
466 static const struct attribute_group rtc_calib_attr_group = {
467 	.attrs		= rtc_calib_attrs,
468 };
469 
abx80x_alarm_irq_enable(struct device * dev,unsigned int enabled)470 static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
471 {
472 	struct i2c_client *client = to_i2c_client(dev);
473 	int err;
474 
475 	if (enabled)
476 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
477 						(ABX8XX_IRQ_IM_1_4 |
478 						 ABX8XX_IRQ_AIE));
479 	else
480 		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
481 						ABX8XX_IRQ_IM_1_4);
482 	return err;
483 }
484 
485 static const struct rtc_class_ops abx80x_rtc_ops = {
486 	.read_time	= abx80x_rtc_read_time,
487 	.set_time	= abx80x_rtc_set_time,
488 	.read_alarm	= abx80x_read_alarm,
489 	.set_alarm	= abx80x_set_alarm,
490 	.alarm_irq_enable = abx80x_alarm_irq_enable,
491 };
492 
abx80x_dt_trickle_cfg(struct device_node * np)493 static int abx80x_dt_trickle_cfg(struct device_node *np)
494 {
495 	const char *diode;
496 	int trickle_cfg = 0;
497 	int i, ret;
498 	u32 tmp;
499 
500 	ret = of_property_read_string(np, "abracon,tc-diode", &diode);
501 	if (ret)
502 		return ret;
503 
504 	if (!strcmp(diode, "standard"))
505 		trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
506 	else if (!strcmp(diode, "schottky"))
507 		trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
508 	else
509 		return -EINVAL;
510 
511 	ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
512 	if (ret)
513 		return ret;
514 
515 	for (i = 0; i < sizeof(trickle_resistors); i++)
516 		if (trickle_resistors[i] == tmp)
517 			break;
518 
519 	if (i == sizeof(trickle_resistors))
520 		return -EINVAL;
521 
522 	return (trickle_cfg | i);
523 }
524 
rtc_calib_remove_sysfs_group(void * _dev)525 static void rtc_calib_remove_sysfs_group(void *_dev)
526 {
527 	struct device *dev = _dev;
528 
529 	sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
530 }
531 
abx80x_probe(struct i2c_client * client,const struct i2c_device_id * id)532 static int abx80x_probe(struct i2c_client *client,
533 			const struct i2c_device_id *id)
534 {
535 	struct device_node *np = client->dev.of_node;
536 	struct rtc_device *rtc;
537 	int i, data, err, trickle_cfg = -EINVAL;
538 	char buf[7];
539 	unsigned int part = id->driver_data;
540 	unsigned int partnumber;
541 	unsigned int majrev, minrev;
542 	unsigned int lot;
543 	unsigned int wafer;
544 	unsigned int uid;
545 
546 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
547 		return -ENODEV;
548 
549 	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
550 					    sizeof(buf), buf);
551 	if (err < 0) {
552 		dev_err(&client->dev, "Unable to read partnumber\n");
553 		return -EIO;
554 	}
555 
556 	partnumber = (buf[0] << 8) | buf[1];
557 	majrev = buf[2] >> 3;
558 	minrev = buf[2] & 0x7;
559 	lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
560 	uid = ((buf[4] & 0x7f) << 8) | buf[5];
561 	wafer = (buf[6] & 0x7c) >> 2;
562 	dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
563 		 partnumber, majrev, minrev, lot, wafer, uid);
564 
565 	data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
566 	if (data < 0) {
567 		dev_err(&client->dev, "Unable to read control register\n");
568 		return -EIO;
569 	}
570 
571 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
572 					((data & ~(ABX8XX_CTRL_12_24 |
573 						   ABX8XX_CTRL_ARST)) |
574 					 ABX8XX_CTRL_WRITE));
575 	if (err < 0) {
576 		dev_err(&client->dev, "Unable to write control register\n");
577 		return -EIO;
578 	}
579 
580 	/* part autodetection */
581 	if (part == ABX80X) {
582 		for (i = 0; abx80x_caps[i].pn; i++)
583 			if (partnumber == abx80x_caps[i].pn)
584 				break;
585 		if (abx80x_caps[i].pn == 0) {
586 			dev_err(&client->dev, "Unknown part: %04x\n",
587 				partnumber);
588 			return -EINVAL;
589 		}
590 		part = i;
591 	}
592 
593 	if (partnumber != abx80x_caps[part].pn) {
594 		dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
595 			partnumber, abx80x_caps[part].pn);
596 		return -EINVAL;
597 	}
598 
599 	if (np && abx80x_caps[part].has_tc)
600 		trickle_cfg = abx80x_dt_trickle_cfg(np);
601 
602 	if (trickle_cfg > 0) {
603 		dev_info(&client->dev, "Enabling trickle charger: %02x\n",
604 			 trickle_cfg);
605 		abx80x_enable_trickle_charger(client, trickle_cfg);
606 	}
607 
608 	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
609 					BIT(2));
610 	if (err)
611 		return err;
612 
613 	rtc = devm_rtc_allocate_device(&client->dev);
614 	if (IS_ERR(rtc))
615 		return PTR_ERR(rtc);
616 
617 	rtc->ops = &abx80x_rtc_ops;
618 
619 	i2c_set_clientdata(client, rtc);
620 
621 	if (client->irq > 0) {
622 		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
623 		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
624 						abx80x_handle_irq,
625 						IRQF_SHARED | IRQF_ONESHOT,
626 						"abx8xx",
627 						client);
628 		if (err) {
629 			dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
630 			client->irq = 0;
631 		}
632 	}
633 
634 	/* Export sysfs entries */
635 	err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
636 	if (err) {
637 		dev_err(&client->dev, "Failed to create sysfs group: %d\n",
638 			err);
639 		return err;
640 	}
641 
642 	err = devm_add_action_or_reset(&client->dev,
643 				       rtc_calib_remove_sysfs_group,
644 				       &client->dev);
645 	if (err) {
646 		dev_err(&client->dev,
647 			"Failed to add sysfs cleanup action: %d\n",
648 			err);
649 		return err;
650 	}
651 
652 	err = rtc_register_device(rtc);
653 
654 	return err;
655 }
656 
abx80x_remove(struct i2c_client * client)657 static int abx80x_remove(struct i2c_client *client)
658 {
659 	return 0;
660 }
661 
662 static const struct i2c_device_id abx80x_id[] = {
663 	{ "abx80x", ABX80X },
664 	{ "ab0801", AB0801 },
665 	{ "ab0803", AB0803 },
666 	{ "ab0804", AB0804 },
667 	{ "ab0805", AB0805 },
668 	{ "ab1801", AB1801 },
669 	{ "ab1803", AB1803 },
670 	{ "ab1804", AB1804 },
671 	{ "ab1805", AB1805 },
672 	{ "rv1805", AB1805 },
673 	{ }
674 };
675 MODULE_DEVICE_TABLE(i2c, abx80x_id);
676 
677 static struct i2c_driver abx80x_driver = {
678 	.driver		= {
679 		.name	= "rtc-abx80x",
680 	},
681 	.probe		= abx80x_probe,
682 	.remove		= abx80x_remove,
683 	.id_table	= abx80x_id,
684 };
685 
686 module_i2c_driver(abx80x_driver);
687 
688 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
689 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
690 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
691 MODULE_LICENSE("GPL v2");
692