Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nvmem: add support for the write-protect pin

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>

authored by

Khouloud Touil and committed by
Bartosz Golaszewski
2a127da4 14f49573

+22 -2
+17 -2
drivers/nvmem/core.c
··· 15 15 #include <linux/module.h> 16 16 #include <linux/nvmem-consumer.h> 17 17 #include <linux/nvmem-provider.h> 18 + #include <linux/gpio/consumer.h> 18 19 #include <linux/of.h> 19 20 #include <linux/slab.h> 20 21 #include "nvmem.h" ··· 55 54 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, 56 55 void *val, size_t bytes) 57 56 { 58 - if (nvmem->reg_write) 59 - return nvmem->reg_write(nvmem->priv, offset, val, bytes); 57 + int ret; 58 + 59 + if (nvmem->reg_write) { 60 + gpiod_set_value_cansleep(nvmem->wp_gpio, 0); 61 + ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); 62 + gpiod_set_value_cansleep(nvmem->wp_gpio, 1); 63 + return ret; 64 + } 60 65 61 66 return -EINVAL; 62 67 } ··· 345 338 kfree(nvmem); 346 339 return ERR_PTR(rval); 347 340 } 341 + if (config->wp_gpio) 342 + nvmem->wp_gpio = config->wp_gpio; 343 + else 344 + nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", 345 + GPIOD_OUT_HIGH); 346 + if (IS_ERR(nvmem->wp_gpio)) 347 + return PTR_ERR(nvmem->wp_gpio); 348 + 348 349 349 350 kref_init(&nvmem->refcnt); 350 351 INIT_LIST_HEAD(&nvmem->cells);
+2
drivers/nvmem/nvmem.h
··· 9 9 #include <linux/list.h> 10 10 #include <linux/nvmem-consumer.h> 11 11 #include <linux/nvmem-provider.h> 12 + #include <linux/gpio/consumer.h> 12 13 13 14 struct nvmem_device { 14 15 struct module *owner; ··· 27 26 struct list_head cells; 28 27 nvmem_reg_read_t reg_read; 29 28 nvmem_reg_write_t reg_write; 29 + struct gpio_desc *wp_gpio; 30 30 void *priv; 31 31 }; 32 32
+3
include/linux/nvmem-provider.h
··· 11 11 12 12 #include <linux/err.h> 13 13 #include <linux/errno.h> 14 + #include <linux/gpio/consumer.h> 14 15 15 16 struct nvmem_device; 16 17 struct nvmem_cell_info; ··· 46 45 * @word_size: Minimum read/write access granularity. 47 46 * @stride: Minimum read/write access stride. 48 47 * @priv: User context passed to read/write callbacks. 48 + * @wp-gpio: Write protect pin 49 49 * 50 50 * Note: A default "nvmem<id>" name will be assigned to the device if 51 51 * no name is specified in its configuration. In such case "<id>" is ··· 60 58 const char *name; 61 59 int id; 62 60 struct module *owner; 61 + struct gpio_desc *wp_gpio; 63 62 const struct nvmem_cell_info *cells; 64 63 int ncells; 65 64 enum nvmem_type type;