Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5544471ybl; Tue, 10 Dec 2019 07:43:31 -0800 (PST) X-Google-Smtp-Source: APXvYqzTTS5hNenFk47WpPv4G7yZWOPWlEoFtF4bN/2I1GPvvCPDd4gDQ6JICk+QTtp8XsksFQGf X-Received: by 2002:a9d:6395:: with SMTP id w21mr26294352otk.3.1575992611043; Tue, 10 Dec 2019 07:43:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575992611; cv=none; d=google.com; s=arc-20160816; b=uAAHne6l1M89mkrPSSjZAF/2KcClL//thNlxvzW4LVu2sT4P9zoHeYryw7O3cpcT73 E7ZVDZh1PNq2jM192Bx6PaR7EMJpFqU4Aj9RXFMo+hOAoqLqj1gGNVgGKtu0tQGRDDDl 3OluYdeO1oHjUrfAUjhPldvMkYSqhamiTUJVr8dpN0EcbhUWk/1HZMVUc+RslkmBf8xd s+1aAnzJP0IaoxwQurcuyrpFqysNfRY+lZllpAHWfh23R6OD5gQqUwt8ZHIqzsJd1TEt za2g+NI/gHiLIUB2czyfrHR8obVME9dVg7i1ECruxDK2j0XCMSAXSQiyZwaBuEIeePDq hNyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=w4D6ZqamJW9QSroTdCyeCwKsa+r24vCro2cOz6VJshk=; b=FvljyOZYeUnsyy0iDUBzFm9KQymNkN1f0yojn8zGond8h4Cz1NFO95aitzQK3Abx24 uCqsHFHPEKq11Oi4TJTntnZR89n4VBm6UnsVN+A2WYPQeXi+BIDQoC9V2dV6GqgOCJf8 gXhBbp2fttOE2SZ4u6qvD3Lx5wy+w6/R43ySqZ+E/CDVe9F2gNLZbXRz8WKZIOCYpbT7 8DbyaI7/dOo5w6jAhAI+5nsgzt+3E2DHkZhfC4k814h3fIbLe+tbnZ4JomqyTBdVlR9G lXfhMx+66o2LxO4N0incN1JDJOxEcOnCcQL2FvePJin/KXyMFui+9OFAj1HKuZgSijGp 4qaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=XDJgVY9N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m5si1936107ote.187.2019.12.10.07.43.18; Tue, 10 Dec 2019 07:43:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=XDJgVY9N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727606AbfLJPmc (ORCPT + 99 others); Tue, 10 Dec 2019 10:42:32 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:34361 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727540AbfLJPmT (ORCPT ); Tue, 10 Dec 2019 10:42:19 -0500 Received: by mail-wr1-f66.google.com with SMTP id t2so20663299wrr.1 for ; Tue, 10 Dec 2019 07:42:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=w4D6ZqamJW9QSroTdCyeCwKsa+r24vCro2cOz6VJshk=; b=XDJgVY9Nq/Bwd9tasYyqOJZUxEl9HxMnhBkzMYCb4XFprY/wyK/6NOZ9cYHDFugzms mjqEk/NTc0CAUlPwrkDFlpkJ/lUBK4Ztuc8/QBDtRfGD6a6n+iu3W6zO/W4zht9X0ZpO Uj+jgkNncdn80EFCOc2nwuctP8hFPeWnhgGrFZM/AjOjjhxzfXNycgswVJBJ7zZnoQtI EEH05c0gCrzmXgu0T2BN/5mHVeRSHlb5fs7QgXqPlpNaY7e2UdPnGEpH9hiIYq9P4i/8 T29uZh7WCTQHJ4hKtMaWoEiknjuTNGMBwzAhFcUKZ1EU2OxqQVJBNYw36/V0IQTlfBxx MYyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=w4D6ZqamJW9QSroTdCyeCwKsa+r24vCro2cOz6VJshk=; b=M4HgOHQf0Yi/CHx1cjkuQtGP6A/BLm+1bWv7QdwOljCLvChMV6H59C4TMKeTNuCr8Z Qn4ysZRWpofavg/7OnpxWhaNagQieJDv43uvUNpR5m6AE1zT/QTgyCjTcsZbRFU1AKxr FfIp6HCC+3pPhVqhj6G4Ik17oluqTKfvGxSn/7C7Ulb1eqDwau3CFjlANpZXbt5cuujo cGqH/4fcR636WWU7glQEIUrlp01pTloe1lkFkPAJIzDNDFYYH5vG5LBKE1gEfacF6ACw cMwTXGhv76ohsBOHOkrDuYBGZgeWX82T7bqD9I0QTUt6lQTsAMM2zZKxLXm3FcVdtdFK DM7A== X-Gm-Message-State: APjAAAWGbRi8QwN5dEWvJQW2DB3U3s2lpr83fBzPn/OkEYV3sxU9w3KU JOdp5ngLTK5ETgy/+Dj4APZuKA== X-Received: by 2002:adf:b605:: with SMTP id f5mr3847966wre.383.1575992537146; Tue, 10 Dec 2019 07:42:17 -0800 (PST) Received: from khouloud-ThinkPad-T470p.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.googlemail.com with ESMTPSA id o7sm3469085wmc.41.2019.12.10.07.42.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2019 07:42:16 -0800 (PST) From: Khouloud Touil To: bgolaszewski@baylibre.com, robh+dt@kernel.org, mark.rutland@arm.com, srinivas.kandagatla@linaro.org, baylibre-upstreaming@groups.io Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-i2c@vger.kernel.org, linus.walleij@linaro.org, Khouloud Touil Subject: [PATCH v2 2/4] nvmem: add support for the write-protect pin Date: Tue, 10 Dec 2019 16:41:55 +0100 Message-Id: <20191210154157.21930-3-ktouil@baylibre.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191210154157.21930-1-ktouil@baylibre.com> References: <20191210154157.21930-1-ktouil@baylibre.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The write-protect pin handling looks like a standard property that could benefit other users if available in the core nvmem framework. Instead of modifying all the memory drivers to check this pin, make the NVMEM subsystem check if the write-protect GPIO being passed through the nvmem_config or defined in the device tree and pull it low whenever writing to the memory. There was a suggestion for introducing the gpiodesc from pdata, but as pdata is already removed it could be replaced by adding it to nvmem_config. Reference: https://lists.96boards.org/pipermail/dev/2018-August/001056.html Signed-off-by: Khouloud Touil --- drivers/nvmem/core.c | 19 +++++++++++++++++-- drivers/nvmem/nvmem.h | 2 ++ include/linux/nvmem-provider.h | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 9f1ee9c766ec..3e1c94c4eee8 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include "nvmem.h" @@ -54,8 +55,14 @@ static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { - if (nvmem->reg_write) - return nvmem->reg_write(nvmem->priv, offset, val, bytes); + int ret; + + if (nvmem->reg_write) { + gpiod_set_value_cansleep(nvmem->wp_gpio, 0); + ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); + gpiod_set_value_cansleep(nvmem->wp_gpio, 1); + return ret; + } return -EINVAL; } @@ -338,6 +345,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) kfree(nvmem); return ERR_PTR(rval); } + if (config->wp_gpio) + nvmem->wp_gpio = config->wp_gpio; + else + nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", + GPIOD_OUT_HIGH); + if (IS_ERR(nvmem->wp_gpio)) + return PTR_ERR(nvmem->wp_gpio); + kref_init(&nvmem->refcnt); INIT_LIST_HEAD(&nvmem->cells); diff --git a/drivers/nvmem/nvmem.h b/drivers/nvmem/nvmem.h index eb8ed7121fa3..be0d66d75c8a 100644 --- a/drivers/nvmem/nvmem.h +++ b/drivers/nvmem/nvmem.h @@ -9,6 +9,7 @@ #include #include #include +#include struct nvmem_device { struct module *owner; @@ -26,6 +27,7 @@ struct nvmem_device { struct list_head cells; nvmem_reg_read_t reg_read; nvmem_reg_write_t reg_write; + struct gpio_desc *wp_gpio; void *priv; }; diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index fe051323be0a..6d6f8e5d24c9 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -11,6 +11,7 @@ #include #include +#include struct nvmem_device; struct nvmem_cell_info; @@ -45,6 +46,7 @@ enum nvmem_type { * @word_size: Minimum read/write access granularity. * @stride: Minimum read/write access stride. * @priv: User context passed to read/write callbacks. + * @wp-gpio: Write protect pin * * Note: A default "nvmem" name will be assigned to the device if * no name is specified in its configuration. In such case "" is @@ -58,6 +60,7 @@ struct nvmem_config { const char *name; int id; struct module *owner; + struct gpio_desc *wp_gpio; const struct nvmem_cell_info *cells; int ncells; enum nvmem_type type; -- 2.17.1