blob: fa492cd84e2ae46d21aed497f594cb89fd0e765c [file] [log] [blame]
Renaud Cerrato18cb6362013-07-03 15:08:01 -07001/*
2 * An I2C driver for the NXP PCF2127 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 * http://www.nxp.com/documents/data_sheet/PCF2127AT.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/bcd.h>
18#include <linux/rtc.h>
19#include <linux/slab.h>
20#include <linux/module.h>
21#include <linux/of.h>
Akinobu Mita907b3262016-03-14 23:44:59 +090022#include <linux/regmap.h>
Renaud Cerrato18cb6362013-07-03 15:08:01 -070023
Renaud Cerrato18cb6362013-07-03 15:08:01 -070024#define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
25#define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020026
Renaud Cerrato18cb6362013-07-03 15:08:01 -070027#define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020028#define PCF2127_REG_CTRL3_BLF BIT(2)
29
Renaud Cerrato18cb6362013-07-03 15:08:01 -070030#define PCF2127_REG_SC (0x03) /* datetime */
31#define PCF2127_REG_MN (0x04)
32#define PCF2127_REG_HR (0x05)
33#define PCF2127_REG_DM (0x06)
34#define PCF2127_REG_DW (0x07)
35#define PCF2127_REG_MO (0x08)
36#define PCF2127_REG_YR (0x09)
37
Andrea Scian653ebd72015-06-16 11:39:47 +020038#define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
39
Renaud Cerrato18cb6362013-07-03 15:08:01 -070040struct pcf2127 {
41 struct rtc_device *rtc;
Akinobu Mita907b3262016-03-14 23:44:59 +090042 struct regmap *regmap;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070043};
44
45/*
46 * In the routines that deal directly with the pcf2127 hardware, we use
47 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
48 */
Akinobu Mita907b3262016-03-14 23:44:59 +090049static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
Renaud Cerrato18cb6362013-07-03 15:08:01 -070050{
Akinobu Mita907b3262016-03-14 23:44:59 +090051 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
52 unsigned char buf[10];
53 int ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070054
Akinobu Mita907b3262016-03-14 23:44:59 +090055 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, buf,
56 sizeof(buf));
57 if (ret) {
58 dev_err(dev, "%s: read error\n", __func__);
59 return ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070060 }
61
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +020062 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
Akinobu Mita907b3262016-03-14 23:44:59 +090063 dev_info(dev,
Andrea Scian653ebd72015-06-16 11:39:47 +020064 "low voltage detected, check/replace RTC battery.\n");
Andrea Scian653ebd72015-06-16 11:39:47 +020065
66 if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
67 /*
68 * no need clear the flag here,
69 * it will be cleared once the new date is saved
70 */
Akinobu Mita907b3262016-03-14 23:44:59 +090071 dev_warn(dev,
Andrea Scian653ebd72015-06-16 11:39:47 +020072 "oscillator stop detected, date/time is not reliable\n");
73 return -EINVAL;
Renaud Cerrato18cb6362013-07-03 15:08:01 -070074 }
75
Akinobu Mita907b3262016-03-14 23:44:59 +090076 dev_dbg(dev,
Renaud Cerrato18cb6362013-07-03 15:08:01 -070077 "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
78 "sec=%02x, min=%02x, hr=%02x, "
79 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
80 __func__,
81 buf[0], buf[1], buf[2],
82 buf[3], buf[4], buf[5],
83 buf[6], buf[7], buf[8], buf[9]);
84
85
86 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
87 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
88 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
89 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
90 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
91 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
92 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
93 if (tm->tm_year < 70)
94 tm->tm_year += 100; /* assume we are in 1970...2069 */
95
Akinobu Mita907b3262016-03-14 23:44:59 +090096 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Renaud Cerrato18cb6362013-07-03 15:08:01 -070097 "mday=%d, mon=%d, year=%d, wday=%d\n",
98 __func__,
99 tm->tm_sec, tm->tm_min, tm->tm_hour,
100 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
101
Andrea Scian821f51c2015-06-16 11:35:19 +0200102 return rtc_valid_tm(tm);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700103}
104
Akinobu Mita907b3262016-03-14 23:44:59 +0900105static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700106{
Akinobu Mita907b3262016-03-14 23:44:59 +0900107 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
108 unsigned char buf[7];
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700109 int i = 0, err;
110
Akinobu Mita907b3262016-03-14 23:44:59 +0900111 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700112 "mday=%d, mon=%d, year=%d, wday=%d\n",
113 __func__,
114 tm->tm_sec, tm->tm_min, tm->tm_hour,
115 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
116
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700117 /* hours, minutes and seconds */
Andrea Scian653ebd72015-06-16 11:39:47 +0200118 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700119 buf[i++] = bin2bcd(tm->tm_min);
120 buf[i++] = bin2bcd(tm->tm_hour);
121 buf[i++] = bin2bcd(tm->tm_mday);
122 buf[i++] = tm->tm_wday & 0x07;
123
124 /* month, 1 - 12 */
125 buf[i++] = bin2bcd(tm->tm_mon + 1);
126
127 /* year */
128 buf[i++] = bin2bcd(tm->tm_year % 100);
129
130 /* write register's data */
Akinobu Mita907b3262016-03-14 23:44:59 +0900131 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
132 if (err) {
133 dev_err(dev,
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700134 "%s: err=%d", __func__, err);
Akinobu Mita907b3262016-03-14 23:44:59 +0900135 return err;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700136 }
137
138 return 0;
139}
140
141#ifdef CONFIG_RTC_INTF_DEV
142static int pcf2127_rtc_ioctl(struct device *dev,
143 unsigned int cmd, unsigned long arg)
144{
Akinobu Mita907b3262016-03-14 23:44:59 +0900145 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200146 int touser;
147 int ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700148
149 switch (cmd) {
150 case RTC_VL_READ:
Akinobu Mita907b3262016-03-14 23:44:59 +0900151 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
152 if (ret)
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200153 return ret;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700154
Akinobu Mita907b3262016-03-14 23:44:59 +0900155 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
Uwe Kleine-Königf97cfdd2015-10-02 11:17:19 +0200156
157 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700158 return -EFAULT;
159 return 0;
160 default:
161 return -ENOIOCTLCMD;
162 }
163}
164#else
165#define pcf2127_rtc_ioctl NULL
166#endif
167
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700168static const struct rtc_class_ops pcf2127_rtc_ops = {
169 .ioctl = pcf2127_rtc_ioctl,
170 .read_time = pcf2127_rtc_read_time,
171 .set_time = pcf2127_rtc_set_time,
172};
173
Akinobu Mita907b3262016-03-14 23:44:59 +0900174static int pcf2127_probe(struct device *dev, struct regmap *regmap,
175 const char *name)
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700176{
177 struct pcf2127 *pcf2127;
178
Akinobu Mita907b3262016-03-14 23:44:59 +0900179 dev_dbg(dev, "%s\n", __func__);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700180
Akinobu Mita907b3262016-03-14 23:44:59 +0900181 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700182 if (!pcf2127)
183 return -ENOMEM;
184
Akinobu Mita907b3262016-03-14 23:44:59 +0900185 pcf2127->regmap = regmap;
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700186
Akinobu Mita907b3262016-03-14 23:44:59 +0900187 dev_set_drvdata(dev, pcf2127);
188
189 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
190 THIS_MODULE);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700191
Duan Jiong7ab26cd2014-01-23 15:55:13 -0800192 return PTR_ERR_OR_ZERO(pcf2127->rtc);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700193}
194
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700195#ifdef CONFIG_OF
196static const struct of_device_id pcf2127_of_match[] = {
197 { .compatible = "nxp,pcf2127" },
198 {}
199};
200MODULE_DEVICE_TABLE(of, pcf2127_of_match);
201#endif
202
Akinobu Mita907b3262016-03-14 23:44:59 +0900203static int pcf2127_i2c_write(void *context, const void *data, size_t count)
204{
205 struct device *dev = context;
206 struct i2c_client *client = to_i2c_client(dev);
207 int ret;
208
209 ret = i2c_master_send(client, data, count);
210 if (ret != count)
211 return ret < 0 ? ret : -EIO;
212
213 return 0;
214}
215
216static int pcf2127_i2c_gather_write(void *context,
217 const void *reg, size_t reg_size,
218 const void *val, size_t val_size)
219{
220 struct device *dev = context;
221 struct i2c_client *client = to_i2c_client(dev);
222 int ret;
223 void *buf;
224
225 if (WARN_ON(reg_size != 1))
226 return -EINVAL;
227
228 buf = kmalloc(val_size + 1, GFP_KERNEL);
229 if (!buf)
230 return -ENOMEM;
231
232 memcpy(buf, reg, 1);
233 memcpy(buf + 1, val, val_size);
234
235 ret = i2c_master_send(client, buf, val_size + 1);
236 if (ret != val_size + 1)
237 return ret < 0 ? ret : -EIO;
238
239 return 0;
240}
241
242static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
243 void *val, size_t val_size)
244{
245 struct device *dev = context;
246 struct i2c_client *client = to_i2c_client(dev);
247 int ret;
248
249 if (WARN_ON(reg_size != 1))
250 return -EINVAL;
251
252 ret = i2c_master_send(client, reg, 1);
253 if (ret != 1)
254 return ret < 0 ? ret : -EIO;
255
256 ret = i2c_master_recv(client, val, val_size);
257 if (ret != val_size)
258 return ret < 0 ? ret : -EIO;
259
260 return 0;
261}
262
263/*
264 * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
265 * is that the STOP condition is required between set register address and
266 * read register data when reading from registers.
267 */
268static const struct regmap_bus pcf2127_i2c_regmap = {
269 .write = pcf2127_i2c_write,
270 .gather_write = pcf2127_i2c_gather_write,
271 .read = pcf2127_i2c_read,
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700272};
273
Akinobu Mita907b3262016-03-14 23:44:59 +0900274static struct i2c_driver pcf2127_i2c_driver;
275
276static int pcf2127_i2c_probe(struct i2c_client *client,
277 const struct i2c_device_id *id)
278{
279 struct regmap *regmap;
280 static const struct regmap_config config = {
281 .reg_bits = 8,
282 .val_bits = 8,
283 };
284
285 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
286 return -ENODEV;
287
288 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
289 &client->dev, &config);
290 if (IS_ERR(regmap)) {
291 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
292 __func__, PTR_ERR(regmap));
293 return PTR_ERR(regmap);
294 }
295
296 return pcf2127_probe(&client->dev, regmap,
297 pcf2127_i2c_driver.driver.name);
298}
299
300static const struct i2c_device_id pcf2127_i2c_id[] = {
301 { "pcf2127", 0 },
302 { }
303};
304MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
305
306static struct i2c_driver pcf2127_i2c_driver = {
307 .driver = {
308 .name = "rtc-pcf2127-i2c",
309 .of_match_table = of_match_ptr(pcf2127_of_match),
310 },
311 .probe = pcf2127_i2c_probe,
312 .id_table = pcf2127_i2c_id,
313};
314module_i2c_driver(pcf2127_i2c_driver);
Renaud Cerrato18cb6362013-07-03 15:08:01 -0700315
316MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
317MODULE_DESCRIPTION("NXP PCF2127 RTC driver");
Uwe Kleine-König4d8318b2015-09-09 11:29:10 +0200318MODULE_LICENSE("GPL v2");