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>
19 #define PCF85063_REG_CTRL1 0x00 /* status */
20 #define PCF85063_REG_CTRL1_STOP BIT(5)
21 #define PCF85063_REG_CTRL2 0x01
23 #define PCF85063_REG_SC 0x04 /* datetime */
24 #define PCF85063_REG_SC_OS 0x80
25 #define PCF85063_REG_MN 0x05
26 #define PCF85063_REG_HR 0x06
27 #define PCF85063_REG_DM 0x07
28 #define PCF85063_REG_DW 0x08
29 #define PCF85063_REG_MO 0x09
30 #define PCF85063_REG_YR 0x0A
32 static struct i2c_driver pcf85063_driver;
35 struct rtc_device *rtc;
36 int voltage_low; /* indicates if a low_voltage was detected */
39 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
43 ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
45 dev_err(&client->dev, "Failing to stop the clock\n");
50 ret |= PCF85063_REG_CTRL1_STOP;
52 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
54 dev_err(&client->dev, "Failing to stop the clock\n");
64 * In the routines that deal directly with the pcf85063 hardware, we use
65 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
67 static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
73 * while reading, the time/date registers are blocked and not updated
74 * anymore until the access is finished. To not lose a second
75 * event, the access must be finished within one second. So, read all
76 * time/date registers in one turn.
78 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
80 if (rc != sizeof(regs)) {
81 dev_err(&client->dev, "date/time register read error\n");
85 /* if the clock has lost its power it makes no sense to use its time */
86 if (regs[0] & PCF85063_REG_SC_OS) {
87 dev_warn(&client->dev, "Power loss detected, invalid time\n");
91 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
92 tm->tm_min = bcd2bin(regs[1] & 0x7F);
93 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
94 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
95 tm->tm_wday = regs[4] & 0x07;
96 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
97 tm->tm_year = bcd2bin(regs[6]);
99 tm->tm_year += 100; /* assume we are in 1970...2069 */
101 return rtc_valid_tm(tm);
104 static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
110 * to accurately set the time, reset the divider chain and keep it in
111 * reset state until all time/date registers are written
113 rc = pcf85063_stop_clock(client, ®s[7]);
117 /* hours, minutes and seconds */
118 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
120 regs[1] = bin2bcd(tm->tm_min);
121 regs[2] = bin2bcd(tm->tm_hour);
123 /* Day of month, 1 - 31 */
124 regs[3] = bin2bcd(tm->tm_mday);
127 regs[4] = tm->tm_wday & 0x07;
130 regs[5] = bin2bcd(tm->tm_mon + 1);
132 /* year and century */
133 regs[6] = bin2bcd(tm->tm_year % 100);
136 * after all time/date registers are written, let the 'address auto
137 * increment' feature wrap around and write register CTRL1 to re-enable
138 * the clock divider chain again
140 regs[7] &= ~PCF85063_REG_CTRL1_STOP;
142 /* write all registers at once */
143 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
146 dev_err(&client->dev, "date/time register write error\n");
153 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
155 return pcf85063_get_datetime(to_i2c_client(dev), tm);
158 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
160 return pcf85063_set_datetime(to_i2c_client(dev), tm);
163 static const struct rtc_class_ops pcf85063_rtc_ops = {
164 .read_time = pcf85063_rtc_read_time,
165 .set_time = pcf85063_rtc_set_time
168 static int pcf85063_probe(struct i2c_client *client,
169 const struct i2c_device_id *id)
171 struct pcf85063 *pcf85063;
173 dev_dbg(&client->dev, "%s\n", __func__);
175 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
178 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
183 i2c_set_clientdata(client, pcf85063);
185 pcf85063->rtc = devm_rtc_device_register(&client->dev,
186 pcf85063_driver.driver.name,
187 &pcf85063_rtc_ops, THIS_MODULE);
189 return PTR_ERR_OR_ZERO(pcf85063->rtc);
192 static const struct i2c_device_id pcf85063_id[] = {
196 MODULE_DEVICE_TABLE(i2c, pcf85063_id);
199 static const struct of_device_id pcf85063_of_match[] = {
200 { .compatible = "nxp,pcf85063" },
203 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
206 static struct i2c_driver pcf85063_driver = {
208 .name = "rtc-pcf85063",
209 .of_match_table = of_match_ptr(pcf85063_of_match),
211 .probe = pcf85063_probe,
212 .id_table = pcf85063_id,
215 module_i2c_driver(pcf85063_driver);
217 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
218 MODULE_DESCRIPTION("PCF85063 RTC driver");
219 MODULE_LICENSE("GPL");