1 // SPDX-License-Identifier: GPL-2.0
3 * Real Time Clock driver for AB-RTCMC-32.768kHz-EOZ9 chip.
4 * Copyright (C) 2019 Orolia
8 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/bcd.h>
13 #include <linux/regmap.h>
14 #include <linux/bitfield.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
18 #define ABEOZ9_REG_CTRL1 0x00
19 #define ABEOZ9_REG_CTRL1_MASK GENMASK(7, 0)
20 #define ABEOZ9_REG_CTRL1_WE BIT(0)
21 #define ABEOZ9_REG_CTRL1_TE BIT(1)
22 #define ABEOZ9_REG_CTRL1_TAR BIT(2)
23 #define ABEOZ9_REG_CTRL1_EERE BIT(3)
24 #define ABEOZ9_REG_CTRL1_SRON BIT(4)
25 #define ABEOZ9_REG_CTRL1_TD0 BIT(5)
26 #define ABEOZ9_REG_CTRL1_TD1 BIT(6)
27 #define ABEOZ9_REG_CTRL1_CLKINT BIT(7)
29 #define ABEOZ9_REG_CTRL_INT 0x01
30 #define ABEOZ9_REG_CTRL_INT_AIE BIT(0)
31 #define ABEOZ9_REG_CTRL_INT_TIE BIT(1)
32 #define ABEOZ9_REG_CTRL_INT_V1IE BIT(2)
33 #define ABEOZ9_REG_CTRL_INT_V2IE BIT(3)
34 #define ABEOZ9_REG_CTRL_INT_SRIE BIT(4)
36 #define ABEOZ9_REG_CTRL_INT_FLAG 0x02
37 #define ABEOZ9_REG_CTRL_INT_FLAG_AF BIT(0)
38 #define ABEOZ9_REG_CTRL_INT_FLAG_TF BIT(1)
39 #define ABEOZ9_REG_CTRL_INT_FLAG_V1IF BIT(2)
40 #define ABEOZ9_REG_CTRL_INT_FLAG_V2IF BIT(3)
41 #define ABEOZ9_REG_CTRL_INT_FLAG_SRF BIT(4)
43 #define ABEOZ9_REG_CTRL_STATUS 0x03
44 #define ABEOZ9_REG_CTRL_STATUS_V1F BIT(2)
45 #define ABEOZ9_REG_CTRL_STATUS_V2F BIT(3)
46 #define ABEOZ9_REG_CTRL_STATUS_SR BIT(4)
47 #define ABEOZ9_REG_CTRL_STATUS_PON BIT(5)
48 #define ABEOZ9_REG_CTRL_STATUS_EEBUSY BIT(7)
50 #define ABEOZ9_REG_SEC 0x08
51 #define ABEOZ9_REG_MIN 0x09
52 #define ABEOZ9_REG_HOURS 0x0A
53 #define ABEOZ9_HOURS_PM BIT(6)
54 #define ABEOZ9_REG_DAYS 0x0B
55 #define ABEOZ9_REG_WEEKDAYS 0x0C
56 #define ABEOZ9_REG_MONTHS 0x0D
57 #define ABEOZ9_REG_YEARS 0x0E
59 #define ABEOZ9_SEC_LEN 7
61 #define ABEOZ9_REG_ALARM_SEC 0x10
62 #define ABEOZ9_BIT_ALARM_SEC GENMASK(6, 0)
63 #define ABEOZ9_REG_ALARM_MIN 0x11
64 #define ABEOZ9_BIT_ALARM_MIN GENMASK(6, 0)
65 #define ABEOZ9_REG_ALARM_HOURS 0x12
66 #define ABEOZ9_BIT_ALARM_HOURS_PM BIT(5)
67 #define ABEOZ9_BIT_ALARM_HOURS GENMASK(4, 0)
68 #define ABEOZ9_REG_ALARM_DAYS 0x13
69 #define ABEOZ9_BIT_ALARM_DAYS GENMASK(5, 0)
70 #define ABEOZ9_REG_ALARM_WEEKDAYS 0x14
71 #define ABEOZ9_BIT_ALARM_WEEKDAYS GENMASK(2, 0)
72 #define ABEOZ9_REG_ALARM_MONTHS 0x15
73 #define ABEOZ9_BIT_ALARM_MONTHS GENMASK(4, 0)
74 #define ABEOZ9_REG_ALARM_YEARS 0x16
76 #define ABEOZ9_ALARM_LEN 7
77 #define ABEOZ9_BIT_ALARM_AE BIT(7)
79 #define ABEOZ9_REG_REG_TEMP 0x20
80 #define ABEOZ953_TEMP_MAX 120
81 #define ABEOZ953_TEMP_MIN -60
83 #define ABEOZ9_REG_EEPROM 0x30
84 #define ABEOZ9_REG_EEPROM_MASK GENMASK(8, 0)
85 #define ABEOZ9_REG_EEPROM_THP BIT(0)
86 #define ABEOZ9_REG_EEPROM_THE BIT(1)
87 #define ABEOZ9_REG_EEPROM_FD0 BIT(2)
88 #define ABEOZ9_REG_EEPROM_FD1 BIT(3)
89 #define ABEOZ9_REG_EEPROM_R1K BIT(4)
90 #define ABEOZ9_REG_EEPROM_R5K BIT(5)
91 #define ABEOZ9_REG_EEPROM_R20K BIT(6)
92 #define ABEOZ9_REG_EEPROM_R80K BIT(7)
94 struct abeoz9_rtc_data {
95 struct rtc_device *rtc;
96 struct regmap *regmap;
97 struct device *hwmon_dev;
100 static int abeoz9_check_validity(struct device *dev)
102 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
103 struct regmap *regmap = data->regmap;
107 ret = regmap_read(regmap, ABEOZ9_REG_CTRL_STATUS, &val);
110 "unable to get CTRL_STATUS register (%d)\n", ret);
114 if (val & ABEOZ9_REG_CTRL_STATUS_PON) {
115 dev_warn(dev, "power-on reset detected, date is invalid\n");
119 if (val & ABEOZ9_REG_CTRL_STATUS_V1F) {
121 "voltage drops below VLOW1 threshold, date is invalid\n");
125 if ((val & ABEOZ9_REG_CTRL_STATUS_V2F)) {
127 "voltage drops below VLOW2 threshold, date is invalid\n");
134 static int abeoz9_reset_validity(struct regmap *regmap)
136 return regmap_update_bits(regmap, ABEOZ9_REG_CTRL_STATUS,
137 ABEOZ9_REG_CTRL_STATUS_V1F |
138 ABEOZ9_REG_CTRL_STATUS_V2F |
139 ABEOZ9_REG_CTRL_STATUS_PON,
143 static int abeoz9_rtc_get_time(struct device *dev, struct rtc_time *tm)
145 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
146 u8 regs[ABEOZ9_SEC_LEN];
149 ret = abeoz9_check_validity(dev);
153 ret = regmap_bulk_read(data->regmap, ABEOZ9_REG_SEC,
157 dev_err(dev, "reading RTC time failed (%d)\n", ret);
161 tm->tm_sec = bcd2bin(regs[ABEOZ9_REG_SEC - ABEOZ9_REG_SEC] & 0x7F);
162 tm->tm_min = bcd2bin(regs[ABEOZ9_REG_MIN - ABEOZ9_REG_SEC] & 0x7F);
164 if (regs[ABEOZ9_REG_HOURS - ABEOZ9_REG_SEC] & ABEOZ9_HOURS_PM) {
166 bcd2bin(regs[ABEOZ9_REG_HOURS - ABEOZ9_REG_SEC] & 0x1f);
167 if (regs[ABEOZ9_REG_HOURS - ABEOZ9_REG_SEC] & ABEOZ9_HOURS_PM)
170 tm->tm_hour = bcd2bin(regs[ABEOZ9_REG_HOURS - ABEOZ9_REG_SEC]);
173 tm->tm_mday = bcd2bin(regs[ABEOZ9_REG_DAYS - ABEOZ9_REG_SEC]);
174 tm->tm_wday = bcd2bin(regs[ABEOZ9_REG_WEEKDAYS - ABEOZ9_REG_SEC]);
175 tm->tm_mon = bcd2bin(regs[ABEOZ9_REG_MONTHS - ABEOZ9_REG_SEC]) - 1;
176 tm->tm_year = bcd2bin(regs[ABEOZ9_REG_YEARS - ABEOZ9_REG_SEC]) + 100;
181 static int abeoz9_rtc_set_time(struct device *dev, struct rtc_time *tm)
183 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
184 struct regmap *regmap = data->regmap;
185 u8 regs[ABEOZ9_SEC_LEN];
188 regs[ABEOZ9_REG_SEC - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_sec);
189 regs[ABEOZ9_REG_MIN - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_min);
190 regs[ABEOZ9_REG_HOURS - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_hour);
191 regs[ABEOZ9_REG_DAYS - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_mday);
192 regs[ABEOZ9_REG_WEEKDAYS - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_wday);
193 regs[ABEOZ9_REG_MONTHS - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_mon + 1);
194 regs[ABEOZ9_REG_YEARS - ABEOZ9_REG_SEC] = bin2bcd(tm->tm_year - 100);
196 ret = regmap_bulk_write(data->regmap, ABEOZ9_REG_SEC,
201 dev_err(dev, "set RTC time failed (%d)\n", ret);
205 return abeoz9_reset_validity(regmap);
208 static int abeoz9_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
210 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
211 struct regmap *regmap = data->regmap;
212 u8 regs[ABEOZ9_ALARM_LEN];
216 ret = abeoz9_check_validity(dev);
220 ret = regmap_bulk_read(regmap, ABEOZ9_REG_CTRL_INT, val, sizeof(val));
224 alarm->enabled = val[0] & ABEOZ9_REG_CTRL_INT_AIE;
225 alarm->pending = val[1] & ABEOZ9_REG_CTRL_INT_FLAG_AF;
227 ret = regmap_bulk_read(regmap, ABEOZ9_REG_ALARM_SEC, regs, sizeof(regs));
231 alarm->time.tm_sec = bcd2bin(FIELD_GET(ABEOZ9_BIT_ALARM_SEC, regs[0]));
232 alarm->time.tm_min = bcd2bin(FIELD_GET(ABEOZ9_BIT_ALARM_MIN, regs[1]));
233 alarm->time.tm_hour = bcd2bin(FIELD_GET(ABEOZ9_BIT_ALARM_HOURS, regs[2]));
234 if (FIELD_GET(ABEOZ9_BIT_ALARM_HOURS_PM, regs[2]))
235 alarm->time.tm_hour += 12;
237 alarm->time.tm_mday = bcd2bin(FIELD_GET(ABEOZ9_BIT_ALARM_DAYS, regs[3]));
242 static int abeoz9_rtc_alarm_irq_enable(struct device *dev, u32 enable)
244 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
246 return regmap_update_bits(data->regmap, ABEOZ9_REG_CTRL_INT,
247 ABEOZ9_REG_CTRL_INT_AIE,
248 FIELD_PREP(ABEOZ9_REG_CTRL_INT_AIE, enable));
251 static int abeoz9_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
253 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
254 u8 regs[ABEOZ9_ALARM_LEN] = {0};
257 ret = regmap_update_bits(data->regmap, ABEOZ9_REG_CTRL_INT_FLAG,
258 ABEOZ9_REG_CTRL_INT_FLAG_AF, 0);
262 regs[0] = ABEOZ9_BIT_ALARM_AE | FIELD_PREP(ABEOZ9_BIT_ALARM_SEC,
263 bin2bcd(alarm->time.tm_sec));
264 regs[1] = ABEOZ9_BIT_ALARM_AE | FIELD_PREP(ABEOZ9_BIT_ALARM_MIN,
265 bin2bcd(alarm->time.tm_min));
266 regs[2] = ABEOZ9_BIT_ALARM_AE | FIELD_PREP(ABEOZ9_BIT_ALARM_HOURS,
267 bin2bcd(alarm->time.tm_hour));
268 regs[3] = ABEOZ9_BIT_ALARM_AE | FIELD_PREP(ABEOZ9_BIT_ALARM_DAYS,
269 bin2bcd(alarm->time.tm_mday));
271 ret = regmap_bulk_write(data->regmap, ABEOZ9_REG_ALARM_SEC, regs,
276 return abeoz9_rtc_alarm_irq_enable(dev, alarm->enabled);
279 static irqreturn_t abeoz9_rtc_irq(int irq, void *dev)
281 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
285 ret = regmap_read(data->regmap, ABEOZ9_REG_CTRL_INT_FLAG, &val);
289 if (!FIELD_GET(ABEOZ9_REG_CTRL_INT_FLAG_AF, val))
292 regmap_update_bits(data->regmap, ABEOZ9_REG_CTRL_INT_FLAG,
293 ABEOZ9_REG_CTRL_INT_FLAG_AF, 0);
295 rtc_update_irq(data->rtc, 1, RTC_IRQF | RTC_AF);
300 static int abeoz9_trickle_parse_dt(struct device_node *node)
304 if (of_property_read_u32(node, "trickle-resistor-ohms", &ohms))
309 return ABEOZ9_REG_EEPROM_R1K;
311 return ABEOZ9_REG_EEPROM_R5K;
313 return ABEOZ9_REG_EEPROM_R20K;
315 return ABEOZ9_REG_EEPROM_R80K;
321 static int abeoz9_rtc_setup(struct device *dev, struct device_node *node)
323 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
324 struct regmap *regmap = data->regmap;
327 /* Enable Self Recovery, Clock for Watch and EEPROM refresh functions */
328 ret = regmap_update_bits(regmap, ABEOZ9_REG_CTRL1,
329 ABEOZ9_REG_CTRL1_MASK,
330 ABEOZ9_REG_CTRL1_WE |
331 ABEOZ9_REG_CTRL1_EERE |
332 ABEOZ9_REG_CTRL1_SRON);
334 dev_err(dev, "unable to set CTRL_1 register (%d)\n", ret);
338 ret = regmap_write(regmap, ABEOZ9_REG_CTRL_INT, 0);
341 "unable to set control CTRL_INT register (%d)\n",
346 ret = regmap_write(regmap, ABEOZ9_REG_CTRL_INT_FLAG, 0);
349 "unable to set control CTRL_INT_FLAG register (%d)\n",
354 ret = abeoz9_trickle_parse_dt(node);
356 /* Enable built-in termometer */
357 ret |= ABEOZ9_REG_EEPROM_THE;
359 ret = regmap_update_bits(regmap, ABEOZ9_REG_EEPROM,
360 ABEOZ9_REG_EEPROM_MASK,
363 dev_err(dev, "unable to set EEPROM register (%d)\n", ret);
370 static const struct rtc_class_ops rtc_ops = {
371 .read_time = abeoz9_rtc_get_time,
372 .set_time = abeoz9_rtc_set_time,
373 .read_alarm = abeoz9_rtc_read_alarm,
374 .set_alarm = abeoz9_rtc_set_alarm,
375 .alarm_irq_enable = abeoz9_rtc_alarm_irq_enable,
378 static const struct regmap_config abeoz9_rtc_regmap_config = {
381 .max_register = 0x3f,
384 #if IS_REACHABLE(CONFIG_HWMON)
386 static int abeoz9z3_temp_read(struct device *dev,
387 enum hwmon_sensor_types type,
388 u32 attr, int channel, long *temp)
390 struct abeoz9_rtc_data *data = dev_get_drvdata(dev);
391 struct regmap *regmap = data->regmap;
395 ret = regmap_read(regmap, ABEOZ9_REG_CTRL_STATUS, &val);
399 if ((val & ABEOZ9_REG_CTRL_STATUS_V1F) ||
400 (val & ABEOZ9_REG_CTRL_STATUS_V2F)) {
402 "thermometer might be disabled due to low voltage\n");
407 case hwmon_temp_input:
408 ret = regmap_read(regmap, ABEOZ9_REG_REG_TEMP, &val);
411 *temp = 1000 * (val + ABEOZ953_TEMP_MIN);
414 *temp = 1000 * ABEOZ953_TEMP_MAX;
417 *temp = 1000 * ABEOZ953_TEMP_MIN;
424 static umode_t abeoz9_is_visible(const void *data,
425 enum hwmon_sensor_types type,
426 u32 attr, int channel)
429 case hwmon_temp_input:
438 static const u32 abeoz9_chip_config[] = {
443 static const struct hwmon_channel_info abeoz9_chip = {
445 .config = abeoz9_chip_config,
448 static const u32 abeoz9_temp_config[] = {
449 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN,
453 static const struct hwmon_channel_info abeoz9_temp = {
455 .config = abeoz9_temp_config,
458 static const struct hwmon_channel_info *abeoz9_info[] = {
464 static const struct hwmon_ops abeoz9_hwmon_ops = {
465 .is_visible = abeoz9_is_visible,
466 .read = abeoz9z3_temp_read,
469 static const struct hwmon_chip_info abeoz9_chip_info = {
470 .ops = &abeoz9_hwmon_ops,
474 static void abeoz9_hwmon_register(struct device *dev,
475 struct abeoz9_rtc_data *data)
478 devm_hwmon_device_register_with_info(dev,
483 if (IS_ERR(data->hwmon_dev)) {
484 dev_warn(dev, "unable to register hwmon device %ld\n",
485 PTR_ERR(data->hwmon_dev));
491 static void abeoz9_hwmon_register(struct device *dev,
492 struct abeoz9_rtc_data *data)
498 static int abeoz9_probe(struct i2c_client *client,
499 const struct i2c_device_id *id)
501 struct abeoz9_rtc_data *data = NULL;
502 struct device *dev = &client->dev;
503 struct regmap *regmap;
506 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
507 I2C_FUNC_SMBUS_BYTE_DATA |
508 I2C_FUNC_SMBUS_I2C_BLOCK))
511 regmap = devm_regmap_init_i2c(client, &abeoz9_rtc_regmap_config);
512 if (IS_ERR(regmap)) {
513 ret = PTR_ERR(regmap);
514 dev_err(dev, "regmap allocation failed: %d\n", ret);
518 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
522 data->regmap = regmap;
523 dev_set_drvdata(dev, data);
525 ret = abeoz9_rtc_setup(dev, client->dev.of_node);
529 data->rtc = devm_rtc_allocate_device(dev);
530 ret = PTR_ERR_OR_ZERO(data->rtc);
534 data->rtc->ops = &rtc_ops;
535 data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
536 data->rtc->range_max = RTC_TIMESTAMP_END_2099;
537 data->rtc->uie_unsupported = 1;
538 clear_bit(RTC_FEATURE_ALARM, data->rtc->features);
540 if (client->irq > 0) {
541 ret = devm_request_threaded_irq(dev, client->irq, NULL,
543 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
546 dev_err(dev, "failed to request alarm irq\n");
551 if (client->irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
552 ret = device_init_wakeup(dev, true);
553 set_bit(RTC_FEATURE_ALARM, data->rtc->features);
556 ret = devm_rtc_register_device(data->rtc);
560 abeoz9_hwmon_register(dev, data);
565 static const struct of_device_id abeoz9_dt_match[] = {
566 { .compatible = "abracon,abeoz9" },
569 MODULE_DEVICE_TABLE(of, abeoz9_dt_match);
572 static const struct i2c_device_id abeoz9_id[] = {
577 static struct i2c_driver abeoz9_driver = {
579 .name = "rtc-ab-eoz9",
580 .of_match_table = of_match_ptr(abeoz9_dt_match),
582 .probe = abeoz9_probe,
583 .id_table = abeoz9_id,
586 module_i2c_driver(abeoz9_driver);
588 MODULE_AUTHOR("Artem Panfilov <panfilov.artyom@gmail.com>");
589 MODULE_DESCRIPTION("Abracon AB-RTCMC-32.768kHz-EOZ9 RTC driver");
590 MODULE_LICENSE("GPL");