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) {
247 * If the oscillator was stopped, try to clear the flag. Upon
248 * power-up the flag is always set, but if we cannot clear it
249 * the oscillator isn't running properly for some reason. The
250 * sensible thing therefore is to return an error, signalling
251 * that the clock cannot be assumed to be correct.
254 regs[0] &= ~PCF8523_SECONDS_OS;
256 err = pcf8523_write(client, PCF8523_REG_SECONDS, regs[0]);
260 err = pcf8523_read(client, PCF8523_REG_SECONDS, ®s[0]);
264 if (regs[0] & PCF8523_SECONDS_OS)
268 tm->tm_sec = bcd2bin(regs[0] & 0x7f);
269 tm->tm_min = bcd2bin(regs[1] & 0x7f);
270 tm->tm_hour = bcd2bin(regs[2] & 0x3f);
271 tm->tm_mday = bcd2bin(regs[3] & 0x3f);
272 tm->tm_wday = regs[4] & 0x7;
273 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
274 tm->tm_year = bcd2bin(regs[6]) + 100;
279 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
281 struct i2c_client *client = to_i2c_client(dev);
286 err = pcf8523_stop_rtc(client);
290 regs[0] = PCF8523_REG_SECONDS;
291 /* This will purposely overwrite PCF8523_SECONDS_OS */
292 regs[1] = bin2bcd(tm->tm_sec);
293 regs[2] = bin2bcd(tm->tm_min);
294 regs[3] = bin2bcd(tm->tm_hour);
295 regs[4] = bin2bcd(tm->tm_mday);
296 regs[5] = tm->tm_wday;
297 regs[6] = bin2bcd(tm->tm_mon + 1);
298 regs[7] = bin2bcd(tm->tm_year - 100);
300 msg.addr = client->addr;
302 msg.len = sizeof(regs);
305 err = i2c_transfer(client->adapter, &msg, 1);
308 * If the time cannot be set, restart the RTC anyway. Note
309 * that errors are ignored if the RTC cannot be started so
310 * that we have a chance to propagate the original error.
312 pcf8523_start_rtc(client);
316 return pcf8523_start_rtc(client);
319 static int pcf8523_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
321 struct i2c_client *client = to_i2c_client(dev);
322 u8 start = PCF8523_REG_MINUTE_ALARM, regs[4];
323 struct i2c_msg msgs[2];
327 msgs[0].addr = client->addr;
330 msgs[0].buf = &start;
332 msgs[1].addr = client->addr;
333 msgs[1].flags = I2C_M_RD;
334 msgs[1].len = sizeof(regs);
337 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
342 tm->time.tm_min = bcd2bin(regs[0] & 0x7F);
343 tm->time.tm_hour = bcd2bin(regs[1] & 0x3F);
344 tm->time.tm_mday = bcd2bin(regs[2] & 0x3F);
345 tm->time.tm_wday = bcd2bin(regs[3] & 0x7);
347 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
350 tm->enabled = !!(value & PCF8523_CONTROL1_AIE);
352 err = pcf8523_read(client, PCF8523_REG_CONTROL2, &value);
355 tm->pending = !!(value & PCF8523_CONTROL2_AF);
360 static int pcf8523_irq_enable(struct device *dev, unsigned int enabled)
362 struct i2c_client *client = to_i2c_client(dev);
366 err = pcf8523_read(client, PCF8523_REG_CONTROL1, &value);
370 value &= PCF8523_CONTROL1_AIE;
373 value |= PCF8523_CONTROL1_AIE;
375 err = pcf8523_write(client, PCF8523_REG_CONTROL1, value);
382 static int pcf8523_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
384 struct i2c_client *client = to_i2c_client(dev);
389 err = pcf8523_irq_enable(dev, 0);
393 err = pcf8523_write(client, PCF8523_REG_CONTROL2, 0);
397 /* The alarm has no seconds, round up to nearest minute */
398 if (tm->time.tm_sec) {
399 time64_t alarm_time = rtc_tm_to_time64(&tm->time);
401 alarm_time += 60 - tm->time.tm_sec;
402 rtc_time64_to_tm(alarm_time, &tm->time);
405 regs[0] = PCF8523_REG_MINUTE_ALARM;
406 regs[1] = bin2bcd(tm->time.tm_min);
407 regs[2] = bin2bcd(tm->time.tm_hour);
408 regs[3] = bin2bcd(tm->time.tm_mday);
410 msg.addr = client->addr;
412 msg.len = sizeof(regs);
414 err = i2c_transfer(client->adapter, &msg, 1);
419 return pcf8523_irq_enable(dev, tm->enabled);
424 #ifdef CONFIG_RTC_INTF_DEV
425 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
428 struct i2c_client *client = to_i2c_client(dev);
429 unsigned int flags = 0;
435 ret = pcf8523_voltage_low(client);
439 flags |= RTC_VL_BACKUP_LOW;
441 ret = pcf8523_read(client, PCF8523_REG_SECONDS, &value);
445 if (value & PCF8523_SECONDS_OS)
446 flags |= RTC_VL_DATA_INVALID;
448 return put_user(flags, (unsigned int __user *)arg);
455 #define pcf8523_rtc_ioctl NULL
458 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
460 struct i2c_client *client = to_i2c_client(dev);
465 err = pcf8523_read(client, PCF8523_REG_OFFSET, &value);
469 /* sign extend the 7-bit offset value */
471 *offset = (value & PCF8523_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
476 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
478 struct i2c_client *client = to_i2c_client(dev);
482 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
483 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
485 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
486 value = reg_m0 & 0x7f;
488 value = (reg_m1 & 0x7f) | PCF8523_OFFSET_MODE;
490 return pcf8523_write(client, PCF8523_REG_OFFSET, value);
493 static const struct rtc_class_ops pcf8523_rtc_ops = {
494 .read_time = pcf8523_rtc_read_time,
495 .set_time = pcf8523_rtc_set_time,
496 .read_alarm = pcf8523_rtc_read_alarm,
497 .set_alarm = pcf8523_rtc_set_alarm,
498 .alarm_irq_enable = pcf8523_irq_enable,
499 .ioctl = pcf8523_rtc_ioctl,
500 .read_offset = pcf8523_rtc_read_offset,
501 .set_offset = pcf8523_rtc_set_offset,
504 static int pcf8523_probe(struct i2c_client *client,
505 const struct i2c_device_id *id)
507 struct pcf8523 *pcf8523;
508 struct rtc_device *rtc;
509 bool wakeup_source = false;
512 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
515 pcf8523 = devm_kzalloc(&client->dev, sizeof(struct pcf8523), GFP_KERNEL);
519 i2c_set_clientdata(client, pcf8523);
520 pcf8523->client = client;
522 err = pcf8523_load_capacitance(client);
524 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
527 err = pcf8523_set_pm(client, 0);
531 rtc = devm_rtc_allocate_device(&client->dev);
536 rtc->ops = &pcf8523_rtc_ops;
537 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
538 rtc->range_max = RTC_TIMESTAMP_END_2099;
539 rtc->uie_unsupported = 1;
541 if (client->irq > 0) {
542 err = pcf8523_write(client, PCF8523_TMR_CLKOUT_CTRL, 0x38);
546 err = devm_request_threaded_irq(&client->dev, client->irq,
548 IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_LOW,
549 dev_name(&rtc->dev), client);
553 dev_pm_set_wake_irq(&client->dev, client->irq);
557 wakeup_source = of_property_read_bool(client->dev.of_node, "wakeup-source");
559 if (client->irq > 0 || wakeup_source)
560 device_init_wakeup(&client->dev, true);
562 return devm_rtc_register_device(rtc);
565 static const struct i2c_device_id pcf8523_id[] = {
569 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
572 static const struct of_device_id pcf8523_of_match[] = {
573 { .compatible = "nxp,pcf8523" },
574 { .compatible = "microcrystal,rv8523" },
577 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
580 static struct i2c_driver pcf8523_driver = {
582 .name = "rtc-pcf8523",
583 .of_match_table = of_match_ptr(pcf8523_of_match),
585 .probe = pcf8523_probe,
586 .id_table = pcf8523_id,
588 module_i2c_driver(pcf8523_driver);
590 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
591 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
592 MODULE_LICENSE("GPL v2");