]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/rtc/rtc-pcf2127.c
rtc: pcf2127: add watchdog feature support
[linux.git] / drivers / rtc / rtc-pcf2127.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * An I2C and SPI driver for the NXP PCF2127/29 RTC
4  * Copyright 2013 Til-Technologies
5  *
6  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
7  *
8  * Watchdog and tamper functions
9  * Author: Bruno Thomsen <bruno.thomsen@gmail.com>
10  *
11  * based on the other drivers in this same directory.
12  *
13  * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
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 #include <linux/watchdog.h>
25
26 /* Control register 1 */
27 #define PCF2127_REG_CTRL1               0x00
28 /* Control register 2 */
29 #define PCF2127_REG_CTRL2               0x01
30 /* Control register 3 */
31 #define PCF2127_REG_CTRL3               0x02
32 #define PCF2127_BIT_CTRL3_BLF                   BIT(2)
33 /* Time and date registers */
34 #define PCF2127_REG_SC                  0x03
35 #define PCF2127_BIT_SC_OSF                      BIT(7)
36 #define PCF2127_REG_MN                  0x04
37 #define PCF2127_REG_HR                  0x05
38 #define PCF2127_REG_DM                  0x06
39 #define PCF2127_REG_DW                  0x07
40 #define PCF2127_REG_MO                  0x08
41 #define PCF2127_REG_YR                  0x09
42 /* Watchdog registers */
43 #define PCF2127_REG_WD_CTL              0x10
44 #define PCF2127_BIT_WD_CTL_TF0                  BIT(0)
45 #define PCF2127_BIT_WD_CTL_TF1                  BIT(1)
46 #define PCF2127_BIT_WD_CTL_CD0                  BIT(6)
47 #define PCF2127_BIT_WD_CTL_CD1                  BIT(7)
48 #define PCF2127_REG_WD_VAL              0x11
49 /*
50  * RAM registers
51  * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
52  * battery backed and can survive a power outage.
53  * PCF2129 doesn't have this feature.
54  */
55 #define PCF2127_REG_RAM_ADDR_MSB        0x1A
56 #define PCF2127_REG_RAM_WRT_CMD         0x1C
57 #define PCF2127_REG_RAM_RD_CMD          0x1D
58
59 /* Watchdog timer value constants */
60 #define PCF2127_WD_VAL_STOP             0
61 #define PCF2127_WD_VAL_MIN              2
62 #define PCF2127_WD_VAL_MAX              255
63 #define PCF2127_WD_VAL_DEFAULT          60
64
65 struct pcf2127 {
66         struct rtc_device *rtc;
67         struct watchdog_device wdd;
68         struct regmap *regmap;
69 };
70
71 /*
72  * In the routines that deal directly with the pcf2127 hardware, we use
73  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
74  */
75 static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
76 {
77         struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
78         unsigned char buf[10];
79         int ret;
80
81         /*
82          * Avoid reading CTRL2 register as it causes WD_VAL register
83          * value to reset to 0 which means watchdog is stopped.
84          */
85         ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3,
86                                (buf + PCF2127_REG_CTRL3),
87                                ARRAY_SIZE(buf) - PCF2127_REG_CTRL3);
88         if (ret) {
89                 dev_err(dev, "%s: read error\n", __func__);
90                 return ret;
91         }
92
93         if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF)
94                 dev_info(dev,
95                         "low voltage detected, check/replace RTC battery.\n");
96
97         /* Clock integrity is not guaranteed when OSF flag is set. */
98         if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) {
99                 /*
100                  * no need clear the flag here,
101                  * it will be cleared once the new date is saved
102                  */
103                 dev_warn(dev,
104                          "oscillator stop detected, date/time is not reliable\n");
105                 return -EINVAL;
106         }
107
108         dev_dbg(dev,
109                 "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, "
110                 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
111                 __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC],
112                 buf[PCF2127_REG_MN], buf[PCF2127_REG_HR],
113                 buf[PCF2127_REG_DM], buf[PCF2127_REG_DW],
114                 buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]);
115
116         tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
117         tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
118         tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
119         tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
120         tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
121         tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
122         tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
123         if (tm->tm_year < 70)
124                 tm->tm_year += 100;     /* assume we are in 1970...2069 */
125
126         dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
127                 "mday=%d, mon=%d, year=%d, wday=%d\n",
128                 __func__,
129                 tm->tm_sec, tm->tm_min, tm->tm_hour,
130                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
131
132         return 0;
133 }
134
135 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
136 {
137         struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
138         unsigned char buf[7];
139         int i = 0, err;
140
141         dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
142                 "mday=%d, mon=%d, year=%d, wday=%d\n",
143                 __func__,
144                 tm->tm_sec, tm->tm_min, tm->tm_hour,
145                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
146
147         /* hours, minutes and seconds */
148         buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
149         buf[i++] = bin2bcd(tm->tm_min);
150         buf[i++] = bin2bcd(tm->tm_hour);
151         buf[i++] = bin2bcd(tm->tm_mday);
152         buf[i++] = tm->tm_wday & 0x07;
153
154         /* month, 1 - 12 */
155         buf[i++] = bin2bcd(tm->tm_mon + 1);
156
157         /* year */
158         buf[i++] = bin2bcd(tm->tm_year % 100);
159
160         /* write register's data */
161         err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
162         if (err) {
163                 dev_err(dev,
164                         "%s: err=%d", __func__, err);
165                 return err;
166         }
167
168         return 0;
169 }
170
171 #ifdef CONFIG_RTC_INTF_DEV
172 static int pcf2127_rtc_ioctl(struct device *dev,
173                                 unsigned int cmd, unsigned long arg)
174 {
175         struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
176         int touser;
177         int ret;
178
179         switch (cmd) {
180         case RTC_VL_READ:
181                 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
182                 if (ret)
183                         return ret;
184
185                 touser = touser & PCF2127_BIT_CTRL3_BLF ? 1 : 0;
186
187                 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
188                         return -EFAULT;
189                 return 0;
190         default:
191                 return -ENOIOCTLCMD;
192         }
193 }
194 #else
195 #define pcf2127_rtc_ioctl NULL
196 #endif
197
198 static const struct rtc_class_ops pcf2127_rtc_ops = {
199         .ioctl          = pcf2127_rtc_ioctl,
200         .read_time      = pcf2127_rtc_read_time,
201         .set_time       = pcf2127_rtc_set_time,
202 };
203
204 static int pcf2127_nvmem_read(void *priv, unsigned int offset,
205                               void *val, size_t bytes)
206 {
207         struct pcf2127 *pcf2127 = priv;
208         int ret;
209         unsigned char offsetbuf[] = { offset >> 8, offset };
210
211         ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
212                                 offsetbuf, 2);
213         if (ret)
214                 return ret;
215
216         ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD,
217                                val, bytes);
218
219         return ret ?: bytes;
220 }
221
222 static int pcf2127_nvmem_write(void *priv, unsigned int offset,
223                                void *val, size_t bytes)
224 {
225         struct pcf2127 *pcf2127 = priv;
226         int ret;
227         unsigned char offsetbuf[] = { offset >> 8, offset };
228
229         ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB,
230                                 offsetbuf, 2);
231         if (ret)
232                 return ret;
233
234         ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD,
235                                 val, bytes);
236
237         return ret ?: bytes;
238 }
239
240 /* watchdog driver */
241
242 static int pcf2127_wdt_ping(struct watchdog_device *wdd)
243 {
244         struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
245
246         return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout);
247 }
248
249 /*
250  * Restart watchdog timer if feature is active.
251  *
252  * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate,
253  * since register also contain control/status flags for other features.
254  * Always call this function after reading CTRL2 register.
255  */
256 static int pcf2127_wdt_active_ping(struct watchdog_device *wdd)
257 {
258         int ret = 0;
259
260         if (watchdog_active(wdd)) {
261                 ret = pcf2127_wdt_ping(wdd);
262                 if (ret)
263                         dev_err(wdd->parent,
264                                 "%s: watchdog restart failed, ret=%d\n",
265                                 __func__, ret);
266         }
267
268         return ret;
269 }
270
271 static int pcf2127_wdt_start(struct watchdog_device *wdd)
272 {
273         return pcf2127_wdt_ping(wdd);
274 }
275
276 static int pcf2127_wdt_stop(struct watchdog_device *wdd)
277 {
278         struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd);
279
280         return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL,
281                             PCF2127_WD_VAL_STOP);
282 }
283
284 static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd,
285                                    unsigned int new_timeout)
286 {
287         dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n",
288                 new_timeout, wdd->timeout);
289
290         wdd->timeout = new_timeout;
291
292         return pcf2127_wdt_active_ping(wdd);
293 }
294
295 static const struct watchdog_info pcf2127_wdt_info = {
296         .identity = "NXP PCF2127/PCF2129 Watchdog",
297         .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
298 };
299
300 static const struct watchdog_ops pcf2127_watchdog_ops = {
301         .owner = THIS_MODULE,
302         .start = pcf2127_wdt_start,
303         .stop = pcf2127_wdt_stop,
304         .ping = pcf2127_wdt_ping,
305         .set_timeout = pcf2127_wdt_set_timeout,
306 };
307
308 static int pcf2127_probe(struct device *dev, struct regmap *regmap,
309                         const char *name, bool has_nvmem)
310 {
311         struct pcf2127 *pcf2127;
312         int ret = 0;
313
314         dev_dbg(dev, "%s\n", __func__);
315
316         pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
317         if (!pcf2127)
318                 return -ENOMEM;
319
320         pcf2127->regmap = regmap;
321
322         dev_set_drvdata(dev, pcf2127);
323
324         pcf2127->rtc = devm_rtc_allocate_device(dev);
325         if (IS_ERR(pcf2127->rtc))
326                 return PTR_ERR(pcf2127->rtc);
327
328         pcf2127->rtc->ops = &pcf2127_rtc_ops;
329
330         pcf2127->wdd.parent = dev;
331         pcf2127->wdd.info = &pcf2127_wdt_info;
332         pcf2127->wdd.ops = &pcf2127_watchdog_ops;
333         pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN;
334         pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX;
335         pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT;
336         pcf2127->wdd.min_hw_heartbeat_ms = 500;
337
338         watchdog_set_drvdata(&pcf2127->wdd, pcf2127);
339
340         if (has_nvmem) {
341                 struct nvmem_config nvmem_cfg = {
342                         .priv = pcf2127,
343                         .reg_read = pcf2127_nvmem_read,
344                         .reg_write = pcf2127_nvmem_write,
345                         .size = 512,
346                 };
347
348                 ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
349         }
350
351         /*
352          * Watchdog timer enabled and reset pin /RST activated when timed out.
353          * Select 1Hz clock source for watchdog timer.
354          * Timer is not started until WD_VAL is loaded with a valid value.
355          * Note: Countdown timer disabled and not available.
356          */
357         ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL,
358                                  PCF2127_BIT_WD_CTL_CD1 |
359                                  PCF2127_BIT_WD_CTL_CD0 |
360                                  PCF2127_BIT_WD_CTL_TF1 |
361                                  PCF2127_BIT_WD_CTL_TF0,
362                                  PCF2127_BIT_WD_CTL_CD1 |
363                                  PCF2127_BIT_WD_CTL_CD0 |
364                                  PCF2127_BIT_WD_CTL_TF1);
365         if (ret) {
366                 dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__);
367                 return ret;
368         }
369
370         ret = devm_watchdog_register_device(dev, &pcf2127->wdd);
371         if (ret)
372                 return ret;
373
374         return rtc_register_device(pcf2127->rtc);
375 }
376
377 #ifdef CONFIG_OF
378 static const struct of_device_id pcf2127_of_match[] = {
379         { .compatible = "nxp,pcf2127" },
380         { .compatible = "nxp,pcf2129" },
381         {}
382 };
383 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
384 #endif
385
386 #if IS_ENABLED(CONFIG_I2C)
387
388 static int pcf2127_i2c_write(void *context, const void *data, size_t count)
389 {
390         struct device *dev = context;
391         struct i2c_client *client = to_i2c_client(dev);
392         int ret;
393
394         ret = i2c_master_send(client, data, count);
395         if (ret != count)
396                 return ret < 0 ? ret : -EIO;
397
398         return 0;
399 }
400
401 static int pcf2127_i2c_gather_write(void *context,
402                                 const void *reg, size_t reg_size,
403                                 const void *val, size_t val_size)
404 {
405         struct device *dev = context;
406         struct i2c_client *client = to_i2c_client(dev);
407         int ret;
408         void *buf;
409
410         if (WARN_ON(reg_size != 1))
411                 return -EINVAL;
412
413         buf = kmalloc(val_size + 1, GFP_KERNEL);
414         if (!buf)
415                 return -ENOMEM;
416
417         memcpy(buf, reg, 1);
418         memcpy(buf + 1, val, val_size);
419
420         ret = i2c_master_send(client, buf, val_size + 1);
421
422         kfree(buf);
423
424         if (ret != val_size + 1)
425                 return ret < 0 ? ret : -EIO;
426
427         return 0;
428 }
429
430 static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
431                                 void *val, size_t val_size)
432 {
433         struct device *dev = context;
434         struct i2c_client *client = to_i2c_client(dev);
435         int ret;
436
437         if (WARN_ON(reg_size != 1))
438                 return -EINVAL;
439
440         ret = i2c_master_send(client, reg, 1);
441         if (ret != 1)
442                 return ret < 0 ? ret : -EIO;
443
444         ret = i2c_master_recv(client, val, val_size);
445         if (ret != val_size)
446                 return ret < 0 ? ret : -EIO;
447
448         return 0;
449 }
450
451 /*
452  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
453  * is that the STOP condition is required between set register address and
454  * read register data when reading from registers.
455  */
456 static const struct regmap_bus pcf2127_i2c_regmap = {
457         .write = pcf2127_i2c_write,
458         .gather_write = pcf2127_i2c_gather_write,
459         .read = pcf2127_i2c_read,
460 };
461
462 static struct i2c_driver pcf2127_i2c_driver;
463
464 static int pcf2127_i2c_probe(struct i2c_client *client,
465                                 const struct i2c_device_id *id)
466 {
467         struct regmap *regmap;
468         static const struct regmap_config config = {
469                 .reg_bits = 8,
470                 .val_bits = 8,
471         };
472
473         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
474                 return -ENODEV;
475
476         regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
477                                         &client->dev, &config);
478         if (IS_ERR(regmap)) {
479                 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
480                         __func__, PTR_ERR(regmap));
481                 return PTR_ERR(regmap);
482         }
483
484         return pcf2127_probe(&client->dev, regmap,
485                              pcf2127_i2c_driver.driver.name, id->driver_data);
486 }
487
488 static const struct i2c_device_id pcf2127_i2c_id[] = {
489         { "pcf2127", 1 },
490         { "pcf2129", 0 },
491         { }
492 };
493 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
494
495 static struct i2c_driver pcf2127_i2c_driver = {
496         .driver         = {
497                 .name   = "rtc-pcf2127-i2c",
498                 .of_match_table = of_match_ptr(pcf2127_of_match),
499         },
500         .probe          = pcf2127_i2c_probe,
501         .id_table       = pcf2127_i2c_id,
502 };
503
504 static int pcf2127_i2c_register_driver(void)
505 {
506         return i2c_add_driver(&pcf2127_i2c_driver);
507 }
508
509 static void pcf2127_i2c_unregister_driver(void)
510 {
511         i2c_del_driver(&pcf2127_i2c_driver);
512 }
513
514 #else
515
516 static int pcf2127_i2c_register_driver(void)
517 {
518         return 0;
519 }
520
521 static void pcf2127_i2c_unregister_driver(void)
522 {
523 }
524
525 #endif
526
527 #if IS_ENABLED(CONFIG_SPI_MASTER)
528
529 static struct spi_driver pcf2127_spi_driver;
530
531 static int pcf2127_spi_probe(struct spi_device *spi)
532 {
533         static const struct regmap_config config = {
534                 .reg_bits = 8,
535                 .val_bits = 8,
536                 .read_flag_mask = 0xa0,
537                 .write_flag_mask = 0x20,
538         };
539         struct regmap *regmap;
540
541         regmap = devm_regmap_init_spi(spi, &config);
542         if (IS_ERR(regmap)) {
543                 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
544                         __func__, PTR_ERR(regmap));
545                 return PTR_ERR(regmap);
546         }
547
548         return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name,
549                              spi_get_device_id(spi)->driver_data);
550 }
551
552 static const struct spi_device_id pcf2127_spi_id[] = {
553         { "pcf2127", 1 },
554         { "pcf2129", 0 },
555         { }
556 };
557 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
558
559 static struct spi_driver pcf2127_spi_driver = {
560         .driver         = {
561                 .name   = "rtc-pcf2127-spi",
562                 .of_match_table = of_match_ptr(pcf2127_of_match),
563         },
564         .probe          = pcf2127_spi_probe,
565         .id_table       = pcf2127_spi_id,
566 };
567
568 static int pcf2127_spi_register_driver(void)
569 {
570         return spi_register_driver(&pcf2127_spi_driver);
571 }
572
573 static void pcf2127_spi_unregister_driver(void)
574 {
575         spi_unregister_driver(&pcf2127_spi_driver);
576 }
577
578 #else
579
580 static int pcf2127_spi_register_driver(void)
581 {
582         return 0;
583 }
584
585 static void pcf2127_spi_unregister_driver(void)
586 {
587 }
588
589 #endif
590
591 static int __init pcf2127_init(void)
592 {
593         int ret;
594
595         ret = pcf2127_i2c_register_driver();
596         if (ret) {
597                 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
598                 return ret;
599         }
600
601         ret = pcf2127_spi_register_driver();
602         if (ret) {
603                 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
604                 pcf2127_i2c_unregister_driver();
605         }
606
607         return ret;
608 }
609 module_init(pcf2127_init)
610
611 static void __exit pcf2127_exit(void)
612 {
613         pcf2127_spi_unregister_driver();
614         pcf2127_i2c_unregister_driver();
615 }
616 module_exit(pcf2127_exit)
617
618 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
619 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
620 MODULE_LICENSE("GPL v2");