libcxgb,iw_cxgb4,cxgbit: add cxgb_is_neg_adv()
[linux/fpc-iii.git] / drivers / rtc / rtc-pcf2127.c
blob2bfdf638b67390661f10e1405eea44d544eda6ad
1 /*
2 * An I2C and SPI driver for the NXP PCF2127/29 RTC
3 * Copyright 2013 Til-Technologies
5 * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
7 * based on the other drivers in this same directory.
9 * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
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.
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>
25 #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
26 #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
28 #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
29 #define PCF2127_REG_CTRL3_BLF BIT(2)
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)
39 #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
41 struct pcf2127 {
42 struct rtc_device *rtc;
43 struct regmap *regmap;
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.
50 static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
52 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
53 unsigned char buf[10];
54 int ret;
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;
63 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
64 dev_info(dev,
65 "low voltage detected, check/replace RTC battery.\n");
67 if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
69 * no need clear the flag here,
70 * it will be cleared once the new date is saved
72 dev_warn(dev,
73 "oscillator stop detected, date/time is not reliable\n");
74 return -EINVAL;
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]);
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 */
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);
103 return rtc_valid_tm(tm);
106 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
108 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
109 unsigned char buf[7];
110 int i = 0, err;
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);
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;
125 /* month, 1 - 12 */
126 buf[i++] = bin2bcd(tm->tm_mon + 1);
128 /* year */
129 buf[i++] = bin2bcd(tm->tm_year % 100);
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;
139 return 0;
142 #ifdef CONFIG_RTC_INTF_DEV
143 static int pcf2127_rtc_ioctl(struct device *dev,
144 unsigned int cmd, unsigned long arg)
146 struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
147 int touser;
148 int ret;
150 switch (cmd) {
151 case RTC_VL_READ:
152 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
153 if (ret)
154 return ret;
156 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
158 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
159 return -EFAULT;
160 return 0;
161 default:
162 return -ENOIOCTLCMD;
165 #else
166 #define pcf2127_rtc_ioctl NULL
167 #endif
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,
175 static int pcf2127_probe(struct device *dev, struct regmap *regmap,
176 const char *name)
178 struct pcf2127 *pcf2127;
180 dev_dbg(dev, "%s\n", __func__);
182 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
183 if (!pcf2127)
184 return -ENOMEM;
186 pcf2127->regmap = regmap;
188 dev_set_drvdata(dev, pcf2127);
190 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
191 THIS_MODULE);
193 return PTR_ERR_OR_ZERO(pcf2127->rtc);
196 #ifdef CONFIG_OF
197 static const struct of_device_id pcf2127_of_match[] = {
198 { .compatible = "nxp,pcf2127" },
199 { .compatible = "nxp,pcf2129" },
202 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
203 #endif
205 #if IS_ENABLED(CONFIG_I2C)
207 static int pcf2127_i2c_write(void *context, const void *data, size_t count)
209 struct device *dev = context;
210 struct i2c_client *client = to_i2c_client(dev);
211 int ret;
213 ret = i2c_master_send(client, data, count);
214 if (ret != count)
215 return ret < 0 ? ret : -EIO;
217 return 0;
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)
224 struct device *dev = context;
225 struct i2c_client *client = to_i2c_client(dev);
226 int ret;
227 void *buf;
229 if (WARN_ON(reg_size != 1))
230 return -EINVAL;
232 buf = kmalloc(val_size + 1, GFP_KERNEL);
233 if (!buf)
234 return -ENOMEM;
236 memcpy(buf, reg, 1);
237 memcpy(buf + 1, val, val_size);
239 ret = i2c_master_send(client, buf, val_size + 1);
240 if (ret != val_size + 1)
241 return ret < 0 ? ret : -EIO;
243 return 0;
246 static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
247 void *val, size_t val_size)
249 struct device *dev = context;
250 struct i2c_client *client = to_i2c_client(dev);
251 int ret;
253 if (WARN_ON(reg_size != 1))
254 return -EINVAL;
256 ret = i2c_master_send(client, reg, 1);
257 if (ret != 1)
258 return ret < 0 ? ret : -EIO;
260 ret = i2c_master_recv(client, val, val_size);
261 if (ret != val_size)
262 return ret < 0 ? ret : -EIO;
264 return 0;
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.
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,
278 static struct i2c_driver pcf2127_i2c_driver;
280 static int pcf2127_i2c_probe(struct i2c_client *client,
281 const struct i2c_device_id *id)
283 struct regmap *regmap;
284 static const struct regmap_config config = {
285 .reg_bits = 8,
286 .val_bits = 8,
289 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
290 return -ENODEV;
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);
300 return pcf2127_probe(&client->dev, regmap,
301 pcf2127_i2c_driver.driver.name);
304 static const struct i2c_device_id pcf2127_i2c_id[] = {
305 { "pcf2127", 0 },
306 { "pcf2129", 0 },
309 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
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),
316 .probe = pcf2127_i2c_probe,
317 .id_table = pcf2127_i2c_id,
320 static int pcf2127_i2c_register_driver(void)
322 return i2c_add_driver(&pcf2127_i2c_driver);
325 static void pcf2127_i2c_unregister_driver(void)
327 i2c_del_driver(&pcf2127_i2c_driver);
330 #else
332 static int pcf2127_i2c_register_driver(void)
334 return 0;
337 static void pcf2127_i2c_unregister_driver(void)
341 #endif
343 #if IS_ENABLED(CONFIG_SPI_MASTER)
345 static struct spi_driver pcf2127_spi_driver;
347 static int pcf2127_spi_probe(struct spi_device *spi)
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,
355 struct regmap *regmap;
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);
364 return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name);
367 static const struct spi_device_id pcf2127_spi_id[] = {
368 { "pcf2127", 0 },
369 { "pcf2129", 0 },
372 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
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),
379 .probe = pcf2127_spi_probe,
380 .id_table = pcf2127_spi_id,
383 static int pcf2127_spi_register_driver(void)
385 return spi_register_driver(&pcf2127_spi_driver);
388 static void pcf2127_spi_unregister_driver(void)
390 spi_unregister_driver(&pcf2127_spi_driver);
393 #else
395 static int pcf2127_spi_register_driver(void)
397 return 0;
400 static void pcf2127_spi_unregister_driver(void)
404 #endif
406 static int __init pcf2127_init(void)
408 int ret;
410 ret = pcf2127_i2c_register_driver();
411 if (ret) {
412 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
413 return ret;
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();
422 return ret;
424 module_init(pcf2127_init)
426 static void __exit pcf2127_exit(void)
428 pcf2127_spi_unregister_driver();
429 pcf2127_i2c_unregister_driver();
431 module_exit(pcf2127_exit)
433 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
434 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
435 MODULE_LICENSE("GPL v2");