blob: a06792966ea90fa580ab2a8e71d08913fcae88e3 [file] [log] [blame]
Thierry Redingf803f0d2012-12-17 16:02:44 -08001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 *
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 */
8
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>
14
15#define DRIVER_NAME "rtc-pcf8523"
16
17#define REG_CONTROL1 0x00
18#define REG_CONTROL1_CAP_SEL (1 << 7)
19#define REG_CONTROL1_STOP (1 << 5)
20
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
Jesper Nilssonf32bc702013-02-21 16:44:27 -080026#define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
Thierry Redingf803f0d2012-12-17 16:02:44 -080027
28#define REG_SECONDS 0x03
29#define REG_SECONDS_OS (1 << 7)
30
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
37
38struct pcf8523 {
39 struct rtc_device *rtc;
40};
41
42static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
43{
44 struct i2c_msg msgs[2];
45 u8 value = 0;
46 int err;
47
48 msgs[0].addr = client->addr;
49 msgs[0].flags = 0;
50 msgs[0].len = sizeof(reg);
51 msgs[0].buf = &reg;
52
53 msgs[1].addr = client->addr;
54 msgs[1].flags = I2C_M_RD;
55 msgs[1].len = sizeof(value);
56 msgs[1].buf = &value;
57
58 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
59 if (err < 0)
60 return err;
61
62 *valuep = value;
63
64 return 0;
65}
66
67static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
68{
69 u8 buffer[2] = { reg, value };
70 struct i2c_msg msg;
71 int err;
72
73 msg.addr = client->addr;
74 msg.flags = 0;
75 msg.len = sizeof(buffer);
76 msg.buf = buffer;
77
78 err = i2c_transfer(client->adapter, &msg, 1);
79 if (err < 0)
80 return err;
81
82 return 0;
83}
84
Baruch Siach2eaaeb32018-12-05 17:00:09 +020085static int pcf8523_voltage_low(struct i2c_client *client)
86{
87 u8 value;
88 int err;
89
90 err = pcf8523_read(client, REG_CONTROL3, &value);
91 if (err < 0)
92 return err;
93
94 return !!(value & REG_CONTROL3_BLF);
95}
96
Sam Ravnborg6ec05642019-01-19 10:00:30 +010097static int pcf8523_load_capacitance(struct i2c_client *client)
Thierry Redingf803f0d2012-12-17 16:02:44 -080098{
Sam Ravnborg6ec05642019-01-19 10:00:30 +010099 u32 load;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800100 u8 value;
101 int err;
102
103 err = pcf8523_read(client, REG_CONTROL1, &value);
104 if (err < 0)
105 return err;
106
Sam Ravnborg6ec05642019-01-19 10:00:30 +0100107 load = 12500;
108 of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
109 &load);
110
111 switch (load) {
112 default:
113 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
114 load);
115 /* fall through */
116 case 12500:
Thierry Redingf803f0d2012-12-17 16:02:44 -0800117 value |= REG_CONTROL1_CAP_SEL;
Sam Ravnborg6ec05642019-01-19 10:00:30 +0100118 break;
119 case 7000:
120 value &= ~REG_CONTROL1_CAP_SEL;
121 break;
122 }
Thierry Redingf803f0d2012-12-17 16:02:44 -0800123
124 err = pcf8523_write(client, REG_CONTROL1, value);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800125
126 return err;
127}
128
129static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
130{
131 u8 value;
132 int err;
133
134 err = pcf8523_read(client, REG_CONTROL3, &value);
135 if (err < 0)
136 return err;
137
138 value = (value & ~REG_CONTROL3_PM_MASK) | pm;
139
140 err = pcf8523_write(client, REG_CONTROL3, value);
141 if (err < 0)
142 return err;
143
144 return 0;
145}
146
147static int pcf8523_stop_rtc(struct i2c_client *client)
148{
149 u8 value;
150 int err;
151
152 err = pcf8523_read(client, REG_CONTROL1, &value);
153 if (err < 0)
154 return err;
155
156 value |= REG_CONTROL1_STOP;
157
158 err = pcf8523_write(client, REG_CONTROL1, value);
159 if (err < 0)
160 return err;
161
162 return 0;
163}
164
165static int pcf8523_start_rtc(struct i2c_client *client)
166{
167 u8 value;
168 int err;
169
170 err = pcf8523_read(client, REG_CONTROL1, &value);
171 if (err < 0)
172 return err;
173
174 value &= ~REG_CONTROL1_STOP;
175
176 err = pcf8523_write(client, REG_CONTROL1, value);
177 if (err < 0)
178 return err;
179
180 return 0;
181}
182
183static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
184{
185 struct i2c_client *client = to_i2c_client(dev);
186 u8 start = REG_SECONDS, regs[7];
187 struct i2c_msg msgs[2];
188 int err;
189
Baruch Siach2eaaeb32018-12-05 17:00:09 +0200190 err = pcf8523_voltage_low(client);
191 if (err < 0) {
192 return err;
193 } else if (err > 0) {
194 dev_err(dev, "low voltage detected, time is unreliable\n");
195 return -EINVAL;
196 }
197
Thierry Redingf803f0d2012-12-17 16:02:44 -0800198 msgs[0].addr = client->addr;
199 msgs[0].flags = 0;
200 msgs[0].len = 1;
201 msgs[0].buf = &start;
202
203 msgs[1].addr = client->addr;
204 msgs[1].flags = I2C_M_RD;
205 msgs[1].len = sizeof(regs);
206 msgs[1].buf = regs;
207
208 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
209 if (err < 0)
210 return err;
211
Alexandre Belloniede44c92016-03-03 09:55:47 +0100212 if (regs[0] & REG_SECONDS_OS)
213 return -EINVAL;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800214
215 tm->tm_sec = bcd2bin(regs[0] & 0x7f);
216 tm->tm_min = bcd2bin(regs[1] & 0x7f);
217 tm->tm_hour = bcd2bin(regs[2] & 0x3f);
218 tm->tm_mday = bcd2bin(regs[3] & 0x3f);
219 tm->tm_wday = regs[4] & 0x7;
Chris Cui35738392014-05-06 12:49:58 -0700220 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800221 tm->tm_year = bcd2bin(regs[6]) + 100;
222
223 return rtc_valid_tm(tm);
224}
225
226static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
227{
228 struct i2c_client *client = to_i2c_client(dev);
229 struct i2c_msg msg;
230 u8 regs[8];
231 int err;
232
Uwe Kleine-Königfbbf53f2015-11-06 17:37:56 +0100233 /*
234 * The hardware can only store values between 0 and 99 in it's YEAR
235 * register (with 99 overflowing to 0 on increment).
236 * After 2100-02-28 we could start interpreting the year to be in the
237 * interval [2100, 2199], but there is no path to switch in a smooth way
238 * because the chip handles YEAR=0x00 (and the out-of-spec
239 * YEAR=0xa0) as a leap year, but 2100 isn't.
240 */
241 if (tm->tm_year < 100 || tm->tm_year >= 200)
242 return -EINVAL;
243
Thierry Redingf803f0d2012-12-17 16:02:44 -0800244 err = pcf8523_stop_rtc(client);
245 if (err < 0)
246 return err;
247
248 regs[0] = REG_SECONDS;
Alexandre Belloniede44c92016-03-03 09:55:47 +0100249 /* This will purposely overwrite REG_SECONDS_OS */
Thierry Redingf803f0d2012-12-17 16:02:44 -0800250 regs[1] = bin2bcd(tm->tm_sec);
251 regs[2] = bin2bcd(tm->tm_min);
252 regs[3] = bin2bcd(tm->tm_hour);
253 regs[4] = bin2bcd(tm->tm_mday);
254 regs[5] = tm->tm_wday;
Chris Cui35738392014-05-06 12:49:58 -0700255 regs[6] = bin2bcd(tm->tm_mon + 1);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800256 regs[7] = bin2bcd(tm->tm_year - 100);
257
258 msg.addr = client->addr;
259 msg.flags = 0;
260 msg.len = sizeof(regs);
261 msg.buf = regs;
262
263 err = i2c_transfer(client->adapter, &msg, 1);
264 if (err < 0) {
265 /*
266 * If the time cannot be set, restart the RTC anyway. Note
267 * that errors are ignored if the RTC cannot be started so
268 * that we have a chance to propagate the original error.
269 */
270 pcf8523_start_rtc(client);
271 return err;
272 }
273
274 return pcf8523_start_rtc(client);
275}
276
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800277#ifdef CONFIG_RTC_INTF_DEV
278static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
279 unsigned long arg)
280{
281 struct i2c_client *client = to_i2c_client(dev);
Baruch Siach2eaaeb32018-12-05 17:00:09 +0200282 int ret;
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800283
284 switch (cmd) {
285 case RTC_VL_READ:
Baruch Siach2eaaeb32018-12-05 17:00:09 +0200286 ret = pcf8523_voltage_low(client);
287 if (ret < 0)
288 return ret;
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800289
290 if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
291 return -EFAULT;
292
293 return 0;
294 default:
295 return -ENOIOCTLCMD;
296 }
297}
298#else
299#define pcf8523_rtc_ioctl NULL
300#endif
301
Thierry Redingf803f0d2012-12-17 16:02:44 -0800302static const struct rtc_class_ops pcf8523_rtc_ops = {
303 .read_time = pcf8523_rtc_read_time,
304 .set_time = pcf8523_rtc_set_time,
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800305 .ioctl = pcf8523_rtc_ioctl,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800306};
307
308static int pcf8523_probe(struct i2c_client *client,
309 const struct i2c_device_id *id)
310{
311 struct pcf8523 *pcf;
312 int err;
313
314 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
315 return -ENODEV;
316
317 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
318 if (!pcf)
319 return -ENOMEM;
320
Sam Ravnborg6ec05642019-01-19 10:00:30 +0100321 err = pcf8523_load_capacitance(client);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800322 if (err < 0)
Sam Ravnborg6ec05642019-01-19 10:00:30 +0100323 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
324 err);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800325
326 err = pcf8523_set_pm(client, 0);
327 if (err < 0)
328 return err;
329
Jingoo Han288031b2013-04-29 16:19:11 -0700330 pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800331 &pcf8523_rtc_ops, THIS_MODULE);
332 if (IS_ERR(pcf->rtc))
333 return PTR_ERR(pcf->rtc);
334
335 i2c_set_clientdata(client, pcf);
336
337 return 0;
338}
339
Thierry Redingf803f0d2012-12-17 16:02:44 -0800340static const struct i2c_device_id pcf8523_id[] = {
341 { "pcf8523", 0 },
342 { }
343};
344MODULE_DEVICE_TABLE(i2c, pcf8523_id);
345
346#ifdef CONFIG_OF
347static const struct of_device_id pcf8523_of_match[] = {
348 { .compatible = "nxp,pcf8523" },
349 { }
350};
351MODULE_DEVICE_TABLE(of, pcf8523_of_match);
352#endif
353
354static struct i2c_driver pcf8523_driver = {
355 .driver = {
356 .name = DRIVER_NAME,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800357 .of_match_table = of_match_ptr(pcf8523_of_match),
358 },
359 .probe = pcf8523_probe,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800360 .id_table = pcf8523_id,
361};
362module_i2c_driver(pcf8523_driver);
363
364MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
365MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
366MODULE_LICENSE("GPL v2");