1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * An I2C and SPI driver for the NXP PCF2127/29 RTC 4 * Copyright 2013 Til-Technologies 5 * 6 * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> 7 * 8 * based on the other drivers in this same directory. 9 * 10 * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf 11 */ 12 13 #include <linux/i2c.h> 14 #include <linux/spi/spi.h> 15 #include <linux/bcd.h> 16 #include <linux/rtc.h> 17 #include <linux/slab.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/regmap.h> 21 22 #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ 23 #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ 24 25 #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ 26 #define PCF2127_REG_CTRL3_BLF BIT(2) 27 28 #define PCF2127_REG_SC (0x03) /* datetime */ 29 #define PCF2127_REG_MN (0x04) 30 #define PCF2127_REG_HR (0x05) 31 #define PCF2127_REG_DM (0x06) 32 #define PCF2127_REG_DW (0x07) 33 #define PCF2127_REG_MO (0x08) 34 #define PCF2127_REG_YR (0x09) 35 36 /* the pcf2127 has 512 bytes nvmem, pcf2129 doesn't */ 37 #define PCF2127_REG_RAM_addr_MSB 0x1a 38 #define PCF2127_REG_RAM_wrt_cmd 0x1c 39 #define PCF2127_REG_RAM_rd_cmd 0x1d 40 41 #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ 42 43 struct pcf2127 { 44 struct rtc_device *rtc; 45 struct regmap *regmap; 46 }; 47 48 /* 49 * In the routines that deal directly with the pcf2127 hardware, we use 50 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 51 */ 52 static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 53 { 54 struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 55 unsigned char buf[10]; 56 int ret; 57 int i; 58 59 for (i = 0; i <= PCF2127_REG_CTRL3; i++) { 60 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1 + i, 61 (unsigned int *)(buf + i)); 62 if (ret) { 63 dev_err(dev, "%s: read error\n", __func__); 64 return ret; 65 } 66 } 67 68 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC, 69 (buf + PCF2127_REG_SC), 70 ARRAY_SIZE(buf) - PCF2127_REG_SC); 71 if (ret) { 72 dev_err(dev, "%s: read error\n", __func__); 73 return ret; 74 } 75 76 if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF) 77 dev_info(dev, 78 "low voltage detected, check/replace RTC battery.\n"); 79 80 if (buf[PCF2127_REG_SC] & PCF2127_OSF) { 81 /* 82 * no need clear the flag here, 83 * it will be cleared once the new date is saved 84 */ 85 dev_warn(dev, 86 "oscillator stop detected, date/time is not reliable\n"); 87 return -EINVAL; 88 } 89 90 dev_dbg(dev, 91 "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " 92 "sec=%02x, min=%02x, hr=%02x, " 93 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 94 __func__, 95 buf[0], buf[1], buf[2], 96 buf[3], buf[4], buf[5], 97 buf[6], buf[7], buf[8], buf[9]); 98 99 100 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); 101 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); 102 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ 103 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); 104 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; 105 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 106 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); 107 if (tm->tm_year < 70) 108 tm->tm_year += 100; /* assume we are in 1970...2069 */ 109 110 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 111 "mday=%d, mon=%d, year=%d, wday=%d\n", 112 __func__, 113 tm->tm_sec, tm->tm_min, tm->tm_hour, 114 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 115 116 return 0; 117 } 118 119 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 120 { 121 struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 122 unsigned char buf[7]; 123 int i = 0, err; 124 125 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 126 "mday=%d, mon=%d, year=%d, wday=%d\n", 127 __func__, 128 tm->tm_sec, tm->tm_min, tm->tm_hour, 129 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 130 131 /* hours, minutes and seconds */ 132 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 133 buf[i++] = bin2bcd(tm->tm_min); 134 buf[i++] = bin2bcd(tm->tm_hour); 135 buf[i++] = bin2bcd(tm->tm_mday); 136 buf[i++] = tm->tm_wday & 0x07; 137 138 /* month, 1 - 12 */ 139 buf[i++] = bin2bcd(tm->tm_mon + 1); 140 141 /* year */ 142 buf[i++] = bin2bcd(tm->tm_year % 100); 143 144 /* write register's data */ 145 err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); 146 if (err) { 147 dev_err(dev, 148 "%s: err=%d", __func__, err); 149 return err; 150 } 151 152 return 0; 153 } 154 155 #ifdef CONFIG_RTC_INTF_DEV 156 static int pcf2127_rtc_ioctl(struct device *dev, 157 unsigned int cmd, unsigned long arg) 158 { 159 struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 160 int touser; 161 int ret; 162 163 switch (cmd) { 164 case RTC_VL_READ: 165 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser); 166 if (ret) 167 return ret; 168 169 touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0; 170 171 if (copy_to_user((void __user *)arg, &touser, sizeof(int))) 172 return -EFAULT; 173 return 0; 174 default: 175 return -ENOIOCTLCMD; 176 } 177 } 178 #else 179 #define pcf2127_rtc_ioctl NULL 180 #endif 181 182 static const struct rtc_class_ops pcf2127_rtc_ops = { 183 .ioctl = pcf2127_rtc_ioctl, 184 .read_time = pcf2127_rtc_read_time, 185 .set_time = pcf2127_rtc_set_time, 186 }; 187 188 static int pcf2127_nvmem_read(void *priv, unsigned int offset, 189 void *val, size_t bytes) 190 { 191 struct pcf2127 *pcf2127 = priv; 192 int ret; 193 unsigned char offsetbuf[] = { offset >> 8, offset }; 194 195 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, 196 offsetbuf, 2); 197 if (ret) 198 return ret; 199 200 ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_rd_cmd, 201 val, bytes); 202 203 return ret ?: bytes; 204 } 205 206 static int pcf2127_nvmem_write(void *priv, unsigned int offset, 207 void *val, size_t bytes) 208 { 209 struct pcf2127 *pcf2127 = priv; 210 int ret; 211 unsigned char offsetbuf[] = { offset >> 8, offset }; 212 213 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, 214 offsetbuf, 2); 215 if (ret) 216 return ret; 217 218 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_wrt_cmd, 219 val, bytes); 220 221 return ret ?: bytes; 222 } 223 224 static int pcf2127_probe(struct device *dev, struct regmap *regmap, 225 const char *name, bool has_nvmem) 226 { 227 struct pcf2127 *pcf2127; 228 int ret = 0; 229 230 dev_dbg(dev, "%s\n", __func__); 231 232 pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 233 if (!pcf2127) 234 return -ENOMEM; 235 236 pcf2127->regmap = regmap; 237 238 dev_set_drvdata(dev, pcf2127); 239 240 pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops, 241 THIS_MODULE); 242 if (IS_ERR(pcf2127->rtc)) 243 return PTR_ERR(pcf2127->rtc); 244 245 if (has_nvmem) { 246 struct nvmem_config nvmem_cfg = { 247 .priv = pcf2127, 248 .reg_read = pcf2127_nvmem_read, 249 .reg_write = pcf2127_nvmem_write, 250 .size = 512, 251 }; 252 253 ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 254 } 255 256 return ret; 257 } 258 259 #ifdef CONFIG_OF 260 static const struct of_device_id pcf2127_of_match[] = { 261 { .compatible = "nxp,pcf2127" }, 262 { .compatible = "nxp,pcf2129" }, 263 {} 264 }; 265 MODULE_DEVICE_TABLE(of, pcf2127_of_match); 266 #endif 267 268 #if IS_ENABLED(CONFIG_I2C) 269 270 static int pcf2127_i2c_write(void *context, const void *data, size_t count) 271 { 272 struct device *dev = context; 273 struct i2c_client *client = to_i2c_client(dev); 274 int ret; 275 276 ret = i2c_master_send(client, data, count); 277 if (ret != count) 278 return ret < 0 ? ret : -EIO; 279 280 return 0; 281 } 282 283 static int pcf2127_i2c_gather_write(void *context, 284 const void *reg, size_t reg_size, 285 const void *val, size_t val_size) 286 { 287 struct device *dev = context; 288 struct i2c_client *client = to_i2c_client(dev); 289 int ret; 290 void *buf; 291 292 if (WARN_ON(reg_size != 1)) 293 return -EINVAL; 294 295 buf = kmalloc(val_size + 1, GFP_KERNEL); 296 if (!buf) 297 return -ENOMEM; 298 299 memcpy(buf, reg, 1); 300 memcpy(buf + 1, val, val_size); 301 302 ret = i2c_master_send(client, buf, val_size + 1); 303 304 kfree(buf); 305 306 if (ret != val_size + 1) 307 return ret < 0 ? ret : -EIO; 308 309 return 0; 310 } 311 312 static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 313 void *val, size_t val_size) 314 { 315 struct device *dev = context; 316 struct i2c_client *client = to_i2c_client(dev); 317 int ret; 318 319 if (WARN_ON(reg_size != 1)) 320 return -EINVAL; 321 322 ret = i2c_master_send(client, reg, 1); 323 if (ret != 1) 324 return ret < 0 ? ret : -EIO; 325 326 ret = i2c_master_recv(client, val, val_size); 327 if (ret != val_size) 328 return ret < 0 ? ret : -EIO; 329 330 return 0; 331 } 332 333 /* 334 * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 335 * is that the STOP condition is required between set register address and 336 * read register data when reading from registers. 337 */ 338 static const struct regmap_bus pcf2127_i2c_regmap = { 339 .write = pcf2127_i2c_write, 340 .gather_write = pcf2127_i2c_gather_write, 341 .read = pcf2127_i2c_read, 342 }; 343 344 static struct i2c_driver pcf2127_i2c_driver; 345 346 static int pcf2127_i2c_probe(struct i2c_client *client, 347 const struct i2c_device_id *id) 348 { 349 struct regmap *regmap; 350 static const struct regmap_config config = { 351 .reg_bits = 8, 352 .val_bits = 8, 353 }; 354 355 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 356 return -ENODEV; 357 358 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 359 &client->dev, &config); 360 if (IS_ERR(regmap)) { 361 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 362 __func__, PTR_ERR(regmap)); 363 return PTR_ERR(regmap); 364 } 365 366 return pcf2127_probe(&client->dev, regmap, 367 pcf2127_i2c_driver.driver.name, id->driver_data); 368 } 369 370 static const struct i2c_device_id pcf2127_i2c_id[] = { 371 { "pcf2127", 1 }, 372 { "pcf2129", 0 }, 373 { } 374 }; 375 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 376 377 static struct i2c_driver pcf2127_i2c_driver = { 378 .driver = { 379 .name = "rtc-pcf2127-i2c", 380 .of_match_table = of_match_ptr(pcf2127_of_match), 381 }, 382 .probe = pcf2127_i2c_probe, 383 .id_table = pcf2127_i2c_id, 384 }; 385 386 static int pcf2127_i2c_register_driver(void) 387 { 388 return i2c_add_driver(&pcf2127_i2c_driver); 389 } 390 391 static void pcf2127_i2c_unregister_driver(void) 392 { 393 i2c_del_driver(&pcf2127_i2c_driver); 394 } 395 396 #else 397 398 static int pcf2127_i2c_register_driver(void) 399 { 400 return 0; 401 } 402 403 static void pcf2127_i2c_unregister_driver(void) 404 { 405 } 406 407 #endif 408 409 #if IS_ENABLED(CONFIG_SPI_MASTER) 410 411 static struct spi_driver pcf2127_spi_driver; 412 413 static int pcf2127_spi_probe(struct spi_device *spi) 414 { 415 static const struct regmap_config config = { 416 .reg_bits = 8, 417 .val_bits = 8, 418 .read_flag_mask = 0xa0, 419 .write_flag_mask = 0x20, 420 }; 421 struct regmap *regmap; 422 423 regmap = devm_regmap_init_spi(spi, &config); 424 if (IS_ERR(regmap)) { 425 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 426 __func__, PTR_ERR(regmap)); 427 return PTR_ERR(regmap); 428 } 429 430 return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name, 431 spi_get_device_id(spi)->driver_data); 432 } 433 434 static const struct spi_device_id pcf2127_spi_id[] = { 435 { "pcf2127", 1 }, 436 { "pcf2129", 0 }, 437 { } 438 }; 439 MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 440 441 static struct spi_driver pcf2127_spi_driver = { 442 .driver = { 443 .name = "rtc-pcf2127-spi", 444 .of_match_table = of_match_ptr(pcf2127_of_match), 445 }, 446 .probe = pcf2127_spi_probe, 447 .id_table = pcf2127_spi_id, 448 }; 449 450 static int pcf2127_spi_register_driver(void) 451 { 452 return spi_register_driver(&pcf2127_spi_driver); 453 } 454 455 static void pcf2127_spi_unregister_driver(void) 456 { 457 spi_unregister_driver(&pcf2127_spi_driver); 458 } 459 460 #else 461 462 static int pcf2127_spi_register_driver(void) 463 { 464 return 0; 465 } 466 467 static void pcf2127_spi_unregister_driver(void) 468 { 469 } 470 471 #endif 472 473 static int __init pcf2127_init(void) 474 { 475 int ret; 476 477 ret = pcf2127_i2c_register_driver(); 478 if (ret) { 479 pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 480 return ret; 481 } 482 483 ret = pcf2127_spi_register_driver(); 484 if (ret) { 485 pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 486 pcf2127_i2c_unregister_driver(); 487 } 488 489 return ret; 490 } 491 module_init(pcf2127_init) 492 493 static void __exit pcf2127_exit(void) 494 { 495 pcf2127_spi_unregister_driver(); 496 pcf2127_i2c_unregister_driver(); 497 } 498 module_exit(pcf2127_exit) 499 500 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 501 MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); 502 MODULE_LICENSE("GPL v2"); 503