1 // SPDX-License-Identifier: GPL-2.0-only 2 /* rtc-ds1343.c 3 * 4 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible 5 * Real Time Clock 6 * 7 * Author : Raghavendra Chandra Ganiga <[email protected]> 8 * Ankur Srivastava <[email protected]> : DS1343 Nvram Support 9 */ 10 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/interrupt.h> 14 #include <linux/device.h> 15 #include <linux/spi/spi.h> 16 #include <linux/regmap.h> 17 #include <linux/rtc.h> 18 #include <linux/bcd.h> 19 #include <linux/pm.h> 20 #include <linux/pm_wakeirq.h> 21 #include <linux/slab.h> 22 23 #define DALLAS_MAXIM_DS1343 0 24 #define DALLAS_MAXIM_DS1344 1 25 26 /* RTC DS1343 Registers */ 27 #define DS1343_SECONDS_REG 0x00 28 #define DS1343_MINUTES_REG 0x01 29 #define DS1343_HOURS_REG 0x02 30 #define DS1343_DAY_REG 0x03 31 #define DS1343_DATE_REG 0x04 32 #define DS1343_MONTH_REG 0x05 33 #define DS1343_YEAR_REG 0x06 34 #define DS1343_ALM0_SEC_REG 0x07 35 #define DS1343_ALM0_MIN_REG 0x08 36 #define DS1343_ALM0_HOUR_REG 0x09 37 #define DS1343_ALM0_DAY_REG 0x0A 38 #define DS1343_ALM1_SEC_REG 0x0B 39 #define DS1343_ALM1_MIN_REG 0x0C 40 #define DS1343_ALM1_HOUR_REG 0x0D 41 #define DS1343_ALM1_DAY_REG 0x0E 42 #define DS1343_CONTROL_REG 0x0F 43 #define DS1343_STATUS_REG 0x10 44 #define DS1343_TRICKLE_REG 0x11 45 #define DS1343_NVRAM 0x20 46 47 #define DS1343_NVRAM_LEN 96 48 49 /* DS1343 Control Registers bits */ 50 #define DS1343_EOSC 0x80 51 #define DS1343_DOSF 0x20 52 #define DS1343_EGFIL 0x10 53 #define DS1343_SQW 0x08 54 #define DS1343_INTCN 0x04 55 #define DS1343_A1IE 0x02 56 #define DS1343_A0IE 0x01 57 58 /* DS1343 Status Registers bits */ 59 #define DS1343_OSF 0x80 60 #define DS1343_IRQF1 0x02 61 #define DS1343_IRQF0 0x01 62 63 /* DS1343 Trickle Charger Registers bits */ 64 #define DS1343_TRICKLE_MAGIC 0xa0 65 #define DS1343_TRICKLE_DS1 0x08 66 #define DS1343_TRICKLE_1K 0x01 67 #define DS1343_TRICKLE_2K 0x02 68 #define DS1343_TRICKLE_4K 0x03 69 70 static const struct spi_device_id ds1343_id[] = { 71 { "ds1343", DALLAS_MAXIM_DS1343 }, 72 { "ds1344", DALLAS_MAXIM_DS1344 }, 73 { } 74 }; 75 MODULE_DEVICE_TABLE(spi, ds1343_id); 76 77 struct ds1343_priv { 78 struct spi_device *spi; 79 struct rtc_device *rtc; 80 struct regmap *map; 81 struct mutex mutex; 82 unsigned int irqen; 83 int irq; 84 int alarm_sec; 85 int alarm_min; 86 int alarm_hour; 87 int alarm_mday; 88 }; 89 90 static ssize_t ds1343_show_glitchfilter(struct device *dev, 91 struct device_attribute *attr, char *buf) 92 { 93 struct ds1343_priv *priv = dev_get_drvdata(dev->parent); 94 int glitch_filt_status, data; 95 96 regmap_read(priv->map, DS1343_CONTROL_REG, &data); 97 98 glitch_filt_status = !!(data & DS1343_EGFIL); 99 100 if (glitch_filt_status) 101 return sprintf(buf, "enabled\n"); 102 else 103 return sprintf(buf, "disabled\n"); 104 } 105 106 static ssize_t ds1343_store_glitchfilter(struct device *dev, 107 struct device_attribute *attr, 108 const char *buf, size_t count) 109 { 110 struct ds1343_priv *priv = dev_get_drvdata(dev->parent); 111 int data; 112 113 regmap_read(priv->map, DS1343_CONTROL_REG, &data); 114 115 if (strncmp(buf, "enabled", 7) == 0) 116 data |= DS1343_EGFIL; 117 118 else if (strncmp(buf, "disabled", 8) == 0) 119 data &= ~(DS1343_EGFIL); 120 121 else 122 return -EINVAL; 123 124 regmap_write(priv->map, DS1343_CONTROL_REG, data); 125 126 return count; 127 } 128 129 static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter, 130 ds1343_store_glitchfilter); 131 132 static int ds1343_nvram_write(void *priv, unsigned int off, void *val, 133 size_t bytes) 134 { 135 struct ds1343_priv *ds1343 = priv; 136 137 return regmap_bulk_write(ds1343->map, DS1343_NVRAM + off, val, bytes); 138 } 139 140 static int ds1343_nvram_read(void *priv, unsigned int off, void *val, 141 size_t bytes) 142 { 143 struct ds1343_priv *ds1343 = priv; 144 145 return regmap_bulk_read(ds1343->map, DS1343_NVRAM + off, val, bytes); 146 } 147 148 static ssize_t ds1343_show_tricklecharger(struct device *dev, 149 struct device_attribute *attr, char *buf) 150 { 151 struct ds1343_priv *priv = dev_get_drvdata(dev->parent); 152 int data; 153 char *diodes = "disabled", *resistors = " "; 154 155 regmap_read(priv->map, DS1343_TRICKLE_REG, &data); 156 157 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) { 158 switch (data & 0x0c) { 159 case DS1343_TRICKLE_DS1: 160 diodes = "one diode,"; 161 break; 162 163 default: 164 diodes = "no diode,"; 165 break; 166 } 167 168 switch (data & 0x03) { 169 case DS1343_TRICKLE_1K: 170 resistors = "1k Ohm"; 171 break; 172 173 case DS1343_TRICKLE_2K: 174 resistors = "2k Ohm"; 175 break; 176 177 case DS1343_TRICKLE_4K: 178 resistors = "4k Ohm"; 179 break; 180 181 default: 182 diodes = "disabled"; 183 break; 184 } 185 } 186 187 return sprintf(buf, "%s %s\n", diodes, resistors); 188 } 189 190 static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL); 191 192 static struct attribute *ds1343_attrs[] = { 193 &dev_attr_glitch_filter.attr, 194 &dev_attr_trickle_charger.attr, 195 NULL 196 }; 197 198 static const struct attribute_group ds1343_attr_group = { 199 .attrs = ds1343_attrs, 200 }; 201 202 static int ds1343_read_time(struct device *dev, struct rtc_time *dt) 203 { 204 struct ds1343_priv *priv = dev_get_drvdata(dev); 205 unsigned char buf[7]; 206 int res; 207 208 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7); 209 if (res) 210 return res; 211 212 dt->tm_sec = bcd2bin(buf[0]); 213 dt->tm_min = bcd2bin(buf[1]); 214 dt->tm_hour = bcd2bin(buf[2] & 0x3F); 215 dt->tm_wday = bcd2bin(buf[3]) - 1; 216 dt->tm_mday = bcd2bin(buf[4]); 217 dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; 218 dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */ 219 220 return 0; 221 } 222 223 static int ds1343_set_time(struct device *dev, struct rtc_time *dt) 224 { 225 struct ds1343_priv *priv = dev_get_drvdata(dev); 226 u8 buf[7]; 227 228 buf[0] = bin2bcd(dt->tm_sec); 229 buf[1] = bin2bcd(dt->tm_min); 230 buf[2] = bin2bcd(dt->tm_hour) & 0x3F; 231 buf[3] = bin2bcd(dt->tm_wday + 1); 232 buf[4] = bin2bcd(dt->tm_mday); 233 buf[5] = bin2bcd(dt->tm_mon + 1); 234 buf[6] = bin2bcd(dt->tm_year - 100); 235 236 return regmap_bulk_write(priv->map, DS1343_SECONDS_REG, 237 buf, sizeof(buf)); 238 } 239 240 static int ds1343_update_alarm(struct device *dev) 241 { 242 struct ds1343_priv *priv = dev_get_drvdata(dev); 243 unsigned int control, stat; 244 unsigned char buf[4]; 245 int res = 0; 246 247 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control); 248 if (res) 249 return res; 250 251 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat); 252 if (res) 253 return res; 254 255 control &= ~(DS1343_A0IE); 256 stat &= ~(DS1343_IRQF0); 257 258 res = regmap_write(priv->map, DS1343_CONTROL_REG, control); 259 if (res) 260 return res; 261 262 res = regmap_write(priv->map, DS1343_STATUS_REG, stat); 263 if (res) 264 return res; 265 266 buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ? 267 0x80 : bin2bcd(priv->alarm_sec) & 0x7F; 268 buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ? 269 0x80 : bin2bcd(priv->alarm_min) & 0x7F; 270 buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ? 271 0x80 : bin2bcd(priv->alarm_hour) & 0x3F; 272 buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ? 273 0x80 : bin2bcd(priv->alarm_mday) & 0x7F; 274 275 res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4); 276 if (res) 277 return res; 278 279 if (priv->irqen) { 280 control |= DS1343_A0IE; 281 res = regmap_write(priv->map, DS1343_CONTROL_REG, control); 282 } 283 284 return res; 285 } 286 287 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 288 { 289 struct ds1343_priv *priv = dev_get_drvdata(dev); 290 int res = 0; 291 unsigned int stat; 292 293 if (priv->irq <= 0) 294 return -EINVAL; 295 296 mutex_lock(&priv->mutex); 297 298 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat); 299 if (res) 300 goto out; 301 302 alarm->enabled = !!(priv->irqen & RTC_AF); 303 alarm->pending = !!(stat & DS1343_IRQF0); 304 305 alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec; 306 alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min; 307 alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour; 308 alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday; 309 310 out: 311 mutex_unlock(&priv->mutex); 312 return res; 313 } 314 315 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 316 { 317 struct ds1343_priv *priv = dev_get_drvdata(dev); 318 int res = 0; 319 320 if (priv->irq <= 0) 321 return -EINVAL; 322 323 mutex_lock(&priv->mutex); 324 325 priv->alarm_sec = alarm->time.tm_sec; 326 priv->alarm_min = alarm->time.tm_min; 327 priv->alarm_hour = alarm->time.tm_hour; 328 priv->alarm_mday = alarm->time.tm_mday; 329 330 if (alarm->enabled) 331 priv->irqen |= RTC_AF; 332 333 res = ds1343_update_alarm(dev); 334 335 mutex_unlock(&priv->mutex); 336 337 return res; 338 } 339 340 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled) 341 { 342 struct ds1343_priv *priv = dev_get_drvdata(dev); 343 int res = 0; 344 345 if (priv->irq <= 0) 346 return -EINVAL; 347 348 mutex_lock(&priv->mutex); 349 350 if (enabled) 351 priv->irqen |= RTC_AF; 352 else 353 priv->irqen &= ~RTC_AF; 354 355 res = ds1343_update_alarm(dev); 356 357 mutex_unlock(&priv->mutex); 358 359 return res; 360 } 361 362 static irqreturn_t ds1343_thread(int irq, void *dev_id) 363 { 364 struct ds1343_priv *priv = dev_id; 365 unsigned int stat, control; 366 int res = 0; 367 368 mutex_lock(&priv->mutex); 369 370 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat); 371 if (res) 372 goto out; 373 374 if (stat & DS1343_IRQF0) { 375 stat &= ~DS1343_IRQF0; 376 regmap_write(priv->map, DS1343_STATUS_REG, stat); 377 378 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control); 379 if (res) 380 goto out; 381 382 control &= ~DS1343_A0IE; 383 regmap_write(priv->map, DS1343_CONTROL_REG, control); 384 385 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF); 386 } 387 388 out: 389 mutex_unlock(&priv->mutex); 390 return IRQ_HANDLED; 391 } 392 393 static const struct rtc_class_ops ds1343_rtc_ops = { 394 .read_time = ds1343_read_time, 395 .set_time = ds1343_set_time, 396 .read_alarm = ds1343_read_alarm, 397 .set_alarm = ds1343_set_alarm, 398 .alarm_irq_enable = ds1343_alarm_irq_enable, 399 }; 400 401 static int ds1343_probe(struct spi_device *spi) 402 { 403 struct ds1343_priv *priv; 404 struct regmap_config config = { .reg_bits = 8, .val_bits = 8, 405 .write_flag_mask = 0x80, }; 406 unsigned int data; 407 int res; 408 struct nvmem_config nvmem_cfg = { 409 .name = "ds1343-", 410 .word_size = 1, 411 .stride = 1, 412 .size = DS1343_NVRAM_LEN, 413 .reg_read = ds1343_nvram_read, 414 .reg_write = ds1343_nvram_write, 415 }; 416 417 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL); 418 if (!priv) 419 return -ENOMEM; 420 421 priv->spi = spi; 422 mutex_init(&priv->mutex); 423 424 /* RTC DS1347 works in spi mode 3 and 425 * its chip select is active high 426 */ 427 spi->mode = SPI_MODE_3 | SPI_CS_HIGH; 428 spi->bits_per_word = 8; 429 res = spi_setup(spi); 430 if (res) 431 return res; 432 433 spi_set_drvdata(spi, priv); 434 435 priv->map = devm_regmap_init_spi(spi, &config); 436 437 if (IS_ERR(priv->map)) { 438 dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n"); 439 return PTR_ERR(priv->map); 440 } 441 442 res = regmap_read(priv->map, DS1343_SECONDS_REG, &data); 443 if (res) 444 return res; 445 446 regmap_read(priv->map, DS1343_CONTROL_REG, &data); 447 data |= DS1343_INTCN; 448 data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE); 449 regmap_write(priv->map, DS1343_CONTROL_REG, data); 450 451 regmap_read(priv->map, DS1343_STATUS_REG, &data); 452 data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0); 453 regmap_write(priv->map, DS1343_STATUS_REG, data); 454 455 priv->rtc = devm_rtc_allocate_device(&spi->dev); 456 if (IS_ERR(priv->rtc)) 457 return PTR_ERR(priv->rtc); 458 459 priv->rtc->nvram_old_abi = true; 460 priv->rtc->ops = &ds1343_rtc_ops; 461 priv->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 462 priv->rtc->range_max = RTC_TIMESTAMP_END_2099; 463 464 res = rtc_add_group(priv->rtc, &ds1343_attr_group); 465 if (res) 466 dev_err(&spi->dev, 467 "unable to create sysfs entries for rtc ds1343\n"); 468 469 res = rtc_register_device(priv->rtc); 470 if (res) 471 return res; 472 473 nvmem_cfg.priv = priv; 474 rtc_nvmem_register(priv->rtc, &nvmem_cfg); 475 476 priv->irq = spi->irq; 477 478 if (priv->irq >= 0) { 479 res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, 480 ds1343_thread, IRQF_ONESHOT, 481 "ds1343", priv); 482 if (res) { 483 priv->irq = -1; 484 dev_err(&spi->dev, 485 "unable to request irq for rtc ds1343\n"); 486 } else { 487 device_init_wakeup(&spi->dev, true); 488 dev_pm_set_wake_irq(&spi->dev, spi->irq); 489 } 490 } 491 492 return 0; 493 } 494 495 static int ds1343_remove(struct spi_device *spi) 496 { 497 struct ds1343_priv *priv = spi_get_drvdata(spi); 498 499 if (spi->irq) { 500 mutex_lock(&priv->mutex); 501 priv->irqen &= ~RTC_AF; 502 mutex_unlock(&priv->mutex); 503 504 dev_pm_clear_wake_irq(&spi->dev); 505 device_init_wakeup(&spi->dev, false); 506 devm_free_irq(&spi->dev, spi->irq, priv); 507 } 508 509 spi_set_drvdata(spi, NULL); 510 511 return 0; 512 } 513 514 #ifdef CONFIG_PM_SLEEP 515 516 static int ds1343_suspend(struct device *dev) 517 { 518 struct spi_device *spi = to_spi_device(dev); 519 520 if (spi->irq >= 0 && device_may_wakeup(dev)) 521 enable_irq_wake(spi->irq); 522 523 return 0; 524 } 525 526 static int ds1343_resume(struct device *dev) 527 { 528 struct spi_device *spi = to_spi_device(dev); 529 530 if (spi->irq >= 0 && device_may_wakeup(dev)) 531 disable_irq_wake(spi->irq); 532 533 return 0; 534 } 535 536 #endif 537 538 static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume); 539 540 static struct spi_driver ds1343_driver = { 541 .driver = { 542 .name = "ds1343", 543 .pm = &ds1343_pm, 544 }, 545 .probe = ds1343_probe, 546 .remove = ds1343_remove, 547 .id_table = ds1343_id, 548 }; 549 550 module_spi_driver(ds1343_driver); 551 552 MODULE_DESCRIPTION("DS1343 RTC SPI Driver"); 553 MODULE_AUTHOR("Raghavendra Chandra Ganiga <[email protected]>," 554 "Ankur Srivastava <[email protected]>"); 555 MODULE_LICENSE("GPL v2"); 556