blob: 367e0f803440c600f1da817f16883bacb3918f7d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * An I2C and SPI driver for the NXP PCF2127/29 RTC
3 * Copyright 2013 Til-Technologies
4 *
5 * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
6 *
7 * based on the other drivers in this same directory.
8 *
9 * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/i2c.h>
17#include <linux/spi/spi.h>
18#include <linux/bcd.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/regmap.h>
24
25#define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
26#define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
27
28#define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
29#define PCF2127_REG_CTRL3_BLF BIT(2)
30
31#define PCF2127_REG_SC (0x03) /* datetime */
32#define PCF2127_REG_MN (0x04)
33#define PCF2127_REG_HR (0x05)
34#define PCF2127_REG_DM (0x06)
35#define PCF2127_REG_DW (0x07)
36#define PCF2127_REG_MO (0x08)
37#define PCF2127_REG_YR (0x09)
38
39#define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
40
41struct pcf2127 {
42 struct rtc_device *rtc;
43 struct regmap *regmap;
44};
45
46/*
47 * In the routines that deal directly with the pcf2127 hardware, we use
48 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
49 */
50static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
51{
52 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
53 unsigned char buf[10];
54 int ret;
55
56 /*
57 * Avoid reading CTRL2 register as it causes WD_VAL register
58 * value to reset to 0 which means watchdog is stopped.
59 */
60 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3,
61 (buf + PCF2127_REG_CTRL3),
62 ARRAY_SIZE(buf) - PCF2127_REG_CTRL3);
63 if (ret) {
64 dev_err(dev, "%s: read error\n", __func__);
65 return ret;
66 }
67
68 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
69 dev_info(dev,
70 "low voltage detected, check/replace RTC battery.\n");
71
72 if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
73 /*
74 * no need clear the flag here,
75 * it will be cleared once the new date is saved
76 */
77 dev_warn(dev,
78 "oscillator stop detected, date/time is not reliable\n");
79 return -EINVAL;
80 }
81
82 dev_dbg(dev,
83 "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, "
84 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
85 __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC],
86 buf[PCF2127_REG_MN], buf[PCF2127_REG_HR],
87 buf[PCF2127_REG_DM], buf[PCF2127_REG_DW],
88 buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]);
89
90 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
91 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
92 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
93 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
94 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
95 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
96 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
97 if (tm->tm_year < 70)
98 tm->tm_year += 100; /* assume we are in 1970...2069 */
99
100 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
101 "mday=%d, mon=%d, year=%d, wday=%d\n",
102 __func__,
103 tm->tm_sec, tm->tm_min, tm->tm_hour,
104 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
105
106 return rtc_valid_tm(tm);
107}
108
109static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
110{
111 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
112 unsigned char buf[7];
113 int i = 0, err;
114
115 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
116 "mday=%d, mon=%d, year=%d, wday=%d\n",
117 __func__,
118 tm->tm_sec, tm->tm_min, tm->tm_hour,
119 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
120
121 /* hours, minutes and seconds */
122 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
123 buf[i++] = bin2bcd(tm->tm_min);
124 buf[i++] = bin2bcd(tm->tm_hour);
125 buf[i++] = bin2bcd(tm->tm_mday);
126 buf[i++] = tm->tm_wday & 0x07;
127
128 /* month, 1 - 12 */
129 buf[i++] = bin2bcd(tm->tm_mon + 1);
130
131 /* year */
132 buf[i++] = bin2bcd(tm->tm_year % 100);
133
134 /* write register's data */
135 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
136 if (err) {
137 dev_err(dev,
138 "%s: err=%d", __func__, err);
139 return err;
140 }
141
142 return 0;
143}
144
145#ifdef CONFIG_RTC_INTF_DEV
146static int pcf2127_rtc_ioctl(struct device *dev,
147 unsigned int cmd, unsigned long arg)
148{
149 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
150 int touser;
151 int ret;
152
153 switch (cmd) {
154 case RTC_VL_READ:
155 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
156 if (ret)
157 return ret;
158
159 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
160
161 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
162 return -EFAULT;
163 return 0;
164 default:
165 return -ENOIOCTLCMD;
166 }
167}
168#else
169#define pcf2127_rtc_ioctl NULL
170#endif
171
172static const struct rtc_class_ops pcf2127_rtc_ops = {
173 .ioctl = pcf2127_rtc_ioctl,
174 .read_time = pcf2127_rtc_read_time,
175 .set_time = pcf2127_rtc_set_time,
176};
177
178static int pcf2127_probe(struct device *dev, struct regmap *regmap,
179 const char *name)
180{
181 struct pcf2127 *pcf2127;
182
183 dev_dbg(dev, "%s\n", __func__);
184
185 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
186 if (!pcf2127)
187 return -ENOMEM;
188
189 pcf2127->regmap = regmap;
190
191 dev_set_drvdata(dev, pcf2127);
192
193 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
194 THIS_MODULE);
195
196 return PTR_ERR_OR_ZERO(pcf2127->rtc);
197}
198
199#ifdef CONFIG_OF
200static const struct of_device_id pcf2127_of_match[] = {
201 { .compatible = "nxp,pcf2127" },
202 { .compatible = "nxp,pcf2129" },
203 {}
204};
205MODULE_DEVICE_TABLE(of, pcf2127_of_match);
206#endif
207
208#if IS_ENABLED(CONFIG_I2C)
209
210static int pcf2127_i2c_write(void *context, const void *data, size_t count)
211{
212 struct device *dev = context;
213 struct i2c_client *client = to_i2c_client(dev);
214 int ret;
215
216 ret = i2c_master_send(client, data, count);
217 if (ret != count)
218 return ret < 0 ? ret : -EIO;
219
220 return 0;
221}
222
223static int pcf2127_i2c_gather_write(void *context,
224 const void *reg, size_t reg_size,
225 const void *val, size_t val_size)
226{
227 struct device *dev = context;
228 struct i2c_client *client = to_i2c_client(dev);
229 int ret;
230 void *buf;
231
232 if (WARN_ON(reg_size != 1))
233 return -EINVAL;
234
235 buf = kmalloc(val_size + 1, GFP_KERNEL);
236 if (!buf)
237 return -ENOMEM;
238
239 memcpy(buf, reg, 1);
240 memcpy(buf + 1, val, val_size);
241
242 ret = i2c_master_send(client, buf, val_size + 1);
243
244 kfree(buf);
245
246 if (ret != val_size + 1)
247 return ret < 0 ? ret : -EIO;
248
249 return 0;
250}
251
252static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
253 void *val, size_t val_size)
254{
255 struct device *dev = context;
256 struct i2c_client *client = to_i2c_client(dev);
257 int ret;
258
259 if (WARN_ON(reg_size != 1))
260 return -EINVAL;
261
262 ret = i2c_master_send(client, reg, 1);
263 if (ret != 1)
264 return ret < 0 ? ret : -EIO;
265
266 ret = i2c_master_recv(client, val, val_size);
267 if (ret != val_size)
268 return ret < 0 ? ret : -EIO;
269
270 return 0;
271}
272
273/*
274 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
275 * is that the STOP condition is required between set register address and
276 * read register data when reading from registers.
277 */
278static const struct regmap_bus pcf2127_i2c_regmap = {
279 .write = pcf2127_i2c_write,
280 .gather_write = pcf2127_i2c_gather_write,
281 .read = pcf2127_i2c_read,
282};
283
284static struct i2c_driver pcf2127_i2c_driver;
285
286static int pcf2127_i2c_probe(struct i2c_client *client,
287 const struct i2c_device_id *id)
288{
289 struct regmap *regmap;
290 static const struct regmap_config config = {
291 .reg_bits = 8,
292 .val_bits = 8,
293 };
294
295 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
296 return -ENODEV;
297
298 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
299 &client->dev, &config);
300 if (IS_ERR(regmap)) {
301 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
302 __func__, PTR_ERR(regmap));
303 return PTR_ERR(regmap);
304 }
305
306 return pcf2127_probe(&client->dev, regmap,
307 pcf2127_i2c_driver.driver.name);
308}
309
310static const struct i2c_device_id pcf2127_i2c_id[] = {
311 { "pcf2127", 0 },
312 { "pcf2129", 0 },
313 { }
314};
315MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
316
317static struct i2c_driver pcf2127_i2c_driver = {
318 .driver = {
319 .name = "rtc-pcf2127-i2c",
320 .of_match_table = of_match_ptr(pcf2127_of_match),
321 },
322 .probe = pcf2127_i2c_probe,
323 .id_table = pcf2127_i2c_id,
324};
325
326static int pcf2127_i2c_register_driver(void)
327{
328 return i2c_add_driver(&pcf2127_i2c_driver);
329}
330
331static void pcf2127_i2c_unregister_driver(void)
332{
333 i2c_del_driver(&pcf2127_i2c_driver);
334}
335
336#else
337
338static int pcf2127_i2c_register_driver(void)
339{
340 return 0;
341}
342
343static void pcf2127_i2c_unregister_driver(void)
344{
345}
346
347#endif
348
349#if IS_ENABLED(CONFIG_SPI_MASTER)
350
351static struct spi_driver pcf2127_spi_driver;
352
353static int pcf2127_spi_probe(struct spi_device *spi)
354{
355 static const struct regmap_config config = {
356 .reg_bits = 8,
357 .val_bits = 8,
358 .read_flag_mask = 0xa0,
359 .write_flag_mask = 0x20,
360 };
361 struct regmap *regmap;
362
363 regmap = devm_regmap_init_spi(spi, &config);
364 if (IS_ERR(regmap)) {
365 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
366 __func__, PTR_ERR(regmap));
367 return PTR_ERR(regmap);
368 }
369
370 return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name);
371}
372
373static const struct spi_device_id pcf2127_spi_id[] = {
374 { "pcf2127", 0 },
375 { "pcf2129", 0 },
376 { }
377};
378MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
379
380static struct spi_driver pcf2127_spi_driver = {
381 .driver = {
382 .name = "rtc-pcf2127-spi",
383 .of_match_table = of_match_ptr(pcf2127_of_match),
384 },
385 .probe = pcf2127_spi_probe,
386 .id_table = pcf2127_spi_id,
387};
388
389static int pcf2127_spi_register_driver(void)
390{
391 return spi_register_driver(&pcf2127_spi_driver);
392}
393
394static void pcf2127_spi_unregister_driver(void)
395{
396 spi_unregister_driver(&pcf2127_spi_driver);
397}
398
399#else
400
401static int pcf2127_spi_register_driver(void)
402{
403 return 0;
404}
405
406static void pcf2127_spi_unregister_driver(void)
407{
408}
409
410#endif
411
412static int __init pcf2127_init(void)
413{
414 int ret;
415
416 ret = pcf2127_i2c_register_driver();
417 if (ret) {
418 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
419 return ret;
420 }
421
422 ret = pcf2127_spi_register_driver();
423 if (ret) {
424 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
425 pcf2127_i2c_unregister_driver();
426 }
427
428 return ret;
429}
430module_init(pcf2127_init)
431
432static void __exit pcf2127_exit(void)
433{
434 pcf2127_spi_unregister_driver();
435 pcf2127_i2c_unregister_driver();
436}
437module_exit(pcf2127_exit)
438
439MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
440MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
441MODULE_LICENSE("GPL v2");