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)
38 static int pcf8523_read(struct i2c_client
*client
, u8 reg
, u8
*valuep
)
40 struct i2c_msg msgs
[2];
44 msgs
[0].addr
= client
->addr
;
46 msgs
[0].len
= sizeof(reg
);
49 msgs
[1].addr
= client
->addr
;
50 msgs
[1].flags
= I2C_M_RD
;
51 msgs
[1].len
= sizeof(value
);
54 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
63 static int pcf8523_write(struct i2c_client
*client
, u8 reg
, u8 value
)
65 u8 buffer
[2] = { reg
, value
};
69 msg
.addr
= client
->addr
;
71 msg
.len
= sizeof(buffer
);
74 err
= i2c_transfer(client
->adapter
, &msg
, 1);
81 static int pcf8523_voltage_low(struct i2c_client
*client
)
86 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
90 return !!(value
& REG_CONTROL3_BLF
);
93 static int pcf8523_load_capacitance(struct i2c_client
*client
)
99 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
104 of_property_read_u32(client
->dev
.of_node
, "quartz-load-femtofarads",
109 dev_warn(&client
->dev
, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
113 value
|= REG_CONTROL1_CAP_SEL
;
116 value
&= ~REG_CONTROL1_CAP_SEL
;
120 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
125 static int pcf8523_set_pm(struct i2c_client
*client
, u8 pm
)
130 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
134 value
= (value
& ~REG_CONTROL3_PM_MASK
) | pm
;
136 err
= pcf8523_write(client
, REG_CONTROL3
, value
);
143 static int pcf8523_stop_rtc(struct i2c_client
*client
)
148 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
152 value
|= REG_CONTROL1_STOP
;
154 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
161 static int pcf8523_start_rtc(struct i2c_client
*client
)
166 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
170 value
&= ~REG_CONTROL1_STOP
;
172 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
179 static int pcf8523_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
181 struct i2c_client
*client
= to_i2c_client(dev
);
182 u8 start
= REG_SECONDS
, regs
[7];
183 struct i2c_msg msgs
[2];
186 err
= pcf8523_voltage_low(client
);
189 } else if (err
> 0) {
190 dev_err(dev
, "low voltage detected, time is unreliable\n");
194 msgs
[0].addr
= client
->addr
;
197 msgs
[0].buf
= &start
;
199 msgs
[1].addr
= client
->addr
;
200 msgs
[1].flags
= I2C_M_RD
;
201 msgs
[1].len
= sizeof(regs
);
204 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
208 if (regs
[0] & REG_SECONDS_OS
)
211 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7f);
212 tm
->tm_min
= bcd2bin(regs
[1] & 0x7f);
213 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
214 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3f);
215 tm
->tm_wday
= regs
[4] & 0x7;
216 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1f) - 1;
217 tm
->tm_year
= bcd2bin(regs
[6]) + 100;
222 static int pcf8523_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
224 struct i2c_client
*client
= to_i2c_client(dev
);
230 * The hardware can only store values between 0 and 99 in it's YEAR
231 * register (with 99 overflowing to 0 on increment).
232 * After 2100-02-28 we could start interpreting the year to be in the
233 * interval [2100, 2199], but there is no path to switch in a smooth way
234 * because the chip handles YEAR=0x00 (and the out-of-spec
235 * YEAR=0xa0) as a leap year, but 2100 isn't.
237 if (tm
->tm_year
< 100 || tm
->tm_year
>= 200)
240 err
= pcf8523_stop_rtc(client
);
244 regs
[0] = REG_SECONDS
;
245 /* This will purposely overwrite REG_SECONDS_OS */
246 regs
[1] = bin2bcd(tm
->tm_sec
);
247 regs
[2] = bin2bcd(tm
->tm_min
);
248 regs
[3] = bin2bcd(tm
->tm_hour
);
249 regs
[4] = bin2bcd(tm
->tm_mday
);
250 regs
[5] = tm
->tm_wday
;
251 regs
[6] = bin2bcd(tm
->tm_mon
+ 1);
252 regs
[7] = bin2bcd(tm
->tm_year
- 100);
254 msg
.addr
= client
->addr
;
256 msg
.len
= sizeof(regs
);
259 err
= i2c_transfer(client
->adapter
, &msg
, 1);
262 * If the time cannot be set, restart the RTC anyway. Note
263 * that errors are ignored if the RTC cannot be started so
264 * that we have a chance to propagate the original error.
266 pcf8523_start_rtc(client
);
270 return pcf8523_start_rtc(client
);
273 #ifdef CONFIG_RTC_INTF_DEV
274 static int pcf8523_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
277 struct i2c_client
*client
= to_i2c_client(dev
);
282 ret
= pcf8523_voltage_low(client
);
286 if (copy_to_user((void __user
*)arg
, &ret
, sizeof(int)))
295 #define pcf8523_rtc_ioctl NULL
298 static int pcf8523_rtc_read_offset(struct device
*dev
, long *offset
)
300 struct i2c_client
*client
= to_i2c_client(dev
);
305 err
= pcf8523_read(client
, REG_OFFSET
, &value
);
309 /* sign extend the 7-bit offset value */
311 *offset
= (value
& REG_OFFSET_MODE
? 4069 : 4340) * (val
>> 1);
316 static int pcf8523_rtc_set_offset(struct device
*dev
, long offset
)
318 struct i2c_client
*client
= to_i2c_client(dev
);
322 reg_m0
= clamp(DIV_ROUND_CLOSEST(offset
, 4340), -64L, 63L);
323 reg_m1
= clamp(DIV_ROUND_CLOSEST(offset
, 4069), -64L, 63L);
325 if (abs(reg_m0
* 4340 - offset
) < abs(reg_m1
* 4069 - offset
))
326 value
= reg_m0
& 0x7f;
328 value
= (reg_m1
& 0x7f) | REG_OFFSET_MODE
;
330 return pcf8523_write(client
, REG_OFFSET
, value
);
333 static const struct rtc_class_ops pcf8523_rtc_ops
= {
334 .read_time
= pcf8523_rtc_read_time
,
335 .set_time
= pcf8523_rtc_set_time
,
336 .ioctl
= pcf8523_rtc_ioctl
,
337 .read_offset
= pcf8523_rtc_read_offset
,
338 .set_offset
= pcf8523_rtc_set_offset
,
341 static int pcf8523_probe(struct i2c_client
*client
,
342 const struct i2c_device_id
*id
)
344 struct rtc_device
*rtc
;
347 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
350 err
= pcf8523_load_capacitance(client
);
352 dev_warn(&client
->dev
, "failed to set xtal load capacitance: %d",
355 err
= pcf8523_set_pm(client
, 0);
359 rtc
= devm_rtc_device_register(&client
->dev
, DRIVER_NAME
,
360 &pcf8523_rtc_ops
, THIS_MODULE
);
367 static const struct i2c_device_id pcf8523_id
[] = {
371 MODULE_DEVICE_TABLE(i2c
, pcf8523_id
);
374 static const struct of_device_id pcf8523_of_match
[] = {
375 { .compatible
= "nxp,pcf8523" },
376 { .compatible
= "microcrystal,rv8523" },
379 MODULE_DEVICE_TABLE(of
, pcf8523_of_match
);
382 static struct i2c_driver pcf8523_driver
= {
385 .of_match_table
= of_match_ptr(pcf8523_of_match
),
387 .probe
= pcf8523_probe
,
388 .id_table
= pcf8523_id
,
390 module_i2c_driver(pcf8523_driver
);
392 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
393 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
394 MODULE_LICENSE("GPL v2");