public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvmem: core: eeprom: at24: Handle EEPROM with both read-only and wp-gpios
@ 2026-04-21 14:06 Marek Vasut
  0 siblings, 0 replies; only message in thread
From: Marek Vasut @ 2026-04-21 14:06 UTC (permalink / raw)
  To: linux-i2c
  Cc: Marek Vasut, Arnd Bergmann, Bartosz Golaszewski,
	Greg Kroah-Hartman, Srinivas Kandagatla, linux-kernel

Handle special-case of AT24 EEPROM described in DT, which contains both
"read-only" and "wp-gpios" properties. Interpret this configuration as
default read-only, but with the possibility of unlock via force_ro sysfs
attribute.

For this to work, the nvmem core must keep the "nvmem" sysfs bin attr as
read-write, any writes into the bin attr which occur while the device is
read-only are blocked in bin_attr_nvmem_write(). The AT24 EEPROM driver
must not mark the EEPROM as read-only if both "read-only" and "wp-gpios"
DT properties are present.

The updated behavior can be tested as follows:

Current content:
"
$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  66 6f 6f 0a ff ff ff ff
"

Write into default-read-only device:
"
$ echo bar > /sys/bus/nvmem/devices/logging7/nvmem
bash: echo: write error: Operation not permitted
$ cat /sys/bus/nvmem/devices/logging7/force_ro
1
"

Unlock and write into device:
"
$ echo 0 > /sys/bus/nvmem/devices/logging7/force_ro
$ cat /sys/bus/nvmem/devices/logging7/force_ro
0

$ echo bar > /sys/bus/nvmem/devices/logging7/nvmem
$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  62 61 72 0a ff ff ff ff
"

Relock and write into device, fails because device is read-only again:
"
$ echo 1 > /sys/bus/nvmem/devices/logging7/force_ro
$ echo baz > /sys/bus/nvmem/devices/logging7/nvmem
bash: echo: write error: Operation not permitted

$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  62 61 72 0a ff ff ff ff
"

Signed-off-by: Marek Vasut <marex@nabladev.com>
---
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Bartosz Golaszewski <brgl@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Srinivas Kandagatla <srini@kernel.org>
Cc: linux-i2c@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/misc/eeprom/at24.c | 3 ++-
 drivers/nvmem/core.c       | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index 0200288d3a7ab..9ef00b0ce83d2 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -627,7 +627,8 @@ static int at24_probe(struct i2c_client *client)
 		page_size = 1;
 
 	flags = cdata->flags;
-	if (device_property_present(dev, "read-only"))
+	if (device_property_present(dev, "read-only") &&
+	    !device_property_present(dev, "wp-gpios"))
 		flags |= AT24_FLAG_READONLY;
 	if (device_property_present(dev, "no-read-rollover"))
 		flags |= AT24_FLAG_NO_RDROL;
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 311cb2e5a5c02..c0eedfe8d706c 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -282,6 +282,9 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 	if (!nvmem->root_only)
 		mode |= 0044;
 
+	if (nvmem->read_only && nvmem->reg_write)
+		mode |= 0200;
+
 	if (!nvmem->read_only)
 		mode |= 0200;
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-21 14:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-21 14:06 [PATCH] nvmem: core: eeprom: at24: Handle EEPROM with both read-only and wp-gpios Marek Vasut

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox