rtc: pcf85063: fix time/date setting
[deliverable/linux.git] / drivers / rtc / rtc-pcf85063.c
1 /*
2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
4 *
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
7 *
8 * based on the other drivers in this same directory.
9 *
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.
13 */
14 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/module.h>
18
19 #define DRV_VERSION "0.0.1"
20
21 #define PCF85063_REG_CTRL1 0x00 /* status */
22 #define PCF85063_REG_CTRL1_STOP BIT(5)
23 #define PCF85063_REG_CTRL2 0x01
24
25 #define PCF85063_REG_SC 0x04 /* datetime */
26 #define PCF85063_REG_SC_OS 0x80
27 #define PCF85063_REG_MN 0x05
28 #define PCF85063_REG_HR 0x06
29 #define PCF85063_REG_DM 0x07
30 #define PCF85063_REG_DW 0x08
31 #define PCF85063_REG_MO 0x09
32 #define PCF85063_REG_YR 0x0A
33
34 #define PCF85063_MO_C 0x80 /* century */
35
36 static struct i2c_driver pcf85063_driver;
37
38 struct pcf85063 {
39 struct rtc_device *rtc;
40 int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
41 int voltage_low; /* indicates if a low_voltage was detected */
42 };
43
44 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
45 {
46 s32 ret;
47
48 ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
49 if (ret < 0) {
50 dev_err(&client->dev, "Failing to stop the clock\n");
51 return -EIO;
52 }
53
54 /* stop the clock */
55 ret |= PCF85063_REG_CTRL1_STOP;
56
57 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
58 if (ret < 0) {
59 dev_err(&client->dev, "Failing to stop the clock\n");
60 return -EIO;
61 }
62
63 *ctrl1 = ret;
64
65 return 0;
66 }
67
68 /*
69 * In the routines that deal directly with the pcf85063 hardware, we use
70 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
71 */
72 static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
73 {
74 int rc;
75 struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
76 u8 regs[7];
77
78 /*
79 * while reading, the time/date registers are blocked and not updated
80 * anymore until the access is finished. To not lose a second
81 * event, the access must be finished within one second. So, read all
82 * time/date registers in one turn.
83 */
84 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
85 sizeof(regs), regs);
86 if (rc != sizeof(regs)) {
87 dev_err(&client->dev, "date/time register read error\n");
88 return -EIO;
89 }
90
91 /* if the clock has lost its power it makes no sense to use its time */
92 if (regs[0] & PCF85063_REG_SC_OS) {
93 dev_warn(&client->dev, "Power loss detected, invalid time\n");
94 return -EINVAL;
95 }
96
97 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
98 tm->tm_min = bcd2bin(regs[1] & 0x7F);
99 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
100 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
101 tm->tm_wday = regs[4] & 0x07;
102 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
103 tm->tm_year = bcd2bin(regs[6]);
104 if (tm->tm_year < 70)
105 tm->tm_year += 100; /* assume we are in 1970...2069 */
106 /* detect the polarity heuristically. see note above. */
107 pcf85063->c_polarity = (regs[5] & PCF85063_MO_C) ?
108 (tm->tm_year >= 100) : (tm->tm_year < 100);
109
110 return rtc_valid_tm(tm);
111 }
112
113 static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
114 {
115 int rc;
116 u8 regs[8];
117
118 /*
119 * to accurately set the time, reset the divider chain and keep it in
120 * reset state until all time/date registers are written
121 */
122 rc = pcf85063_stop_clock(client, &regs[7]);
123 if (rc != 0)
124 return rc;
125
126 /* hours, minutes and seconds */
127 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
128
129 regs[1] = bin2bcd(tm->tm_min);
130 regs[2] = bin2bcd(tm->tm_hour);
131
132 /* Day of month, 1 - 31 */
133 regs[3] = bin2bcd(tm->tm_mday);
134
135 /* Day, 0 - 6 */
136 regs[4] = tm->tm_wday & 0x07;
137
138 /* month, 1 - 12 */
139 regs[5] = bin2bcd(tm->tm_mon + 1);
140
141 /* year and century */
142 regs[6] = bin2bcd(tm->tm_year % 100);
143
144 /*
145 * after all time/date registers are written, let the 'address auto
146 * increment' feature wrap around and write register CTRL1 to re-enable
147 * the clock divider chain again
148 */
149 regs[7] &= ~PCF85063_REG_CTRL1_STOP;
150
151 /* write all registers at once */
152 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
153 sizeof(regs), regs);
154 if (rc < 0) {
155 dev_err(&client->dev, "date/time register write error\n");
156 return rc;
157 }
158
159 return 0;
160 }
161
162 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
163 {
164 return pcf85063_get_datetime(to_i2c_client(dev), tm);
165 }
166
167 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
168 {
169 return pcf85063_set_datetime(to_i2c_client(dev), tm);
170 }
171
172 static const struct rtc_class_ops pcf85063_rtc_ops = {
173 .read_time = pcf85063_rtc_read_time,
174 .set_time = pcf85063_rtc_set_time
175 };
176
177 static int pcf85063_probe(struct i2c_client *client,
178 const struct i2c_device_id *id)
179 {
180 struct pcf85063 *pcf85063;
181
182 dev_dbg(&client->dev, "%s\n", __func__);
183
184 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
185 return -ENODEV;
186
187 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
188 GFP_KERNEL);
189 if (!pcf85063)
190 return -ENOMEM;
191
192 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
193
194 i2c_set_clientdata(client, pcf85063);
195
196 pcf85063->rtc = devm_rtc_device_register(&client->dev,
197 pcf85063_driver.driver.name,
198 &pcf85063_rtc_ops, THIS_MODULE);
199
200 return PTR_ERR_OR_ZERO(pcf85063->rtc);
201 }
202
203 static const struct i2c_device_id pcf85063_id[] = {
204 { "pcf85063", 0 },
205 { }
206 };
207 MODULE_DEVICE_TABLE(i2c, pcf85063_id);
208
209 #ifdef CONFIG_OF
210 static const struct of_device_id pcf85063_of_match[] = {
211 { .compatible = "nxp,pcf85063" },
212 {}
213 };
214 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
215 #endif
216
217 static struct i2c_driver pcf85063_driver = {
218 .driver = {
219 .name = "rtc-pcf85063",
220 .of_match_table = of_match_ptr(pcf85063_of_match),
221 },
222 .probe = pcf85063_probe,
223 .id_table = pcf85063_id,
224 };
225
226 module_i2c_driver(pcf85063_driver);
227
228 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
229 MODULE_DESCRIPTION("PCF85063 RTC driver");
230 MODULE_LICENSE("GPL");
231 MODULE_VERSION(DRV_VERSION);
This page took 0.036578 seconds and 6 git commands to generate.