nvmem: add support for the write-protect pin
authorKhouloud Touil <ktouil@baylibre.com>
Tue, 7 Jan 2020 09:29:19 +0000 (10:29 +0100)
committerBartosz Golaszewski <bgolaszewski@baylibre.com>
Thu, 9 Jan 2020 09:48:54 +0000 (10:48 +0100)
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 <ktouil@baylibre.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
drivers/nvmem/core.c
drivers/nvmem/nvmem.h
include/linux/nvmem-provider.h

index 9f1ee9c766eca9bb76685e03aa2b741a9a5602d8..3e1c94c4eee8ccaf529b8e8e7360d6ced495b5f9 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/module.h>
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
+#include <linux/gpio/consumer.h>
 #include <linux/of.h>
 #include <linux/slab.h>
 #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);
index eb8ed7121fa3a5dcf0842ed6ac885a05e40686a4..be0d66d75c8aa50cbae773e097f28ac11e88eb19 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/list.h>
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
+#include <linux/gpio/consumer.h>
 
 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;
 };
 
index fe051323be0a2c0c44778426f3012731d03b9015..6d6f8e5d24c97ca71bf7863fdf98c3bdb0ee9a2b 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/gpio/consumer.h>
 
 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<id>" name will be assigned to the device if
  * no name is specified in its configuration. In such case "<id>" 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;