2 * A driver for the I2C members of the Abracon AB x8xx RTC family,
3 * and compatible: AB 1805 and AB 0805
5 * Copyright 2014-2015 Macq S.A.
7 * Author: Philippe De Muyter <phdm@macqel.be>
8 * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
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.
16 #include <linux/bcd.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/rtc.h>
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
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
38 #define ABX8XX_REG_STATUS 0x0f
39 #define ABX8XX_STATUS_AF BIT(2)
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)
46 #define ABX8XX_REG_IRQ 0x12
47 #define ABX8XX_IRQ_AIE BIT(2)
48 #define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
50 #define ABX8XX_REG_CD_TIMER_CTL 0x18
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)
58 #define ABX8XX_OSC_OSEL BIT(7)
60 #define ABX8XX_REG_OSS 0x1d
61 #define ABX8XX_OSS_OF BIT(1)
62 #define ABX8XX_OSS_OMODE BIT(4)
64 #define ABX8XX_REG_CFG_KEY 0x1f
65 #define ABX8XX_CFG_KEY_OSC 0xa1
66 #define ABX8XX_CFG_KEY_MISC 0x9d
68 #define ABX8XX_REG_ID0 0x28
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
75 static u8 trickle_resistors
[] = {0, 3, 6, 11};
77 enum abx80x_chip
{AB0801
, AB0803
, AB0804
, AB0805
,
78 AB1801
, AB1803
, AB1804
, AB1805
, ABX80X
};
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},
97 static int abx80x_is_rc_mode(struct i2c_client
*client
)
101 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSS
);
103 dev_err(&client
->dev
,
104 "Failed to read autocalibration attribute\n");
108 return (flags
& ABX8XX_OSS_OMODE
) ? 1 : 0;
111 static int abx80x_enable_trickle_charger(struct i2c_client
*client
,
117 * Write the configuration key register to enable access to the Trickle
120 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_CFG_KEY
,
121 ABX8XX_CFG_KEY_MISC
);
123 dev_err(&client
->dev
, "Unable to write configuration key\n");
127 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_TRICKLE
,
128 ABX8XX_TRICKLE_CHARGE_ENABLE
|
131 dev_err(&client
->dev
, "Unable to write trickle register\n");
138 static int abx80x_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
140 struct i2c_client
*client
= to_i2c_client(dev
);
141 unsigned char buf
[8];
142 int err
, flags
, rc_mode
= 0;
144 /* Read the Oscillator Failure only in XT mode */
145 rc_mode
= abx80x_is_rc_mode(client
);
150 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSS
);
154 if (flags
& ABX8XX_OSS_OF
) {
155 dev_err(dev
, "Oscillator failure, data is invalid.\n");
160 err
= i2c_smbus_read_i2c_block_data(client
, ABX8XX_REG_HTH
,
163 dev_err(&client
->dev
, "Unable to read date\n");
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;
175 err
= rtc_valid_tm(tm
);
177 dev_err(&client
->dev
, "retrieved date/time is not valid.\n");
182 static int abx80x_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
184 struct i2c_client
*client
= to_i2c_client(dev
);
185 unsigned char buf
[8];
188 if (tm
->tm_year
< 100)
191 buf
[ABX8XX_REG_HTH
] = 0;
192 buf
[ABX8XX_REG_SC
] = bin2bcd(tm
->tm_sec
);
193 buf
[ABX8XX_REG_MN
] = bin2bcd(tm
->tm_min
);
194 buf
[ABX8XX_REG_HR
] = bin2bcd(tm
->tm_hour
);
195 buf
[ABX8XX_REG_DA
] = bin2bcd(tm
->tm_mday
);
196 buf
[ABX8XX_REG_MO
] = bin2bcd(tm
->tm_mon
+ 1);
197 buf
[ABX8XX_REG_YR
] = bin2bcd(tm
->tm_year
- 100);
198 buf
[ABX8XX_REG_WD
] = tm
->tm_wday
;
200 err
= i2c_smbus_write_i2c_block_data(client
, ABX8XX_REG_HTH
,
203 dev_err(&client
->dev
, "Unable to write to date registers\n");
207 /* Clear the OF bit of Oscillator Status Register */
208 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSS
);
212 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_OSS
,
213 flags
& ~ABX8XX_OSS_OF
);
215 dev_err(&client
->dev
, "Unable to write oscillator status register\n");
222 static irqreturn_t
abx80x_handle_irq(int irq
, void *dev_id
)
224 struct i2c_client
*client
= dev_id
;
225 struct rtc_device
*rtc
= i2c_get_clientdata(client
);
228 status
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_STATUS
);
232 if (status
& ABX8XX_STATUS_AF
)
233 rtc_update_irq(rtc
, 1, RTC_AF
| RTC_IRQF
);
235 i2c_smbus_write_byte_data(client
, ABX8XX_REG_STATUS
, 0);
240 static int abx80x_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
242 struct i2c_client
*client
= to_i2c_client(dev
);
243 unsigned char buf
[7];
247 if (client
->irq
<= 0)
250 err
= i2c_smbus_read_i2c_block_data(client
, ABX8XX_REG_ASC
,
255 irq_mask
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_IRQ
);
259 t
->time
.tm_sec
= bcd2bin(buf
[0] & 0x7F);
260 t
->time
.tm_min
= bcd2bin(buf
[1] & 0x7F);
261 t
->time
.tm_hour
= bcd2bin(buf
[2] & 0x3F);
262 t
->time
.tm_mday
= bcd2bin(buf
[3] & 0x3F);
263 t
->time
.tm_mon
= bcd2bin(buf
[4] & 0x1F) - 1;
264 t
->time
.tm_wday
= buf
[5] & 0x7;
266 t
->enabled
= !!(irq_mask
& ABX8XX_IRQ_AIE
);
267 t
->pending
= (buf
[6] & ABX8XX_STATUS_AF
) && t
->enabled
;
272 static int abx80x_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
274 struct i2c_client
*client
= to_i2c_client(dev
);
278 if (client
->irq
<= 0)
282 alarm
[1] = bin2bcd(t
->time
.tm_sec
);
283 alarm
[2] = bin2bcd(t
->time
.tm_min
);
284 alarm
[3] = bin2bcd(t
->time
.tm_hour
);
285 alarm
[4] = bin2bcd(t
->time
.tm_mday
);
286 alarm
[5] = bin2bcd(t
->time
.tm_mon
+ 1);
288 err
= i2c_smbus_write_i2c_block_data(client
, ABX8XX_REG_AHTH
,
289 sizeof(alarm
), alarm
);
291 dev_err(&client
->dev
, "Unable to write alarm registers\n");
296 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_IRQ
,
306 static int abx80x_rtc_set_autocalibration(struct device
*dev
,
309 struct i2c_client
*client
= to_i2c_client(dev
);
310 int retval
, flags
= 0;
312 if ((autocalibration
!= 0) && (autocalibration
!= 1024) &&
313 (autocalibration
!= 512)) {
314 dev_err(dev
, "autocalibration value outside permitted range\n");
318 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSC
);
322 if (autocalibration
== 0) {
323 flags
&= ~(ABX8XX_OSC_ACAL_512
| ABX8XX_OSC_ACAL_1024
);
324 } else if (autocalibration
== 1024) {
325 /* 1024 autocalibration is 0x10 */
326 flags
|= ABX8XX_OSC_ACAL_1024
;
327 flags
&= ~(ABX8XX_OSC_ACAL_512
);
329 /* 512 autocalibration is 0x11 */
330 flags
|= (ABX8XX_OSC_ACAL_1024
| ABX8XX_OSC_ACAL_512
);
333 /* Unlock write access to Oscillator Control Register */
334 retval
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_CFG_KEY
,
337 dev_err(dev
, "Failed to write CONFIG_KEY register\n");
341 retval
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_OSC
, flags
);
346 static int abx80x_rtc_get_autocalibration(struct device
*dev
)
348 struct i2c_client
*client
= to_i2c_client(dev
);
349 int flags
= 0, autocalibration
;
351 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSC
);
355 if (flags
& ABX8XX_OSC_ACAL_512
)
356 autocalibration
= 512;
357 else if (flags
& ABX8XX_OSC_ACAL_1024
)
358 autocalibration
= 1024;
362 return autocalibration
;
365 static ssize_t
autocalibration_store(struct device
*dev
,
366 struct device_attribute
*attr
,
367 const char *buf
, size_t count
)
370 unsigned long autocalibration
= 0;
372 retval
= kstrtoul(buf
, 10, &autocalibration
);
374 dev_err(dev
, "Failed to store RTC autocalibration attribute\n");
378 retval
= abx80x_rtc_set_autocalibration(dev
, autocalibration
);
380 return retval
? retval
: count
;
383 static ssize_t
autocalibration_show(struct device
*dev
,
384 struct device_attribute
*attr
, char *buf
)
386 int autocalibration
= 0;
388 autocalibration
= abx80x_rtc_get_autocalibration(dev
);
389 if (autocalibration
< 0) {
390 dev_err(dev
, "Failed to read RTC autocalibration\n");
392 return autocalibration
;
395 return sprintf(buf
, "%d\n", autocalibration
);
398 static DEVICE_ATTR_RW(autocalibration
);
400 static ssize_t
oscillator_store(struct device
*dev
,
401 struct device_attribute
*attr
,
402 const char *buf
, size_t count
)
404 struct i2c_client
*client
= to_i2c_client(dev
);
405 int retval
, flags
, rc_mode
= 0;
407 if (strncmp(buf
, "rc", 2) == 0) {
409 } else if (strncmp(buf
, "xtal", 4) == 0) {
412 dev_err(dev
, "Oscillator selection value outside permitted ones\n");
416 flags
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_OSC
);
421 flags
&= ~(ABX8XX_OSC_OSEL
);
423 flags
|= (ABX8XX_OSC_OSEL
);
425 /* Unlock write access on Oscillator Control register */
426 retval
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_CFG_KEY
,
429 dev_err(dev
, "Failed to write CONFIG_KEY register\n");
433 retval
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_OSC
, flags
);
435 dev_err(dev
, "Failed to write Oscillator Control register\n");
439 return retval
? retval
: count
;
442 static ssize_t
oscillator_show(struct device
*dev
,
443 struct device_attribute
*attr
, char *buf
)
446 struct i2c_client
*client
= to_i2c_client(dev
);
448 rc_mode
= abx80x_is_rc_mode(client
);
451 dev_err(dev
, "Failed to read RTC oscillator selection\n");
457 return sprintf(buf
, "rc\n");
459 return sprintf(buf
, "xtal\n");
462 static DEVICE_ATTR_RW(oscillator
);
464 static struct attribute
*rtc_calib_attrs
[] = {
465 &dev_attr_autocalibration
.attr
,
466 &dev_attr_oscillator
.attr
,
470 static const struct attribute_group rtc_calib_attr_group
= {
471 .attrs
= rtc_calib_attrs
,
474 static int abx80x_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
476 struct i2c_client
*client
= to_i2c_client(dev
);
480 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_IRQ
,
484 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_IRQ
,
489 static const struct rtc_class_ops abx80x_rtc_ops
= {
490 .read_time
= abx80x_rtc_read_time
,
491 .set_time
= abx80x_rtc_set_time
,
492 .read_alarm
= abx80x_read_alarm
,
493 .set_alarm
= abx80x_set_alarm
,
494 .alarm_irq_enable
= abx80x_alarm_irq_enable
,
497 static int abx80x_dt_trickle_cfg(struct device_node
*np
)
504 ret
= of_property_read_string(np
, "abracon,tc-diode", &diode
);
508 if (!strcmp(diode
, "standard"))
509 trickle_cfg
|= ABX8XX_TRICKLE_STANDARD_DIODE
;
510 else if (!strcmp(diode
, "schottky"))
511 trickle_cfg
|= ABX8XX_TRICKLE_SCHOTTKY_DIODE
;
515 ret
= of_property_read_u32(np
, "abracon,tc-resistor", &tmp
);
519 for (i
= 0; i
< sizeof(trickle_resistors
); i
++)
520 if (trickle_resistors
[i
] == tmp
)
523 if (i
== sizeof(trickle_resistors
))
526 return (trickle_cfg
| i
);
529 static void rtc_calib_remove_sysfs_group(void *_dev
)
531 struct device
*dev
= _dev
;
533 sysfs_remove_group(&dev
->kobj
, &rtc_calib_attr_group
);
536 static int abx80x_probe(struct i2c_client
*client
,
537 const struct i2c_device_id
*id
)
539 struct device_node
*np
= client
->dev
.of_node
;
540 struct rtc_device
*rtc
;
541 int i
, data
, err
, trickle_cfg
= -EINVAL
;
543 unsigned int part
= id
->driver_data
;
544 unsigned int partnumber
;
545 unsigned int majrev
, minrev
;
550 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
553 err
= i2c_smbus_read_i2c_block_data(client
, ABX8XX_REG_ID0
,
556 dev_err(&client
->dev
, "Unable to read partnumber\n");
560 partnumber
= (buf
[0] << 8) | buf
[1];
561 majrev
= buf
[2] >> 3;
562 minrev
= buf
[2] & 0x7;
563 lot
= ((buf
[4] & 0x80) << 2) | ((buf
[6] & 0x80) << 1) | buf
[3];
564 uid
= ((buf
[4] & 0x7f) << 8) | buf
[5];
565 wafer
= (buf
[6] & 0x7c) >> 2;
566 dev_info(&client
->dev
, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
567 partnumber
, majrev
, minrev
, lot
, wafer
, uid
);
569 data
= i2c_smbus_read_byte_data(client
, ABX8XX_REG_CTRL1
);
571 dev_err(&client
->dev
, "Unable to read control register\n");
575 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_CTRL1
,
576 ((data
& ~(ABX8XX_CTRL_12_24
|
580 dev_err(&client
->dev
, "Unable to write control register\n");
584 /* part autodetection */
585 if (part
== ABX80X
) {
586 for (i
= 0; abx80x_caps
[i
].pn
; i
++)
587 if (partnumber
== abx80x_caps
[i
].pn
)
589 if (abx80x_caps
[i
].pn
== 0) {
590 dev_err(&client
->dev
, "Unknown part: %04x\n",
597 if (partnumber
!= abx80x_caps
[part
].pn
) {
598 dev_err(&client
->dev
, "partnumber mismatch %04x != %04x\n",
599 partnumber
, abx80x_caps
[part
].pn
);
603 if (np
&& abx80x_caps
[part
].has_tc
)
604 trickle_cfg
= abx80x_dt_trickle_cfg(np
);
606 if (trickle_cfg
> 0) {
607 dev_info(&client
->dev
, "Enabling trickle charger: %02x\n",
609 abx80x_enable_trickle_charger(client
, trickle_cfg
);
612 err
= i2c_smbus_write_byte_data(client
, ABX8XX_REG_CD_TIMER_CTL
,
617 rtc
= devm_rtc_device_register(&client
->dev
, "abx8xx",
618 &abx80x_rtc_ops
, THIS_MODULE
);
623 i2c_set_clientdata(client
, rtc
);
625 if (client
->irq
> 0) {
626 dev_info(&client
->dev
, "IRQ %d supplied\n", client
->irq
);
627 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
629 IRQF_SHARED
| IRQF_ONESHOT
,
633 dev_err(&client
->dev
, "unable to request IRQ, alarms disabled\n");
638 /* Export sysfs entries */
639 err
= sysfs_create_group(&(&client
->dev
)->kobj
, &rtc_calib_attr_group
);
641 dev_err(&client
->dev
, "Failed to create sysfs group: %d\n",
646 err
= devm_add_action(&client
->dev
, rtc_calib_remove_sysfs_group
,
649 rtc_calib_remove_sysfs_group(&client
->dev
);
650 dev_err(&client
->dev
,
651 "Failed to add sysfs cleanup action: %d\n",
659 static int abx80x_remove(struct i2c_client
*client
)
664 static const struct i2c_device_id abx80x_id
[] = {
665 { "abx80x", ABX80X
},
666 { "ab0801", AB0801
},
667 { "ab0803", AB0803
},
668 { "ab0804", AB0804
},
669 { "ab0805", AB0805
},
670 { "ab1801", AB1801
},
671 { "ab1803", AB1803
},
672 { "ab1804", AB1804
},
673 { "ab1805", AB1805
},
674 { "rv1805", AB1805
},
677 MODULE_DEVICE_TABLE(i2c
, abx80x_id
);
679 static struct i2c_driver abx80x_driver
= {
681 .name
= "rtc-abx80x",
683 .probe
= abx80x_probe
,
684 .remove
= abx80x_remove
,
685 .id_table
= abx80x_id
,
688 module_i2c_driver(abx80x_driver
);
690 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
691 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
692 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
693 MODULE_LICENSE("GPL v2");