blob: 8a3667e761ddbb2abfd1efad183f925d8b120f4f [file] [log] [blame]
Renaud Cerrato18cb6362013-07-03 15:08:01 -07001/*
Akinobu Mitacee2cc22016-03-14 23:45:01 +09002 * An I2C and SPI driver for the NXP PCF2127/29 RTC
Renaud Cerrato18cb6362013-07-03 15:08:01 -07003 * 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 *
Akinobu Mitacee2cc22016-03-14 23:45:01 +09009 * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
Renaud Cerrato18cb6362013-07-03 15:08:01 -070010 *
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>
Akinobu Mita9408ec12016-03-14 23:45:00 +090017#include <linux/spi/spi.h>
Renaud Cerrato18cb6362013-07-03 15:08:01 -070018#include <linux/bcd.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/of.h>
Akinobu Mita907b3262016-03-14 23:44:59 +090023#include <linux/regmap.h>
Renaud Cerrato18cb6362013-07-03 15:08:01 -070024
Renaud Cerrato18cb6362013-07-03 15:08:01 -070025#define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
26#define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020027
Renaud Cerrato18cb6362013-07-03 15:08:01 -070028#define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020029#define PCF2127_REG_CTRL3_BLF BIT(2)
30
Renaud Cerrato18cb6362013-07-03 15:08:01 -070031#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
Andrea Scian653ebd72015-06-16 11:39:47 +020039#define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
40
Renaud Cerrato18cb6362013-07-03 15:08:01 -070041struct pcf2127 {
42 struct rtc_device *rtc;
Akinobu Mita907b3262016-03-14 23:44:59 +090043 struct regmap *regmap;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070044};
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 */
Akinobu Mita907b3262016-03-14 23:44:59 +090050static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
Renaud Cerrato18cb6362013-07-03 15:08:01 -070051{
Akinobu Mita907b3262016-03-14 23:44:59 +090052 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
53 unsigned char buf[10];
54 int ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070055
Akinobu Mita907b3262016-03-14 23:44:59 +090056 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, buf,
57 sizeof(buf));
58 if (ret) {
59 dev_err(dev, "%s: read error\n", __func__);
60 return ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070061 }
62
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020063 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
Akinobu Mita907b3262016-03-14 23:44:59 +090064 dev_info(dev,
Andrea Scian653ebd72015-06-16 11:39:47 +020065 "low voltage detected, check/replace RTC battery.\n");
Andrea Scian653ebd72015-06-16 11:39:47 +020066
67 if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
68 /*
69 * no need clear the flag here,
70 * it will be cleared once the new date is saved
71 */
Akinobu Mita907b3262016-03-14 23:44:59 +090072 dev_warn(dev,
Andrea Scian653ebd72015-06-16 11:39:47 +020073 "oscillator stop detected, date/time is not reliable\n");
74 return -EINVAL;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070075 }
76
Akinobu Mita907b3262016-03-14 23:44:59 +090077 dev_dbg(dev,
Renaud Cerrato18cb6362013-07-03 15:08:01 -070078 "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
79 "sec=%02x, min=%02x, hr=%02x, "
80 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
81 __func__,
82 buf[0], buf[1], buf[2],
83 buf[3], buf[4], buf[5],
84 buf[6], buf[7], buf[8], buf[9]);
85
86
87 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
88 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
89 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
90 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
91 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
92 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
93 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
94 if (tm->tm_year < 70)
95 tm->tm_year += 100; /* assume we are in 1970...2069 */
96
Akinobu Mita907b3262016-03-14 23:44:59 +090097 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Renaud Cerrato18cb6362013-07-03 15:08:01 -070098 "mday=%d, mon=%d, year=%d, wday=%d\n",
99 __func__,
100 tm->tm_sec, tm->tm_min, tm->tm_hour,
101 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
102
Andrea Scian821f51c2015-06-16 11:35:19 +0200103 return rtc_valid_tm(tm);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700104}
105
Akinobu Mita907b3262016-03-14 23:44:59 +0900106static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700107{
Akinobu Mita907b3262016-03-14 23:44:59 +0900108 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
109 unsigned char buf[7];
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700110 int i = 0, err;
111
Akinobu Mita907b3262016-03-14 23:44:59 +0900112 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700113 "mday=%d, mon=%d, year=%d, wday=%d\n",
114 __func__,
115 tm->tm_sec, tm->tm_min, tm->tm_hour,
116 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
117
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700118 /* hours, minutes and seconds */
Andrea Scian653ebd72015-06-16 11:39:47 +0200119 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700120 buf[i++] = bin2bcd(tm->tm_min);
121 buf[i++] = bin2bcd(tm->tm_hour);
122 buf[i++] = bin2bcd(tm->tm_mday);
123 buf[i++] = tm->tm_wday & 0x07;
124
125 /* month, 1 - 12 */
126 buf[i++] = bin2bcd(tm->tm_mon + 1);
127
128 /* year */
129 buf[i++] = bin2bcd(tm->tm_year % 100);
130
131 /* write register's data */
Akinobu Mita907b3262016-03-14 23:44:59 +0900132 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
133 if (err) {
134 dev_err(dev,
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700135 "%s: err=%d", __func__, err);
Akinobu Mita907b3262016-03-14 23:44:59 +0900136 return err;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700137 }
138
139 return 0;
140}
141
142#ifdef CONFIG_RTC_INTF_DEV
143static int pcf2127_rtc_ioctl(struct device *dev,
144 unsigned int cmd, unsigned long arg)
145{
Akinobu Mita907b3262016-03-14 23:44:59 +0900146 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200147 int touser;
148 int ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700149
150 switch (cmd) {
151 case RTC_VL_READ:
Akinobu Mita907b3262016-03-14 23:44:59 +0900152 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
153 if (ret)
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200154 return ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700155
Akinobu Mita907b3262016-03-14 23:44:59 +0900156 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200157
158 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700159 return -EFAULT;
160 return 0;
161 default:
162 return -ENOIOCTLCMD;
163 }
164}
165#else
166#define pcf2127_rtc_ioctl NULL
167#endif
168
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700169static const struct rtc_class_ops pcf2127_rtc_ops = {
170 .ioctl = pcf2127_rtc_ioctl,
171 .read_time = pcf2127_rtc_read_time,
172 .set_time = pcf2127_rtc_set_time,
173};
174
Akinobu Mita907b3262016-03-14 23:44:59 +0900175static int pcf2127_probe(struct device *dev, struct regmap *regmap,
176 const char *name)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700177{
178 struct pcf2127 *pcf2127;
179
Akinobu Mita907b3262016-03-14 23:44:59 +0900180 dev_dbg(dev, "%s\n", __func__);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700181
Akinobu Mita907b3262016-03-14 23:44:59 +0900182 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700183 if (!pcf2127)
184 return -ENOMEM;
185
Akinobu Mita907b3262016-03-14 23:44:59 +0900186 pcf2127->regmap = regmap;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700187
Akinobu Mita907b3262016-03-14 23:44:59 +0900188 dev_set_drvdata(dev, pcf2127);
189
190 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
191 THIS_MODULE);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700192
Duan Jiong7ab26cd2014-01-23 15:55:13 -0800193 return PTR_ERR_OR_ZERO(pcf2127->rtc);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700194}
195
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700196#ifdef CONFIG_OF
197static const struct of_device_id pcf2127_of_match[] = {
198 { .compatible = "nxp,pcf2127" },
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900199 { .compatible = "nxp,pcf2129" },
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700200 {}
201};
202MODULE_DEVICE_TABLE(of, pcf2127_of_match);
203#endif
204
Akinobu Mita9408ec12016-03-14 23:45:00 +0900205#if IS_ENABLED(CONFIG_I2C)
206
Akinobu Mita907b3262016-03-14 23:44:59 +0900207static int pcf2127_i2c_write(void *context, const void *data, size_t count)
208{
209 struct device *dev = context;
210 struct i2c_client *client = to_i2c_client(dev);
211 int ret;
212
213 ret = i2c_master_send(client, data, count);
214 if (ret != count)
215 return ret < 0 ? ret : -EIO;
216
217 return 0;
218}
219
220static int pcf2127_i2c_gather_write(void *context,
221 const void *reg, size_t reg_size,
222 const void *val, size_t val_size)
223{
224 struct device *dev = context;
225 struct i2c_client *client = to_i2c_client(dev);
226 int ret;
227 void *buf;
228
229 if (WARN_ON(reg_size != 1))
230 return -EINVAL;
231
232 buf = kmalloc(val_size + 1, GFP_KERNEL);
233 if (!buf)
234 return -ENOMEM;
235
236 memcpy(buf, reg, 1);
237 memcpy(buf + 1, val, val_size);
238
239 ret = i2c_master_send(client, buf, val_size + 1);
Xulin Sunab509b42018-11-06 16:42:19 +0800240
241 kfree(buf);
242
Akinobu Mita907b3262016-03-14 23:44:59 +0900243 if (ret != val_size + 1)
244 return ret < 0 ? ret : -EIO;
245
246 return 0;
247}
248
249static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
250 void *val, size_t val_size)
251{
252 struct device *dev = context;
253 struct i2c_client *client = to_i2c_client(dev);
254 int ret;
255
256 if (WARN_ON(reg_size != 1))
257 return -EINVAL;
258
259 ret = i2c_master_send(client, reg, 1);
260 if (ret != 1)
261 return ret < 0 ? ret : -EIO;
262
263 ret = i2c_master_recv(client, val, val_size);
264 if (ret != val_size)
265 return ret < 0 ? ret : -EIO;
266
267 return 0;
268}
269
270/*
271 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
272 * is that the STOP condition is required between set register address and
273 * read register data when reading from registers.
274 */
275static const struct regmap_bus pcf2127_i2c_regmap = {
276 .write = pcf2127_i2c_write,
277 .gather_write = pcf2127_i2c_gather_write,
278 .read = pcf2127_i2c_read,
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700279};
280
Akinobu Mita907b3262016-03-14 23:44:59 +0900281static struct i2c_driver pcf2127_i2c_driver;
282
283static int pcf2127_i2c_probe(struct i2c_client *client,
284 const struct i2c_device_id *id)
285{
286 struct regmap *regmap;
287 static const struct regmap_config config = {
288 .reg_bits = 8,
289 .val_bits = 8,
290 };
291
292 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
293 return -ENODEV;
294
295 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
296 &client->dev, &config);
297 if (IS_ERR(regmap)) {
298 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
299 __func__, PTR_ERR(regmap));
300 return PTR_ERR(regmap);
301 }
302
303 return pcf2127_probe(&client->dev, regmap,
304 pcf2127_i2c_driver.driver.name);
305}
306
307static const struct i2c_device_id pcf2127_i2c_id[] = {
308 { "pcf2127", 0 },
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900309 { "pcf2129", 0 },
Akinobu Mita907b3262016-03-14 23:44:59 +0900310 { }
311};
312MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
313
314static struct i2c_driver pcf2127_i2c_driver = {
315 .driver = {
316 .name = "rtc-pcf2127-i2c",
317 .of_match_table = of_match_ptr(pcf2127_of_match),
318 },
319 .probe = pcf2127_i2c_probe,
320 .id_table = pcf2127_i2c_id,
321};
Akinobu Mita9408ec12016-03-14 23:45:00 +0900322
323static int pcf2127_i2c_register_driver(void)
324{
325 return i2c_add_driver(&pcf2127_i2c_driver);
326}
327
328static void pcf2127_i2c_unregister_driver(void)
329{
330 i2c_del_driver(&pcf2127_i2c_driver);
331}
332
333#else
334
335static int pcf2127_i2c_register_driver(void)
336{
337 return 0;
338}
339
340static void pcf2127_i2c_unregister_driver(void)
341{
342}
343
344#endif
345
346#if IS_ENABLED(CONFIG_SPI_MASTER)
347
348static struct spi_driver pcf2127_spi_driver;
349
350static int pcf2127_spi_probe(struct spi_device *spi)
351{
352 static const struct regmap_config config = {
353 .reg_bits = 8,
354 .val_bits = 8,
355 .read_flag_mask = 0xa0,
356 .write_flag_mask = 0x20,
357 };
358 struct regmap *regmap;
359
360 regmap = devm_regmap_init_spi(spi, &config);
361 if (IS_ERR(regmap)) {
362 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
363 __func__, PTR_ERR(regmap));
364 return PTR_ERR(regmap);
365 }
366
367 return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name);
368}
369
370static const struct spi_device_id pcf2127_spi_id[] = {
371 { "pcf2127", 0 },
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900372 { "pcf2129", 0 },
Akinobu Mita9408ec12016-03-14 23:45:00 +0900373 { }
374};
375MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
376
377static struct spi_driver pcf2127_spi_driver = {
378 .driver = {
379 .name = "rtc-pcf2127-spi",
380 .of_match_table = of_match_ptr(pcf2127_of_match),
381 },
382 .probe = pcf2127_spi_probe,
383 .id_table = pcf2127_spi_id,
384};
385
386static int pcf2127_spi_register_driver(void)
387{
388 return spi_register_driver(&pcf2127_spi_driver);
389}
390
391static void pcf2127_spi_unregister_driver(void)
392{
393 spi_unregister_driver(&pcf2127_spi_driver);
394}
395
396#else
397
398static int pcf2127_spi_register_driver(void)
399{
400 return 0;
401}
402
403static void pcf2127_spi_unregister_driver(void)
404{
405}
406
407#endif
408
409static int __init pcf2127_init(void)
410{
411 int ret;
412
413 ret = pcf2127_i2c_register_driver();
414 if (ret) {
415 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
416 return ret;
417 }
418
419 ret = pcf2127_spi_register_driver();
420 if (ret) {
421 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
422 pcf2127_i2c_unregister_driver();
423 }
424
425 return ret;
426}
427module_init(pcf2127_init)
428
429static void __exit pcf2127_exit(void)
430{
431 pcf2127_spi_unregister_driver();
432 pcf2127_i2c_unregister_driver();
433}
434module_exit(pcf2127_exit)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700435
436MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900437MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
Uwe Kleine-König4d8318b2015-09-09 11:29:10 +0200438MODULE_LICENSE("GPL v2");