1 From 9d7eb234ac7a56b88aea8a52ed81553a730fe25c Mon Sep 17 00:00:00 2001
2 From: Marek Vasut <marex@denx.de>
3 Date: Fri, 5 Jul 2024 08:48:52 +0100
4 Subject: [PATCH] nvmem: core: Implement force_ro sysfs attribute
6 Implement "force_ro" sysfs attribute to allow users to set read-write
7 devices as read-only and back to read-write from userspace. The choice
8 of the name is based on MMC core 'force_ro' attribute.
10 This solves a situation where an AT24 I2C EEPROM with GPIO based nWP
11 signal may have to be occasionally updated. Such I2C EEPROM device is
12 usually set as read-only during most of the regular system operation,
13 but in case it has to be updated in a controlled manner, it could be
14 unlocked using this new "force_ro" sysfs attribute and then re-locked
17 The "read-only" DT property and config->read_only configuration is
18 respected and is used to set default state of the device, read-only
19 or read-write, for devices which do implement .reg_write function.
20 For devices which do not implement .reg_write function, the device
21 is unconditionally read-only and the "force_ro" attribute is not
24 Signed-off-by: Marek Vasut <marex@denx.de>
25 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
26 Link: https://lore.kernel.org/r/20240705074852.423202-16-srinivas.kandagatla@linaro.org
27 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
29 Documentation/ABI/stable/sysfs-bus-nvmem | 17 ++++++++++
30 drivers/nvmem/core.c | 43 ++++++++++++++++++++++++
31 2 files changed, 60 insertions(+)
33 --- a/Documentation/ABI/stable/sysfs-bus-nvmem
34 +++ b/Documentation/ABI/stable/sysfs-bus-nvmem
36 +What: /sys/bus/nvmem/devices/.../force_ro
39 +Contact: Marek Vasut <marex@denx.de>
41 + This read/write attribute allows users to set read-write
42 + devices as read-only and back to read-write from userspace.
43 + This can be used to unlock and relock write-protection of
44 + devices which are generally locked, except during sporadic
45 + programming operation.
46 + Read returns '0' or '1' for read-write or read-only modes
48 + Write parses one of 'YyTt1NnFf0', or [oO][NnFf] for "on"
49 + and "off", i.e. what kstrbool() supports.
50 + Note: This file is only present if CONFIG_NVMEM_SYSFS
53 What: /sys/bus/nvmem/devices/.../nvmem
56 --- a/drivers/nvmem/core.c
57 +++ b/drivers/nvmem/core.c
58 @@ -184,7 +184,30 @@ static ssize_t type_show(struct device *
60 static DEVICE_ATTR_RO(type);
62 +static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
65 + struct nvmem_device *nvmem = to_nvmem_device(dev);
67 + return sysfs_emit(buf, "%d\n", nvmem->read_only);
70 +static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
71 + const char *buf, size_t count)
73 + struct nvmem_device *nvmem = to_nvmem_device(dev);
74 + int ret = kstrtobool(buf, &nvmem->read_only);
82 +static DEVICE_ATTR_RW(force_ro);
84 static struct attribute *nvmem_attrs[] = {
85 + &dev_attr_force_ro.attr,
89 @@ -285,6 +308,25 @@ static umode_t nvmem_bin_attr_is_visible
90 return nvmem_bin_attr_get_umode(nvmem);
93 +static umode_t nvmem_attr_is_visible(struct kobject *kobj,
94 + struct attribute *attr, int i)
96 + struct device *dev = kobj_to_dev(kobj);
97 + struct nvmem_device *nvmem = to_nvmem_device(dev);
100 + * If the device has no .reg_write operation, do not allow
101 + * configuration as read-write.
102 + * If the device is set as read-only by configuration, it
103 + * can be forced into read-write mode using the 'force_ro'
106 + if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
107 + return 0; /* Attribute not visible */
112 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
113 const char *id, int index);
115 @@ -341,6 +383,7 @@ static const struct attribute_group nvme
116 .bin_attrs = nvmem_bin_attributes,
117 .attrs = nvmem_attrs,
118 .is_bin_visible = nvmem_bin_attr_is_visible,
119 + .is_visible = nvmem_attr_is_visible,
122 static const struct attribute_group *nvmem_dev_groups[] = {