1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Avionic Design GmbH
8 #include <linux/module.h>
11 #include <linux/pm_wakeirq.h>
13 #define PCF8523_REG_CONTROL1 0x00
14 #define PCF8523_CONTROL1_CAP_SEL BIT(7)
15 #define PCF8523_CONTROL1_STOP BIT(5)
16 #define PCF8523_CONTROL1_AIE BIT(1)
18 #define PCF8523_REG_CONTROL2 0x01
19 #define PCF8523_CONTROL2_AF BIT(3)
21 #define PCF8523_REG_CONTROL3 0x02
22 #define PCF8523_CONTROL3_PM_BLD BIT(7) /* battery low detection disabled */
23 #define PCF8523_CONTROL3_PM_VDD BIT(6) /* switch-over disabled */
24 #define PCF8523_CONTROL3_PM_DSM BIT(5) /* direct switching mode */
25 #define PCF8523_CONTROL3_PM_MASK 0xe0
26 #define PCF8523_CONTROL3_BLF BIT(2) /* battery low bit, read-only */
28 #define PCF8523_REG_SECONDS 0x03
29 #define PCF8523_SECONDS_OS BIT(7)
31 #define PCF8523_REG_MINUTES 0x04
32 #define PCF8523_REG_HOURS 0x05
33 #define PCF8523_REG_DAYS 0x06
34 #define PCF8523_REG_WEEKDAYS 0x07
35 #define PCF8523_REG_MONTHS 0x08
36 #define PCF8523_REG_YEARS 0x09
38 #define PCF8523_REG_MINUTE_ALARM 0x0a
39 #define PCF8523_REG_HOUR_ALARM 0x0b
40 #define PCF8523_REG_DAY_ALARM 0x0c
41 #define PCF8523_REG_WEEKDAY_ALARM 0x0d
42 #define ALARM_DIS BIT(7)
44 #define PCF8523_REG_OFFSET 0x0e
45 #define PCF8523_OFFSET_MODE BIT(7)
47 #define PCF8523_TMR_CLKOUT_CTRL 0x0f
50 struct rtc_device *rtc;
51 struct i2c_client *client;
54 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
56 struct i2c_msg msgs[2];
60 msgs[0].addr = client->addr;
62 msgs[0].len = sizeof(reg);
65 msgs[1].addr = client->addr;
66 msgs[1].flags = I2C_M_RD;
67 msgs[1].len = sizeof(value);
70 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
79 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
81 u8 buffer[2] = { reg, value };
85 msg.addr = client->addr;
87 msg.len = sizeof(buffer);
90 err = i2c_transfer(client->adapter, &msg, 1);
97 static int pcf8523_voltage_low(struct i2c_client *client)
102 err = pcf8523_read(client, PCF8523_REG_CONTROL3, &value);
106 return !!(value & PCF8523_CONTROL3_BLF);
109 static int pcf8523_load_capacitance(struct i2c_client *client)
115 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
120 of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
125 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
129 value |= PCF8523_CONTROL1_CAP_SEL;
132 value &= ~PCF8523_CONTROL1_CAP_SEL;
136 err = pcf8523_write(client, PCF8523_REG_CONTROL1, value);
141 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
146 err = pcf8523_read(client, PCF8523_REG_CONTROL3, &value);
150 value = (value & ~PCF8523_CONTROL3_PM_MASK) | pm;
152 err = pcf8523_write(client, PCF8523_REG_CONTROL3, value);
159 static irqreturn_t pcf8523_irq(int irq, void *dev_id)
161 struct pcf8523 *pcf8523 = i2c_get_clientdata(dev_id);
165 err = pcf8523_read(pcf8523->client, PCF8523_REG_CONTROL2, &value);
169 if (value & PCF8523_CONTROL2_AF) {
170 value &= ~PCF8523_CONTROL2_AF;
171 pcf8523_write(pcf8523->client, PCF8523_REG_CONTROL2, value);
172 rtc_update_irq(pcf8523->rtc, 1, RTC_IRQF | RTC_AF);
180 static int pcf8523_stop_rtc(struct i2c_client *client)
185 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
189 value |= PCF8523_CONTROL1_STOP;
191 err = pcf8523_write(client, PCF8523_REG_CONTROL1, value);
198 static int pcf8523_start_rtc(struct i2c_client *client)
203 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
207 value &= ~PCF8523_CONTROL1_STOP;
209 err = pcf8523_write(client, PCF8523_REG_CONTROL1, value);
216 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
218 struct i2c_client *client = to_i2c_client(dev);
219 u8 start = PCF8523_REG_SECONDS, regs[7];
220 struct i2c_msg msgs[2];
223 err = pcf8523_voltage_low(client);
226 } else if (err > 0) {
227 dev_err(dev, "low voltage detected, time is unreliable\n");
231 msgs[0].addr = client->addr;
234 msgs[0].buf = &start;
236 msgs[1].addr = client->addr;
237 msgs[1].flags = I2C_M_RD;
238 msgs[1].len = sizeof(regs);
241 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
245 if (regs[0] & PCF8523_SECONDS_OS)
248 tm->tm_sec = bcd2bin(regs[0] & 0x7f);
249 tm->tm_min = bcd2bin(regs[1] & 0x7f);
250 tm->tm_hour = bcd2bin(regs[2] & 0x3f);
251 tm->tm_mday = bcd2bin(regs[3] & 0x3f);
252 tm->tm_wday = regs[4] & 0x7;
253 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
254 tm->tm_year = bcd2bin(regs[6]) + 100;
259 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
261 struct i2c_client *client = to_i2c_client(dev);
266 err = pcf8523_stop_rtc(client);
270 regs[0] = PCF8523_REG_SECONDS;
271 /* This will purposely overwrite PCF8523_SECONDS_OS */
272 regs[1] = bin2bcd(tm->tm_sec);
273 regs[2] = bin2bcd(tm->tm_min);
274 regs[3] = bin2bcd(tm->tm_hour);
275 regs[4] = bin2bcd(tm->tm_mday);
276 regs[5] = tm->tm_wday;
277 regs[6] = bin2bcd(tm->tm_mon + 1);
278 regs[7] = bin2bcd(tm->tm_year - 100);
280 msg.addr = client->addr;
282 msg.len = sizeof(regs);
285 err = i2c_transfer(client->adapter, &msg, 1);
288 * If the time cannot be set, restart the RTC anyway. Note
289 * that errors are ignored if the RTC cannot be started so
290 * that we have a chance to propagate the original error.
292 pcf8523_start_rtc(client);
296 return pcf8523_start_rtc(client);
299 static int pcf8523_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
301 struct i2c_client *client = to_i2c_client(dev);
302 u8 start = PCF8523_REG_MINUTE_ALARM, regs[4];
303 struct i2c_msg msgs[2];
307 msgs[0].addr = client->addr;
310 msgs[0].buf = &start;
312 msgs[1].addr = client->addr;
313 msgs[1].flags = I2C_M_RD;
314 msgs[1].len = sizeof(regs);
317 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
322 tm->time.tm_min = bcd2bin(regs[0] & 0x7F);
323 tm->time.tm_hour = bcd2bin(regs[1] & 0x3F);
324 tm->time.tm_mday = bcd2bin(regs[2] & 0x3F);
325 tm->time.tm_wday = bcd2bin(regs[3] & 0x7);
327 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
330 tm->enabled = !!(value & PCF8523_CONTROL1_AIE);
332 err = pcf8523_read(client, PCF8523_REG_CONTROL2, &value);
335 tm->pending = !!(value & PCF8523_CONTROL2_AF);
340 static int pcf8523_irq_enable(struct device *dev, unsigned int enabled)
342 struct i2c_client *client = to_i2c_client(dev);
346 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
350 value &= PCF8523_CONTROL1_AIE;
353 value |= PCF8523_CONTROL1_AIE;
355 err = pcf8523_write(client, PCF8523_REG_CONTROL1, value);
362 static int pcf8523_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
364 struct i2c_client *client = to_i2c_client(dev);
369 err = pcf8523_irq_enable(dev, 0);
373 err = pcf8523_write(client, PCF8523_REG_CONTROL2, 0);
377 /* The alarm has no seconds, round up to nearest minute */
378 if (tm->time.tm_sec) {
379 time64_t alarm_time = rtc_tm_to_time64(&tm->time);
381 alarm_time += 60 - tm->time.tm_sec;
382 rtc_time64_to_tm(alarm_time, &tm->time);
385 regs[0] = PCF8523_REG_MINUTE_ALARM;
386 regs[1] = bin2bcd(tm->time.tm_min);
387 regs[2] = bin2bcd(tm->time.tm_hour);
388 regs[3] = bin2bcd(tm->time.tm_mday);
390 msg.addr = client->addr;
392 msg.len = sizeof(regs);
394 err = i2c_transfer(client->adapter, &msg, 1);
399 return pcf8523_irq_enable(dev, tm->enabled);
404 #ifdef CONFIG_RTC_INTF_DEV
405 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
408 struct i2c_client *client = to_i2c_client(dev);
409 unsigned int flags = 0;
415 ret = pcf8523_voltage_low(client);
419 flags |= RTC_VL_BACKUP_LOW;
421 ret = pcf8523_read(client, PCF8523_REG_SECONDS, &value);
425 if (value & PCF8523_SECONDS_OS)
426 flags |= RTC_VL_DATA_INVALID;
428 return put_user(flags, (unsigned int __user *)arg);
435 #define pcf8523_rtc_ioctl NULL
438 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
440 struct i2c_client *client = to_i2c_client(dev);
445 err = pcf8523_read(client, PCF8523_REG_OFFSET, &value);
449 /* sign extend the 7-bit offset value */
451 *offset = (value & PCF8523_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
456 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
458 struct i2c_client *client = to_i2c_client(dev);
462 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
463 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
465 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
466 value = reg_m0 & 0x7f;
468 value = (reg_m1 & 0x7f) | PCF8523_OFFSET_MODE;
470 return pcf8523_write(client, PCF8523_REG_OFFSET, value);
473 static const struct rtc_class_ops pcf8523_rtc_ops = {
474 .read_time = pcf8523_rtc_read_time,
475 .set_time = pcf8523_rtc_set_time,
476 .read_alarm = pcf8523_rtc_read_alarm,
477 .set_alarm = pcf8523_rtc_set_alarm,
478 .alarm_irq_enable = pcf8523_irq_enable,
479 .ioctl = pcf8523_rtc_ioctl,
480 .read_offset = pcf8523_rtc_read_offset,
481 .set_offset = pcf8523_rtc_set_offset,
484 static int pcf8523_probe(struct i2c_client *client,
485 const struct i2c_device_id *id)
487 struct pcf8523 *pcf8523;
488 struct rtc_device *rtc;
489 bool wakeup_source = false;
492 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
495 pcf8523 = devm_kzalloc(&client->dev, sizeof(struct pcf8523), GFP_KERNEL);
499 i2c_set_clientdata(client, pcf8523);
500 pcf8523->client = client;
502 err = pcf8523_load_capacitance(client);
504 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
507 err = pcf8523_set_pm(client, 0);
511 rtc = devm_rtc_allocate_device(&client->dev);
516 rtc->ops = &pcf8523_rtc_ops;
517 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
518 rtc->range_max = RTC_TIMESTAMP_END_2099;
519 rtc->uie_unsupported = 1;
521 if (client->irq > 0) {
522 err = pcf8523_write(client, PCF8523_TMR_CLKOUT_CTRL, 0x38);
526 err = devm_request_threaded_irq(&client->dev, client->irq,
528 IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_LOW,
529 dev_name(&rtc->dev), client);
533 dev_pm_set_wake_irq(&client->dev, client->irq);
537 wakeup_source = of_property_read_bool(client->dev.of_node, "wakeup-source");
539 if (client->irq > 0 || wakeup_source)
540 device_init_wakeup(&client->dev, true);
542 return devm_rtc_register_device(rtc);
545 static const struct i2c_device_id pcf8523_id[] = {
549 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
552 static const struct of_device_id pcf8523_of_match[] = {
553 { .compatible = "nxp,pcf8523" },
554 { .compatible = "microcrystal,rv8523" },
557 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
560 static struct i2c_driver pcf8523_driver = {
562 .name = "rtc-pcf8523",
563 .of_match_table = of_match_ptr(pcf8523_of_match),
565 .probe = pcf8523_probe,
566 .id_table = pcf8523_id,
568 module_i2c_driver(pcf8523_driver);
571 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
572 MODULE_LICENSE("GPL v2");