Skip to content

Commit b362bfc

Browse files
pelwellpopcornmix
authored andcommitted
rtc: rv3028: Write BSM and TCE/TCR to EEPROM
Periodically the RV3028 refreshes registers from the EEPROM. When this happens, some settings that have only been committed to registers are lost. Change the handling of backup-switchover-mode and trickle-resistor-ohms to write the EEPROM instead (if something has changed), on the understanding that registers will be refreshed afterwards. See: raspberrypi#2912 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent 4bd1295 commit b362bfc

1 file changed

Lines changed: 43 additions & 17 deletions

File tree

‎drivers/rtc/rtc-rv3028.c‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/of_device.h>
2020
#include <linux/regmap.h>
2121
#include <linux/rtc.h>
22+
//#include "rtc-core.h"
2223

2324
#define RV3028_SEC 0x00
2425
#define RV3028_MIN 0x01
@@ -79,7 +80,7 @@
7980

8081
#define RV3028_BACKUP_TCE BIT(5)
8182
#define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
82-
#define RV3028_BACKUP_BSM_MASK 0x0C
83+
#define RV3028_BACKUP_BSM_MASK GENMASK(3,2)
8384

8485
#define OFFSET_STEP_PPT 953674
8586

@@ -731,7 +732,8 @@ static int rv3028_probe(struct i2c_client *client)
731732
struct rv3028_data *rv3028;
732733
int ret, status;
733734
u32 ohms;
734-
u8 bsm;
735+
u32 bsm;
736+
u8 backup, backup_bits, backup_mask;
735737
struct nvmem_config nvmem_cfg = {
736738
.name = "rv3028_nvram",
737739
.word_size = 1,
@@ -803,16 +805,17 @@ static int rv3028_probe(struct i2c_client *client)
803805
if (ret)
804806
return ret;
805807

808+
backup_bits = 0;
809+
backup_mask = 0;
810+
806811
/* setup backup switchover mode */
807-
if (!device_property_read_u8(&client->dev, "backup-switchover-mode",
808-
&bsm)) {
812+
dev_dbg(&client->dev, "Checking RTC backup switchover-mode\n");
813+
if (!device_property_read_u32(&client->dev,
814+
"backup-switchover-mode",
815+
&bsm)) {
809816
if (bsm <= 3) {
810-
ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
811-
RV3028_BACKUP_BSM_MASK,
812-
(bsm & 0x03) << 2);
813-
814-
if (ret)
815-
return ret;
817+
backup_bits |= (u8)(bsm << 2);
818+
backup_mask |= RV3028_BACKUP_BSM_MASK;
816819
} else {
817820
dev_warn(&client->dev, "invalid backup switchover mode value\n");
818821
}
@@ -828,15 +831,38 @@ static int rv3028_probe(struct i2c_client *client)
828831
break;
829832

830833
if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
831-
ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
832-
RV3028_BACKUP_TCE |
833-
RV3028_BACKUP_TCR_MASK,
834-
RV3028_BACKUP_TCE | i);
835-
if (ret)
836-
return ret;
834+
backup_bits |= RV3028_BACKUP_TCE | i;
835+
backup_mask |= RV3028_BACKUP_TCE |
836+
RV3028_BACKUP_TCR_MASK;
837837
} else {
838-
dev_warn(&client->dev, "invalid trickle resistor value\n");
838+
dev_warn(&client->dev,
839+
"invalid trickle resistor value\n");
840+
}
841+
}
842+
843+
if (backup_mask) {
844+
ret = rv3028_eeprom_read((void *)(rv3028->regmap),
845+
RV3028_BACKUP,
846+
(void *)&backup, 1);
847+
if (!ret) {
848+
/* Write EEPROM only if needed */
849+
if ((backup & backup_mask) != backup_bits) {
850+
backup = (backup & ~backup_mask) | backup_bits;
851+
dev_dbg(&client->dev,
852+
"Backup register doesn't match: EEPROM write required\n");
853+
ret = rv3028_eeprom_write(
854+
(void *)(rv3028->regmap),
855+
RV3028_BACKUP, (void *)&backup, 1);
856+
}
839857
}
858+
859+
/* In the event of an EEPROM failure, update the register
860+
instead. */
861+
if (ret)
862+
ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
863+
backup_mask, backup_bits);
864+
if (ret)
865+
return ret;
840866
}
841867

842868
ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);

0 commit comments

Comments
 (0)