blob: 9d49054a0a4a0ffb0a228b7619a84887287c870a [file] [log] [blame]
Philippe De Muyter4d61ff62015-05-05 16:23:44 -07001/*
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
Alexandre Belloni718a8202015-12-17 00:36:22 +010030#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
Philippe De Muyter4d61ff62015-05-05 16:23:44 -070041#define ABX8XX_REG_CTRL1 0x10
Mitja Spes5f1b2f72015-09-02 10:02:29 +020042#define ABX8XX_CTRL_WRITE BIT(0)
Alexandre Belloni718a8202015-12-17 00:36:22 +010043#define ABX8XX_CTRL_ARST BIT(2)
Philippe De Muyter4d61ff62015-05-05 16:23:44 -070044#define ABX8XX_CTRL_12_24 BIT(6)
45
Alexandre Belloni718a8202015-12-17 00:36:22 +010046#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
Mylène Josserand59a83832016-03-21 18:06:09 +010052#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
Mylène Josserandee087742016-03-21 18:06:10 +010061#define ABX8XX_OSS_OF BIT(1)
Mylène Josserand59a83832016-03-21 18:06:09 +010062#define ABX8XX_OSS_OMODE BIT(4)
63
Philippe De Muyter4d61ff62015-05-05 16:23:44 -070064#define ABX8XX_REG_CFG_KEY 0x1f
Mylène Josserand59a83832016-03-21 18:06:09 +010065#define ABX8XX_CFG_KEY_OSC 0xa1
Philippe De Muyter4d61ff62015-05-05 16:23:44 -070066#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
75static u8 trickle_resistors[] = {0, 3, 6, 11};
76
77enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
78 AB1801, AB1803, AB1804, AB1805, ABX80X};
79
80struct abx80x_cap {
81 u16 pn;
82 bool has_tc;
83};
84
85static 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
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -060097struct abx80x_priv {
98 struct rtc_device *rtc;
99 struct i2c_client *client;
100};
101
Mylène Josserand59a83832016-03-21 18:06:09 +0100102static int abx80x_is_rc_mode(struct i2c_client *client)
103{
104 int flags = 0;
105
106 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
107 if (flags < 0) {
108 dev_err(&client->dev,
109 "Failed to read autocalibration attribute\n");
110 return flags;
111 }
112
113 return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
114}
115
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700116static int abx80x_enable_trickle_charger(struct i2c_client *client,
117 u8 trickle_cfg)
118{
119 int err;
120
121 /*
122 * Write the configuration key register to enable access to the Trickle
123 * register
124 */
125 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
126 ABX8XX_CFG_KEY_MISC);
127 if (err < 0) {
128 dev_err(&client->dev, "Unable to write configuration key\n");
129 return -EIO;
130 }
131
132 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
133 ABX8XX_TRICKLE_CHARGE_ENABLE |
134 trickle_cfg);
135 if (err < 0) {
136 dev_err(&client->dev, "Unable to write trickle register\n");
137 return -EIO;
138 }
139
140 return 0;
141}
142
143static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
144{
145 struct i2c_client *client = to_i2c_client(dev);
146 unsigned char buf[8];
Mylène Josserandee087742016-03-21 18:06:10 +0100147 int err, flags, rc_mode = 0;
148
149 /* Read the Oscillator Failure only in XT mode */
150 rc_mode = abx80x_is_rc_mode(client);
151 if (rc_mode < 0)
152 return rc_mode;
153
154 if (!rc_mode) {
155 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
156 if (flags < 0)
157 return flags;
158
159 if (flags & ABX8XX_OSS_OF) {
160 dev_err(dev, "Oscillator failure, data is invalid.\n");
161 return -EINVAL;
162 }
163 }
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700164
165 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
166 sizeof(buf), buf);
167 if (err < 0) {
168 dev_err(&client->dev, "Unable to read date\n");
169 return -EIO;
170 }
171
172 tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
173 tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
174 tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
175 tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
176 tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
177 tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
178 tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
179
Alexandre Bellonifbfd36f2018-02-20 23:42:27 +0100180 return 0;
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700181}
182
183static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
184{
185 struct i2c_client *client = to_i2c_client(dev);
186 unsigned char buf[8];
Mylène Josserandee087742016-03-21 18:06:10 +0100187 int err, flags;
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700188
189 if (tm->tm_year < 100)
190 return -EINVAL;
191
192 buf[ABX8XX_REG_HTH] = 0;
193 buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
194 buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
195 buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
196 buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
197 buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
198 buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
199 buf[ABX8XX_REG_WD] = tm->tm_wday;
200
201 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
202 sizeof(buf), buf);
203 if (err < 0) {
204 dev_err(&client->dev, "Unable to write to date registers\n");
205 return -EIO;
206 }
207
Mylène Josserandee087742016-03-21 18:06:10 +0100208 /* Clear the OF bit of Oscillator Status Register */
209 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
210 if (flags < 0)
211 return flags;
212
213 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
214 flags & ~ABX8XX_OSS_OF);
215 if (err < 0) {
216 dev_err(&client->dev, "Unable to write oscillator status register\n");
217 return err;
218 }
219
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700220 return 0;
221}
222
Alexandre Belloni718a8202015-12-17 00:36:22 +0100223static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
224{
225 struct i2c_client *client = dev_id;
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600226 struct abx80x_priv *priv = i2c_get_clientdata(client);
227 struct rtc_device *rtc = priv->rtc;
Alexandre Belloni718a8202015-12-17 00:36:22 +0100228 int status;
229
230 status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
231 if (status < 0)
232 return IRQ_NONE;
233
234 if (status & ABX8XX_STATUS_AF)
235 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
236
237 i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
238
239 return IRQ_HANDLED;
240}
241
242static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
243{
244 struct i2c_client *client = to_i2c_client(dev);
245 unsigned char buf[7];
246
247 int irq_mask, err;
248
249 if (client->irq <= 0)
250 return -EINVAL;
251
252 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
253 sizeof(buf), buf);
254 if (err)
255 return err;
256
257 irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
258 if (irq_mask < 0)
259 return irq_mask;
260
261 t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
262 t->time.tm_min = bcd2bin(buf[1] & 0x7F);
263 t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
264 t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
265 t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
266 t->time.tm_wday = buf[5] & 0x7;
267
268 t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
269 t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
270
271 return err;
272}
273
274static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
275{
276 struct i2c_client *client = to_i2c_client(dev);
277 u8 alarm[6];
278 int err;
279
280 if (client->irq <= 0)
281 return -EINVAL;
282
283 alarm[0] = 0x0;
284 alarm[1] = bin2bcd(t->time.tm_sec);
285 alarm[2] = bin2bcd(t->time.tm_min);
286 alarm[3] = bin2bcd(t->time.tm_hour);
287 alarm[4] = bin2bcd(t->time.tm_mday);
288 alarm[5] = bin2bcd(t->time.tm_mon + 1);
289
290 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
291 sizeof(alarm), alarm);
292 if (err < 0) {
293 dev_err(&client->dev, "Unable to write alarm registers\n");
294 return -EIO;
295 }
296
297 if (t->enabled) {
298 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
299 (ABX8XX_IRQ_IM_1_4 |
300 ABX8XX_IRQ_AIE));
301 if (err)
302 return err;
303 }
304
305 return 0;
306}
307
Mylène Josserand59a83832016-03-21 18:06:09 +0100308static int abx80x_rtc_set_autocalibration(struct device *dev,
309 int autocalibration)
310{
311 struct i2c_client *client = to_i2c_client(dev);
312 int retval, flags = 0;
313
314 if ((autocalibration != 0) && (autocalibration != 1024) &&
315 (autocalibration != 512)) {
316 dev_err(dev, "autocalibration value outside permitted range\n");
317 return -EINVAL;
318 }
319
320 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
321 if (flags < 0)
322 return flags;
323
324 if (autocalibration == 0) {
325 flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
326 } else if (autocalibration == 1024) {
327 /* 1024 autocalibration is 0x10 */
328 flags |= ABX8XX_OSC_ACAL_1024;
329 flags &= ~(ABX8XX_OSC_ACAL_512);
330 } else {
331 /* 512 autocalibration is 0x11 */
332 flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
333 }
334
335 /* Unlock write access to Oscillator Control Register */
336 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
337 ABX8XX_CFG_KEY_OSC);
338 if (retval < 0) {
339 dev_err(dev, "Failed to write CONFIG_KEY register\n");
340 return retval;
341 }
342
343 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
344
345 return retval;
346}
347
348static int abx80x_rtc_get_autocalibration(struct device *dev)
349{
350 struct i2c_client *client = to_i2c_client(dev);
351 int flags = 0, autocalibration;
352
353 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
354 if (flags < 0)
355 return flags;
356
357 if (flags & ABX8XX_OSC_ACAL_512)
358 autocalibration = 512;
359 else if (flags & ABX8XX_OSC_ACAL_1024)
360 autocalibration = 1024;
361 else
362 autocalibration = 0;
363
364 return autocalibration;
365}
366
367static ssize_t autocalibration_store(struct device *dev,
368 struct device_attribute *attr,
369 const char *buf, size_t count)
370{
371 int retval;
372 unsigned long autocalibration = 0;
373
374 retval = kstrtoul(buf, 10, &autocalibration);
375 if (retval < 0) {
376 dev_err(dev, "Failed to store RTC autocalibration attribute\n");
377 return -EINVAL;
378 }
379
380 retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
381
382 return retval ? retval : count;
383}
384
385static ssize_t autocalibration_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
388 int autocalibration = 0;
389
390 autocalibration = abx80x_rtc_get_autocalibration(dev);
391 if (autocalibration < 0) {
392 dev_err(dev, "Failed to read RTC autocalibration\n");
393 sprintf(buf, "0\n");
394 return autocalibration;
395 }
396
397 return sprintf(buf, "%d\n", autocalibration);
398}
399
400static DEVICE_ATTR_RW(autocalibration);
401
402static ssize_t oscillator_store(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
405{
406 struct i2c_client *client = to_i2c_client(dev);
407 int retval, flags, rc_mode = 0;
408
409 if (strncmp(buf, "rc", 2) == 0) {
410 rc_mode = 1;
411 } else if (strncmp(buf, "xtal", 4) == 0) {
412 rc_mode = 0;
413 } else {
414 dev_err(dev, "Oscillator selection value outside permitted ones\n");
415 return -EINVAL;
416 }
417
418 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
419 if (flags < 0)
420 return flags;
421
422 if (rc_mode == 0)
423 flags &= ~(ABX8XX_OSC_OSEL);
424 else
425 flags |= (ABX8XX_OSC_OSEL);
426
427 /* Unlock write access on Oscillator Control register */
428 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
429 ABX8XX_CFG_KEY_OSC);
430 if (retval < 0) {
431 dev_err(dev, "Failed to write CONFIG_KEY register\n");
432 return retval;
433 }
434
435 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
436 if (retval < 0) {
437 dev_err(dev, "Failed to write Oscillator Control register\n");
438 return retval;
439 }
440
441 return retval ? retval : count;
442}
443
444static ssize_t oscillator_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
447 int rc_mode = 0;
448 struct i2c_client *client = to_i2c_client(dev);
449
450 rc_mode = abx80x_is_rc_mode(client);
451
452 if (rc_mode < 0) {
453 dev_err(dev, "Failed to read RTC oscillator selection\n");
454 sprintf(buf, "\n");
455 return rc_mode;
456 }
457
458 if (rc_mode)
459 return sprintf(buf, "rc\n");
460 else
461 return sprintf(buf, "xtal\n");
462}
463
464static DEVICE_ATTR_RW(oscillator);
465
466static struct attribute *rtc_calib_attrs[] = {
467 &dev_attr_autocalibration.attr,
468 &dev_attr_oscillator.attr,
469 NULL,
470};
471
472static const struct attribute_group rtc_calib_attr_group = {
473 .attrs = rtc_calib_attrs,
474};
475
Alexandre Belloni718a8202015-12-17 00:36:22 +0100476static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
477{
478 struct i2c_client *client = to_i2c_client(dev);
479 int err;
480
481 if (enabled)
482 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
483 (ABX8XX_IRQ_IM_1_4 |
484 ABX8XX_IRQ_AIE));
485 else
486 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
487 ABX8XX_IRQ_IM_1_4);
488 return err;
489}
490
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700491static const struct rtc_class_ops abx80x_rtc_ops = {
492 .read_time = abx80x_rtc_read_time,
493 .set_time = abx80x_rtc_set_time,
Alexandre Belloni718a8202015-12-17 00:36:22 +0100494 .read_alarm = abx80x_read_alarm,
495 .set_alarm = abx80x_set_alarm,
496 .alarm_irq_enable = abx80x_alarm_irq_enable,
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700497};
498
499static int abx80x_dt_trickle_cfg(struct device_node *np)
500{
501 const char *diode;
502 int trickle_cfg = 0;
503 int i, ret;
504 u32 tmp;
505
506 ret = of_property_read_string(np, "abracon,tc-diode", &diode);
507 if (ret)
508 return ret;
509
510 if (!strcmp(diode, "standard"))
511 trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
512 else if (!strcmp(diode, "schottky"))
513 trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
514 else
515 return -EINVAL;
516
517 ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
518 if (ret)
519 return ret;
520
521 for (i = 0; i < sizeof(trickle_resistors); i++)
522 if (trickle_resistors[i] == tmp)
523 break;
524
525 if (i == sizeof(trickle_resistors))
526 return -EINVAL;
527
528 return (trickle_cfg | i);
529}
530
Mylène Josserand59a83832016-03-21 18:06:09 +0100531static void rtc_calib_remove_sysfs_group(void *_dev)
532{
533 struct device *dev = _dev;
534
535 sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
536}
537
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700538static int abx80x_probe(struct i2c_client *client,
539 const struct i2c_device_id *id)
540{
541 struct device_node *np = client->dev.of_node;
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600542 struct abx80x_priv *priv;
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700543 int i, data, err, trickle_cfg = -EINVAL;
544 char buf[7];
545 unsigned int part = id->driver_data;
546 unsigned int partnumber;
547 unsigned int majrev, minrev;
548 unsigned int lot;
549 unsigned int wafer;
550 unsigned int uid;
551
552 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
553 return -ENODEV;
554
555 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
556 sizeof(buf), buf);
557 if (err < 0) {
558 dev_err(&client->dev, "Unable to read partnumber\n");
559 return -EIO;
560 }
561
562 partnumber = (buf[0] << 8) | buf[1];
563 majrev = buf[2] >> 3;
564 minrev = buf[2] & 0x7;
565 lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
566 uid = ((buf[4] & 0x7f) << 8) | buf[5];
567 wafer = (buf[6] & 0x7c) >> 2;
568 dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
569 partnumber, majrev, minrev, lot, wafer, uid);
570
571 data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
572 if (data < 0) {
573 dev_err(&client->dev, "Unable to read control register\n");
574 return -EIO;
575 }
576
577 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
Alexandre Belloni718a8202015-12-17 00:36:22 +0100578 ((data & ~(ABX8XX_CTRL_12_24 |
579 ABX8XX_CTRL_ARST)) |
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700580 ABX8XX_CTRL_WRITE));
581 if (err < 0) {
582 dev_err(&client->dev, "Unable to write control register\n");
583 return -EIO;
584 }
585
586 /* part autodetection */
587 if (part == ABX80X) {
588 for (i = 0; abx80x_caps[i].pn; i++)
589 if (partnumber == abx80x_caps[i].pn)
590 break;
591 if (abx80x_caps[i].pn == 0) {
592 dev_err(&client->dev, "Unknown part: %04x\n",
593 partnumber);
594 return -EINVAL;
595 }
596 part = i;
597 }
598
599 if (partnumber != abx80x_caps[part].pn) {
600 dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
601 partnumber, abx80x_caps[part].pn);
602 return -EINVAL;
603 }
604
605 if (np && abx80x_caps[part].has_tc)
606 trickle_cfg = abx80x_dt_trickle_cfg(np);
607
608 if (trickle_cfg > 0) {
609 dev_info(&client->dev, "Enabling trickle charger: %02x\n",
610 trickle_cfg);
611 abx80x_enable_trickle_charger(client, trickle_cfg);
612 }
613
Alexandre Belloni718a8202015-12-17 00:36:22 +0100614 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
615 BIT(2));
616 if (err)
617 return err;
618
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600619 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
620 if (priv == NULL)
621 return -ENOMEM;
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700622
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600623 priv->rtc = devm_rtc_allocate_device(&client->dev);
624 if (IS_ERR(priv->rtc))
625 return PTR_ERR(priv->rtc);
Alexandre Belloni9360a6a2017-10-13 00:04:46 +0200626
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600627 priv->rtc->ops = &abx80x_rtc_ops;
628 priv->client = client;
629
630 i2c_set_clientdata(client, priv);
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700631
Alexandre Belloni718a8202015-12-17 00:36:22 +0100632 if (client->irq > 0) {
633 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
634 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
635 abx80x_handle_irq,
636 IRQF_SHARED | IRQF_ONESHOT,
637 "abx8xx",
638 client);
639 if (err) {
640 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
641 client->irq = 0;
642 }
643 }
644
Mylène Josserand59a83832016-03-21 18:06:09 +0100645 /* Export sysfs entries */
646 err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
647 if (err) {
648 dev_err(&client->dev, "Failed to create sysfs group: %d\n",
649 err);
650 return err;
651 }
652
Sudip Mukherjeed8cac8d2016-07-03 21:32:22 +0100653 err = devm_add_action_or_reset(&client->dev,
654 rtc_calib_remove_sysfs_group,
655 &client->dev);
Alexandre Belloni9da32ba2017-10-13 00:04:47 +0200656 if (err) {
Mylène Josserand59a83832016-03-21 18:06:09 +0100657 dev_err(&client->dev,
658 "Failed to add sysfs cleanup action: %d\n",
659 err);
Alexandre Belloni9da32ba2017-10-13 00:04:47 +0200660 return err;
661 }
662
Jeremy Gebbenaf69f9a2018-09-11 11:28:25 -0600663 err = rtc_register_device(priv->rtc);
Mylène Josserand59a83832016-03-21 18:06:09 +0100664
Sudip Mukherjeed8cac8d2016-07-03 21:32:22 +0100665 return err;
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700666}
667
668static int abx80x_remove(struct i2c_client *client)
669{
670 return 0;
671}
672
673static const struct i2c_device_id abx80x_id[] = {
674 { "abx80x", ABX80X },
675 { "ab0801", AB0801 },
676 { "ab0803", AB0803 },
677 { "ab0804", AB0804 },
678 { "ab0805", AB0805 },
679 { "ab1801", AB1801 },
680 { "ab1803", AB1803 },
681 { "ab1804", AB1804 },
682 { "ab1805", AB1805 },
Alexandre Bellonifca733a2015-12-17 00:36:21 +0100683 { "rv1805", AB1805 },
Philippe De Muyter4d61ff62015-05-05 16:23:44 -0700684 { }
685};
686MODULE_DEVICE_TABLE(i2c, abx80x_id);
687
688static struct i2c_driver abx80x_driver = {
689 .driver = {
690 .name = "rtc-abx80x",
691 },
692 .probe = abx80x_probe,
693 .remove = abx80x_remove,
694 .id_table = abx80x_id,
695};
696
697module_i2c_driver(abx80x_driver);
698
699MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
700MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
701MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
702MODULE_LICENSE("GPL v2");