blob: 2bfdf638b67390661f10e1405eea44d544eda6ad [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);
240 if (ret != val_size + 1)
241 return ret < 0 ? ret : -EIO;
242
243 return 0;
244}
245
246static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
247 void *val, size_t val_size)
248{
249 struct device *dev = context;
250 struct i2c_client *client = to_i2c_client(dev);
251 int ret;
252
253 if (WARN_ON(reg_size != 1))
254 return -EINVAL;
255
256 ret = i2c_master_send(client, reg, 1);
257 if (ret != 1)
258 return ret < 0 ? ret : -EIO;
259
260 ret = i2c_master_recv(client, val, val_size);
261 if (ret != val_size)
262 return ret < 0 ? ret : -EIO;
263
264 return 0;
265}
266
267/*
268 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
269 * is that the STOP condition is required between set register address and
270 * read register data when reading from registers.
271 */
272static const struct regmap_bus pcf2127_i2c_regmap = {
273 .write = pcf2127_i2c_write,
274 .gather_write = pcf2127_i2c_gather_write,
275 .read = pcf2127_i2c_read,
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700276};
277
Akinobu Mita907b3262016-03-14 23:44:59 +0900278static struct i2c_driver pcf2127_i2c_driver;
279
280static int pcf2127_i2c_probe(struct i2c_client *client,
281 const struct i2c_device_id *id)
282{
283 struct regmap *regmap;
284 static const struct regmap_config config = {
285 .reg_bits = 8,
286 .val_bits = 8,
287 };
288
289 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
290 return -ENODEV;
291
292 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
293 &client->dev, &config);
294 if (IS_ERR(regmap)) {
295 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
296 __func__, PTR_ERR(regmap));
297 return PTR_ERR(regmap);
298 }
299
300 return pcf2127_probe(&client->dev, regmap,
301 pcf2127_i2c_driver.driver.name);
302}
303
304static const struct i2c_device_id pcf2127_i2c_id[] = {
305 { "pcf2127", 0 },
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900306 { "pcf2129", 0 },
Akinobu Mita907b3262016-03-14 23:44:59 +0900307 { }
308};
309MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
310
311static struct i2c_driver pcf2127_i2c_driver = {
312 .driver = {
313 .name = "rtc-pcf2127-i2c",
314 .of_match_table = of_match_ptr(pcf2127_of_match),
315 },
316 .probe = pcf2127_i2c_probe,
317 .id_table = pcf2127_i2c_id,
318};
Akinobu Mita9408ec12016-03-14 23:45:00 +0900319
320static int pcf2127_i2c_register_driver(void)
321{
322 return i2c_add_driver(&pcf2127_i2c_driver);
323}
324
325static void pcf2127_i2c_unregister_driver(void)
326{
327 i2c_del_driver(&pcf2127_i2c_driver);
328}
329
330#else
331
332static int pcf2127_i2c_register_driver(void)
333{
334 return 0;
335}
336
337static void pcf2127_i2c_unregister_driver(void)
338{
339}
340
341#endif
342
343#if IS_ENABLED(CONFIG_SPI_MASTER)
344
345static struct spi_driver pcf2127_spi_driver;
346
347static int pcf2127_spi_probe(struct spi_device *spi)
348{
349 static const struct regmap_config config = {
350 .reg_bits = 8,
351 .val_bits = 8,
352 .read_flag_mask = 0xa0,
353 .write_flag_mask = 0x20,
354 };
355 struct regmap *regmap;
356
357 regmap = devm_regmap_init_spi(spi, &config);
358 if (IS_ERR(regmap)) {
359 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
360 __func__, PTR_ERR(regmap));
361 return PTR_ERR(regmap);
362 }
363
364 return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name);
365}
366
367static const struct spi_device_id pcf2127_spi_id[] = {
368 { "pcf2127", 0 },
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900369 { "pcf2129", 0 },
Akinobu Mita9408ec12016-03-14 23:45:00 +0900370 { }
371};
372MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
373
374static struct spi_driver pcf2127_spi_driver = {
375 .driver = {
376 .name = "rtc-pcf2127-spi",
377 .of_match_table = of_match_ptr(pcf2127_of_match),
378 },
379 .probe = pcf2127_spi_probe,
380 .id_table = pcf2127_spi_id,
381};
382
383static int pcf2127_spi_register_driver(void)
384{
385 return spi_register_driver(&pcf2127_spi_driver);
386}
387
388static void pcf2127_spi_unregister_driver(void)
389{
390 spi_unregister_driver(&pcf2127_spi_driver);
391}
392
393#else
394
395static int pcf2127_spi_register_driver(void)
396{
397 return 0;
398}
399
400static void pcf2127_spi_unregister_driver(void)
401{
402}
403
404#endif
405
406static int __init pcf2127_init(void)
407{
408 int ret;
409
410 ret = pcf2127_i2c_register_driver();
411 if (ret) {
412 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
413 return ret;
414 }
415
416 ret = pcf2127_spi_register_driver();
417 if (ret) {
418 pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
419 pcf2127_i2c_unregister_driver();
420 }
421
422 return ret;
423}
424module_init(pcf2127_init)
425
426static void __exit pcf2127_exit(void)
427{
428 pcf2127_spi_unregister_driver();
429 pcf2127_i2c_unregister_driver();
430}
431module_exit(pcf2127_exit)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700432
433MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
Akinobu Mitacee2cc22016-03-14 23:45:01 +0900434MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
Uwe Kleine-König4d8318b2015-09-09 11:29:10 +0200435MODULE_LICENSE("GPL v2");