From nobody Sun Feb 8 15:30:59 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 92A2B132461 for ; Thu, 15 Feb 2024 21:14:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708031657; cv=none; b=VccMJpe60yCfpkpNEphBxxGSishxt2gLF16YN4I3RH+A+RXPND1A0hr5saV56C6bEmALCIO3zy9T6E34hxkG0BADS14bo2WHdWrJfL3T/tYAClh7ME9q7ykzJw90/D0JqmXUd2AxYiHqIWTcHEaHOSM+iVthaK3CmVWsdTlbbQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708031657; c=relaxed/simple; bh=XVy3hOobm6zUM/uUVP4IVcxxMoT0bKOg5SLpDVCDiZE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=X/EJwnXo2dZAO9cUSx+TK22RYj5DS85FjpzBC6r1os6huJv7wllWXUYmk8dnvceWAoaff8Jyb3PBvLIBSZtno2DdOFe5QqbHm+hfAM/J9GufmTO3hUBFO1XlIZ7T42wXORIRu84tupwxzRSRTXh/aB0lyRs0E51D38CRUVSIxTE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1raj3W-0001Cg-Ff; Thu, 15 Feb 2024 22:14:06 +0100 From: Marco Felsch To: srinivas.kandagatla@linaro.org, gregkh@linuxfoundation.org, miquel.raynal@bootlin.com, michael@walle.cc, rafal@milecki.pl Cc: linux-kernel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH] nvmem: core: add sysfs cell write support Date: Thu, 15 Feb 2024 22:14:01 +0100 Message-Id: <20240215211401.1201004-1-m.felsch@pengutronix.de> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: m.felsch@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the sysfs cell write support to make it possible to write to exposed cells from sysfs as well e.g. for device provisioning. Signed-off-by: Marco Felsch --- Hi, the purpose of this patch is to make the NVMEM cells exposed via the testing ABI sysfs writeable, to allow changes during devie life-time. Regards, Marco drivers/nvmem/core.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 980123fb4dde..5a497188cfea 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -336,6 +336,40 @@ static ssize_t nvmem_cell_attr_read(struct file *filp,= struct kobject *kobj, return read_len; } =20 +static ssize_t nvmem_cell_attr_write(struct file *filp, struct kobject *ko= bj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + struct nvmem_cell_entry *entry; + struct nvmem_cell *cell; + int ret; + + entry =3D attr->private; + + if (!entry->nvmem->reg_write) + return -EPERM; + + if (pos >=3D entry->bytes) + return -EFBIG; + + if (pos + count > entry->bytes) + count =3D entry->bytes - pos; + + cell =3D nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + if (!cell) + return -EINVAL; + + ret =3D nvmem_cell_write(cell, buf, count); + + kfree_const(cell->id); + kfree(cell); + + return ret; +} + /* default read/write permissions */ static struct bin_attribute bin_attr_rw_nvmem =3D { .attr =3D { @@ -432,6 +466,7 @@ static int nvmem_populate_sysfs_cells(struct nvmem_devi= ce *nvmem) struct bin_attribute **cells_attrs, *attrs; struct nvmem_cell_entry *entry; unsigned int ncells =3D 0, i =3D 0; + umode_t mode; int ret =3D 0; =20 mutex_lock(&nvmem_mutex); @@ -456,15 +491,18 @@ static int nvmem_populate_sysfs_cells(struct nvmem_de= vice *nvmem) goto unlock_mutex; } =20 + mode =3D nvmem_bin_attr_get_umode(nvmem); + /* Initialize each attribute to take the name and size of the cell */ list_for_each_entry(entry, &nvmem->cells, node) { sysfs_bin_attr_init(&attrs[i]); attrs[i].attr.name =3D devm_kasprintf(&nvmem->dev, GFP_KERNEL, "%s@%x", entry->name, entry->offset); - attrs[i].attr.mode =3D 0444; + attrs[i].attr.mode =3D mode; attrs[i].size =3D entry->bytes; attrs[i].read =3D &nvmem_cell_attr_read; + attrs[i].write =3D &nvmem_cell_attr_write; attrs[i].private =3D entry; if (!attrs[i].attr.name) { ret =3D -ENOMEM; --=20 2.39.2