blob: 47e0f411dd5cb744f84f0fa2269a882b9038e08c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thierry Redingf803f0d2012-12-17 16:02:44 -08002/*
3 * Copyright (C) 2012 Avionic Design GmbH
Thierry Redingf803f0d2012-12-17 16:02:44 -08004 */
5
6#include <linux/bcd.h>
7#include <linux/i2c.h>
8#include <linux/module.h>
9#include <linux/rtc.h>
10#include <linux/of.h>
11
12#define DRIVER_NAME "rtc-pcf8523"
13
14#define REG_CONTROL1 0x00
15#define REG_CONTROL1_CAP_SEL (1 << 7)
16#define REG_CONTROL1_STOP (1 << 5)
17
18#define REG_CONTROL3 0x02
19#define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
20#define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
21#define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
22#define REG_CONTROL3_PM_MASK 0xe0
Jesper Nilssonf32bc702013-02-21 16:44:27 -080023#define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
Thierry Redingf803f0d2012-12-17 16:02:44 -080024
25#define REG_SECONDS 0x03
26#define REG_SECONDS_OS (1 << 7)
27
28#define REG_MINUTES 0x04
29#define REG_HOURS 0x05
30#define REG_DAYS 0x06
31#define REG_WEEKDAYS 0x07
32#define REG_MONTHS 0x08
33#define REG_YEARS 0x09
34
Russell Kingbc3bee02017-09-29 11:23:36 +010035#define REG_OFFSET 0x0e
36#define REG_OFFSET_MODE BIT(7)
37
Thierry Redingf803f0d2012-12-17 16:02:44 -080038static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
39{
40 struct i2c_msg msgs[2];
41 u8 value = 0;
42 int err;
43
44 msgs[0].addr = client->addr;
45 msgs[0].flags = 0;
46 msgs[0].len = sizeof(reg);
47 msgs[0].buf = &reg;
48
49 msgs[1].addr = client->addr;
50 msgs[1].flags = I2C_M_RD;
51 msgs[1].len = sizeof(value);
52 msgs[1].buf = &value;
53
54 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
55 if (err < 0)
56 return err;
57
58 *valuep = value;
59
60 return 0;
61}
62
63static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
64{
65 u8 buffer[2] = { reg, value };
66 struct i2c_msg msg;
67 int err;
68
69 msg.addr = client->addr;
70 msg.flags = 0;
71 msg.len = sizeof(buffer);
72 msg.buf = buffer;
73
74 err = i2c_transfer(client->adapter, &msg, 1);
75 if (err < 0)
76 return err;
77
78 return 0;
79}
80
Baruch Siachecb4a352018-12-05 17:00:09 +020081static int pcf8523_voltage_low(struct i2c_client *client)
82{
83 u8 value;
84 int err;
85
86 err = pcf8523_read(client, REG_CONTROL3, &value);
87 if (err < 0)
88 return err;
89
90 return !!(value & REG_CONTROL3_BLF);
91}
92
Sam Ravnborg189927e2019-01-19 10:00:30 +010093static int pcf8523_load_capacitance(struct i2c_client *client)
Thierry Redingf803f0d2012-12-17 16:02:44 -080094{
Sam Ravnborg189927e2019-01-19 10:00:30 +010095 u32 load;
Thierry Redingf803f0d2012-12-17 16:02:44 -080096 u8 value;
97 int err;
98
99 err = pcf8523_read(client, REG_CONTROL1, &value);
100 if (err < 0)
101 return err;
102
Sam Ravnborg189927e2019-01-19 10:00:30 +0100103 load = 12500;
104 of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
105 &load);
106
107 switch (load) {
108 default:
109 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
110 load);
111 /* fall through */
112 case 12500:
Thierry Redingf803f0d2012-12-17 16:02:44 -0800113 value |= REG_CONTROL1_CAP_SEL;
Sam Ravnborg189927e2019-01-19 10:00:30 +0100114 break;
115 case 7000:
116 value &= ~REG_CONTROL1_CAP_SEL;
117 break;
118 }
Thierry Redingf803f0d2012-12-17 16:02:44 -0800119
120 err = pcf8523_write(client, REG_CONTROL1, value);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800121
122 return err;
123}
124
125static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
126{
127 u8 value;
128 int err;
129
130 err = pcf8523_read(client, REG_CONTROL3, &value);
131 if (err < 0)
132 return err;
133
134 value = (value & ~REG_CONTROL3_PM_MASK) | pm;
135
136 err = pcf8523_write(client, REG_CONTROL3, value);
137 if (err < 0)
138 return err;
139
140 return 0;
141}
142
143static int pcf8523_stop_rtc(struct i2c_client *client)
144{
145 u8 value;
146 int err;
147
148 err = pcf8523_read(client, REG_CONTROL1, &value);
149 if (err < 0)
150 return err;
151
152 value |= REG_CONTROL1_STOP;
153
154 err = pcf8523_write(client, REG_CONTROL1, value);
155 if (err < 0)
156 return err;
157
158 return 0;
159}
160
161static int pcf8523_start_rtc(struct i2c_client *client)
162{
163 u8 value;
164 int err;
165
166 err = pcf8523_read(client, REG_CONTROL1, &value);
167 if (err < 0)
168 return err;
169
170 value &= ~REG_CONTROL1_STOP;
171
172 err = pcf8523_write(client, REG_CONTROL1, value);
173 if (err < 0)
174 return err;
175
176 return 0;
177}
178
179static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
180{
181 struct i2c_client *client = to_i2c_client(dev);
182 u8 start = REG_SECONDS, regs[7];
183 struct i2c_msg msgs[2];
184 int err;
185
Baruch Siachecb4a352018-12-05 17:00:09 +0200186 err = pcf8523_voltage_low(client);
187 if (err < 0) {
188 return err;
189 } else if (err > 0) {
190 dev_err(dev, "low voltage detected, time is unreliable\n");
191 return -EINVAL;
192 }
193
Thierry Redingf803f0d2012-12-17 16:02:44 -0800194 msgs[0].addr = client->addr;
195 msgs[0].flags = 0;
196 msgs[0].len = 1;
197 msgs[0].buf = &start;
198
199 msgs[1].addr = client->addr;
200 msgs[1].flags = I2C_M_RD;
201 msgs[1].len = sizeof(regs);
202 msgs[1].buf = regs;
203
204 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
205 if (err < 0)
206 return err;
207
Alexandre Belloniede44c92016-03-03 09:55:47 +0100208 if (regs[0] & REG_SECONDS_OS)
209 return -EINVAL;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800210
211 tm->tm_sec = bcd2bin(regs[0] & 0x7f);
212 tm->tm_min = bcd2bin(regs[1] & 0x7f);
213 tm->tm_hour = bcd2bin(regs[2] & 0x3f);
214 tm->tm_mday = bcd2bin(regs[3] & 0x3f);
215 tm->tm_wday = regs[4] & 0x7;
Chris Cui35738392014-05-06 12:49:58 -0700216 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800217 tm->tm_year = bcd2bin(regs[6]) + 100;
218
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100219 return 0;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800220}
221
222static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
223{
224 struct i2c_client *client = to_i2c_client(dev);
225 struct i2c_msg msg;
226 u8 regs[8];
227 int err;
228
Uwe Kleine-Königfbbf53f2015-11-06 17:37:56 +0100229 /*
230 * The hardware can only store values between 0 and 99 in it's YEAR
231 * register (with 99 overflowing to 0 on increment).
232 * After 2100-02-28 we could start interpreting the year to be in the
233 * interval [2100, 2199], but there is no path to switch in a smooth way
234 * because the chip handles YEAR=0x00 (and the out-of-spec
235 * YEAR=0xa0) as a leap year, but 2100 isn't.
236 */
237 if (tm->tm_year < 100 || tm->tm_year >= 200)
238 return -EINVAL;
239
Thierry Redingf803f0d2012-12-17 16:02:44 -0800240 err = pcf8523_stop_rtc(client);
241 if (err < 0)
242 return err;
243
244 regs[0] = REG_SECONDS;
Alexandre Belloniede44c92016-03-03 09:55:47 +0100245 /* This will purposely overwrite REG_SECONDS_OS */
Thierry Redingf803f0d2012-12-17 16:02:44 -0800246 regs[1] = bin2bcd(tm->tm_sec);
247 regs[2] = bin2bcd(tm->tm_min);
248 regs[3] = bin2bcd(tm->tm_hour);
249 regs[4] = bin2bcd(tm->tm_mday);
250 regs[5] = tm->tm_wday;
Chris Cui35738392014-05-06 12:49:58 -0700251 regs[6] = bin2bcd(tm->tm_mon + 1);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800252 regs[7] = bin2bcd(tm->tm_year - 100);
253
254 msg.addr = client->addr;
255 msg.flags = 0;
256 msg.len = sizeof(regs);
257 msg.buf = regs;
258
259 err = i2c_transfer(client->adapter, &msg, 1);
260 if (err < 0) {
261 /*
262 * If the time cannot be set, restart the RTC anyway. Note
263 * that errors are ignored if the RTC cannot be started so
264 * that we have a chance to propagate the original error.
265 */
266 pcf8523_start_rtc(client);
267 return err;
268 }
269
270 return pcf8523_start_rtc(client);
271}
272
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800273#ifdef CONFIG_RTC_INTF_DEV
274static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
275 unsigned long arg)
276{
277 struct i2c_client *client = to_i2c_client(dev);
Baruch Siachecb4a352018-12-05 17:00:09 +0200278 int ret;
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800279
280 switch (cmd) {
281 case RTC_VL_READ:
Baruch Siachecb4a352018-12-05 17:00:09 +0200282 ret = pcf8523_voltage_low(client);
283 if (ret < 0)
284 return ret;
Alexandre Belloni244cf8f2019-12-14 23:02:47 +0100285 if (ret)
286 ret = RTC_VL_BACKUP_LOW;
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800287
Alexandre Belloni244cf8f2019-12-14 23:02:47 +0100288 return put_user(ret, (unsigned int __user *)arg);
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800289
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800290 default:
291 return -ENOIOCTLCMD;
292 }
293}
294#else
295#define pcf8523_rtc_ioctl NULL
296#endif
297
Russell Kingbc3bee02017-09-29 11:23:36 +0100298static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
299{
300 struct i2c_client *client = to_i2c_client(dev);
301 int err;
302 u8 value;
303 s8 val;
304
305 err = pcf8523_read(client, REG_OFFSET, &value);
306 if (err < 0)
307 return err;
308
309 /* sign extend the 7-bit offset value */
310 val = value << 1;
311 *offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
312
313 return 0;
314}
315
316static int pcf8523_rtc_set_offset(struct device *dev, long offset)
317{
318 struct i2c_client *client = to_i2c_client(dev);
319 long reg_m0, reg_m1;
320 u8 value;
321
322 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
323 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
324
325 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
326 value = reg_m0 & 0x7f;
327 else
328 value = (reg_m1 & 0x7f) | REG_OFFSET_MODE;
329
330 return pcf8523_write(client, REG_OFFSET, value);
331}
332
Thierry Redingf803f0d2012-12-17 16:02:44 -0800333static const struct rtc_class_ops pcf8523_rtc_ops = {
334 .read_time = pcf8523_rtc_read_time,
335 .set_time = pcf8523_rtc_set_time,
Jesper Nilssonf32bc702013-02-21 16:44:27 -0800336 .ioctl = pcf8523_rtc_ioctl,
Russell Kingbc3bee02017-09-29 11:23:36 +0100337 .read_offset = pcf8523_rtc_read_offset,
338 .set_offset = pcf8523_rtc_set_offset,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800339};
340
341static int pcf8523_probe(struct i2c_client *client,
342 const struct i2c_device_id *id)
343{
Nobuhiro Iwamatsu93966242019-11-23 18:08:38 +0900344 struct rtc_device *rtc;
Thierry Redingf803f0d2012-12-17 16:02:44 -0800345 int err;
346
347 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
348 return -ENODEV;
349
Sam Ravnborg189927e2019-01-19 10:00:30 +0100350 err = pcf8523_load_capacitance(client);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800351 if (err < 0)
Sam Ravnborg189927e2019-01-19 10:00:30 +0100352 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
353 err);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800354
355 err = pcf8523_set_pm(client, 0);
356 if (err < 0)
357 return err;
358
Nobuhiro Iwamatsu93966242019-11-23 18:08:38 +0900359 rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800360 &pcf8523_rtc_ops, THIS_MODULE);
Nobuhiro Iwamatsu93966242019-11-23 18:08:38 +0900361 if (IS_ERR(rtc))
362 return PTR_ERR(rtc);
Thierry Redingf803f0d2012-12-17 16:02:44 -0800363
364 return 0;
365}
366
Thierry Redingf803f0d2012-12-17 16:02:44 -0800367static const struct i2c_device_id pcf8523_id[] = {
368 { "pcf8523", 0 },
369 { }
370};
371MODULE_DEVICE_TABLE(i2c, pcf8523_id);
372
373#ifdef CONFIG_OF
374static const struct of_device_id pcf8523_of_match[] = {
375 { .compatible = "nxp,pcf8523" },
Alexandre Belloni7c617e02018-12-18 22:52:12 +0100376 { .compatible = "microcrystal,rv8523" },
Thierry Redingf803f0d2012-12-17 16:02:44 -0800377 { }
378};
379MODULE_DEVICE_TABLE(of, pcf8523_of_match);
380#endif
381
382static struct i2c_driver pcf8523_driver = {
383 .driver = {
384 .name = DRIVER_NAME,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800385 .of_match_table = of_match_ptr(pcf8523_of_match),
386 },
387 .probe = pcf8523_probe,
Thierry Redingf803f0d2012-12-17 16:02:44 -0800388 .id_table = pcf8523_id,
389};
390module_i2c_driver(pcf8523_driver);
391
392MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
393MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
394MODULE_LICENSE("GPL v2");