rtc: pcf8523: refuse to write dates later than 2099
[linux/fpc-iii.git] / drivers / rtc / rtc-pcf8523.c
blob988566caaaa639c7854f3a78c1aecbf21c478139
1 /*
2 * Copyright (C) 2012 Avionic Design GmbH
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/bcd.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/rtc.h>
13 #include <linux/of.h>
15 #define DRIVER_NAME "rtc-pcf8523"
17 #define REG_CONTROL1 0x00
18 #define REG_CONTROL1_CAP_SEL (1 << 7)
19 #define REG_CONTROL1_STOP (1 << 5)
21 #define REG_CONTROL3 0x02
22 #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
23 #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
24 #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
25 #define REG_CONTROL3_PM_MASK 0xe0
26 #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
28 #define REG_SECONDS 0x03
29 #define REG_SECONDS_OS (1 << 7)
31 #define REG_MINUTES 0x04
32 #define REG_HOURS 0x05
33 #define REG_DAYS 0x06
34 #define REG_WEEKDAYS 0x07
35 #define REG_MONTHS 0x08
36 #define REG_YEARS 0x09
38 struct pcf8523 {
39 struct rtc_device *rtc;
42 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
44 struct i2c_msg msgs[2];
45 u8 value = 0;
46 int err;
48 msgs[0].addr = client->addr;
49 msgs[0].flags = 0;
50 msgs[0].len = sizeof(reg);
51 msgs[0].buf = &reg;
53 msgs[1].addr = client->addr;
54 msgs[1].flags = I2C_M_RD;
55 msgs[1].len = sizeof(value);
56 msgs[1].buf = &value;
58 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
59 if (err < 0)
60 return err;
62 *valuep = value;
64 return 0;
67 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
69 u8 buffer[2] = { reg, value };
70 struct i2c_msg msg;
71 int err;
73 msg.addr = client->addr;
74 msg.flags = 0;
75 msg.len = sizeof(buffer);
76 msg.buf = buffer;
78 err = i2c_transfer(client->adapter, &msg, 1);
79 if (err < 0)
80 return err;
82 return 0;
85 static int pcf8523_select_capacitance(struct i2c_client *client, bool high)
87 u8 value;
88 int err;
90 err = pcf8523_read(client, REG_CONTROL1, &value);
91 if (err < 0)
92 return err;
94 if (!high)
95 value &= ~REG_CONTROL1_CAP_SEL;
96 else
97 value |= REG_CONTROL1_CAP_SEL;
99 err = pcf8523_write(client, REG_CONTROL1, value);
100 if (err < 0)
101 return err;
103 return err;
106 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
108 u8 value;
109 int err;
111 err = pcf8523_read(client, REG_CONTROL3, &value);
112 if (err < 0)
113 return err;
115 value = (value & ~REG_CONTROL3_PM_MASK) | pm;
117 err = pcf8523_write(client, REG_CONTROL3, value);
118 if (err < 0)
119 return err;
121 return 0;
124 static int pcf8523_stop_rtc(struct i2c_client *client)
126 u8 value;
127 int err;
129 err = pcf8523_read(client, REG_CONTROL1, &value);
130 if (err < 0)
131 return err;
133 value |= REG_CONTROL1_STOP;
135 err = pcf8523_write(client, REG_CONTROL1, value);
136 if (err < 0)
137 return err;
139 return 0;
142 static int pcf8523_start_rtc(struct i2c_client *client)
144 u8 value;
145 int err;
147 err = pcf8523_read(client, REG_CONTROL1, &value);
148 if (err < 0)
149 return err;
151 value &= ~REG_CONTROL1_STOP;
153 err = pcf8523_write(client, REG_CONTROL1, value);
154 if (err < 0)
155 return err;
157 return 0;
160 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
162 struct i2c_client *client = to_i2c_client(dev);
163 u8 start = REG_SECONDS, regs[7];
164 struct i2c_msg msgs[2];
165 int err;
167 msgs[0].addr = client->addr;
168 msgs[0].flags = 0;
169 msgs[0].len = 1;
170 msgs[0].buf = &start;
172 msgs[1].addr = client->addr;
173 msgs[1].flags = I2C_M_RD;
174 msgs[1].len = sizeof(regs);
175 msgs[1].buf = regs;
177 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
178 if (err < 0)
179 return err;
181 if (regs[0] & REG_SECONDS_OS) {
183 * If the oscillator was stopped, try to clear the flag. Upon
184 * power-up the flag is always set, but if we cannot clear it
185 * the oscillator isn't running properly for some reason. The
186 * sensible thing therefore is to return an error, signalling
187 * that the clock cannot be assumed to be correct.
190 regs[0] &= ~REG_SECONDS_OS;
192 err = pcf8523_write(client, REG_SECONDS, regs[0]);
193 if (err < 0)
194 return err;
196 err = pcf8523_read(client, REG_SECONDS, &regs[0]);
197 if (err < 0)
198 return err;
200 if (regs[0] & REG_SECONDS_OS)
201 return -EAGAIN;
204 tm->tm_sec = bcd2bin(regs[0] & 0x7f);
205 tm->tm_min = bcd2bin(regs[1] & 0x7f);
206 tm->tm_hour = bcd2bin(regs[2] & 0x3f);
207 tm->tm_mday = bcd2bin(regs[3] & 0x3f);
208 tm->tm_wday = regs[4] & 0x7;
209 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
210 tm->tm_year = bcd2bin(regs[6]) + 100;
212 return rtc_valid_tm(tm);
215 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
217 struct i2c_client *client = to_i2c_client(dev);
218 struct i2c_msg msg;
219 u8 regs[8];
220 int err;
223 * The hardware can only store values between 0 and 99 in it's YEAR
224 * register (with 99 overflowing to 0 on increment).
225 * After 2100-02-28 we could start interpreting the year to be in the
226 * interval [2100, 2199], but there is no path to switch in a smooth way
227 * because the chip handles YEAR=0x00 (and the out-of-spec
228 * YEAR=0xa0) as a leap year, but 2100 isn't.
230 if (tm->tm_year < 100 || tm->tm_year >= 200)
231 return -EINVAL;
233 err = pcf8523_stop_rtc(client);
234 if (err < 0)
235 return err;
237 regs[0] = REG_SECONDS;
238 regs[1] = bin2bcd(tm->tm_sec);
239 regs[2] = bin2bcd(tm->tm_min);
240 regs[3] = bin2bcd(tm->tm_hour);
241 regs[4] = bin2bcd(tm->tm_mday);
242 regs[5] = tm->tm_wday;
243 regs[6] = bin2bcd(tm->tm_mon + 1);
244 regs[7] = bin2bcd(tm->tm_year - 100);
246 msg.addr = client->addr;
247 msg.flags = 0;
248 msg.len = sizeof(regs);
249 msg.buf = regs;
251 err = i2c_transfer(client->adapter, &msg, 1);
252 if (err < 0) {
254 * If the time cannot be set, restart the RTC anyway. Note
255 * that errors are ignored if the RTC cannot be started so
256 * that we have a chance to propagate the original error.
258 pcf8523_start_rtc(client);
259 return err;
262 return pcf8523_start_rtc(client);
265 #ifdef CONFIG_RTC_INTF_DEV
266 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
267 unsigned long arg)
269 struct i2c_client *client = to_i2c_client(dev);
270 u8 value;
271 int ret = 0, err;
273 switch (cmd) {
274 case RTC_VL_READ:
275 err = pcf8523_read(client, REG_CONTROL3, &value);
276 if (err < 0)
277 return err;
279 if (value & REG_CONTROL3_BLF)
280 ret = 1;
282 if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
283 return -EFAULT;
285 return 0;
286 default:
287 return -ENOIOCTLCMD;
290 #else
291 #define pcf8523_rtc_ioctl NULL
292 #endif
294 static const struct rtc_class_ops pcf8523_rtc_ops = {
295 .read_time = pcf8523_rtc_read_time,
296 .set_time = pcf8523_rtc_set_time,
297 .ioctl = pcf8523_rtc_ioctl,
300 static int pcf8523_probe(struct i2c_client *client,
301 const struct i2c_device_id *id)
303 struct pcf8523 *pcf;
304 int err;
306 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
307 return -ENODEV;
309 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
310 if (!pcf)
311 return -ENOMEM;
313 err = pcf8523_select_capacitance(client, true);
314 if (err < 0)
315 return err;
317 err = pcf8523_set_pm(client, 0);
318 if (err < 0)
319 return err;
321 pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
322 &pcf8523_rtc_ops, THIS_MODULE);
323 if (IS_ERR(pcf->rtc))
324 return PTR_ERR(pcf->rtc);
326 i2c_set_clientdata(client, pcf);
328 return 0;
331 static const struct i2c_device_id pcf8523_id[] = {
332 { "pcf8523", 0 },
335 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
337 #ifdef CONFIG_OF
338 static const struct of_device_id pcf8523_of_match[] = {
339 { .compatible = "nxp,pcf8523" },
342 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
343 #endif
345 static struct i2c_driver pcf8523_driver = {
346 .driver = {
347 .name = DRIVER_NAME,
348 .of_match_table = of_match_ptr(pcf8523_of_match),
350 .probe = pcf8523_probe,
351 .id_table = pcf8523_id,
353 module_i2c_driver(pcf8523_driver);
355 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
356 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
357 MODULE_LICENSE("GPL v2");