Skip to contentSkip to conversation

rtc: isl12022: battery backup voltage and clock support

PATCH Superseded Rasmus Villemoes 7 participants 78 messages Jun 12, 2023 – Aug 15, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rtc: isl12022: battery backup voltage and clock support

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

Rasmus Villemoes (8):
rtc: isl12022: remove wrong warning for low battery level
dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own
schema file
dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels
rtc: isl12022: add support for trip level DT bindings
rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls
rtc: isl12022: trigger battery level detection during probe
dt-bindings: rtc: isl12022: add #clock-cells property
rtc: isl12022: implement support for the #clock-cells DT property

 .../bindings/rtc/intersil,isl12022.yaml       |  66 +++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |   2 -
 drivers/rtc/rtc-isl12022.c                    | 140 +++++++++++++++++-
 3 files changed, 200 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/8 rtc: isl12022: remove wrong warning for low battery level

There are multiple problems with this warning.

First of all, it triggers way too often, in fact nearly on every boot, because the SR_LBAT85/SR_LBAT75 bits have another meaning when in battery backup mode. Quoting from the data sheet:

LOW BATTERY INDICATOR 85% BIT (LBAT85)

In Normal Mode (VDD), this bit indicates when the battery level has dropped below the pre-selected trip levels. [...] The LBAT85 detection happens automatically once every minute when seconds register reaches 59.

In Battery Mode (VBAT), this bit indicates the device has entered into battery mode by polling once every 10 minutes. The LBAT85 detection happens automatically once when the minute register reaches x9h or x0h minutes.

Similar wording applies to the LBAT75 bit.

This means that if the device is powered off for more than 10 minutes, the LBAT85 bit is guaranteed to be set. Upon power-on, unless we're close enough to the end of a minute and/or the boot is slow enough that the second register passes 59, the LBAT85 bit is still set when the kernel (or early userspace) reads the RTC to set the system's wallclock time.

Another minor problem is with the bit logic. If the 75% level is reached, logically we're also below 85%, so both bits would most likely be set. So even if the battery is below 75%, the warning would still say "voltage dropped below 85%".

A third problem is that the driver and current DT binding offer no way to indicate the nominal battery level and/or settings of the Battery Level Monitor Trip Bits. Since the default value of the VB85TP[2:0] and VB75TP[2:0] bits are 000, this means the actual setting of the LBAT85/LBAT75 bits in VDD mode doesn't happen until the battery is below 2.125V/1.875V, which for a standard 3V battery is way too late.

A fourth problem is emitting this warning from ->read_time:
util-linux' hwclock will, in the absence of support for getting an interrupt when the seconds counter is updated, issue ioctl(RTC_RD_TIME) in a busy-loop until it sees a change in the seconds field. In that case, if the battery low bits are set (either genuinely, more than a minute after boot, due to the battery actually being low, or as above, bogusly shortly after boot), the kernel log is swamped with hundreds of identical warnings.

Subsequent patches will add such bindings and driver support, and also proper support for RTC_VL_READ. For now, remove the broken warning.

---
 drivers/rtc/rtc-isl12022.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -6
index e68a79b5e00e..ebd66b835cef 100644
@@ -141,12 +141,6 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	if (ret)
 		return ret;
 
-	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(dev,
-			 "voltage dropped below %u%%, date and time is not reliable.\n",
-			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
-	}
-
 	dev_dbg(dev,
 		"raw data is sec=%02x, min=%02x, hr=%02x, mday=%02x, mon=%02x, year=%02x, wday=%02x, sr=%02x, int=%02x",
 		buf[ISL12022_REG_SC],
Signature · 1 line
2.37.2
#2 lore raw

PATCH 2/8 dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

In reply to Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml new file +42 -0
new file mode 100644
index 000000000000..899c5edc72e4
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/intersil,isl12022.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Intersil ISL12022 Real-time Clock
+
+allOf:
+  - $ref: rtc.yaml#
+
+properties:
+  compatible:
+    const: isil,isl12022
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        rtc@6f {
+            compatible = "isil,isl12022";
+            reg = <0x6f>;
+            interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
+
+...
Documentation/devicetree/bindings/rtc/trivial-rtc.yaml +0 -2
index a3603e638c37..b062c64266a6 100644
@@ -45,8 +45,6 @@ properties:
       - isil,isl1208
       # Intersil ISL1218 Low Power RTC with Battery Backed SRAM
       - isil,isl1218
-      # Intersil ISL12022 Real-time Clock
-      - isil,isl12022
       # Loongson-2K Socs/LS7A bridge Real-time Clock
       - loongson,ls2x-rtc
       # Real Time Clock Module with I2C-Bus
Signature · 1 line
2.37.2
maintainer
#3 lore raw

Re: [PATCH 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Traceback (most recent call last):
File "/usr/local/bin/dt-doc-validate", line 62, in <module>

    ret |= check_doc(f)

File "/usr/local/bin/dt-doc-validate", line 31, in check_doc
for error in sorted(dtschema.DTValidator.iter_schema_errors(testtree), key=lambda e: e.linecol):
File "/usr/local/lib/python3.10/dist-packages/dtschema/lib.py", line 736, in iter_schema_errors
cls.annotate_error(error, meta_schema, error.schema_path)
File "/usr/local/lib/python3.10/dist-packages/dtschema/lib.py", line 712, in annotate_error
schema = schema[p]
KeyError: 'type' /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#4 lore raw

Re: [PATCH 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

Quoted 13 lines · Rob Herring
Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Quoted 8 lines

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#

Hm ok. Can/should I copy the value from the trivial-rtc.yaml? Alexandre, would that be ok with you?

Is there some simple way to do that dt_binding_check for a single file or just a few? It seems to take forever to run on the whole tree.

Rasmus

#5 lore raw

Re: [PATCH 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

Quoted 17 lines · Rasmus Villemoes
Quoted 16 lines · Rob Herring
Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

Quoted 9 lines
Quoted 5 lines

yamllint warnings/errors:

/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#

Hm ok. Can/should I copy the value from the trivial-rtc.yaml? Alexandre, would that be ok with you?

Yes

Quoted 3 lines

Is there some simple way to do that dt_binding_check for a single file or just a few? It seems to take forever to run on the whole tree.

The kernel documentation has this example:

make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#6 lore raw

Re: [PATCH 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

In reply to Rasmus Villemoes
Quoted 17 lines · Rasmus Villemoes
Quoted 16 lines · Rob Herring
Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

Quoted 8 lines
Quoted 5 lines

yamllint warnings/errors:

/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#

Hm ok. Can/should I copy the value from the trivial-rtc.yaml? Alexandre, would that be ok with you?

Alexandre agreed, but in general the maintainer here should be someone that has the h/w and/or cares about it, not subsystem maintainers.

Rob

#7 lore raw

Re: [PATCH 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

Quoted 24 lines · Rob Herring
Quoted 23 lines · Rasmus Villemoes
Quoted 21 lines · Rob Herring
Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 42 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#

Quoted 5 lines
Quoted 2 lines

Hm ok. Can/should I copy the value from the trivial-rtc.yaml? Alexandre, would that be ok with you?

Alexandre agreed, but in general the maintainer here should be someone that has the h/w and/or cares about it, not subsystem maintainers.

OK. Right now I have the hardware and care about it because I've been hired to work on it.

Incidentally, my backlog for this project/product also contains upstreaming of a new gpiochip driver and DT bindings. I assume I should just list myself as maintainer in that new .yaml file, even if I can't promise to have time to review changes and/or even hardware to test on 12 months from now.

Rasmus

#8 lore raw

PATCH 3/8 dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

In reply to Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

There are total of 7 levels that could be selected for the first alarm. Any of the of levels could be selected as the first alarm with no reference as to nominal Battery voltage level.

Hence this provides the hardware designer the ability to choose values based on the discharge characteristics of the battery chosen for the given product, rather than just having one battery-microvolt property and having the driver choose levels close to 0.85/0.75 times that.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +14 -0
index 899c5edc72e4..1e85a9c8945b 100644
@@ -19,6 +19,18 @@ properties:
   interrupts:
     maxItems: 1
 
+  isil,trip-level85-microvolt:
+    description: |
+      The battery voltage at which the first alarm should trigger
+      (normally ~85% of nominal V_BAT).
+    enum: [2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000]
+
+  isil,trip-level75-microvolt:
+    description: |
+      The battery voltage at which the second alarm should trigger
+      (normally ~75% of nominal V_BAT).
+    enum: [1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000]
+
 required:
   - compatible
   - reg
@@ -36,6 +48,8 @@ examples:
             compatible = "isil,isl12022";
             reg = <0x6f>;
             interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+            isil,trip-level85-microvolt = <2550000>;
+            isil,trip-level75-microvolt = <2250000>;
         };
     };
 
Signature · 1 line
2.37.2
#9 lore raw

PATCH 4/8 rtc: isl12022: add support for trip level DT bindings

In reply to Rasmus Villemoes

Implement support for using the values given in the isil,trip-level[87]5-microvolt properties to set appropriate values in the VB[87]5TP bits in the PWR_VBAT register.

---
 drivers/rtc/rtc-isl12022.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
drivers/rtc/rtc-isl12022.c +35 -0
index ebd66b835cef..cb8f1d92e116 100644
@@ -31,6 +31,8 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_PWR_VBAT	0x0a
+
 #define ISL12022_REG_BETA	0x0d
 #define ISL12022_REG_TEMP_L	0x28
 
@@ -42,6 +44,9 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
+#define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
+
 #define ISL12022_BETA_TSE	(1 << 7)
 
 static umode_t isl12022_hwmon_is_visible(const void *data,
@@ -209,6 +214,35 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static const u32 trip_level85[] = { 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 };
+static const u32 trip_level75[] = { 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 };
+
+static void isl12022_set_trip_levels(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 level85 = 0, level75 = 0;
+	int ret, x85, x75;
+	u8 val, mask;
+
+	device_property_read_u32(dev, "isil,trip-level85-microvolt", &level85);
+	device_property_read_u32(dev, "isil,trip-level75-microvolt", &level75);
+
+	for (x85 = 0; x85 < ARRAY_SIZE(trip_level85) - 1; ++x85)
+		if (level85 <= trip_level85[x85])
+			break;
+
+	for (x75 = 0; x75 < ARRAY_SIZE(trip_level75) - 1; ++x75)
+		if (level75 <= trip_level75[x75])
+			break;
+
+	val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);
+	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
+
+	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
+	if (ret)
+		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+}
+
 static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
@@ -225,6 +259,7 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
 	rtc = devm_rtc_allocate_device(&client->dev);
Signature · 1 line
2.37.2
#10 lore raw

Re: [PATCH 4/8] rtc: isl12022: add support for trip level DT bindings

Quoted 3 lines · Rasmus Villemoes

Implement support for using the values given in the isil,trip-level[87]5-microvolt properties to set appropriate values in the VB[87]5TP bits in the PWR_VBAT register.

...

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 236

Quoted 7 lines
drivers/rtc/rtc-isl12022.c +7 -0
+	for (x85 = 0; x85 < ARRAY_SIZE(trip_level85) - 1; ++x85)
+		if (level85 <= trip_level85[x85])
+			break;
+
+	for (x75 = 0; x75 < ARRAY_SIZE(trip_level75) - 1; ++x75)
+		if (level75 <= trip_level75[x75])
+			break;

Does preincrement give us anything in comparison to postincrement?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH 4/8] rtc: isl12022: add support for trip level DT bindings

In reply to Rasmus Villemoes
kernel test robot noticed the following build errors:

Hi Rasmus,

kernel test robot noticed the following build errors:

[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on robh/for-next linus/master v6.4-rc6 next-20230609] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/rtc-isl12022-remove-wrong-warning-for-low-battery-level/20230612-211434
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
patch link:    https://lore.kernel.org/r/20230612113059.247275-5-linux%40rasmusvillemoes.dk
patch subject: [PATCH 4/8] rtc: isl12022: add support for trip level DT bindings
config: arm-randconfig-r012-20230612 (https://download.01.org/0day-ci/archive/20230613/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)

reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
git remote add abelloni https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git
git fetch abelloni rtc-next
git checkout abelloni/rtc-next
b4 shazam https://lore.kernel.org/r/[email protected]
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang ~/bin/make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang ~/bin/make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/rtc/

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/rtc/rtc-isl12022.c:238:8: error: call to undeclared function 'FIELD_PREP'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     238 |         val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);
         |               ^

1 error generated.

vim +/FIELD_PREP +238 drivers/rtc/rtc-isl12022.c

219
220 static void isl12022_set_trip_levels(struct device *dev)
221 {
222 struct regmap *regmap = dev_get_drvdata(dev);
223 u32 level85 = 0, level75 = 0;
224 int ret, x85, x75;
225 u8 val, mask;
226
227 device_property_read_u32(dev, "isil,trip-level85-microvolt", &level85);
228 device_property_read_u32(dev, "isil,trip-level75-microvolt", &level75);
229
230 for (x85 = 0; x85 < ARRAY_SIZE(trip_level85) - 1; ++x85)
231 if (level85 <= trip_level85[x85])
232 break;
233
234 for (x75 = 0; x75 < ARRAY_SIZE(trip_level75) - 1; ++x75)
235 if (level75 <= trip_level75[x75])
236 break;
237

 > 238		val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);

239 mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
240
241 ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
242 if (ret)
243 dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
244 }
245

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#12 lore raw

Re: [PATCH 4/8] rtc: isl12022: add support for trip level DT bindings

In reply to Rasmus Villemoes
kernel test robot noticed the following build errors:

Hi Rasmus,

kernel test robot noticed the following build errors:

[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on robh/for-next linus/master v6.4-rc6 next-20230609] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/rtc-isl12022-remove-wrong-warning-for-low-battery-level/20230612-211434
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
patch link:    https://lore.kernel.org/r/20230612113059.247275-5-linux%40rasmusvillemoes.dk
patch subject: [PATCH 4/8] rtc: isl12022: add support for trip level DT bindings
config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20230613/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 12.3.0

reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
git remote add abelloni https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git
git fetch abelloni rtc-next
git checkout abelloni/rtc-next
b4 shazam https://lore.kernel.org/r/[email protected]
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=alpha olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=alpha SHELL=/bin/bash drivers/

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/rtc/rtc-isl12022.c: In function 'isl12022_set_trip_levels':

>> drivers/rtc/rtc-isl12022.c:238:15: error: implicit declaration of function 'FIELD_PREP' [-Werror=implicit-function-declaration]
     238 |         val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);
         |               ^~~~~~~~~~

cc1: some warnings being treated as errors

vim +/FIELD_PREP +238 drivers/rtc/rtc-isl12022.c

219
220 static void isl12022_set_trip_levels(struct device *dev)
221 {
222 struct regmap *regmap = dev_get_drvdata(dev);
223 u32 level85 = 0, level75 = 0;
224 int ret, x85, x75;
225 u8 val, mask;
226
227 device_property_read_u32(dev, "isil,trip-level85-microvolt", &level85);
228 device_property_read_u32(dev, "isil,trip-level75-microvolt", &level75);
229
230 for (x85 = 0; x85 < ARRAY_SIZE(trip_level85) - 1; ++x85)
231 if (level85 <= trip_level85[x85])
232 break;
233
234 for (x75 = 0; x75 < ARRAY_SIZE(trip_level75) - 1; ++x75)
235 if (level75 <= trip_level75[x75])
236 break;
237

 > 238		val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);

239 mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
240
241 ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
242 if (ret)
243 dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
244 }
245

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#13 lore raw

PATCH 5/8 rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

In reply to Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

---
 drivers/rtc/rtc-isl12022.c | 41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
drivers/rtc/rtc-isl12022.c +41 -0
index cb8f1d92e116..1b6659a9b33a 100644
@@ -203,7 +203,48 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return regmap_bulk_write(regmap, ISL12022_REG_SC, buf, sizeof(buf));
 }
 
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;
+}
+
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = isl12022_read_sr(regmap);
+		if (ret < 0)
+			return ret;
+
+		if (ret & ISL12022_SR_LBAT85)
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (ret & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	case RTC_VL_CLR:
+		return regmap_clear_bits(regmap, ISL12022_REG_SR,
+					 ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops isl12022_rtc_ops = {
+	.ioctl		= isl12022_rtc_ioctl,
 	.read_time	= isl12022_rtc_read_time,
 	.set_time	= isl12022_rtc_set_time,
 };
Signature · 1 line
2.37.2
#14 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

Quoted 41 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

---
 drivers/rtc/rtc-isl12022.c | 41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
drivers/rtc/rtc-isl12022.c +24 -0
index cb8f1d92e116..1b6659a9b33a 100644
@@ -203,7 +203,48 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return regmap_bulk_write(regmap, ISL12022_REG_SC, buf, sizeof(buf));
 }
 
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;
+}
+
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = isl12022_read_sr(regmap);
+		if (ret < 0)
+			return ret;
+
+		if (ret & ISL12022_SR_LBAT85)

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 239

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (ret & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	case RTC_VL_CLR:
+		return regmap_clear_bits(regmap, ISL12022_REG_SR,
+					 ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75);

I'm against using RTC_VL_CLR for this as it deletes important information (i.e. the date is probably invalid). You should let the RTC clear the bits once the battery has been changed:

"The LBAT75 bit is set when the
VBAT has dropped below the pre-selected trip level, and will self clear when the VBAT is above the pre-selected trip level at the next detection cycle either by manual or automatic trigger."

Quoted 14 lines
drivers/rtc/rtc-isl12022.c +7 -0
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops isl12022_rtc_ops = {
+	.ioctl		= isl12022_rtc_ioctl,
 	.read_time	= isl12022_rtc_read_time,
 	.set_time	= isl12022_rtc_set_time,
 };
Signature · 1 line
2.37.2
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#15 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

Quoted 30 lines · Alexandre Belloni
Quoted 13 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +13 -0
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = isl12022_read_sr(regmap);
+		if (ret < 0)
+			return ret;
+
+		if (ret & ISL12022_SR_LBAT85)

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 239

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (ret & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	case RTC_VL_CLR:
+		return regmap_clear_bits(regmap, ISL12022_REG_SR,
+					 ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75);

I'm against using RTC_VL_CLR for this as it deletes important information (i.e. the date is probably invalid). You should let the RTC clear the bits once the battery has been changed:

Quoted 4 lines

"The LBAT75 bit is set when the
VBAT has dropped below the pre-selected trip level, and will self clear when the VBAT is above the pre-selected trip level at the next detection cycle either by manual or automatic trigger."

Well, the same thing means that the bit would get set again within a minute after the RTC_VL_CLR, so the information isn't lost as such. I actually don't understand what RTC_VL_CLR would be for if not this (though, again, in this case at least it would only have a very short-lived effect), but I'm perfectly happy to just rip out the RTC_VL_CLR case.

Rasmus

#16 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

In reply to Rasmus Villemoes
Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

...

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 214

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +9 -0
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;

Wondering if the bit 31 is in use with this register (note, I haven't checked the register width nor datasheet).

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#17 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

...

Quoted 13 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 214

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +9 -0
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;

Wondering if the bit 31 is in use with this register (note, I haven't checked the register width nor datasheet).

register width is in the driver:

static const struct regmap_config regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.use_single_write = true,
};
Quoted 7 lines
Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#18 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

Quoted 22 lines · Alexandre Belloni
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

...

Quoted 13 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 214

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +9 -0
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;

Wondering if the bit 31 is in use with this register (note, I haven't checked the register width nor datasheet).

Quoted 7 lines

register width is in the driver:

static const struct regmap_config regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.use_single_write = true,
};

Yeah.

But I only factored that out because I wanted to read the SR also in the isl12022_set_trip_levels() to emit the warning at boot time, but when that goes away, there's no longer any reason to not just fold this back into the ioctl() handler.

Rasmus

#19 lore raw

Re: [PATCH 5/8] rtc: isl12022: implement RTC_VL_READ and RTC_VL_CLR ioctls

Quoted 31 lines · Rasmus Villemoes
Quoted 22 lines · Alexandre Belloni
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

...

Quoted 13 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 214

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +9 -0
+static int isl12022_read_sr(struct regmap *regmap)
+{
+	int ret;
+	u32 val;
+
+	ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+	if (ret < 0)
+		return ret;
+	return val;

Wondering if the bit 31 is in use with this register (note, I haven't checked the register width nor datasheet).

Quoted 7 lines

register width is in the driver:

static const struct regmap_config regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.use_single_write = true,
};
Quoted 6 lines

Yeah.

But I only factored that out because I wanted to read the SR also in the isl12022_set_trip_levels() to emit the warning at boot time, but when that goes away, there's no longer any reason to not just fold this back into the ioctl() handler.

That would be to clear a not self clearable battery low (but not empty) flag or a backup voltage switch flag.

Quoted 3 lines

Rasmus

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#20 lore raw

PATCH 6/8 rtc: isl12022: trigger battery level detection during probe

In reply to Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot (well, probe), and emit a single warning to the kernel log if the battery is already low.

---
 drivers/rtc/rtc-isl12022.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/rtc/rtc-isl12022.c +18 -1
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);
+	if (ret < 0) {
+		dev_warn(dev, "unable to read status register: %d\n", ret);
+	} else if (ret & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
+		dev_warn(dev, "battery voltage is below %u%%\n",
+			 (ret & ISL12022_SR_LBAT75) ? 75 : 85);
+	}
 }
 
 static int isl12022_probe(struct i2c_client *client)
Signature · 1 line
2.37.2
#21 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

Quoted 12 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +2 -1
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 295

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);

So testing this a bit more thoroughly reveals that the LBAT85/LBAT75 bits do not get updated immediately after the TSE bit is set; one needs to wait a little before the battery voltage detection is done and the SR bits updated. Unfortunately, the data sheet doesn't say anything about how long that might be or if there's some busy bit one could look at; all it says is actually exactly what I've done above:

The battery level monitor is not functional in battery backup mode. In order to read the monitor bits after powering up VDD, instigate a battery level measurement by setting the TSE bit to "1" (BETA register), and then read the bits.

IOW, please don't apply this patch until I figure out how to do this properly.

Rasmus

#22 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

Quoted 32 lines · Rasmus Villemoes
Quoted 12 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +2 -1
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 295

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +10 -0
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);

So testing this a bit more thoroughly reveals that the LBAT85/LBAT75 bits do not get updated immediately after the TSE bit is set; one needs to wait a little before the battery voltage detection is done and the SR bits updated. Unfortunately, the data sheet doesn't say anything about how long that might be or if there's some busy bit one could look at; all it says is actually exactly what I've done above:

Quoted 8 lines

The battery level monitor is not functional in battery backup mode. In order to read the monitor bits after powering up VDD, instigate a battery level measurement by setting the TSE bit to "1" (BETA register), and then read the bits.

IOW, please don't apply this patch until I figure out how to do this properly.

The datasheet states 22ms for the temperature conversion so I would expect it takes about that time.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#23 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

Quoted 14 lines · Alexandre Belloni
Quoted 12 lines · Rasmus Villemoes

So testing this a bit more thoroughly reveals that the LBAT85/LBAT75 bits do not get updated immediately after the TSE bit is set; one needs to wait a little before the battery voltage detection is done and the SR bits updated. Unfortunately, the data sheet doesn't say anything about how long that might be or if there's some busy bit one could look at; all it says is actually exactly what I've done above:

The battery level monitor is not functional in battery backup mode. In order to read the monitor bits after powering up VDD, instigate a battery level measurement by setting the TSE bit to "1" (BETA register), and then read the bits.

Quoted 6 lines
Quoted 3 lines

IOW, please don't apply this patch until I figure out how to do this properly.

The datasheet states 22ms for the temperature conversion so I would expect it takes about that time.

It's most likely much shorter than that - if I just busy-read SR until the LBAT bits are clear, that takes no more than 2ms, and the final read of SR still has the BUSY bit set, indicating a temp conversion being (still) in progress. But I'd prefer to have Renesas provide the proper value rather than using some seems-to-work-on-my-desk. But but, it's probably moot, see other reply.

Rasmus

#24 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

In reply to Rasmus Villemoes
Quoted 25 lines · Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot (well, probe), and emit a single warning to the kernel log if the battery is already low.

---
 drivers/rtc/rtc-isl12022.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 291

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +8 -1
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);

This is too verbose, there is no action for the user upon getting this message.

Setting TSE also enables temperature compensation, which may be an undesirable side effect. Shouldn't this be reverted if necessary?

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 300

Quoted 9 lines
drivers/rtc/rtc-isl12022.c +9 -0
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);
+	if (ret < 0) {
+		dev_warn(dev, "unable to read status register: %d\n", ret);
+	} else if (ret & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
+		dev_warn(dev, "battery voltage is below %u%%\n",
+			 (ret & ISL12022_SR_LBAT75) ? 75 : 85);

This message is useless, I'd drop the whole block.

Quoted 7 lines
drivers/rtc/rtc-isl12022.c +1 -0
+	}
 }
 
 static int isl12022_probe(struct i2c_client *client)
Signature · 1 line
2.37.2
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#25 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

Quoted 25 lines · Alexandre Belloni
Quoted 24 lines · Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot (well, probe), and emit a single warning to the kernel log if the battery is already low.

---
 drivers/rtc/rtc-isl12022.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
Quoted 14 lines

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 291

Quoted 11 lines
drivers/rtc/rtc-isl12022.c +8 -1
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);

This is too verbose, there is no action for the user upon getting this message.

OK.

Quoted 2 lines

Setting TSE also enables temperature compensation, which may be an undesirable side effect. Shouldn't this be reverted if necessary?

Well, I can't imagine the board designer not wanting/expecting temperature compensation to be enabled since they've spent the $$ on using a part with that capability. Also, we anyway set TSE if CONFIG_HWMON so that the TEMP registers get updated once per minute.

If you insist I'll do the proper logic to set it back to 0 if it wasn't set beforehand, but I prefer to just keep it as-is.

Quoted 12 lines

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 300

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +9 -0
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);
+	if (ret < 0) {
+		dev_warn(dev, "unable to read status register: %d\n", ret);
+	} else if (ret & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
+		dev_warn(dev, "battery voltage is below %u%%\n",
+			 (ret & ISL12022_SR_LBAT75) ? 75 : 85);

This message is useless, I'd drop the whole block.

I only added this as "compensation" for ripping out the warning in 1/8, since I assumed somebody actually wanted at least one warning in the kernel log if the battery is low.

I/we are not going to scrape dmesg but will use the ioctl() to monitor the battery, so I'm perfectly happy to just remove this. That will also make the question of how long to wait after setting TSE moot, since certainly userspace won't be able to issue the ioctl() soon enough to see stale values in the LBAT bits.

Rasmus

#26 lore raw

Re: [PATCH 6/8] rtc: isl12022: trigger battery level detection during probe

Quoted 46 lines · Rasmus Villemoes
Quoted 25 lines · Alexandre Belloni
Quoted 24 lines · Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot (well, probe), and emit a single warning to the kernel log if the battery is already low.

---
 drivers/rtc/rtc-isl12022.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/rtc/rtc-isl12022.c +0 -0
index 1b6659a9b33a..690dbb446d1a 100644
@@ -280,8 +280,25 @@ static void isl12022_set_trip_levels(struct device *dev)
 	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
 
Quoted 14 lines

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 291

Quoted 11 lines
drivers/rtc/rtc-isl12022.c +8 -1
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
-	if (ret)
+	if (ret) {
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+		return;
+	}
+
+	ret = regmap_write_bits(regmap, ISL12022_REG_BETA,
+				ISL12022_BETA_TSE, ISL12022_BETA_TSE);
+	if (ret) {
+		dev_warn(dev, "unable to trigger battery level detection: %d\n", ret);

This is too verbose, there is no action for the user upon getting this message.

OK.

Quoted 2 lines

Setting TSE also enables temperature compensation, which may be an undesirable side effect. Shouldn't this be reverted if necessary?

Quoted 8 lines

Well, I can't imagine the board designer not wanting/expecting temperature compensation to be enabled since they've spent the $$ on using a part with that capability. Also, we anyway set TSE if CONFIG_HWMON so that the TEMP registers get updated once per minute.

If you insist I'll do the proper logic to set it back to 0 if it wasn't set beforehand, but I prefer to just keep it as-is.

Ok, fine

Quoted 13 lines
Quoted 12 lines

drivers/rtc/rtc-isl12022.c static void isl12022_set_trip_levels(struct device *dev) · line 300

Quoted 10 lines
drivers/rtc/rtc-isl12022.c +9 -0
+		return;
+	}
+
+	ret = isl12022_read_sr(regmap);
+	if (ret < 0) {
+		dev_warn(dev, "unable to read status register: %d\n", ret);
+	} else if (ret & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
+		dev_warn(dev, "battery voltage is below %u%%\n",
+			 (ret & ISL12022_SR_LBAT75) ? 75 : 85);

This message is useless, I'd drop the whole block.

Quoted 4 lines

I only added this as "compensation" for ripping out the warning in 1/8, since I assumed somebody actually wanted at least one warning in the kernel log if the battery is low.

No need, I had a patch removing the message anyway.

Quoted 6 lines

I/we are not going to scrape dmesg but will use the ioctl() to monitor the battery, so I'm perfectly happy to just remove this. That will also make the question of how long to wait after setting TSE moot, since certainly userspace won't be able to issue the ioctl() soon enough to see stale values in the LBAT bits.

Exactly.

Quoted 2 lines

Rasmus

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#27 lore raw

PATCH 7/8 dt-bindings: rtc: isl12022: add #clock-cells property

In reply to Rasmus Villemoes

The isl12022 has a dual-purpose irq/f_out pin, which can either be used as an interrupt or clock output.

---

I've tried to express the fact that the pin can either be used for interrupts or as a clock source, so that at most one of "interrupts" and "#clock-cells" can be present, but I don't really have any idea if this is the proper way to do that.

 .../devicetree/bindings/rtc/intersil,isl12022.yaml     | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +10 -0
index 1e85a9c8945b..345abed9234f 100644
@@ -19,6 +19,9 @@ properties:
   interrupts:
     maxItems: 1
 
+  '#clock-cells':
+    const: 0
+
   isil,trip-level85-microvolt:
     description: |
       The battery voltage at which the first alarm should trigger
@@ -35,6 +38,13 @@ required:
   - compatible
   - reg
 
+if:
+  properties:
+    '#clock-cells'
+then:
+  properties:
+    interrupts: false
+
 unevaluatedProperties: false
 
 examples:
Signature · 1 line
2.37.2
#28 lore raw

Re: [PATCH 7/8] dt-bindings: rtc: isl12022: add #clock-cells property

Quoted 12 lines · Rasmus Villemoes

The isl12022 has a dual-purpose irq/f_out pin, which can either be used as an interrupt or clock output.

---

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
+
   isil,trip-level85-microvolt:
     description: |
       The battery voltage at which the first alarm should trigger

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml required: · line 46

Quoted 9 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +6 -0
@@ -35,6 +38,13 @@ required:
   - compatible
   - reg
 
+if:
+  properties:
+    '#clock-cells'
+then:
+  properties:
+    interrupts: false

https://elixir.bootlin.com/linux/v6.2-rc3/source/Documentation/devicetree/bindings/net/qcom,ipa.yaml#L174

Best regards,
Krzysztof

#29 lore raw

PATCH 8/8 rtc: isl12022: implement support for the #clock-cells DT property

In reply to Rasmus Villemoes

If device tree implies that the chip's IRQ/F_OUT pin is used as a clock, expose that in the driver. For now, pretend it is a fixed-rate (32kHz) clock; if other use cases appear the driver can be updated to provide its own clk_ops etc.

When the clock output is not used on a given board, one can prolong the battery life by ensuring that the FOx bits are 0. For the hardware I'm currently working on, the RTC draws 1.2uA with the FOx bits at their default 0001 value, dropping to 0.88uA when those bits are cleared.

---
 drivers/rtc/rtc-isl12022.c | 41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
drivers/rtc/rtc-isl12022.c +41 -0
index 690dbb446d1a..0054300b744b 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bcd.h>
+#include <linux/clk-provider.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/i2c.h>
@@ -43,6 +44,9 @@
 #define ISL12022_SR_LBAT75	(1 << 1)
 
 #define ISL12022_INT_WRTC	(1 << 6)
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1
 
 #define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
 #define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
@@ -255,6 +259,38 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static int isl12022_register_clock(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	struct clk_hw *hw;
+	int ret;
+
+	if (!device_property_present(dev, "#clock-cells")) {
+		/*
+		 * Disabling the F_OUT pin reduces the power
+		 * consumption in battery mode by ~25%.
+		 */
+		ret = regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK,
+					 ISL12022_INT_FO_OFF);
+		if (ret)
+			dev_warn(dev, "failed to clear FOx bits in INT register: %d", ret);
+		return 0;
+	}
+
+	/*
+	 * For now, only support a fixed clock of 32768Hz (the reset default).
+	 */
+	ret = regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);
+	if (ret)
+		return ret;
+
+	hw = devm_clk_hw_register_fixed_rate(dev, "isl12022_32kHz", NULL, 0, 32768);
+	if (IS_ERR(hw))
+		return PTR_ERR(hw);
+
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
+}
+
 static const u32 trip_level85[] = { 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 };
 static const u32 trip_level75[] = { 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 };
 
@@ -305,6 +341,7 @@ static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
 	struct regmap *regmap;
+	int ret;
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
 		return -ENODEV;
@@ -317,6 +354,10 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	ret = isl12022_register_clock(&client->dev);
+	if (ret)
+		return ret;
+
 	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
Signature · 1 line
2.37.2
#30 lore raw

v2 cover rtc: isl12022: battery backup voltage and clock support

In reply to Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

v2 changes:

Patch 2: add Alexandre as maintainer [Rob's bot].

Patch 4: On arm64, <linux/bitfield.h> apparently ends up being included implicitly, but not so on arm [kernel test robot]. Use the more common post-increment in for loops [Andy].

Patch 5: Drop RTC_VL_CLR, just do RTC_VL_READ [Alexandre].

Patch 6: Set the TSE bit to trigger a manual detection, but drop the part reading the SR register and issuing a dev_warn() in case of low battery [Alexandre].

Patch 7: (Hopefully) properly describe the "at most one of interrupts and #clock-cells" [thanks Krzysztof].

Patch 8: Drop a useless dev_warn() in case clearing the FOx bits fails.

Rasmus Villemoes (8):
rtc: isl12022: remove wrong warning for low battery level
dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own
schema file
dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels
rtc: isl12022: add support for trip level DT bindings
rtc: isl12022: implement RTC_VL_READ ioctl
rtc: isl12022: trigger battery level detection during probe
dt-bindings: rtc: isl12022: add #clock-cells property
rtc: isl12022: implement support for the #clock-cells DT property

 .../bindings/rtc/intersil,isl12022.yaml       |  69 ++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |   2 -
 drivers/rtc/rtc-isl12022.c                    | 118 +++++++++++++++++-
 3 files changed, 181 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Signature · 1 line
2.37.2
#31 lore raw

PATCH v2 1/8 rtc: isl12022: remove wrong warning for low battery level

There are multiple problems with this warning.

First of all, it triggers way too often, in fact nearly on every boot, because the SR_LBAT85/SR_LBAT75 bits have another meaning when in battery backup mode. Quoting from the data sheet:

LOW BATTERY INDICATOR 85% BIT (LBAT85)

In Normal Mode (VDD), this bit indicates when the battery level has dropped below the pre-selected trip levels. [...] The LBAT85 detection happens automatically once every minute when seconds register reaches 59.

In Battery Mode (VBAT), this bit indicates the device has entered into battery mode by polling once every 10 minutes. The LBAT85 detection happens automatically once when the minute register reaches x9h or x0h minutes.

Similar wording applies to the LBAT75 bit.

This means that if the device is powered off for more than 10 minutes, the LBAT85 bit is guaranteed to be set. Upon power-on, unless we're close enough to the end of a minute and/or the boot is slow enough that the second register passes 59, the LBAT85 bit is still set when the kernel (or early userspace) reads the RTC to set the system's wallclock time.

Another minor problem is with the bit logic. If the 75% level is reached, logically we're also below 85%, so both bits would most likely be set. So even if the battery is below 75%, the warning would still say "voltage dropped below 85%".

A third problem is that the driver and current DT binding offer no way to indicate the nominal battery level and/or settings of the Battery Level Monitor Trip Bits. Since the default value of the VB85TP[2:0] and VB75TP[2:0] bits are 000, this means the actual setting of the LBAT85/LBAT75 bits in VDD mode doesn't happen until the battery is below 2.125V/1.875V, which for a standard 3V battery is way too late.

A fourth problem is emitting this warning from ->read_time:
util-linux' hwclock will, in the absence of support for getting an interrupt when the seconds counter is updated, issue ioctl(RTC_RD_TIME) in a busy-loop until it sees a change in the seconds field. In that case, if the battery low bits are set (either genuinely, more than a minute after boot, due to the battery actually being low, or as above, bogusly shortly after boot), the kernel log is swamped with hundreds of identical warnings.

Subsequent patches will add such bindings and driver support, and also proper support for RTC_VL_READ. For now, remove the broken warning.

---
 drivers/rtc/rtc-isl12022.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -6
index e68a79b5e00e..ebd66b835cef 100644
@@ -141,12 +141,6 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	if (ret)
 		return ret;
 
-	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(dev,
-			 "voltage dropped below %u%%, date and time is not reliable.\n",
-			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
-	}
-
 	dev_dbg(dev,
 		"raw data is sec=%02x, min=%02x, hr=%02x, mday=%02x, mon=%02x, year=%02x, wday=%02x, sr=%02x, int=%02x",
 		buf[ISL12022_REG_SC],
Signature · 1 line
2.37.2
#32 lore raw

PATCH v2 2/8 dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

In reply to Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 45 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml new file +45 -0
new file mode 100644
index 000000000000..7c1e638d657a
@@ -0,0 +1,45 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/intersil,isl12022.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Intersil ISL12022 Real-time Clock
+
+maintainers:
+  - Alexandre Belloni <[email protected]>
+
+allOf:
+  - $ref: rtc.yaml#
+
+properties:
+  compatible:
+    const: isil,isl12022
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        rtc@6f {
+            compatible = "isil,isl12022";
+            reg = <0x6f>;
+            interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
+
+...
Documentation/devicetree/bindings/rtc/trivial-rtc.yaml +0 -2
index a3603e638c37..b062c64266a6 100644
@@ -45,8 +45,6 @@ properties:
       - isil,isl1208
       # Intersil ISL1218 Low Power RTC with Battery Backed SRAM
       - isil,isl1218
-      # Intersil ISL12022 Real-time Clock
-      - isil,isl12022
       # Loongson-2K Socs/LS7A bridge Real-time Clock
       - loongson,ls2x-rtc
       # Real Time Clock Module with I2C-Bus
Signature · 1 line
2.37.2
Reviewed-by on v2 2/8
#33 lore raw

Re: [PATCH v2 2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

Quoted 10 lines · Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 45 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml

Best regards,
Krzysztof

#34 lore raw

PATCH v2 3/8 dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

In reply to Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

There are total of 7 levels that could be selected for the first alarm. Any of the of levels could be selected as the first alarm with no reference as to nominal Battery voltage level.

Hence this provides the hardware designer the ability to choose values based on the discharge characteristics of the battery chosen for the given product, rather than just having one battery-microvolt property and having the driver choose levels close to 0.85/0.75 times that.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +14 -0
index 7c1e638d657a..d5d3a687a34d 100644
@@ -22,6 +22,18 @@ properties:
   interrupts:
     maxItems: 1
 
+  isil,trip-level85-microvolt:
+    description: |
+      The battery voltage at which the first alarm should trigger
+      (normally ~85% of nominal V_BAT).
+    enum: [2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000]
+
+  isil,trip-level75-microvolt:
+    description: |
+      The battery voltage at which the second alarm should trigger
+      (normally ~75% of nominal V_BAT).
+    enum: [1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000]
+
 required:
   - compatible
   - reg
@@ -39,6 +51,8 @@ examples:
             compatible = "isil,isl12022";
             reg = <0x6f>;
             interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+            isil,trip-level85-microvolt = <2550000>;
+            isil,trip-level75-microvolt = <2250000>;
         };
     };
 
Signature · 1 line
2.37.2
#35 lore raw

Re: [PATCH v2 3/8] dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

Quoted 30 lines · Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

There are total of 7 levels that could be selected for the first alarm. Any of the of levels could be selected as the first alarm with no reference as to nominal Battery voltage level.

Hence this provides the hardware designer the ability to choose values based on the discharge characteristics of the battery chosen for the given product, rather than just having one battery-microvolt property and having the driver choose levels close to 0.85/0.75 times that.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index 7c1e638d657a..d5d3a687a34d 100644

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml properties: · line 25

Quoted 4 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
@@ -22,6 +22,18 @@ properties:
   interrupts:
     maxItems: 1
 
+  isil,trip-level85-microvolt:

Why encoding level85 in the property name? Your commit msg (datasheet) suggests this is quite flexible, so why it cannot be just list of two trip levels - for first and second interrupt?

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml properties: · line 26

Quoted 1 line
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
+    description: |

Do not need '|' unless you need to preserve formatting.

Best regards,
Krzysztof

#36 lore raw

Re: [PATCH v2 3/8] dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

Quoted 34 lines · Krzysztof Kozlowski
Quoted 33 lines · Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

There are total of 7 levels that could be selected for the first alarm. Any of the of levels could be selected as the first alarm with no reference as to nominal Battery voltage level.

Hence this provides the hardware designer the ability to choose values based on the discharge characteristics of the battery chosen for the given product, rather than just having one battery-microvolt property and having the driver choose levels close to 0.85/0.75 times that.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index 7c1e638d657a..d5d3a687a34d 100644
@@ -22,6 +22,18 @@ properties:
   interrupts:
     maxItems: 1
 
Quoted 5 lines

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml properties: · line 25

Quoted 1 line
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
+  isil,trip-level85-microvolt:

Why encoding level85 in the property name? Your commit msg (datasheet) suggests this is quite flexible, so why it cannot be just list of two trip levels - for first and second interrupt?

Yeah, so I did consider just making it a two-element array isil,trip-levels-microvolt. But then I didn't know how to express the enum constraint, i.e. that the first must be one of the 2125000, ..., 4675000 values and the second one of the 1875000, ..., 4125000 ones. Is that possible, without providing a list of 49 possible pairs? Or is it sufficient to just write this out in prose?

I'm also happy to use other names for these. I just chose to use the 85 and 75 nomenclature because that matches the field names.

Quoted 3 lines

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml properties: · line 26

Quoted 1 line
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
+    description: |

Do not need '|' unless you need to preserve formatting.

OK.

Rasmus

#37 lore raw

Re: [PATCH v2 3/8] dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

Quoted 14 lines · Rasmus Villemoes
Quoted 8 lines
Quoted 8 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index 7c1e638d657a..d5d3a687a34d 100644
@@ -22,6 +22,18 @@ properties:
   interrupts:
     maxItems: 1
 
Quoted 5 lines

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml properties: · line 25

Quoted 1 line
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +1 -0
+  isil,trip-level85-microvolt:

Why encoding level85 in the property name? Your commit msg (datasheet) suggests this is quite flexible, so why it cannot be just list of two trip levels - for first and second interrupt?

Quoted 6 lines

Yeah, so I did consider just making it a two-element array isil,trip-levels-microvolt. But then I didn't know how to express the enum constraint, i.e. that the first must be one of the 2125000, ..., 4675000 values and the second one of the 1875000, ..., 4125000 ones. Is that possible, without providing a list of 49 possible pairs? Or is it sufficient to just write this out in prose?

items:
- enum: [ a, b, c ]
- enum: [ f, d, e ]

Best regards,
Krzysztof

#38 lore raw

PATCH v2 4/8 rtc: isl12022: add support for trip level DT bindings

In reply to Rasmus Villemoes

Implement support for using the values given in the isil,trip-level[87]5-microvolt properties to set appropriate values in the VB[87]5TP bits in the PWR_VBAT register.

---
 drivers/rtc/rtc-isl12022.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/rtc/rtc-isl12022.c +36 -0
index ebd66b835cef..50bbd1fefad8 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bcd.h>
+#include <linux/bitfield.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/i2c.h>
@@ -31,6 +32,8 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_PWR_VBAT	0x0a
+
 #define ISL12022_REG_BETA	0x0d
 #define ISL12022_REG_TEMP_L	0x28
 
@@ -42,6 +45,9 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
+#define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
+
 #define ISL12022_BETA_TSE	(1 << 7)
 
 static umode_t isl12022_hwmon_is_visible(const void *data,
@@ -209,6 +215,35 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static const u32 trip_level85[] = { 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 };
+static const u32 trip_level75[] = { 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 };
+
+static void isl12022_set_trip_levels(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 level85 = 0, level75 = 0;
+	int ret, x85, x75;
+	u8 val, mask;
+
+	device_property_read_u32(dev, "isil,trip-level85-microvolt", &level85);
+	device_property_read_u32(dev, "isil,trip-level75-microvolt", &level75);
+
+	for (x85 = 0; x85 < ARRAY_SIZE(trip_level85) - 1; x85++)
+		if (level85 <= trip_level85[x85])
+			break;
+
+	for (x75 = 0; x75 < ARRAY_SIZE(trip_level75) - 1; x75++)
+		if (level75 <= trip_level75[x75])
+			break;
+
+	val = FIELD_PREP(ISL12022_REG_VB85_MASK, x85) | FIELD_PREP(ISL12022_REG_VB75_MASK, x75);
+	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
+
+	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
+	if (ret)
+		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+}
+
 static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
@@ -225,6 +260,7 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
 	rtc = devm_rtc_allocate_device(&client->dev);
Signature · 1 line
2.37.2
#39 lore raw

PATCH v2 5/8 rtc: isl12022: implement RTC_VL_READ ioctl

In reply to Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

---
 drivers/rtc/rtc-isl12022.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/rtc/rtc-isl12022.c +26 -0
index 50bbd1fefad8..bf0d65643897 100644
@@ -204,7 +204,33 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return regmap_bulk_write(regmap, ISL12022_REG_SC, buf, sizeof(buf));
 }
 
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0, val;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)
+			return ret;
+
+		if (val & ISL12022_SR_LBAT85)
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (val & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops isl12022_rtc_ops = {
+	.ioctl		= isl12022_rtc_ioctl,
 	.read_time	= isl12022_rtc_read_time,
 	.set_time	= isl12022_rtc_set_time,
 };
Signature · 1 line
2.37.2
#40 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

A couple of nit-picks below.

...

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 210

Quoted 4 lines
drivers/rtc/rtc-isl12022.c +4 -0
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0, val;

This assignment can be done in the actual case below.

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 216

Quoted 6 lines
drivers/rtc/rtc-isl12022.c +6 -0
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)

I always feel uneasy with ' < 0' — Does positive error makes sense? Is it even possible? OTOH if the entire driver uses this idiom... oh well, let's make it consistent.

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 217

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+			return ret;
		user = 0;

The rationale to avoid potential mistakes in the future in case this function will be expanded and user will be re-used.

Quoted 12 lines
drivers/rtc/rtc-isl12022.c +12 -0
+		if (val & ISL12022_SR_LBAT85)
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (val & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#41 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

A couple of nit-picks below.

...

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 210

Quoted 4 lines
drivers/rtc/rtc-isl12022.c +4 -0
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user = 0, val;

This assignment can be done in the actual case below.

Quoted 11 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 216

Quoted 6 lines
drivers/rtc/rtc-isl12022.c +6 -0
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)

I always feel uneasy with ' < 0' — Does positive error makes sense? Is it even possible? OTOH if the entire driver uses this idiom... oh well, let's make it consistent.

/**
* regmap_read() - Read a value from a single register
*
* @map: Register map to read from
* @reg: Register to be read from
* @val: Pointer to store read value
*
* A value of zero will be returned on success, a negative errno will
* be returned in error cases.
*/

Quoted 25 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 217

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+			return ret;
		user = 0;

The rationale to avoid potential mistakes in the future in case this function will be expanded and user will be re-used.

Quoted 12 lines
drivers/rtc/rtc-isl12022.c +12 -0
+		if (val & ISL12022_SR_LBAT85)
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (val & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#42 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 2 lines · Alexandre Belloni
Quoted 1 line · Andy Shevchenko

On Tue, Jun 13, 2023 at 03:00:07PM +0200, Rasmus Villemoes wrote:

...

Quoted 7 lines
Quoted 6 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 216

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)

I always feel uneasy with ' < 0' — Does positive error makes sense? Is it even possible? OTOH if the entire driver uses this idiom... oh well, let's make it consistent.

Quoted 10 lines

/**
* regmap_read() - Read a value from a single register
*
* @map: Register map to read from
* @reg: Register to be read from
* @val: Pointer to store read value
*
* A value of zero will be returned on success, a negative errno will
* be returned in error cases.
*/

I'm not sure what you meant by this. Yes, I know that there is no possibility that regmap API returns positive value. Do you mean that regmap API documentation is unclear?

Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+			return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#43 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 24 lines · Andy Shevchenko
Quoted 2 lines · Alexandre Belloni
Quoted 1 line · Andy Shevchenko

On Tue, Jun 13, 2023 at 03:00:07PM +0200, Rasmus Villemoes wrote:

...

Quoted 7 lines
Quoted 6 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 216

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)

I always feel uneasy with ' < 0' — Does positive error makes sense? Is it even possible? OTOH if the entire driver uses this idiom... oh well, let's make it consistent.

Quoted 10 lines

/**
* regmap_read() - Read a value from a single register
*
* @map: Register map to read from
* @reg: Register to be read from
* @val: Pointer to store read value
*
* A value of zero will be returned on success, a negative errno will
* be returned in error cases.
*/

Quoted 3 lines

I'm not sure what you meant by this. Yes, I know that there is no possibility that regmap API returns positive value. Do you mean that regmap API documentation is unclear?

No, I mean that you'd have to be clearer as to why you are uneasy with a test for a negative value when the function returns 0 for success and a negative value for an error. Else, this is pure bullying.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
drivers/rtc/rtc-isl12022.c +1 -0
+			return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#44 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 4 lines · Alexandre Belloni
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Alexandre Belloni
Quoted 1 line · Andy Shevchenko

On Tue, Jun 13, 2023 at 03:00:07PM +0200, Rasmus Villemoes wrote:

...

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines

drivers/rtc/rtc-isl12022.c static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm) · line 216

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret < 0)

I always feel uneasy with ' < 0' — Does positive error makes sense? Is it even possible? OTOH if the entire driver uses this idiom... oh well, let's make it consistent.

/**
* regmap_read() - Read a value from a single register
*
* @map: Register map to read from
* @reg: Register to be read from
* @val: Pointer to store read value
*
* A value of zero will be returned on success, a negative errno will
* be returned in error cases.
*/

Quoted 7 lines
Quoted 3 lines

I'm not sure what you meant by this. Yes, I know that there is no possibility that regmap API returns positive value. Do you mean that regmap API documentation is unclear?

No, I mean that you'd have to be clearer as to why you are uneasy with a test for a negative value when the function returns 0 for success and a negative value for an error. Else, this is pure bullying.

From the perspective of the code reader, a person, who might have not known all the implementation details of the calls this kind of check will always puzzle about positive value.

When reading such a code the following questions are arisen:
1) Can the positive return value be the case?
2) If so, what is the meaning of a such?
3) Why do we not care about it?

All this can simply gone if we use

	ret = foo(...);
	if (ret)
		return ret;

As it's clear that whatever is non-zero we accept as something to be promoted to the upper layer. I hope this explains my position.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+			return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#45 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 5 lines · Andy Shevchenko

When reading such a code the following questions are arisen:
1) Can the positive return value be the case?
2) If so, what is the meaning of a such?
3) Why do we not care about it?

Quoted 8 lines

All this can simply gone if we use

	ret = foo(...);
	if (ret)
		return ret;

As it's clear that whatever is non-zero we accept as something to be promoted to the upper layer. I hope this explains my position.

But we're in a context (in this case an ->ioctl method) where _our_ caller expects 0/-ESOMETHING, so returning something positive would be a bug - i.e., either way of spelling that if(), the reader must know that foo() also has those 0/-ESOMETHING semantics.

I honestly didn't think much about it, but looking at the existing code and the stuff I add, all other places actually do 'if (ret)', so I've updated this site for consistency.

Rasmus

#46 lore raw

Re: [PATCH v2 5/8] rtc: isl12022: implement RTC_VL_READ ioctl

Quoted 12 lines · Rasmus Villemoes
Quoted 11 lines · Andy Shevchenko

When reading such a code the following questions are arisen:
1) Can the positive return value be the case?
2) If so, what is the meaning of a such?
3) Why do we not care about it?

All this can simply gone if we use

	ret = foo(...);
	if (ret)
		return ret;
Quoted 7 lines
Quoted 2 lines

As it's clear that whatever is non-zero we accept as something to be promoted to the upper layer. I hope this explains my position.

But we're in a context (in this case an ->ioctl method) where _our_ caller expects 0/-ESOMETHING, so returning something positive would be a bug - i.e., either way of spelling that if(), the reader must know that foo() also has those 0/-ESOMETHING semantics.

I totally understand this. But then it's either problem of regmap API documentation or API itself. I.o.w. not _your_ problem.

Quoted 3 lines

I honestly didn't think much about it, but looking at the existing code and the stuff I add, all other places actually do 'if (ret)', so I've updated this site for consistency.

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#47 lore raw

PATCH v2 6/8 rtc: isl12022: trigger battery level detection during probe

In reply to Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot. Empirically, this does not immediately update the bits in the status register (i.e., an immediate read of SR after this write can still show stale values), but the update is done after a few milliseconds, so certainly before the RTC device gets registered and userspace has a chance of doing the ioctl() on this device.

---
 drivers/rtc/rtc-isl12022.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/rtc/rtc-isl12022.c +10 -0
index bf0d65643897..44603169e575 100644
@@ -268,6 +268,16 @@ static void isl12022_set_trip_levels(struct device *dev)
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
 	if (ret)
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+
+	/*
+	 * Force a write of the TSE bit in the BETA register, in order
+	 * to trigger an update of the LBAT75 and LBAT85 bits in the
+	 * status register. In battery backup mode, those bits have
+	 * another meaning, so without this, they may contain stale
+	 * values for up to a minute after power-on.
+	 */
+	regmap_write_bits(regmap, ISL12022_REG_BETA,
+			  ISL12022_BETA_TSE, ISL12022_BETA_TSE);
 }
 
 static int isl12022_probe(struct i2c_client *client)
Signature · 1 line
2.37.2
#48 lore raw

PATCH v2 7/8 dt-bindings: rtc: isl12022: add #clock-cells property

In reply to Rasmus Villemoes

The isl12022 has a dual-purpose irq/f_out pin, which can either be used as an interrupt or clock output.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml     | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +10 -0
index d5d3a687a34d..a9ef68b5fdcd 100644
@@ -11,6 +11,13 @@ maintainers:
 
 allOf:
   - $ref: rtc.yaml#
+  # If #clock-cells is present, interrupts must not be present
+  - if:
+      required:
+        - '#clock-cells'
+    then:
+      properties:
+        interrupts: false
 
 properties:
   compatible:
@@ -22,6 +29,9 @@ properties:
   interrupts:
     maxItems: 1
 
+  '#clock-cells':
+    const: 0
+
   isil,trip-level85-microvolt:
     description: |
       The battery voltage at which the first alarm should trigger
Signature · 1 line
2.37.2
#49 lore raw

Re: [PATCH v2 7/8] dt-bindings: rtc: isl12022: add #clock-cells property

Quoted 13 lines · Rasmus Villemoes

The isl12022 has a dual-purpose irq/f_out pin, which can either be used as an interrupt or clock output.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml     | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index d5d3a687a34d..a9ef68b5fdcd 100644

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml maintainers: · line 20

Quoted 10 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +7 -0
@@ -11,6 +11,13 @@ maintainers:
 
 allOf:
   - $ref: rtc.yaml#
+  # If #clock-cells is present, interrupts must not be present
+  - if:
+      required:
+        - '#clock-cells'
+    then:
+      properties:
+        interrupts: false

Entire allOf block should be like in example-schema, so before unevaluatedProperties. Please put it in correct place in your first patch so here it does not have to be moved.

Best regards,
Krzysztof

#50 lore raw

Re: [PATCH v2 7/8] dt-bindings: rtc: isl12022: add #clock-cells property

Quoted 8 lines · Krzysztof Kozlowski
Quoted 6 lines · Rasmus Villemoes
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index d5d3a687a34d..a9ef68b5fdcd 100644
@@ -11,6 +11,13 @@ maintainers:
 
Quoted 14 lines

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml maintainers: · line 20

Quoted 9 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +7 -0
 allOf:
   - $ref: rtc.yaml#
+  # If #clock-cells is present, interrupts must not be present
+  - if:
+      required:
+        - '#clock-cells'
+    then:
+      properties:
+        interrupts: false

Entire allOf block should be like in example-schema, so before unevaluatedProperties. Please put it in correct place in your first patch so here it does not have to be moved.

OK. That first patch was basically a copy-paste of c690048ed59b, and e.g. ingenic,rtc.yaml has a similar non-trivial allOf block between maintainers and properties. Is there somehow I could have known it should be right before unevaluatedProperties?

Rasmus

#51 lore raw

Re: [PATCH v2 7/8] dt-bindings: rtc: isl12022: add #clock-cells property

Quoted 24 lines · Rasmus Villemoes
Quoted 8 lines · Krzysztof Kozlowski
Quoted 6 lines · Rasmus Villemoes
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +0 -0
index d5d3a687a34d..a9ef68b5fdcd 100644
@@ -11,6 +11,13 @@ maintainers:
 
Quoted 14 lines

Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml maintainers: · line 20

Quoted 9 lines
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +7 -0
 allOf:
   - $ref: rtc.yaml#
+  # If #clock-cells is present, interrupts must not be present
+  - if:
+      required:
+        - '#clock-cells'
+    then:
+      properties:
+        interrupts: false

Entire allOf block should be like in example-schema, so before unevaluatedProperties. Please put it in correct place in your first patch so here it does not have to be moved.

Quoted 4 lines

OK. That first patch was basically a copy-paste of c690048ed59b, and e.g. ingenic,rtc.yaml has a similar non-trivial allOf block between maintainers and properties. Is there somehow I could have known it should be right before unevaluatedProperties?

The trivial - with a $ref - we keep often at the top. But once it starts growing, should be at the bottom. Since you know it will grow, just put it at the bottom (not total bottom, but like in example-schema, so after required:).

Best regards,
Krzysztof

#52 lore raw

PATCH v2 8/8 rtc: isl12022: implement support for the #clock-cells DT property

In reply to Rasmus Villemoes

If device tree implies that the chip's IRQ/F_OUT pin is used as a clock, expose that in the driver. For now, pretend it is a fixed-rate (32kHz) clock; if other use cases appear the driver can be updated to provide its own clk_ops etc.

When the clock output is not used on a given board, one can prolong the battery life by ensuring that the FOx bits are 0. For the hardware I'm currently working on, the RTC draws 1.2uA with the FOx bits at their default 0001 value, dropping to 0.88uA when those bits are cleared.

---
 drivers/rtc/rtc-isl12022.c | 40 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
drivers/rtc/rtc-isl12022.c +40 -0
index 44603169e575..3e69f1a5dc12 100644
@@ -10,6 +10,7 @@
 
 #include <linux/bcd.h>
 #include <linux/bitfield.h>
+#include <linux/clk-provider.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/i2c.h>
@@ -44,6 +45,9 @@
 #define ISL12022_SR_LBAT75	(1 << 1)
 
 #define ISL12022_INT_WRTC	(1 << 6)
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1
 
 #define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
 #define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
@@ -241,6 +245,37 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static int isl12022_register_clock(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	struct clk_hw *hw;
+	int ret;
+
+	if (!device_property_present(dev, "#clock-cells")) {
+		/*
+		 * Disabling the F_OUT pin reduces the power
+		 * consumption in battery mode by ~25%.
+		 */
+		regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK,
+				   ISL12022_INT_FO_OFF);
+
+		return 0;
+	}
+
+	/*
+	 * For now, only support a fixed clock of 32768Hz (the reset default).
+	 */
+	ret = regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);
+	if (ret)
+		return ret;
+
+	hw = devm_clk_hw_register_fixed_rate(dev, "isl12022", NULL, 0, 32768);
+	if (IS_ERR(hw))
+		return PTR_ERR(hw);
+
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
+}
+
 static const u32 trip_level85[] = { 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 };
 static const u32 trip_level75[] = { 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 };
 
@@ -284,6 +319,7 @@ static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
 	struct regmap *regmap;
+	int ret;
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
 		return -ENODEV;
@@ -296,6 +332,10 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	ret = isl12022_register_clock(&client->dev);
+	if (ret)
+		return ret;
+
 	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
Signature · 1 line
2.37.2
#53 lore raw

Re: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property

Quoted 10 lines · Rasmus Villemoes

If device tree implies that the chip's IRQ/F_OUT pin is used as a clock, expose that in the driver. For now, pretend it is a fixed-rate (32kHz) clock; if other use cases appear the driver can be updated to provide its own clk_ops etc.

When the clock output is not used on a given board, one can prolong the battery life by ensuring that the FOx bits are 0. For the hardware I'm currently working on, the RTC draws 1.2uA with the FOx bits at their default 0001 value, dropping to 0.88uA when those bits are cleared.

...

drivers/rtc/rtc-isl12022.c line 50

Quoted 3 lines
drivers/rtc/rtc-isl12022.c +3 -0
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1

A nit-pick. Are they decimal or bit fields? To me seems like the 0x can be dropped.

...

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 268

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+	ret = regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);

Seems too long even for 100 limit.
Maybe:

	ret = regmap_update_bits(regmap, ISL12022_REG_INT,
				 ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);

?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#54 lore raw

Re: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property

Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Rasmus Villemoes

If device tree implies that the chip's IRQ/F_OUT pin is used as a clock, expose that in the driver. For now, pretend it is a fixed-rate (32kHz) clock; if other use cases appear the driver can be updated to provide its own clk_ops etc.

When the clock output is not used on a given board, one can prolong the battery life by ensuring that the FOx bits are 0. For the hardware I'm currently working on, the RTC draws 1.2uA with the FOx bits at their default 0001 value, dropping to 0.88uA when those bits are cleared.

Quoted 7 lines

...

drivers/rtc/rtc-isl12022.c line 50

Quoted 3 lines
drivers/rtc/rtc-isl12022.c +3 -0
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1

A nit-pick. Are they decimal or bit fields?

-ENOPARSE. A number is a number. Its representation in C code may be decimal or hexadecimal (or...). And sure, 0 and 0x0 are different spellings of the same thing. The data sheet lists the possible values in terms of individual bits, so I suppose I could even do 0b0000 and 0b0001, but that's too unusual (even if perfectly acceptable by gcc).

Quoted 1 line

To me seems like the 0x can be dropped.

Can, but won't, a single hex digit is more natural way to represent a four-bit field.

Quoted 7 lines

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 268

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+	ret = regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);

Seems too long even for 100 limit.
Maybe:

	ret = regmap_update_bits(regmap, ISL12022_REG_INT,
				 ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);

Sure.

Rasmus

#55 lore raw

Re: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property

Quoted 2 lines · Rasmus Villemoes
Quoted 1 line · Andy Shevchenko

On Tue, Jun 13, 2023 at 03:00:10PM +0200, Rasmus Villemoes wrote:

...

Quoted 11 lines
Quoted 5 lines

drivers/rtc/rtc-isl12022.c line 50

Quoted 3 lines
drivers/rtc/rtc-isl12022.c +3 -0
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1

A nit-pick. Are they decimal or bit fields?

-ENOPARSE. A number is a number. Its representation in C code may be decimal or hexadecimal (or...). And sure, 0 and 0x0 are different spellings of the same thing. The data sheet lists the possible values in terms of individual bits, so I suppose I could even do 0b0000 and 0b0001, but that's too unusual (even if perfectly acceptable by gcc).

What does datasheet define? bits or the value in a 4-bit field? If bits, why don't you put it that way

#define ISL12022_INT_FO_OFF 0
#define ISL12022_INT_FO_32K BIT(0)

?

It's a nit-pick, of course, but the nuance is that proposed form might give a hint to the reader, current -- not.

Quoted 4 lines
Quoted 1 line

To me seems like the 0x can be dropped.

Can, but won't, a single hex digit is more natural way to represent a four-bit field.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#56 lore raw

Re: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property

In reply to Rasmus Villemoes
kernel test robot noticed the following build errors:

Hi Rasmus,

kernel test robot noticed the following build errors:

[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on robh/for-next linus/master v6.4-rc6 next-20230613] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/rtc-isl12022-remove-wrong-warning-for-low-battery-level/20230613-210308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
patch link:    https://lore.kernel.org/r/20230613130011.305589-9-linux%40rasmusvillemoes.dk
patch subject: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property
config: hexagon-randconfig-r041-20230612 (https://download.01.org/0day-ci/archive/20230614/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)

reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
git remote add abelloni https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git
git fetch abelloni rtc-next
git checkout abelloni/rtc-next
b4 shazam https://lore.kernel.org/r/[email protected]
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang ~/bin/make.cross W=1 O=build_dir ARCH=hexagon olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang ~/bin/make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: __clk_hw_register_fixed_rate
   >>> referenced by rtc-isl12022.c:272 (drivers/rtc/rtc-isl12022.c:272)
   >>>               drivers/rtc/rtc-isl12022.o:(isl12022_probe) in archive vmlinux.a
   >>> referenced by rtc-isl12022.c:272 (drivers/rtc/rtc-isl12022.c:272)
   >>>               drivers/rtc/rtc-isl12022.o:(isl12022_probe) in archive vmlinux.a
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#57 lore raw

Re: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property

In reply to Rasmus Villemoes
kernel test robot noticed the following build errors:

Hi Rasmus,

kernel test robot noticed the following build errors:

[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on robh/for-next linus/master v6.4-rc6 next-20230613] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/rtc-isl12022-remove-wrong-warning-for-low-battery-level/20230613-210308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
patch link:    https://lore.kernel.org/r/20230613130011.305589-9-linux%40rasmusvillemoes.dk
patch subject: [PATCH v2 8/8] rtc: isl12022: implement support for the #clock-cells DT property
config: i386-randconfig-i012-20230612 (https://download.01.org/0day-ci/archive/20230614/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

reproduce (this is a W=1 build):
git remote add abelloni https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git
git fetch abelloni rtc-next
git checkout abelloni/rtc-next
b4 shazam https://lore.kernel.org/r/[email protected]
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "__clk_hw_register_fixed_rate" [drivers/rtc/rtc-isl12022.ko] undefined!
>> ERROR: modpost: "of_clk_hw_simple_get" [drivers/rtc/rtc-isl12022.ko] undefined!
>> ERROR: modpost: "devm_of_clk_add_hw_provider" [drivers/rtc/rtc-isl12022.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#58 lore raw
In reply to Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

v2 changes:

A nit-pick regarding to the process. You used In-reply-to email header and this a bit inconvenient if you operate with a threads in MUA, for example, I would like to delete old thread, but in this case it automatically marks v2 for deletion (I'm using classical mutt).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#59 lore raw
In reply to Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

v2 changes:

Do not attach (thread) your patchsets to some other threads (unrelated or older versions). This buries them deep in the mailbox and might interfere with applying entire sets.

Best regards,
Krzysztof

#60 lore raw
Quoted 11 lines · Krzysztof Kozlowski
Quoted 10 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

Quoted 6 lines
Quoted 1 line

v2 changes:

Do not attach (thread) your patchsets to some other threads (unrelated or older versions). This buries them deep in the mailbox and might interfere with applying entire sets.

Arrgh, I really didn't mean to do that with v3, but I reused the 'git send-email' from my shell history and overlooked that I had that --in-reply-to :(

Sorry folks!

Rasmus

#61 lore raw

v3 cover rtc: isl12022: battery backup voltage and clock support

In reply to Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

v3 changes:

Patch 2: move the allOf block further down, add R-b [Krzysztof]

Patch 3: change to a single property with two values [Krzysztof]

Patch 4: adjust implementation accordingly

Patch 5: move initialization of 'user' variable inside switch case, use 'if (ret)' instead of 'if (ret < 0)' for consistency within the driver [Andy]

Patch 7: semantically identical to v2, just context changes due to changes in 2/8 and 3/8

Patch 8: only do the clock registration when CONFIG_COMMON_CLK [kernel test robot]

v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Rasmus Villemoes (8):
rtc: isl12022: remove wrong warning for low battery level
dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own
schema file
dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels
rtc: isl12022: add support for trip level DT binding
rtc: isl12022: implement RTC_VL_READ ioctl
rtc: isl12022: trigger battery level detection during probe
dt-bindings: rtc: isl12022: add #clock-cells property
rtc: isl12022: implement support for the #clock-cells DT property

 .../bindings/rtc/intersil,isl12022.yaml       |  64 +++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |   2 -
 drivers/rtc/rtc-isl12022.c                    | 126 +++++++++++++++++-
 3 files changed, 184 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Signature · 1 line
2.37.2
#62 lore raw

PATCH v3 1/8 rtc: isl12022: remove wrong warning for low battery level

There are multiple problems with this warning.

First of all, it triggers way too often, in fact nearly on every boot, because the SR_LBAT85/SR_LBAT75 bits have another meaning when in battery backup mode. Quoting from the data sheet:

LOW BATTERY INDICATOR 85% BIT (LBAT85)

In Normal Mode (VDD), this bit indicates when the battery level has dropped below the pre-selected trip levels. [...] The LBAT85 detection happens automatically once every minute when seconds register reaches 59.

In Battery Mode (VBAT), this bit indicates the device has entered into battery mode by polling once every 10 minutes. The LBAT85 detection happens automatically once when the minute register reaches x9h or x0h minutes.

Similar wording applies to the LBAT75 bit.

This means that if the device is powered off for more than 10 minutes, the LBAT85 bit is guaranteed to be set. Upon power-on, unless we're close enough to the end of a minute and/or the boot is slow enough that the second register passes 59, the LBAT85 bit is still set when the kernel (or early userspace) reads the RTC to set the system's wallclock time.

Another minor problem is with the bit logic. If the 75% level is reached, logically we're also below 85%, so both bits would most likely be set. So even if the battery is below 75%, the warning would still say "voltage dropped below 85%".

A third problem is that the driver and current DT binding offer no way to indicate the nominal battery level and/or settings of the Battery Level Monitor Trip Bits. Since the default value of the VB85TP[2:0] and VB75TP[2:0] bits are 000, this means the actual setting of the LBAT85/LBAT75 bits in VDD mode doesn't happen until the battery is below 2.125V/1.875V, which for a standard 3V battery is way too late.

A fourth problem is emitting this warning from ->read_time:
util-linux' hwclock will, in the absence of support for getting an interrupt when the seconds counter is updated, issue ioctl(RTC_RD_TIME) in a busy-loop until it sees a change in the seconds field. In that case, if the battery low bits are set (either genuinely, more than a minute after boot, due to the battery actually being low, or as above, bogusly shortly after boot), the kernel log is swamped with hundreds of identical warnings.

Subsequent patches will add such bindings and driver support, and also proper support for RTC_VL_READ. For now, remove the broken warning.

---
 drivers/rtc/rtc-isl12022.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/rtc/rtc-isl12022.c +0 -6
index e68a79b5e00e..ebd66b835cef 100644
@@ -141,12 +141,6 @@ static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	if (ret)
 		return ret;
 
-	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
-		dev_warn(dev,
-			 "voltage dropped below %u%%, date and time is not reliable.\n",
-			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
-	}
-
 	dev_dbg(dev,
 		"raw data is sec=%02x, min=%02x, hr=%02x, mday=%02x, mon=%02x, year=%02x, wday=%02x, sr=%02x, int=%02x",
 		buf[ISL12022_REG_SC],
Signature · 1 line
2.37.2
#63 lore raw

PATCH v3 2/8 dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file

In reply to Rasmus Villemoes

Move the isil,isl12022 RTC bindings from trivial-rtc.yaml into its own intersil,isl12022.yaml file, in preparation for adding more bindings.

---
 .../bindings/rtc/intersil,isl12022.yaml       | 45 +++++++++++++++++++
 .../devicetree/bindings/rtc/trivial-rtc.yaml  |  2 -
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml new file +45 -0
new file mode 100644
index 000000000000..054d3fc649ba
@@ -0,0 +1,45 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/intersil,isl12022.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Intersil ISL12022 Real-time Clock
+
+maintainers:
+  - Alexandre Belloni <[email protected]>
+
+properties:
+  compatible:
+    const: isil,isl12022
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+allOf:
+  - $ref: rtc.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        rtc@6f {
+            compatible = "isil,isl12022";
+            reg = <0x6f>;
+            interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
+
+...
Documentation/devicetree/bindings/rtc/trivial-rtc.yaml +0 -2
index a3603e638c37..b062c64266a6 100644
@@ -45,8 +45,6 @@ properties:
       - isil,isl1208
       # Intersil ISL1218 Low Power RTC with Battery Backed SRAM
       - isil,isl1218
-      # Intersil ISL12022 Real-time Clock
-      - isil,isl12022
       # Loongson-2K Socs/LS7A bridge Real-time Clock
       - loongson,ls2x-rtc
       # Real Time Clock Module with I2C-Bus
Signature · 1 line
2.37.2
#64 lore raw

PATCH v3 3/8 dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

In reply to Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

There are total of 7 levels that could be selected for the first alarm. Any of the of levels could be selected as the first alarm with no reference as to nominal Battery voltage level.

Hence this provides the hardware designer the ability to choose values based on the discharge characteristics of the battery chosen for the given product, rather than just having one battery-microvolt property and having the driver choose levels close to 0.85/0.75 times that.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml       | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +9 -0
index 054d3fc649ba..3c6c07aaa78f 100644
@@ -19,6 +19,14 @@ properties:
   interrupts:
     maxItems: 1
 
+  isil,battery-trip-levels-microvolt:
+    description:
+      The battery voltages at which the first alarm and second alarm
+      should trigger (normally ~85% and ~75% of nominal V_BAT).
+    items:
+      - enum: [2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000]
+      - enum: [1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000]
+
 required:
   - compatible
   - reg
@@ -39,6 +47,7 @@ examples:
             compatible = "isil,isl12022";
             reg = <0x6f>;
             interrupts-extended = <&gpio1 5 IRQ_TYPE_LEVEL_LOW>;
+            isil,battery-trip-levels-microvolt = <2550000>, <2250000>;
         };
     };
 
Signature · 1 line
2.37.2
Reviewed-by on v3 3/8
#65 lore raw

Re: [PATCH v3 3/8] dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels

Quoted 10 lines · Rasmus Villemoes

The isl12022 has a built-in support for monitoring the voltage of the backup battery, and setting bits in the status register when that voltage drops below two predetermined levels (usually 85% and 75% of the nominal voltage). However, since it can operate at wide range of battery voltages (2.5V - 5.5V), one must configure those trip levels according to which battery is used on a given board.

Add bindings for defining these two trip levels. While the register and bit names suggest that they should correspond to 85% and 75% of the nominal battery voltage, the data sheet also says

Best regards,
Krzysztof

#66 lore raw

PATCH v3 4/8 rtc: isl12022: add support for trip level DT binding

In reply to Rasmus Villemoes

Implement support for using the values given in the isil,battery-trip-levels-microvolt property to set appropriate values in the VB85TP/VB75TP bits in the PWR_VBAT register.

---
 drivers/rtc/rtc-isl12022.c | 39 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/rtc/rtc-isl12022.c +39 -0
index ebd66b835cef..6a757f0a4736 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bcd.h>
+#include <linux/bitfield.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/i2c.h>
@@ -31,6 +32,8 @@
 #define ISL12022_REG_SR		0x07
 #define ISL12022_REG_INT	0x08
 
+#define ISL12022_REG_PWR_VBAT	0x0a
+
 #define ISL12022_REG_BETA	0x0d
 #define ISL12022_REG_TEMP_L	0x28
 
@@ -42,6 +45,9 @@
 
 #define ISL12022_INT_WRTC	(1 << 6)
 
+#define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
+#define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
+
 #define ISL12022_BETA_TSE	(1 << 7)
 
 static umode_t isl12022_hwmon_is_visible(const void *data,
@@ -209,6 +215,38 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static const u32 trip_levels[2][7] = {
+	{ 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 },
+	{ 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 },
+};
+
+static void isl12022_set_trip_levels(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 levels[2] = {0, 0};
+	int ret, i, j, x[2];
+	u8 val, mask;
+
+	device_property_read_u32_array(dev, "isil,battery-trip-levels-microvolt",
+				       levels, 2);
+
+	for (i = 0; i < 2; i++) {
+		for (j = 0; j < ARRAY_SIZE(trip_levels[i]) - 1; j++) {
+			if (levels[i] <= trip_levels[i][j])
+				break;
+		}
+		x[i] = j;
+	}
+
+	val = FIELD_PREP(ISL12022_REG_VB85_MASK, x[0]) |
+		FIELD_PREP(ISL12022_REG_VB75_MASK, x[1]);
+	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
+
+	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
+	if (ret)
+		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+}
+
 static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
@@ -225,6 +263,7 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
 	rtc = devm_rtc_allocate_device(&client->dev);
Signature · 1 line
2.37.2
#67 lore raw

Re: [PATCH v3 4/8] rtc: isl12022: add support for trip level DT binding

Quoted 3 lines · Rasmus Villemoes

Implement support for using the values given in the isil,battery-trip-levels-microvolt property to set appropriate values in the VB85TP/VB75TP bits in the PWR_VBAT register.

A few nit-picks below.

...

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 226

Quoted 4 lines
drivers/rtc/rtc-isl12022.c +4 -0
+static void isl12022_set_trip_levels(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 levels[2] = {0, 0};

A nit, 0, 0 is not needed, {} will do the job.

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 228

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+	int ret, i, j, x[2];
+	u8 val, mask;

BUILD_BUG_ON(ARRAY_SIZE(x) != ARRAY_SIZE(levels)) ?

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 231

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+	device_property_read_u32_array(dev, "isil,battery-trip-levels-microvolt",
+				       levels, 2);

A nit, ARRAY_SIZE(levels) ?

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 233

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+	for (i = 0; i < 2; i++) {

ARRAY_SIZE(x) ?

Quoted 15 lines
drivers/rtc/rtc-isl12022.c +15 -0
+		for (j = 0; j < ARRAY_SIZE(trip_levels[i]) - 1; j++) {
+			if (levels[i] <= trip_levels[i][j])
+				break;
+		}
+		x[i] = j;
+	}
+
+	val = FIELD_PREP(ISL12022_REG_VB85_MASK, x[0]) |
+		FIELD_PREP(ISL12022_REG_VB75_MASK, x[1]);
+	mask = ISL12022_REG_VB85_MASK | ISL12022_REG_VB75_MASK;
+
+	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
+	if (ret)
+		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#68 lore raw

Re: [PATCH v3 4/8] rtc: isl12022: add support for trip level DT binding

Quoted 8 lines · Andy Shevchenko

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 226

Quoted 4 lines · Rasmus Villemoes
drivers/rtc/rtc-isl12022.c +4 -0
+static void isl12022_set_trip_levels(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 levels[2] = {0, 0};

A nit, 0, 0 is not needed, {} will do the job.

So? I'm not code-golfing, and here I really want to initialize to 0 (or any value lower than the first entries in the trip_levels[] arrays so that, lacking the DT property, the code ends up using what are the chip reset defaults).

Quoted 4 lines

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 228

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+	int ret, i, j, x[2];
+	u8 val, mask;

BUILD_BUG_ON(ARRAY_SIZE(x) != ARRAY_SIZE(levels)) ?

BUILD_BUG_ON doesn't make sense when these things are declared within a few lines of each other _and_ since they're sized based on properties of the hardware we're dealing with, nobody would ever have a reason to change either. So no, that would IMO make it harder to read, because one would stop and think "why is this obvious thing asserted?".

Quoted 8 lines

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 231

Quoted 2 lines
drivers/rtc/rtc-isl12022.c +2 -0
+	device_property_read_u32_array(dev, "isil,battery-trip-levels-microvolt",
+				       levels, 2);

A nit, ARRAY_SIZE(levels) ?

drivers/rtc/rtc-isl12022.c static const struct regmap_config regmap_config = { · line 233

Quoted 1 line
drivers/rtc/rtc-isl12022.c +1 -0
+	for (i = 0; i < 2; i++) {

ARRAY_SIZE(x) ?

I considered that, but really didn't think it improved readability. I'll defer to Alexandre on whether to change this.

Rasmus

#69 lore raw

PATCH v3 5/8 rtc: isl12022: implement RTC_VL_READ ioctl

In reply to Rasmus Villemoes

Hook up support for reading the values of the SR_LBAT85 and SR_LBAT75 bits. Translate the former to "battery low", and the latter to "battery empty or not-present".

---
 drivers/rtc/rtc-isl12022.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/rtc/rtc-isl12022.c +27 -0
index 6a757f0a4736..a48456abdcb9 100644
@@ -204,7 +204,34 @@ static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return regmap_bulk_write(regmap, ISL12022_REG_SC, buf, sizeof(buf));
 }
 
+static int isl12022_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	u32 user, val;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(regmap, ISL12022_REG_SR, &val);
+		if (ret)
+			return ret;
+
+		user = 0;
+		if (val & ISL12022_SR_LBAT85)
+			user |= RTC_VL_BACKUP_LOW;
+
+		if (val & ISL12022_SR_LBAT75)
+			user |= RTC_VL_BACKUP_EMPTY;
+
+		return put_user(user, (u32 __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops isl12022_rtc_ops = {
+	.ioctl		= isl12022_rtc_ioctl,
 	.read_time	= isl12022_rtc_read_time,
 	.set_time	= isl12022_rtc_set_time,
 };
Signature · 1 line
2.37.2
#70 lore raw

PATCH v3 6/8 rtc: isl12022: trigger battery level detection during probe

In reply to Rasmus Villemoes

Since the meaning of the SR_LBAT85 and SR_LBAT75 bits are different in battery backup mode, they may very well be set after power on, and stay set for up to a minute (i.e. until the battery detection in VDD mode happens when the seconds counter hits 59). This would mean that userspace doing a ioctl(RTC_VL_READ) early on could get a false positive.

The battery level detection can also be triggered by explicitly writing a 1 to the TSE bit in the BETA register. Do that once during boot. Empirically, this does not immediately update the bits in the status register (i.e., an immediate read of SR after this write can still show stale values), but the update is done after a few milliseconds, so certainly before the RTC device gets registered and userspace has a chance of doing the ioctl() on this device.

---
 drivers/rtc/rtc-isl12022.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/rtc/rtc-isl12022.c +10 -0
index a48456abdcb9..916879b0388c 100644
@@ -272,6 +272,16 @@ static void isl12022_set_trip_levels(struct device *dev)
 	ret = regmap_update_bits(regmap, ISL12022_REG_PWR_VBAT, mask, val);
 	if (ret)
 		dev_warn(dev, "unable to set battery alarm levels: %d\n", ret);
+
+	/*
+	 * Force a write of the TSE bit in the BETA register, in order
+	 * to trigger an update of the LBAT75 and LBAT85 bits in the
+	 * status register. In battery backup mode, those bits have
+	 * another meaning, so without this, they may contain stale
+	 * values for up to a minute after power-on.
+	 */
+	regmap_write_bits(regmap, ISL12022_REG_BETA,
+			  ISL12022_BETA_TSE, ISL12022_BETA_TSE);
 }
 
 static int isl12022_probe(struct i2c_client *client)
Signature · 1 line
2.37.2
#71 lore raw

PATCH v3 7/8 dt-bindings: rtc: isl12022: add #clock-cells property

In reply to Rasmus Villemoes

The isl12022 has a dual-purpose irq/f_out pin, which can either be used as an interrupt or clock output.

---
 .../devicetree/bindings/rtc/intersil,isl12022.yaml     | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/rtc/intersil,isl12022.yaml +10 -0
index 3c6c07aaa78f..c2d1441ef273 100644
@@ -19,6 +19,9 @@ properties:
   interrupts:
     maxItems: 1
 
+  '#clock-cells':
+    const: 0
+
   isil,battery-trip-levels-microvolt:
     description:
       The battery voltages at which the first alarm and second alarm
@@ -33,6 +36,13 @@ required:
 
 allOf:
   - $ref: rtc.yaml#
+  # If #clock-cells is present, interrupts must not be present
+  - if:
+      required:
+        - '#clock-cells'
+    then:
+      properties:
+        interrupts: false
 
 unevaluatedProperties: false
 
Signature · 1 line
2.37.2
#73 lore raw

PATCH v3 8/8 rtc: isl12022: implement support for the #clock-cells DT property

In reply to Rasmus Villemoes

If device tree implies that the chip's IRQ/F_OUT pin is used as a clock, expose that in the driver. For now, pretend it is a fixed-rate (32kHz) clock; if other use cases appear the driver can be updated to provide its own clk_ops etc.

When the clock output is not used on a given board, one can prolong the battery life by ensuring that the FOx bits are 0. For the hardware I'm currently working on, the RTC draws 1.2uA with the FOx bits at their default 0001 value, dropping to 0.88uA when those bits are cleared.

---
 drivers/rtc/rtc-isl12022.c | 44 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/rtc/rtc-isl12022.c +44 -0
index 916879b0388c..05f50ab0e69a 100644
@@ -10,6 +10,7 @@
 
 #include <linux/bcd.h>
 #include <linux/bitfield.h>
+#include <linux/clk-provider.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/i2c.h>
@@ -44,6 +45,9 @@
 #define ISL12022_SR_LBAT75	(1 << 1)
 
 #define ISL12022_INT_WRTC	(1 << 6)
+#define ISL12022_INT_FO_MASK	GENMASK(3, 0)
+#define ISL12022_INT_FO_OFF	0x0
+#define ISL12022_INT_FO_32K	0x1
 
 #define ISL12022_REG_VB85_MASK	GENMASK(5, 3)
 #define ISL12022_REG_VB75_MASK	GENMASK(2, 0)
@@ -242,6 +246,41 @@ static const struct regmap_config regmap_config = {
 	.use_single_write = true,
 };
 
+static int isl12022_register_clock(struct device *dev)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	struct clk_hw *hw;
+	int ret;
+
+	if (!device_property_present(dev, "#clock-cells")) {
+		/*
+		 * Disabling the F_OUT pin reduces the power
+		 * consumption in battery mode by ~25%.
+		 */
+		regmap_update_bits(regmap, ISL12022_REG_INT, ISL12022_INT_FO_MASK,
+				   ISL12022_INT_FO_OFF);
+
+		return 0;
+	}
+
+	if (!IS_ENABLED(CONFIG_COMMON_CLK))
+		return 0;
+
+	/*
+	 * For now, only support a fixed clock of 32768Hz (the reset default).
+	 */
+	ret = regmap_update_bits(regmap, ISL12022_REG_INT,
+				 ISL12022_INT_FO_MASK, ISL12022_INT_FO_32K);
+	if (ret)
+		return ret;
+
+	hw = devm_clk_hw_register_fixed_rate(dev, "isl12022", NULL, 0, 32768);
+	if (IS_ERR(hw))
+		return PTR_ERR(hw);
+
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
+}
+
 static const u32 trip_levels[2][7] = {
 	{ 2125000, 2295000, 2550000, 2805000, 3060000, 4250000, 4675000 },
 	{ 1875000, 2025000, 2250000, 2475000, 2700000, 3750000, 4125000 },
@@ -288,6 +327,7 @@ static int isl12022_probe(struct i2c_client *client)
 {
 	struct rtc_device *rtc;
 	struct regmap *regmap;
+	int ret;
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
 		return -ENODEV;
@@ -300,6 +340,10 @@ static int isl12022_probe(struct i2c_client *client)
 
 	dev_set_drvdata(&client->dev, regmap);
 
+	ret = isl12022_register_clock(&client->dev);
+	if (ret)
+		return ret;
+
 	isl12022_set_trip_levels(&client->dev);
 	isl12022_hwmon_register(&client->dev);
 
Signature · 1 line
2.37.2
#74 lore raw
In reply to Rasmus Villemoes
Quoted 9 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

Ping. Any chance these could be picked up so they make it for v6.6?

Rasmus

#75 lore raw
Quoted 5 lines · Rasmus Villemoes
Quoted 4 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

Quoted 7 lines
Quoted 5 lines

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

Ping. Any chance these could be picked up so they make it for v6.6?

Ping^2.

Rasmus

#76 lore raw
Quoted 12 lines · Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes
Quoted 9 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

Quoted 3 lines
Quoted 1 line

Ping. Any chance these could be picked up so they make it for v6.6?

Ping^2.

Ping^3.

Rasmus

maintainer Applied
#77 lore raw
In reply to Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes

The current handling of the low-battery bits in the status register is wrong. The first six patches fix that and implement proper support for RTC_VL_READ.

The last two patches allow describing the isl12022 as a clock provider, for now just as a fixed 32kHz clock. They are also tangentially related to the backup battery, in that when the isl12022 is not used as a clock source, one can save some power consumption in battery mode by setting the FOx bits to 0.

[...]

Applied, thanks!

[1/8] rtc: isl12022: remove wrong warning for low battery level

      commit: 4d6af37cafad69ff93f62db80d5a3daa9ac3223f
[2/8] dt-bindings: rtc: Move isil,isl12022 from trivial-rtc.yaml into own schema file
      commit: ffc005280a47030d16cbbf3105c75d3562dba5a8
[3/8] dt-bindings: rtc: isl12022: add bindings for battery alarm trip levels
      commit: 69b569c124ffa698de25d039018fe86313c46c84
[4/8] rtc: isl12022: add support for trip level DT binding
      commit: 2caeb566baabb65add7d99ca6d8bfd566fe91582
[5/8] rtc: isl12022: implement RTC_VL_READ ioctl
      commit: eccebd813874b748ac4e79a9fe4c7290117ad3be
[6/8] rtc: isl12022: trigger battery level detection during probe
      commit: a11b6c460620f7fb5fae4c3aee5a5ba2e1e1129b
[7/8] dt-bindings: rtc: isl12022: add #clock-cells property
      commit: ab246c897be0bdf981f776399ca62b5ec4b8138f
[8/8] rtc: isl12022: implement support for the #clock-cells DT property
      commit: d57d12db774820819d0e591548a56b5cfc95f82a

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com