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 REG_CONTROL1 0x00
14 #define REG_CONTROL1_CAP_SEL BIT(7)
15 #define REG_CONTROL1_STOP BIT(5)
16 #define REG_CONTROL1_AIE BIT(1)
18 #define REG_CONTROL2 0x01
19 #define REG_CONTROL2_AF BIT(3)
21 #define REG_CONTROL3 0x02
22 #define REG_CONTROL3_PM_BLD BIT(7) /* battery low detection disabled */
23 #define REG_CONTROL3_PM_VDD BIT(6) /* switch-over disabled */
24 #define REG_CONTROL3_PM_DSM BIT(5) /* direct switching mode */
25 #define REG_CONTROL3_PM_MASK 0xe0
26 #define REG_CONTROL3_BLF BIT(2) /* battery low bit, read-only */
28 #define REG_SECONDS 0x03
29 #define REG_SECONDS_OS BIT(7)
31 #define REG_MINUTES 0x04
32 #define REG_HOURS 0x05
34 #define REG_WEEKDAYS 0x07
35 #define REG_MONTHS 0x08
36 #define REG_YEARS 0x09
38 #define REG_MINUTE_ALARM 0x0a
39 #define REG_HOUR_ALARM 0x0b
40 #define REG_DAY_ALARM 0x0c
41 #define REG_WEEKDAY_ALARM 0x0d
42 #define ALARM_DIS BIT(7)
44 #define REG_OFFSET 0x0e
45 #define REG_OFFSET_MODE BIT(7)
47 #define REG_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, REG_CONTROL3, &value);
106 return !!(value & REG_CONTROL3_BLF);
109 static int pcf8523_load_capacitance(struct i2c_client *client)
115 err = pcf8523_read(client, 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 |= REG_CONTROL1_CAP_SEL;
132 value &= ~REG_CONTROL1_CAP_SEL;
136 err = pcf8523_write(client, REG_CONTROL1, value);
141 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
146 err = pcf8523_read(client, REG_CONTROL3, &value);
150 value = (value & ~REG_CONTROL3_PM_MASK) | pm;
152 err = pcf8523_write(client, 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, REG_CONTROL2, &value);
169 if (value & REG_CONTROL2_AF) {
170 value &= ~REG_CONTROL2_AF;
171 pcf8523_write(pcf8523->client, 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, REG_CONTROL1, &value);
189 value |= REG_CONTROL1_STOP;
191 err = pcf8523_write(client, REG_CONTROL1, value);
198 static int pcf8523_start_rtc(struct i2c_client *client)
203 err = pcf8523_read(client, REG_CONTROL1, &value);
207 value &= ~REG_CONTROL1_STOP;
209 err = pcf8523_write(client, 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 = 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] & REG_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] = REG_SECONDS;
271 /* This will purposely overwrite REG_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 = 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, REG_CONTROL1, &value);
330 tm->enabled = !!(value & REG_CONTROL1_AIE);
332 err = pcf8523_read(client, REG_CONTROL2, &value);
335 tm->pending = !!(value & REG_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, REG_CONTROL1, &value);
350 value &= REG_CONTROL1_AIE;
353 value |= REG_CONTROL1_AIE;
355 err = pcf8523_write(client, 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, 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] = 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);
413 ret = pcf8523_voltage_low(client);
417 ret = RTC_VL_BACKUP_LOW;
419 return put_user(ret, (unsigned int __user *)arg);
426 #define pcf8523_rtc_ioctl NULL
429 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
431 struct i2c_client *client = to_i2c_client(dev);
436 err = pcf8523_read(client, REG_OFFSET, &value);
440 /* sign extend the 7-bit offset value */
442 *offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
447 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
449 struct i2c_client *client = to_i2c_client(dev);
453 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
454 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
456 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
457 value = reg_m0 & 0x7f;
459 value = (reg_m1 & 0x7f) | REG_OFFSET_MODE;
461 return pcf8523_write(client, REG_OFFSET, value);
464 static const struct rtc_class_ops pcf8523_rtc_ops = {
465 .read_time = pcf8523_rtc_read_time,
466 .set_time = pcf8523_rtc_set_time,
467 .read_alarm = pcf8523_rtc_read_alarm,
468 .set_alarm = pcf8523_rtc_set_alarm,
469 .alarm_irq_enable = pcf8523_irq_enable,
470 .ioctl = pcf8523_rtc_ioctl,
471 .read_offset = pcf8523_rtc_read_offset,
472 .set_offset = pcf8523_rtc_set_offset,
475 static int pcf8523_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
478 struct pcf8523 *pcf8523;
479 struct rtc_device *rtc;
480 bool wakeup_source = false;
483 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
486 pcf8523 = devm_kzalloc(&client->dev, sizeof(struct pcf8523), GFP_KERNEL);
490 i2c_set_clientdata(client, pcf8523);
491 pcf8523->client = client;
493 err = pcf8523_load_capacitance(client);
495 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
498 err = pcf8523_set_pm(client, 0);
502 rtc = devm_rtc_allocate_device(&client->dev);
507 rtc->ops = &pcf8523_rtc_ops;
508 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
509 rtc->range_max = RTC_TIMESTAMP_END_2099;
510 rtc->uie_unsupported = 1;
512 if (client->irq > 0) {
513 err = pcf8523_write(client, REG_TMR_CLKOUT_CTRL, 0x38);
517 err = devm_request_threaded_irq(&client->dev, client->irq,
519 IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_LOW,
520 dev_name(&rtc->dev), client);
524 dev_pm_set_wake_irq(&client->dev, client->irq);
528 wakeup_source = of_property_read_bool(client->dev.of_node, "wakeup-source");
530 if (client->irq > 0 || wakeup_source)
531 device_init_wakeup(&client->dev, true);
533 return devm_rtc_register_device(rtc);
536 static const struct i2c_device_id pcf8523_id[] = {
540 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
543 static const struct of_device_id pcf8523_of_match[] = {
544 { .compatible = "nxp,pcf8523" },
545 { .compatible = "microcrystal,rv8523" },
548 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
551 static struct i2c_driver pcf8523_driver = {
553 .name = "rtc-pcf8523",
554 .of_match_table = of_match_ptr(pcf8523_of_match),
556 .probe = pcf8523_probe,
557 .id_table = pcf8523_id,
559 module_i2c_driver(pcf8523_driver);
561 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
562 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
563 MODULE_LICENSE("GPL v2");