1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Avionic Design GmbH
8 #include <linux/module.h>
12 #define DRIVER_NAME "rtc-pcf8523"
14 #define REG_CONTROL1 0x00
15 #define REG_CONTROL1_CAP_SEL (1 << 7)
16 #define REG_CONTROL1_STOP (1 << 5)
18 #define REG_CONTROL3 0x02
19 #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
20 #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
21 #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
22 #define REG_CONTROL3_PM_MASK 0xe0
23 #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
25 #define REG_SECONDS 0x03
26 #define REG_SECONDS_OS (1 << 7)
28 #define REG_MINUTES 0x04
29 #define REG_HOURS 0x05
31 #define REG_WEEKDAYS 0x07
32 #define REG_MONTHS 0x08
33 #define REG_YEARS 0x09
35 #define REG_OFFSET 0x0e
36 #define REG_OFFSET_MODE BIT(7)
39 struct rtc_device
*rtc
;
42 static int pcf8523_read(struct i2c_client
*client
, u8 reg
, u8
*valuep
)
44 struct i2c_msg msgs
[2];
48 msgs
[0].addr
= client
->addr
;
50 msgs
[0].len
= sizeof(reg
);
53 msgs
[1].addr
= client
->addr
;
54 msgs
[1].flags
= I2C_M_RD
;
55 msgs
[1].len
= sizeof(value
);
58 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
67 static int pcf8523_write(struct i2c_client
*client
, u8 reg
, u8 value
)
69 u8 buffer
[2] = { reg
, value
};
73 msg
.addr
= client
->addr
;
75 msg
.len
= sizeof(buffer
);
78 err
= i2c_transfer(client
->adapter
, &msg
, 1);
85 static int pcf8523_voltage_low(struct i2c_client
*client
)
90 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
94 return !!(value
& REG_CONTROL3_BLF
);
97 static int pcf8523_load_capacitance(struct i2c_client
*client
)
103 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
108 of_property_read_u32(client
->dev
.of_node
, "quartz-load-femtofarads",
113 dev_warn(&client
->dev
, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
117 value
|= REG_CONTROL1_CAP_SEL
;
120 value
&= ~REG_CONTROL1_CAP_SEL
;
124 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
129 static int pcf8523_set_pm(struct i2c_client
*client
, u8 pm
)
134 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
138 value
= (value
& ~REG_CONTROL3_PM_MASK
) | pm
;
140 err
= pcf8523_write(client
, REG_CONTROL3
, value
);
147 static int pcf8523_stop_rtc(struct i2c_client
*client
)
152 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
156 value
|= REG_CONTROL1_STOP
;
158 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
165 static int pcf8523_start_rtc(struct i2c_client
*client
)
170 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
174 value
&= ~REG_CONTROL1_STOP
;
176 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
183 static int pcf8523_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
185 struct i2c_client
*client
= to_i2c_client(dev
);
186 u8 start
= REG_SECONDS
, regs
[7];
187 struct i2c_msg msgs
[2];
190 err
= pcf8523_voltage_low(client
);
193 } else if (err
> 0) {
194 dev_err(dev
, "low voltage detected, time is unreliable\n");
198 msgs
[0].addr
= client
->addr
;
201 msgs
[0].buf
= &start
;
203 msgs
[1].addr
= client
->addr
;
204 msgs
[1].flags
= I2C_M_RD
;
205 msgs
[1].len
= sizeof(regs
);
208 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
212 if (regs
[0] & REG_SECONDS_OS
)
215 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7f);
216 tm
->tm_min
= bcd2bin(regs
[1] & 0x7f);
217 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
218 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3f);
219 tm
->tm_wday
= regs
[4] & 0x7;
220 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1f) - 1;
221 tm
->tm_year
= bcd2bin(regs
[6]) + 100;
226 static int pcf8523_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
228 struct i2c_client
*client
= to_i2c_client(dev
);
234 * The hardware can only store values between 0 and 99 in it's YEAR
235 * register (with 99 overflowing to 0 on increment).
236 * After 2100-02-28 we could start interpreting the year to be in the
237 * interval [2100, 2199], but there is no path to switch in a smooth way
238 * because the chip handles YEAR=0x00 (and the out-of-spec
239 * YEAR=0xa0) as a leap year, but 2100 isn't.
241 if (tm
->tm_year
< 100 || tm
->tm_year
>= 200)
244 err
= pcf8523_stop_rtc(client
);
248 regs
[0] = REG_SECONDS
;
249 /* This will purposely overwrite REG_SECONDS_OS */
250 regs
[1] = bin2bcd(tm
->tm_sec
);
251 regs
[2] = bin2bcd(tm
->tm_min
);
252 regs
[3] = bin2bcd(tm
->tm_hour
);
253 regs
[4] = bin2bcd(tm
->tm_mday
);
254 regs
[5] = tm
->tm_wday
;
255 regs
[6] = bin2bcd(tm
->tm_mon
+ 1);
256 regs
[7] = bin2bcd(tm
->tm_year
- 100);
258 msg
.addr
= client
->addr
;
260 msg
.len
= sizeof(regs
);
263 err
= i2c_transfer(client
->adapter
, &msg
, 1);
266 * If the time cannot be set, restart the RTC anyway. Note
267 * that errors are ignored if the RTC cannot be started so
268 * that we have a chance to propagate the original error.
270 pcf8523_start_rtc(client
);
274 return pcf8523_start_rtc(client
);
277 #ifdef CONFIG_RTC_INTF_DEV
278 static int pcf8523_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
281 struct i2c_client
*client
= to_i2c_client(dev
);
286 ret
= pcf8523_voltage_low(client
);
290 if (copy_to_user((void __user
*)arg
, &ret
, sizeof(int)))
299 #define pcf8523_rtc_ioctl NULL
302 static int pcf8523_rtc_read_offset(struct device
*dev
, long *offset
)
304 struct i2c_client
*client
= to_i2c_client(dev
);
309 err
= pcf8523_read(client
, REG_OFFSET
, &value
);
313 /* sign extend the 7-bit offset value */
315 *offset
= (value
& REG_OFFSET_MODE
? 4069 : 4340) * (val
>> 1);
320 static int pcf8523_rtc_set_offset(struct device
*dev
, long offset
)
322 struct i2c_client
*client
= to_i2c_client(dev
);
326 reg_m0
= clamp(DIV_ROUND_CLOSEST(offset
, 4340), -64L, 63L);
327 reg_m1
= clamp(DIV_ROUND_CLOSEST(offset
, 4069), -64L, 63L);
329 if (abs(reg_m0
* 4340 - offset
) < abs(reg_m1
* 4069 - offset
))
330 value
= reg_m0
& 0x7f;
332 value
= (reg_m1
& 0x7f) | REG_OFFSET_MODE
;
334 return pcf8523_write(client
, REG_OFFSET
, value
);
337 static const struct rtc_class_ops pcf8523_rtc_ops
= {
338 .read_time
= pcf8523_rtc_read_time
,
339 .set_time
= pcf8523_rtc_set_time
,
340 .ioctl
= pcf8523_rtc_ioctl
,
341 .read_offset
= pcf8523_rtc_read_offset
,
342 .set_offset
= pcf8523_rtc_set_offset
,
345 static int pcf8523_probe(struct i2c_client
*client
,
346 const struct i2c_device_id
*id
)
351 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
354 pcf
= devm_kzalloc(&client
->dev
, sizeof(*pcf
), GFP_KERNEL
);
358 err
= pcf8523_load_capacitance(client
);
360 dev_warn(&client
->dev
, "failed to set xtal load capacitance: %d",
363 err
= pcf8523_set_pm(client
, 0);
367 pcf
->rtc
= devm_rtc_device_register(&client
->dev
, DRIVER_NAME
,
368 &pcf8523_rtc_ops
, THIS_MODULE
);
369 if (IS_ERR(pcf
->rtc
))
370 return PTR_ERR(pcf
->rtc
);
372 i2c_set_clientdata(client
, pcf
);
377 static const struct i2c_device_id pcf8523_id
[] = {
381 MODULE_DEVICE_TABLE(i2c
, pcf8523_id
);
384 static const struct of_device_id pcf8523_of_match
[] = {
385 { .compatible
= "nxp,pcf8523" },
386 { .compatible
= "microcrystal,rv8523" },
389 MODULE_DEVICE_TABLE(of
, pcf8523_of_match
);
392 static struct i2c_driver pcf8523_driver
= {
395 .of_match_table
= of_match_ptr(pcf8523_of_match
),
397 .probe
= pcf8523_probe
,
398 .id_table
= pcf8523_id
,
400 module_i2c_driver(pcf8523_driver
);
402 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
403 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
404 MODULE_LICENSE("GPL v2");