1 /*
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
41 struct 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 */
pcf2127_rtc_read_time(struct device * dev,struct rtc_time * tm)50 static 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 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;
61 }
62
63 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
64 dev_info(dev,
65 "low voltage detected, check/replace RTC battery.\n");
66
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 */
72 dev_warn(dev,
73 "oscillator stop detected, date/time is not reliable\n");
74 return -EINVAL;
75 }
76
77 dev_dbg(dev,
78 "%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
97 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
98 "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
103 return rtc_valid_tm(tm);
104 }
105
pcf2127_rtc_set_time(struct device * dev,struct rtc_time * tm)106 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
107 {
108 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
109 unsigned char buf[7];
110 int i = 0, err;
111
112 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
113 "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
118 /* hours, minutes and seconds */
119 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
120 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 */
132 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
133 if (err) {
134 dev_err(dev,
135 "%s: err=%d", __func__, err);
136 return err;
137 }
138
139 return 0;
140 }
141
142 #ifdef CONFIG_RTC_INTF_DEV
pcf2127_rtc_ioctl(struct device * dev,unsigned int cmd,unsigned long arg)143 static int pcf2127_rtc_ioctl(struct device *dev,
144 unsigned int cmd, unsigned long arg)
145 {
146 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
147 int touser;
148 int ret;
149
150 switch (cmd) {
151 case RTC_VL_READ:
152 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
153 if (ret)
154 return ret;
155
156 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
157
158 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
159 return -EFAULT;
160 return 0;
161 default:
162 return -ENOIOCTLCMD;
163 }
164 }
165 #else
166 #define pcf2127_rtc_ioctl NULL
167 #endif
168
169 static 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
pcf2127_probe(struct device * dev,struct regmap * regmap,const char * name)175 static int pcf2127_probe(struct device *dev, struct regmap *regmap,
176 const char *name)
177 {
178 struct pcf2127 *pcf2127;
179
180 dev_dbg(dev, "%s\n", __func__);
181
182 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
183 if (!pcf2127)
184 return -ENOMEM;
185
186 pcf2127->regmap = regmap;
187
188 dev_set_drvdata(dev, pcf2127);
189
190 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
191 THIS_MODULE);
192
193 return PTR_ERR_OR_ZERO(pcf2127->rtc);
194 }
195
196 #ifdef CONFIG_OF
197 static const struct of_device_id pcf2127_of_match[] = {
198 { .compatible = "nxp,pcf2127" },
199 { .compatible = "nxp,pcf2129" },
200 {}
201 };
202 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
203 #endif
204
205 #if IS_ENABLED(CONFIG_I2C)
206
pcf2127_i2c_write(void * context,const void * data,size_t count)207 static 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
pcf2127_i2c_gather_write(void * context,const void * reg,size_t reg_size,const void * val,size_t val_size)220 static 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
pcf2127_i2c_read(void * context,const void * reg,size_t reg_size,void * val,size_t val_size)246 static 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 */
272 static const struct regmap_bus pcf2127_i2c_regmap = {
273 .write = pcf2127_i2c_write,
274 .gather_write = pcf2127_i2c_gather_write,
275 .read = pcf2127_i2c_read,
276 };
277
278 static struct i2c_driver pcf2127_i2c_driver;
279
pcf2127_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)280 static 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
304 static const struct i2c_device_id pcf2127_i2c_id[] = {
305 { "pcf2127", 0 },
306 { "pcf2129", 0 },
307 { }
308 };
309 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
310
311 static 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 };
319
pcf2127_i2c_register_driver(void)320 static int pcf2127_i2c_register_driver(void)
321 {
322 return i2c_add_driver(&pcf2127_i2c_driver);
323 }
324
pcf2127_i2c_unregister_driver(void)325 static void pcf2127_i2c_unregister_driver(void)
326 {
327 i2c_del_driver(&pcf2127_i2c_driver);
328 }
329
330 #else
331
pcf2127_i2c_register_driver(void)332 static int pcf2127_i2c_register_driver(void)
333 {
334 return 0;
335 }
336
pcf2127_i2c_unregister_driver(void)337 static void pcf2127_i2c_unregister_driver(void)
338 {
339 }
340
341 #endif
342
343 #if IS_ENABLED(CONFIG_SPI_MASTER)
344
345 static struct spi_driver pcf2127_spi_driver;
346
pcf2127_spi_probe(struct spi_device * spi)347 static 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
367 static const struct spi_device_id pcf2127_spi_id[] = {
368 { "pcf2127", 0 },
369 { "pcf2129", 0 },
370 { }
371 };
372 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
373
374 static 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
pcf2127_spi_register_driver(void)383 static int pcf2127_spi_register_driver(void)
384 {
385 return spi_register_driver(&pcf2127_spi_driver);
386 }
387
pcf2127_spi_unregister_driver(void)388 static void pcf2127_spi_unregister_driver(void)
389 {
390 spi_unregister_driver(&pcf2127_spi_driver);
391 }
392
393 #else
394
pcf2127_spi_register_driver(void)395 static int pcf2127_spi_register_driver(void)
396 {
397 return 0;
398 }
399
pcf2127_spi_unregister_driver(void)400 static void pcf2127_spi_unregister_driver(void)
401 {
402 }
403
404 #endif
405
pcf2127_init(void)406 static 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 }
module_init(pcf2127_init)424 module_init(pcf2127_init)
425
426 static void __exit pcf2127_exit(void)
427 {
428 pcf2127_spi_unregister_driver();
429 pcf2127_i2c_unregister_driver();
430 }
431 module_exit(pcf2127_exit)
432
433 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
434 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
435 MODULE_LICENSE("GPL v2");
436