2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
8 * based on the other drivers in this same directory.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/module.h>
20 * Information for this driver was pulled from the following datasheets.
22 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
23 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
25 * PCF85063A -- Rev. 6 — 18 November 2015
26 * PCF85063TP -- Rev. 4 — 6 May 2015
29 #define PCF85063_REG_CTRL1 0x00 /* status */
30 #define PCF85063_REG_CTRL1_CAP_SEL BIT(0)
31 #define PCF85063_REG_CTRL1_STOP BIT(5)
33 #define PCF85063_REG_SC 0x04 /* datetime */
34 #define PCF85063_REG_SC_OS 0x80
36 static struct i2c_driver pcf85063_driver
;
38 static int pcf85063_stop_clock(struct i2c_client
*client
, u8
*ctrl1
)
43 rc
= i2c_smbus_read_byte_data(client
, PCF85063_REG_CTRL1
);
45 dev_err(&client
->dev
, "Failing to stop the clock\n");
50 reg
= rc
| PCF85063_REG_CTRL1_STOP
;
52 rc
= i2c_smbus_write_byte_data(client
, PCF85063_REG_CTRL1
, reg
);
54 dev_err(&client
->dev
, "Failing to stop the clock\n");
63 static int pcf85063_start_clock(struct i2c_client
*client
, u8 ctrl1
)
68 ctrl1
&= ~PCF85063_REG_CTRL1_STOP
;
70 rc
= i2c_smbus_write_byte_data(client
, PCF85063_REG_CTRL1
, ctrl1
);
72 dev_err(&client
->dev
, "Failing to start the clock\n");
79 static int pcf85063_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
81 struct i2c_client
*client
= to_i2c_client(dev
);
86 * while reading, the time/date registers are blocked and not updated
87 * anymore until the access is finished. To not lose a second
88 * event, the access must be finished within one second. So, read all
89 * time/date registers in one turn.
91 rc
= i2c_smbus_read_i2c_block_data(client
, PCF85063_REG_SC
,
93 if (rc
!= sizeof(regs
)) {
94 dev_err(&client
->dev
, "date/time register read error\n");
98 /* if the clock has lost its power it makes no sense to use its time */
99 if (regs
[0] & PCF85063_REG_SC_OS
) {
100 dev_warn(&client
->dev
, "Power loss detected, invalid time\n");
104 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7F);
105 tm
->tm_min
= bcd2bin(regs
[1] & 0x7F);
106 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3F); /* rtc hr 0-23 */
107 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3F);
108 tm
->tm_wday
= regs
[4] & 0x07;
109 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1F) - 1; /* rtc mn 1-12 */
110 tm
->tm_year
= bcd2bin(regs
[6]);
116 static int pcf85063_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
118 struct i2c_client
*client
= to_i2c_client(dev
);
123 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 199))
127 * to accurately set the time, reset the divider chain and keep it in
128 * reset state until all time/date registers are written
130 rc
= pcf85063_stop_clock(client
, &ctrl1
);
134 /* hours, minutes and seconds */
135 regs
[0] = bin2bcd(tm
->tm_sec
) & 0x7F; /* clear OS flag */
137 regs
[1] = bin2bcd(tm
->tm_min
);
138 regs
[2] = bin2bcd(tm
->tm_hour
);
140 /* Day of month, 1 - 31 */
141 regs
[3] = bin2bcd(tm
->tm_mday
);
144 regs
[4] = tm
->tm_wday
& 0x07;
147 regs
[5] = bin2bcd(tm
->tm_mon
+ 1);
149 /* year and century */
150 regs
[6] = bin2bcd(tm
->tm_year
- 100);
152 /* write all registers at once */
153 rc
= i2c_smbus_write_i2c_block_data(client
, PCF85063_REG_SC
,
156 dev_err(&client
->dev
, "date/time register write error\n");
161 * Write the control register as a separate action since the size of
162 * the register space is different between the PCF85063TP and
163 * PCF85063A devices. The rollover point can not be used.
165 rc
= pcf85063_start_clock(client
, ctrl1
);
172 static const struct rtc_class_ops pcf85063_rtc_ops
= {
173 .read_time
= pcf85063_rtc_read_time
,
174 .set_time
= pcf85063_rtc_set_time
177 static int pcf85063_load_capacitance(struct i2c_client
*client
)
183 rc
= i2c_smbus_read_byte_data(client
, PCF85063_REG_CTRL1
);
189 of_property_read_u32(client
->dev
.of_node
, "quartz-load-femtofarads",
194 dev_warn(&client
->dev
, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
198 reg
&= ~PCF85063_REG_CTRL1_CAP_SEL
;
201 reg
|= PCF85063_REG_CTRL1_CAP_SEL
;
205 rc
= i2c_smbus_write_byte_data(client
, PCF85063_REG_CTRL1
, reg
);
210 static int pcf85063_probe(struct i2c_client
*client
,
211 const struct i2c_device_id
*id
)
213 struct rtc_device
*rtc
;
216 dev_dbg(&client
->dev
, "%s\n", __func__
);
218 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
221 err
= i2c_smbus_read_byte_data(client
, PCF85063_REG_CTRL1
);
223 dev_err(&client
->dev
, "RTC chip is not present\n");
227 err
= pcf85063_load_capacitance(client
);
229 dev_warn(&client
->dev
, "failed to set xtal load capacitance: %d",
232 rtc
= devm_rtc_device_register(&client
->dev
,
233 pcf85063_driver
.driver
.name
,
234 &pcf85063_rtc_ops
, THIS_MODULE
);
236 return PTR_ERR_OR_ZERO(rtc
);
239 static const struct i2c_device_id pcf85063_id
[] = {
243 MODULE_DEVICE_TABLE(i2c
, pcf85063_id
);
246 static const struct of_device_id pcf85063_of_match
[] = {
247 { .compatible
= "nxp,pcf85063" },
250 MODULE_DEVICE_TABLE(of
, pcf85063_of_match
);
253 static struct i2c_driver pcf85063_driver
= {
255 .name
= "rtc-pcf85063",
256 .of_match_table
= of_match_ptr(pcf85063_of_match
),
258 .probe
= pcf85063_probe
,
259 .id_table
= pcf85063_id
,
262 module_i2c_driver(pcf85063_driver
);
264 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
265 MODULE_DESCRIPTION("PCF85063 RTC driver");
266 MODULE_LICENSE("GPL");