]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/rtc/rtc-pcf85063.c
ba65aa4a30401a803ea9a09cc21a626c36116fcb
[linux.git] / drivers / rtc / rtc-pcf85063.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * An I2C driver for the PCF85063 RTC
4  * Copyright 2014 Rose Technology
5  *
6  * Author: Søren Andersen <san@rosetechnology.dk>
7  * Maintainers: http://www.nslu2-linux.org/
8  */
9 #include <linux/i2c.h>
10 #include <linux/bcd.h>
11 #include <linux/rtc.h>
12 #include <linux/module.h>
13
14 /*
15  * Information for this driver was pulled from the following datasheets.
16  *
17  *  http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
18  *  http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
19  *
20  *  PCF85063A -- Rev. 6 — 18 November 2015
21  *  PCF85063TP -- Rev. 4 — 6 May 2015
22 */
23
24 #define PCF85063_REG_CTRL1              0x00 /* status */
25 #define PCF85063_REG_CTRL1_CAP_SEL      BIT(0)
26 #define PCF85063_REG_CTRL1_STOP         BIT(5)
27
28 #define PCF85063_REG_SC                 0x04 /* datetime */
29 #define PCF85063_REG_SC_OS              0x80
30
31 static struct i2c_driver pcf85063_driver;
32
33 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
34 {
35         int rc;
36         u8 reg;
37
38         rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
39         if (rc < 0) {
40                 dev_err(&client->dev, "Failing to stop the clock\n");
41                 return -EIO;
42         }
43
44         /* stop the clock */
45         reg = rc | PCF85063_REG_CTRL1_STOP;
46
47         rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
48         if (rc < 0) {
49                 dev_err(&client->dev, "Failing to stop the clock\n");
50                 return -EIO;
51         }
52
53         *ctrl1 = reg;
54
55         return 0;
56 }
57
58 static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
59 {
60         int rc;
61
62         /* start the clock */
63         ctrl1 &= ~PCF85063_REG_CTRL1_STOP;
64
65         rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
66         if (rc < 0) {
67                 dev_err(&client->dev, "Failing to start the clock\n");
68                 return -EIO;
69         }
70
71         return 0;
72 }
73
74 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
75 {
76         struct i2c_client *client = to_i2c_client(dev);
77         int rc;
78         u8 regs[7];
79
80         /*
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.
85          */
86         rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
87                                            sizeof(regs), regs);
88         if (rc != sizeof(regs)) {
89                 dev_err(&client->dev, "date/time register read error\n");
90                 return -EIO;
91         }
92
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");
96                 return -EINVAL;
97         }
98
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]);
106         tm->tm_year += 100;
107
108         return 0;
109 }
110
111 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
112 {
113         struct i2c_client *client = to_i2c_client(dev);
114         int rc;
115         u8 regs[7];
116         u8 ctrl1;
117
118         if ((tm->tm_year < 100) || (tm->tm_year > 199))
119                 return -EINVAL;
120
121         /*
122          * to accurately set the time, reset the divider chain and keep it in
123          * reset state until all time/date registers are written
124          */
125         rc = pcf85063_stop_clock(client, &ctrl1);
126         if (rc != 0)
127                 return rc;
128
129         /* hours, minutes and seconds */
130         regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
131
132         regs[1] = bin2bcd(tm->tm_min);
133         regs[2] = bin2bcd(tm->tm_hour);
134
135         /* Day of month, 1 - 31 */
136         regs[3] = bin2bcd(tm->tm_mday);
137
138         /* Day, 0 - 6 */
139         regs[4] = tm->tm_wday & 0x07;
140
141         /* month, 1 - 12 */
142         regs[5] = bin2bcd(tm->tm_mon + 1);
143
144         /* year and century */
145         regs[6] = bin2bcd(tm->tm_year - 100);
146
147         /* write all registers at once */
148         rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
149                                             sizeof(regs), regs);
150         if (rc < 0) {
151                 dev_err(&client->dev, "date/time register write error\n");
152                 return rc;
153         }
154
155         /*
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.
159          */
160         rc = pcf85063_start_clock(client, ctrl1);
161         if (rc != 0)
162                 return rc;
163
164         return 0;
165 }
166
167 static const struct rtc_class_ops pcf85063_rtc_ops = {
168         .read_time      = pcf85063_rtc_read_time,
169         .set_time       = pcf85063_rtc_set_time
170 };
171
172 static int pcf85063_load_capacitance(struct i2c_client *client)
173 {
174         u32 load;
175         int rc;
176         u8 reg;
177
178         rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
179         if (rc < 0)
180                 return rc;
181
182         reg = rc;
183         load = 7000;
184         of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
185                              &load);
186
187         switch (load) {
188         default:
189                 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
190                          load);
191                 /* fall through */
192         case 7000:
193                 reg &= ~PCF85063_REG_CTRL1_CAP_SEL;
194                 break;
195         case 12500:
196                 reg |= PCF85063_REG_CTRL1_CAP_SEL;
197                 break;
198         }
199
200         rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
201
202         return rc;
203 }
204
205 static int pcf85063_probe(struct i2c_client *client)
206 {
207         struct rtc_device *rtc;
208         int err;
209
210         dev_dbg(&client->dev, "%s\n", __func__);
211
212         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
213                 return -ENODEV;
214
215         err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
216         if (err < 0) {
217                 dev_err(&client->dev, "RTC chip is not present\n");
218                 return err;
219         }
220
221         err = pcf85063_load_capacitance(client);
222         if (err < 0)
223                 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
224                          err);
225
226         rtc = devm_rtc_device_register(&client->dev,
227                                        pcf85063_driver.driver.name,
228                                        &pcf85063_rtc_ops, THIS_MODULE);
229
230         return PTR_ERR_OR_ZERO(rtc);
231 }
232
233 #ifdef CONFIG_OF
234 static const struct of_device_id pcf85063_of_match[] = {
235         { .compatible = "nxp,pcf85063" },
236         {}
237 };
238 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
239 #endif
240
241 static struct i2c_driver pcf85063_driver = {
242         .driver         = {
243                 .name   = "rtc-pcf85063",
244                 .of_match_table = of_match_ptr(pcf85063_of_match),
245         },
246         .probe_new      = pcf85063_probe,
247 };
248
249 module_i2c_driver(pcf85063_driver);
250
251 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
252 MODULE_DESCRIPTION("PCF85063 RTC driver");
253 MODULE_LICENSE("GPL");