Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 22 | #include <linux/regmap.h> |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 23 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 24 | #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ |
| 25 | #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 26 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 27 | #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 28 | #define PCF2127_REG_CTRL3_BLF BIT(2) |
| 29 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 30 | #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 Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 38 | #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ |
| 39 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 40 | struct pcf2127 { |
| 41 | struct rtc_device *rtc; |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 42 | struct regmap *regmap; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 43 | }; |
| 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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 49 | static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 50 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 51 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
| 52 | unsigned char buf[10]; |
| 53 | int ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 54 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 55 | 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 Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 62 | if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF) |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 63 | dev_info(dev, |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 64 | "low voltage detected, check/replace RTC battery.\n"); |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 65 | |
| 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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 71 | dev_warn(dev, |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 72 | "oscillator stop detected, date/time is not reliable\n"); |
| 73 | return -EINVAL; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 76 | dev_dbg(dev, |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 77 | "%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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 96 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 97 | "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 Scian | 821f51c | 2015-06-16 11:35:19 +0200 | [diff] [blame] | 102 | return rtc_valid_tm(tm); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 105 | static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 106 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 107 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
| 108 | unsigned char buf[7]; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 109 | int i = 0, err; |
| 110 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 111 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 112 | "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 Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 117 | /* hours, minutes and seconds */ |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 118 | buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 119 | 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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 131 | err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); |
| 132 | if (err) { |
| 133 | dev_err(dev, |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 134 | "%s: err=%d", __func__, err); |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 135 | return err; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | #ifdef CONFIG_RTC_INTF_DEV |
| 142 | static int pcf2127_rtc_ioctl(struct device *dev, |
| 143 | unsigned int cmd, unsigned long arg) |
| 144 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 145 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 146 | int touser; |
| 147 | int ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 148 | |
| 149 | switch (cmd) { |
| 150 | case RTC_VL_READ: |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 151 | ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser); |
| 152 | if (ret) |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 153 | return ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 154 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 155 | touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0; |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 156 | |
| 157 | if (copy_to_user((void __user *)arg, &touser, sizeof(int))) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 158 | return -EFAULT; |
| 159 | return 0; |
| 160 | default: |
| 161 | return -ENOIOCTLCMD; |
| 162 | } |
| 163 | } |
| 164 | #else |
| 165 | #define pcf2127_rtc_ioctl NULL |
| 166 | #endif |
| 167 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 168 | static 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 Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 174 | static int pcf2127_probe(struct device *dev, struct regmap *regmap, |
| 175 | const char *name) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 176 | { |
| 177 | struct pcf2127 *pcf2127; |
| 178 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 179 | dev_dbg(dev, "%s\n", __func__); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 180 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 181 | pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 182 | if (!pcf2127) |
| 183 | return -ENOMEM; |
| 184 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 185 | pcf2127->regmap = regmap; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 186 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 187 | dev_set_drvdata(dev, pcf2127); |
| 188 | |
| 189 | pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops, |
| 190 | THIS_MODULE); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 191 | |
Duan Jiong | 7ab26cd | 2014-01-23 15:55:13 -0800 | [diff] [blame] | 192 | return PTR_ERR_OR_ZERO(pcf2127->rtc); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 195 | #ifdef CONFIG_OF |
| 196 | static const struct of_device_id pcf2127_of_match[] = { |
| 197 | { .compatible = "nxp,pcf2127" }, |
| 198 | {} |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(of, pcf2127_of_match); |
| 201 | #endif |
| 202 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 203 | static 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 | |
| 216 | static 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 | |
| 242 | static 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 | */ |
| 268 | static 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 Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame^] | 274 | static struct i2c_driver pcf2127_i2c_driver; |
| 275 | |
| 276 | static 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 | |
| 300 | static const struct i2c_device_id pcf2127_i2c_id[] = { |
| 301 | { "pcf2127", 0 }, |
| 302 | { } |
| 303 | }; |
| 304 | MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); |
| 305 | |
| 306 | static 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 | }; |
| 314 | module_i2c_driver(pcf2127_i2c_driver); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 315 | |
| 316 | MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); |
| 317 | MODULE_DESCRIPTION("NXP PCF2127 RTC driver"); |
Uwe Kleine-König | 4d8318b | 2015-09-09 11:29:10 +0200 | [diff] [blame] | 318 | MODULE_LICENSE("GPL v2"); |