1 // SPDX-License-Identifier: GPL-2.0
3 * An I2C driver for the PCF85063 RTC
4 * Copyright 2014 Rose Technology
6 * Author: Søren Andersen <san@rosetechnology.dk>
7 * Maintainers: http://www.nslu2-linux.org/
10 #include <linux/bcd.h>
11 #include <linux/rtc.h>
12 #include <linux/module.h>
15 * Information for this driver was pulled from the following datasheets.
17 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
18 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
20 * PCF85063A -- Rev. 6 — 18 November 2015
21 * PCF85063TP -- Rev. 4 — 6 May 2015
24 #define PCF85063_REG_CTRL1 0x00 /* status */
25 #define PCF85063_REG_CTRL1_CAP_SEL BIT(0)
26 #define PCF85063_REG_CTRL1_STOP BIT(5)
28 #define PCF85063_REG_SC 0x04 /* datetime */
29 #define PCF85063_REG_SC_OS 0x80
31 static struct i2c_driver pcf85063_driver;
33 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
38 rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
40 dev_err(&client->dev, "Failing to stop the clock\n");
45 reg = rc | PCF85063_REG_CTRL1_STOP;
47 rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
49 dev_err(&client->dev, "Failing to stop the clock\n");
58 static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
63 ctrl1 &= ~PCF85063_REG_CTRL1_STOP;
65 rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
67 dev_err(&client->dev, "Failing to start the clock\n");
74 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
76 struct i2c_client *client = to_i2c_client(dev);
81 * while reading, the time/date registers are blocked and not updated
82 * anymore until the access is finished. To not lose a second
83 * event, the access must be finished within one second. So, read all
84 * time/date registers in one turn.
86 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
88 if (rc != sizeof(regs)) {
89 dev_err(&client->dev, "date/time register read error\n");
93 /* if the clock has lost its power it makes no sense to use its time */
94 if (regs[0] & PCF85063_REG_SC_OS) {
95 dev_warn(&client->dev, "Power loss detected, invalid time\n");
99 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
100 tm->tm_min = bcd2bin(regs[1] & 0x7F);
101 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
102 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
103 tm->tm_wday = regs[4] & 0x07;
104 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
105 tm->tm_year = bcd2bin(regs[6]);
111 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
113 struct i2c_client *client = to_i2c_client(dev);
118 if ((tm->tm_year < 100) || (tm->tm_year > 199))
122 * to accurately set the time, reset the divider chain and keep it in
123 * reset state until all time/date registers are written
125 rc = pcf85063_stop_clock(client, &ctrl1);
129 /* hours, minutes and seconds */
130 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
132 regs[1] = bin2bcd(tm->tm_min);
133 regs[2] = bin2bcd(tm->tm_hour);
135 /* Day of month, 1 - 31 */
136 regs[3] = bin2bcd(tm->tm_mday);
139 regs[4] = tm->tm_wday & 0x07;
142 regs[5] = bin2bcd(tm->tm_mon + 1);
144 /* year and century */
145 regs[6] = bin2bcd(tm->tm_year - 100);
147 /* write all registers at once */
148 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
151 dev_err(&client->dev, "date/time register write error\n");
156 * Write the control register as a separate action since the size of
157 * the register space is different between the PCF85063TP and
158 * PCF85063A devices. The rollover point can not be used.
160 rc = pcf85063_start_clock(client, ctrl1);
167 static const struct rtc_class_ops pcf85063_rtc_ops = {
168 .read_time = pcf85063_rtc_read_time,
169 .set_time = pcf85063_rtc_set_time
172 static int pcf85063_load_capacitance(struct i2c_client *client)
178 rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
184 of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
189 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
193 reg &= ~PCF85063_REG_CTRL1_CAP_SEL;
196 reg |= PCF85063_REG_CTRL1_CAP_SEL;
200 rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
205 static int pcf85063_probe(struct i2c_client *client)
207 struct rtc_device *rtc;
210 dev_dbg(&client->dev, "%s\n", __func__);
212 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
215 err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
217 dev_err(&client->dev, "RTC chip is not present\n");
221 err = pcf85063_load_capacitance(client);
223 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
226 rtc = devm_rtc_device_register(&client->dev,
227 pcf85063_driver.driver.name,
228 &pcf85063_rtc_ops, THIS_MODULE);
230 return PTR_ERR_OR_ZERO(rtc);
234 static const struct of_device_id pcf85063_of_match[] = {
235 { .compatible = "nxp,pcf85063" },
238 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
241 static struct i2c_driver pcf85063_driver = {
243 .name = "rtc-pcf85063",
244 .of_match_table = of_match_ptr(pcf85063_of_match),
246 .probe_new = pcf85063_probe,
249 module_i2c_driver(pcf85063_driver);
251 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
252 MODULE_DESCRIPTION("PCF85063 RTC driver");
253 MODULE_LICENSE("GPL");