Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: pcf2127: add BSM support

The pcf2127 encodes BSM, BLD and power fail detection in the same set of
bits so it is necessary to do some calculation when changing BSM to keep
the rest of the configuration as-is. However, when BSM is disabled, there
is no configuration with BLD enabled so this will be lost when coming back
to a mode with BSM enabled.

Link: https://lore.kernel.org/r/20250127162728.86234-1-alexandre.belloni@bootlin.com
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

+82
+82
drivers/rtc/rtc-pcf2127.c
··· 20 20 #include <linux/i2c.h> 21 21 #include <linux/spi/spi.h> 22 22 #include <linux/bcd.h> 23 + #include <linux/bitfield.h> 23 24 #include <linux/rtc.h> 24 25 #include <linux/slab.h> 25 26 #include <linux/module.h> ··· 49 48 #define PCF2127_BIT_CTRL3_BLF BIT(2) 50 49 #define PCF2127_BIT_CTRL3_BF BIT(3) 51 50 #define PCF2127_BIT_CTRL3_BTSE BIT(4) 51 + #define PCF2127_CTRL3_PM GENMASK(7, 5) 52 52 /* Time and date registers */ 53 53 #define PCF2127_REG_TIME_BASE 0x03 54 54 #define PCF2127_BIT_SC_OSF BIT(7) ··· 328 326 dev_dbg(dev, "clearing STOP bit failed\n"); 329 327 return err; 330 328 } 329 + } 330 + 331 + return 0; 332 + } 333 + 334 + static int pcf2127_param_get(struct device *dev, struct rtc_param *param) 335 + { 336 + struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 337 + u32 value; 338 + int ret; 339 + 340 + switch (param->param) { 341 + case RTC_PARAM_BACKUP_SWITCH_MODE: 342 + ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value); 343 + if (ret < 0) 344 + return ret; 345 + 346 + value = FIELD_GET(PCF2127_CTRL3_PM, value); 347 + 348 + if (value < 0x3) 349 + param->uvalue = RTC_BSM_LEVEL; 350 + else if (value < 0x6) 351 + param->uvalue = RTC_BSM_DIRECT; 352 + else 353 + param->uvalue = RTC_BSM_DISABLED; 354 + 355 + break; 356 + 357 + default: 358 + return -EINVAL; 359 + } 360 + 361 + return 0; 362 + } 363 + 364 + static int pcf2127_param_set(struct device *dev, struct rtc_param *param) 365 + { 366 + struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 367 + u8 mode = 0; 368 + u32 value; 369 + int ret; 370 + 371 + switch (param->param) { 372 + case RTC_PARAM_BACKUP_SWITCH_MODE: 373 + ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value); 374 + if (ret < 0) 375 + return ret; 376 + 377 + value = FIELD_GET(PCF2127_CTRL3_PM, value); 378 + 379 + if (value > 5) 380 + value -= 5; 381 + else if (value > 2) 382 + value -= 3; 383 + 384 + switch (param->uvalue) { 385 + case RTC_BSM_LEVEL: 386 + break; 387 + case RTC_BSM_DIRECT: 388 + mode = 3; 389 + break; 390 + case RTC_BSM_DISABLED: 391 + if (value == 0) 392 + value = 1; 393 + mode = 5; 394 + break; 395 + default: 396 + return -EINVAL; 397 + } 398 + 399 + return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, 400 + PCF2127_CTRL3_PM, 401 + FIELD_PREP(PCF2127_CTRL3_PM, mode + value)); 402 + 403 + break; 404 + 405 + default: 406 + return -EINVAL; 331 407 } 332 408 333 409 return 0; ··· 821 741 .read_alarm = pcf2127_rtc_read_alarm, 822 742 .set_alarm = pcf2127_rtc_set_alarm, 823 743 .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable, 744 + .param_get = pcf2127_param_get, 745 + .param_set = pcf2127_param_set, 824 746 }; 825 747 826 748 /* sysfs interface */