Message ID | 1432226583-8775-1-git-send-email-srinivas.kandagatla@linaro.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On 05/21/2015 09:43 AM, Srinivas Kandagatla wrote: > > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > new file mode 100644 > index 0000000..6c2f0b1 > --- /dev/null > +++ b/drivers/nvmem/core.c > @@ -0,0 +1,398 @@ > +/* > + * nvmem framework core. > + * > + * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> > + * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > + > +#include <linux/device.h> > +#include <linux/nvmem-provider.h> > +#include <linux/export.h> > +#include <linux/fs.h> > +#include <linux/idr.h> > +#include <linux/init.h> > +#include <linux/regmap.h> > +#include <linux/module.h> > +#include <linux/of.h> > +#include <linux/slab.h> > +#include <linux/uaccess.h> Is this include used? > + > +static int of_nvmem_match(struct device *dev, const void *nvmem_np) > +{ > + return dev->of_node == nvmem_np; > +} > + > +static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) const? > +{ > + struct device *d; > + > + if (!nvmem_np) > + return NULL; > + > + d = class_find_device(&nvmem_class, NULL, nvmem_np, of_nvmem_match); > + > + return d ? to_nvmem_device(d) : NULL; > +} > + > +static struct nvmem_cell *nvmem_find_cell(const char *cell_id) > +{ > + struct nvmem_cell *p; > + > + list_for_each_entry(p, &nvmem_cells, node) { Unnecessary braces. > + if (p && !strcmp(p->name, cell_id)) > + return p; > + } > + > + return NULL; > +} > + > +static void nvmem_cell_drop(struct nvmem_cell *cell) > +{ > + mutex_lock(&nvmem_cells_mutex); > + list_del(&cell->node); > + mutex_unlock(&nvmem_cells_mutex); > + kfree(cell); > +} > + > +static void nvmem_device_remove_all_cells(struct nvmem_device *nvmem) > +{ > + struct nvmem_cell *cell = NULL; Unnecessary initialization > + struct list_head *p, *n; > + > + list_for_each_safe(p, n, &nvmem_cells) { > + cell = list_entry(p, struct nvmem_cell, node); > + if (cell->nvmem == nvmem) > + nvmem_cell_drop(cell); > + } [..] > + > +static int nvmem_add_cells(struct nvmem_device *nvmem, > + struct nvmem_config *cfg) > +{ > + struct nvmem_cell **cells; > + struct nvmem_cell_info *info = cfg->cells; > + int i, rval; > + > + cells = kzalloc(sizeof(*cells) * cfg->ncells, GFP_KERNEL); kcalloc > + if (!cells) > + return -ENOMEM; > + > + for (i = 0; i < cfg->ncells; i++) { > + cells[i] = kzalloc(sizeof(struct nvmem_cell), GFP_KERNEL); sizeof(**cells) ? > + if (!cells[i]) { > + rval = -ENOMEM; > + goto err; > + } > + > + rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); > + if (IS_ERR_VALUE(rval)) { > + kfree(cells[i]); > + goto err; > + } > + > + nvmem_cell_add(cells[i]); > + } > + > + nvmem->ncells = cfg->ncells; > + /* remove tmp array */ > + kfree(cells); > + > + return 0; > +err: > + while (--i) > + nvmem_cell_drop(cells[i]); > + > + return rval; > +} > + > +/** > + * nvmem_register(): Register a nvmem device for given nvmem. > + * Also creates an binary entry in /sys/class/nvmem/dev-name/nvmem > + * > + * @nvmem: nvmem device that needs to be created You mean @config? > + * > + * The return value will be an ERR_PTR() on error or a valid pointer > + * to nvmem_device. > + */ > + > +struct nvmem_device *nvmem_register(struct nvmem_config *config) > +{ > + struct nvmem_device *nvmem; > + struct regmap *rm; > + int rval; > + > + if (!config->dev) > + return ERR_PTR(-EINVAL); > + > + rm = dev_get_regmap(config->dev, NULL); > + if (!rm) { > + dev_err(config->dev, "Regmap not found\n"); > + return ERR_PTR(-EINVAL); > + } > + > + nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); > + if (!nvmem) > + return ERR_PTR(-ENOMEM); > + > + nvmem->id = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); > + if (nvmem->id < 0) { > + kfree(nvmem); > + return ERR_PTR(nvmem->id); > + } > + > + nvmem->regmap = rm; > + nvmem->owner = config->owner; > + nvmem->stride = regmap_get_reg_stride(rm); > + nvmem->word_size = regmap_get_val_bytes(rm); > + nvmem->size = regmap_get_max_register(rm) + nvmem->stride; > + nvmem->dev.class = &nvmem_class; > + nvmem->dev.parent = config->dev; > + nvmem->dev.of_node = config->dev->of_node; > + dev_set_name(&nvmem->dev, "%s%d", > + config->name ? : "nvmem", config->id); It may be better to always name it nvmem%d so that we don't allow the possibility of conflicts. > + > + nvmem->read_only = of_property_read_bool(nvmem->dev.of_node, > + "read-only"); What if we're not using DT? How would we specify read_only? > + > + device_initialize(&nvmem->dev); > + > + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", > + dev_name(&nvmem->dev)); > + > + rval = device_add(&nvmem->dev); > + if (rval) { > + ida_simple_remove(&nvmem_ida, nvmem->id); > + kfree(nvmem); > + return ERR_PTR(rval); > + } > + > + /* update sysfs attributes */ > + if (nvmem->read_only) > + sysfs_update_group(&nvmem->dev.kobj, &nvmem_bin_ro_group); It would be nice if this could be done before the device was registered. Perhaps have two device_types, one for read-only and one for read/write? > + > + if (config->cells) > + nvmem_add_cells(nvmem, config); > + > + return nvmem; > +} > +EXPORT_SYMBOL_GPL(nvmem_register); > + > +/** > + * nvmem_unregister(): Unregister previously registered nvmem device > + * > + * @nvmem: Pointer to previously registered nvmem device. > + * > + * The return value will be an non zero on error or a zero on success. > + */ > +int nvmem_unregister(struct nvmem_device *nvmem) > +{ > + mutex_lock(&nvmem_mutex); > + if (nvmem->users) { > + mutex_unlock(&nvmem_mutex); > + return -EBUSY; Hmm... that doesn't seem nice. Typically when something is unregistered we have to pull the rug out from underneath the users and start returning errors to them. The provider needs to be free to unregister because it's been forcibly removed. So really this function should return void. > + } > + mutex_unlock(&nvmem_mutex); > + > + nvmem_device_remove_all_cells(nvmem); > + device_del(&nvmem->dev); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(nvmem_unregister); > + > +static int nvmem_init(void) __init? And __exit on nvmem_exit? > +{ > + return class_register(&nvmem_class); I thought class was on the way out? Aren't we supposed to use bus types for new stuff?
Many thanks for review. On 16/06/15 23:43, Stephen Boyd wrote: > On 05/21/2015 09:43 AM, Srinivas Kandagatla wrote: >> >> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c >> new file mode 100644 >> index 0000000..6c2f0b1 >> --- /dev/null >> +++ b/drivers/nvmem/core.c >> @@ -0,0 +1,398 @@ >> +/* >> + * nvmem framework core. >> + * >> + * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> >> + * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> >> + * >> + * This program is free software; you can redistribute it and/or modify >> + * it under the terms of the GNU General Public License version 2 and >> + * only version 2 as published by the Free Software Foundation. >> + * >> + * This program is distributed in the hope that it will be useful, >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + * GNU General Public License for more details. >> + */ >> + >> +#include <linux/device.h> >> +#include <linux/nvmem-provider.h> >> +#include <linux/export.h> >> +#include <linux/fs.h> >> +#include <linux/idr.h> >> +#include <linux/init.h> >> +#include <linux/regmap.h> >> +#include <linux/module.h> >> +#include <linux/of.h> >> +#include <linux/slab.h> >> +#include <linux/uaccess.h> > > Is this include used? > Yep, not required. >> + >> +static int of_nvmem_match(struct device *dev, const void *nvmem_np) >> +{ >> + return dev->of_node == nvmem_np; >> +} >> + >> +static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) > > const? Sure, > >> +{ >> + struct device *d; >> + >> + if (!nvmem_np) >> + return NULL; >> + >> + d = class_find_device(&nvmem_class, NULL, nvmem_np, of_nvmem_match); >> + >> + return d ? to_nvmem_device(d) : NULL; >> +} >> + >> +static struct nvmem_cell *nvmem_find_cell(const char *cell_id) >> +{ >> + struct nvmem_cell *p; >> + >> + list_for_each_entry(p, &nvmem_cells, node) { > > Unnecessary braces. Yep, Will remove it. > >> + if (p && !strcmp(p->name, cell_id)) >> + return p; >> + } >> + >> + return NULL; >> +} >> + >> +static void nvmem_cell_drop(struct nvmem_cell *cell) >> +{ >> + mutex_lock(&nvmem_cells_mutex); >> + list_del(&cell->node); >> + mutex_unlock(&nvmem_cells_mutex); >> + kfree(cell); >> +} >> + >> +static void nvmem_device_remove_all_cells(struct nvmem_device *nvmem) >> +{ >> + struct nvmem_cell *cell = NULL; > > Unnecessary initialization Yes. > >> + struct list_head *p, *n; >> + >> + list_for_each_safe(p, n, &nvmem_cells) { >> + cell = list_entry(p, struct nvmem_cell, node); >> + if (cell->nvmem == nvmem) >> + nvmem_cell_drop(cell); >> + } > [..] >> + >> +static int nvmem_add_cells(struct nvmem_device *nvmem, >> + struct nvmem_config *cfg) >> +{ >> + struct nvmem_cell **cells; >> + struct nvmem_cell_info *info = cfg->cells; >> + int i, rval; >> + >> + cells = kzalloc(sizeof(*cells) * cfg->ncells, GFP_KERNEL); > > kcalloc This is temporary array, I did this on intention, to make it easy to kfree cells which are found invalid at runtime. > >> + if (!cells) >> + return -ENOMEM; >> + >> + for (i = 0; i < cfg->ncells; i++) { >> + cells[i] = kzalloc(sizeof(struct nvmem_cell), GFP_KERNEL); > > sizeof(**cells) ? Yep. > >> + >> +/** >> + * nvmem_register(): Register a nvmem device for given nvmem. >> + * Also creates an binary entry in /sys/class/nvmem/dev-name/nvmem >> + * >> + * @nvmem: nvmem device that needs to be created > > You mean @config? > Yes, I will fix it. >> + * >> + * The return value will be an ERR_PTR() on error or a valid pointer >> + nvmem->dev.of_node = config->dev->of_node; >> + dev_set_name(&nvmem->dev, "%s%d", >> + config->name ? : "nvmem", config->id); > > It may be better to always name it nvmem%d so that we don't allow the > possibility of conflicts. We can do that, but I wanted to make the sysfs and dev entries more readable than just nvmem0, nvmem1... > >> + >> + nvmem->read_only = of_property_read_bool(nvmem->dev.of_node, >> + "read-only"); > > What if we're not using DT? How would we specify read_only? > Thanks for spotting, you are correct, I need to add read_only flag to nvmem_config too. >> + >> + device_initialize(&nvmem->dev); >> + >> + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", >> + dev_name(&nvmem->dev)); >> + >> + rval = device_add(&nvmem->dev); >> + if (rval) { >> + ida_simple_remove(&nvmem_ida, nvmem->id); >> + kfree(nvmem); >> + return ERR_PTR(rval); >> + } >> + >> + /* update sysfs attributes */ >> + if (nvmem->read_only) >> + sysfs_update_group(&nvmem->dev.kobj, &nvmem_bin_ro_group); > > It would be nice if this could be done before the device was registered. > Perhaps have two device_types, one for read-only and one for read/write? The attributes are actually coming from the class object, so we have no choice to update the attributes before the device is registered. May it would be more safe to have default as read-only and modify it to read/write based on read-only flag? > >> + >> + */ >> +int nvmem_unregister(struct nvmem_device *nvmem) >> +{ >> + mutex_lock(&nvmem_mutex); >> + if (nvmem->users) { >> + mutex_unlock(&nvmem_mutex); >> + return -EBUSY; > > Hmm... that doesn't seem nice. Typically when something is unregistered > we have to pull the rug out from underneath the users and start > returning errors to them. The provider needs to be free to unregister > because it's been forcibly removed. So really this function should > return void. > The consumer api is get/put style, so consumers who already have references to the provider, removing provider forcefully might lead to dangling pointer. Having said that I can give a try and see how it looks. >> + } >> + mutex_unlock(&nvmem_mutex); >> + >> + nvmem_device_remove_all_cells(nvmem); >> + device_del(&nvmem->dev); >> + >> + return 0; >> +} >> +EXPORT_SYMBOL_GPL(nvmem_unregister); >> + >> +static int nvmem_init(void) > > __init? And __exit on nvmem_exit? yep, will do that. > >> +{ >> + return class_register(&nvmem_class); > > I thought class was on the way out? Aren't we supposed to use bus types > for new stuff? Do you remember any conversation on the list about this? I could not find it on web. on the other hand, nvmem is not really a bus, making it a bus type sounds incorrect to me. --srini >
On 06/18/2015 05:46 AM, Srinivas Kandagatla wrote: > Many thanks for review. > > On 16/06/15 23:43, Stephen Boyd wrote: >> On 05/21/2015 09:43 AM, Srinivas Kandagatla wrote: >>> >>>> + >>>> +static int nvmem_add_cells(struct nvmem_device *nvmem, >>>> + struct nvmem_config *cfg) >>>> +{ >>>> + struct nvmem_cell **cells; >>>> + struct nvmem_cell_info *info = cfg->cells; >>>> + int i, rval; >>>> + >>>> + cells = kzalloc(sizeof(*cells) * cfg->ncells, GFP_KERNEL); >>> >>> kcalloc > This is temporary array, I did this on intention, to make it easy to > kfree cells which are found invalid at runtime. Ok, but how does that change using kcalloc over kzalloc? I must have missed something. > > >>> + * >>> + * The return value will be an ERR_PTR() on error or a valid pointer >>> + nvmem->dev.of_node = config->dev->of_node; >>> + dev_set_name(&nvmem->dev, "%s%d", >>> + config->name ? : "nvmem", config->id); >> >> It may be better to always name it nvmem%d so that we don't allow the >> possibility of conflicts. > We can do that, but I wanted to make the sysfs and dev entries more > readable than just nvmem0, nvmem1... Well sysfs is not really for humans. It's for machines. The nvmem devices could have a name property so that a more human readable string is present. > >>> + >>> + device_initialize(&nvmem->dev); >>> + >>> + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", >>> + dev_name(&nvmem->dev)); >>> + >>> + rval = device_add(&nvmem->dev); >>> + if (rval) { >>> + ida_simple_remove(&nvmem_ida, nvmem->id); >>> + kfree(nvmem); >>> + return ERR_PTR(rval); >>> + } >>> + >>> + /* update sysfs attributes */ >>> + if (nvmem->read_only) >>> + sysfs_update_group(&nvmem->dev.kobj, &nvmem_bin_ro_group); >> >> It would be nice if this could be done before the device was registered. >> Perhaps have two device_types, one for read-only and one for read/write? > > The attributes are actually coming from the class object, so we have > no choice to update the attributes before the device is registered. > > May it would be more safe to have default as read-only and modify it > to read/write based on read-only flag? > > Can you assign the attributes to the device_type in the nvmem::struct device? I don't see why these attributes need to be part of the class. >> >>> +{ >>> + return class_register(&nvmem_class); >> >> I thought class was on the way out? Aren't we supposed to use bus types >> for new stuff? > Do you remember any conversation on the list about this? I could not > find it on web. > > on the other hand, nvmem is not really a bus, making it a bus type > sounds incorrect to me. > I found this post on the cpu class that Sudeep tried to introduce[1]. And there's this post from Kay that alludes to a unification of busses and classes[2]. And some other post where Kay says class is dead [3]. [1] https://lkml.org/lkml/2014/8/21/191 [2] https://lwn.net/Articles/471821/ [3] https://lkml.org/lkml/2010/11/11/17
On 24/06/15 01:24, Stephen Boyd wrote: > Can you assign the attributes to the device_type in the nvmem::struct > device? I don't see why these attributes need to be part of the class. > I will fix this. >>> >> >>>> >>>+{ >>>> >>>+ return class_register(&nvmem_class); >>> >> >>> >>I thought class was on the way out? Aren't we supposed to use bus types >>> >>for new stuff? >> >Do you remember any conversation on the list about this? I could not >> >find it on web. >> > >> >on the other hand, nvmem is not really a bus, making it a bus type >> >sounds incorrect to me. >> > > I found this post on the cpu class that Sudeep tried to introduce[1]. > And there's this post from Kay that alludes to a unification of busses > and classes[2]. And some other post where Kay says class is dead [3]. Thanks for the links, Yep, looks like Class is dead, I will change the code to use bus type instead. > > [1]https://lkml.org/lkml/2014/8/21/191 > [2]https://lwn.net/Articles/471821/ > [3]https://lkml.org/lkml/2010/11/11/17 --srini
diff --git a/drivers/Kconfig b/drivers/Kconfig index c0cc96b..69d7305 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -182,4 +182,6 @@ source "drivers/thunderbolt/Kconfig" source "drivers/android/Kconfig" +source "drivers/nvmem/Kconfig" + endmenu diff --git a/drivers/Makefile b/drivers/Makefile index 46d2554..f86b897 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -165,3 +165,4 @@ obj-$(CONFIG_RAS) += ras/ obj-$(CONFIG_THUNDERBOLT) += thunderbolt/ obj-$(CONFIG_CORESIGHT) += hwtracing/coresight/ obj-$(CONFIG_ANDROID) += android/ +obj-$(CONFIG_NVMEM) += nvmem/ diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig new file mode 100644 index 0000000..f157b6d --- /dev/null +++ b/drivers/nvmem/Kconfig @@ -0,0 +1,10 @@ +menuconfig NVMEM + tristate "NVMEM Support" + select REGMAP + help + Support for NVMEM devices. + + This framework is designed to provide a generic interface to NVMEM + from both the Linux Kernel and the userspace. + + If unsure, say no. diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile new file mode 100644 index 0000000..6df2c69 --- /dev/null +++ b/drivers/nvmem/Makefile @@ -0,0 +1,6 @@ +# +# Makefile for nvmem drivers. +# + +obj-$(CONFIG_NVMEM) += nvmem_core.o +nvmem_core-y := core.o diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c new file mode 100644 index 0000000..6c2f0b1 --- /dev/null +++ b/drivers/nvmem/core.c @@ -0,0 +1,398 @@ +/* + * nvmem framework core. + * + * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> + * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <linux/device.h> +#include <linux/nvmem-provider.h> +#include <linux/export.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/init.h> +#include <linux/regmap.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/slab.h> +#include <linux/uaccess.h> + +struct nvmem_device { + const char *name; + struct regmap *regmap; + struct module *owner; + struct device dev; + int stride; + int word_size; + int ncells; + int id; + int users; + size_t size; + bool read_only; +}; + +struct nvmem_cell { + const char *name; + int offset; + int bytes; + int bit_offset; + int nbits; + struct nvmem_device *nvmem; + struct list_head node; +}; + +static DEFINE_MUTEX(nvmem_mutex); +static DEFINE_IDA(nvmem_ida); + +static LIST_HEAD(nvmem_cells); +static DEFINE_MUTEX(nvmem_cells_mutex); + +#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) + +static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, + char *buf, loff_t pos, size_t count) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct nvmem_device *nvmem = to_nvmem_device(dev); + int rc; + + /* Stop the user from reading */ + if (pos > nvmem->size) + return 0; + + if (pos + count > nvmem->size) + count = nvmem->size - pos; + + count = count/nvmem->word_size * nvmem->word_size; + + rc = regmap_raw_read(nvmem->regmap, pos, buf, count); + + if (IS_ERR_VALUE(rc)) + return rc; + + return count; +} + +static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, + char *buf, loff_t pos, size_t count) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct nvmem_device *nvmem = to_nvmem_device(dev); + int rc; + + /* Stop the user from writing */ + if (pos > nvmem->size) + return 0; + + if (pos + count > nvmem->size) + count = nvmem->size - pos; + + count = count/nvmem->word_size * nvmem->word_size; + + rc = regmap_raw_write(nvmem->regmap, pos, buf, count); + + if (IS_ERR_VALUE(rc)) + return rc; + + return count; +} + +/* default read/write permissions */ +static struct bin_attribute bin_attr_nvmem = { + .attr = { + .name = "nvmem", + .mode = S_IWUSR | S_IRUGO, + }, + .read = bin_attr_nvmem_read, + .write = bin_attr_nvmem_write, +}; + +static struct bin_attribute *nvmem_bin_attributes[] = { + &bin_attr_nvmem, + NULL, +}; + +static const struct attribute_group nvmem_bin_group = { + .bin_attrs = nvmem_bin_attributes, +}; + +static const struct attribute_group *nvmem_dev_groups[] = { + &nvmem_bin_group, + NULL, +}; + +/* read only permission */ +static struct bin_attribute bin_attr_ro_nvmem = { + .attr = { + .name = "nvmem", + .mode = S_IRUGO, + }, + .read = bin_attr_nvmem_read, +}; + +static struct bin_attribute *nvmem_bin_ro_attributes[] = { + &bin_attr_ro_nvmem, + NULL, +}; +static const struct attribute_group nvmem_bin_ro_group = { + .bin_attrs = nvmem_bin_ro_attributes, +}; + +static void nvmem_release(struct device *dev) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + + ida_simple_remove(&nvmem_ida, nvmem->id); + kfree(nvmem); +} + +static struct class nvmem_class = { + .name = "nvmem", + .dev_groups = nvmem_dev_groups, + .dev_release = nvmem_release, +}; + +static int of_nvmem_match(struct device *dev, const void *nvmem_np) +{ + return dev->of_node == nvmem_np; +} + +static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) +{ + struct device *d; + + if (!nvmem_np) + return NULL; + + d = class_find_device(&nvmem_class, NULL, nvmem_np, of_nvmem_match); + + return d ? to_nvmem_device(d) : NULL; +} + +static struct nvmem_cell *nvmem_find_cell(const char *cell_id) +{ + struct nvmem_cell *p; + + list_for_each_entry(p, &nvmem_cells, node) { + if (p && !strcmp(p->name, cell_id)) + return p; + } + + return NULL; +} + +static void nvmem_cell_drop(struct nvmem_cell *cell) +{ + mutex_lock(&nvmem_cells_mutex); + list_del(&cell->node); + mutex_unlock(&nvmem_cells_mutex); + kfree(cell); +} + +static void nvmem_device_remove_all_cells(struct nvmem_device *nvmem) +{ + struct nvmem_cell *cell = NULL; + struct list_head *p, *n; + + list_for_each_safe(p, n, &nvmem_cells) { + cell = list_entry(p, struct nvmem_cell, node); + if (cell->nvmem == nvmem) + nvmem_cell_drop(cell); + } +} + +static void nvmem_cell_add(struct nvmem_cell *cell) +{ + mutex_lock(&nvmem_cells_mutex); + list_add_tail(&cell->node, &nvmem_cells); + mutex_unlock(&nvmem_cells_mutex); +} + +static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, + struct nvmem_cell *cell) +{ + cell->nvmem = nvmem; + cell->offset = info->offset; + cell->bytes = info->bytes; + cell->name = info->name; + + cell->bit_offset = info->bit_offset; + cell->nbits = info->nbits; + + if (cell->nbits) + cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, + BITS_PER_BYTE); + + if (!IS_ALIGNED(cell->offset, nvmem->stride)) { + dev_err(&nvmem->dev, + "cell %s unaligned to nvmem stride %d\n", + cell->name, nvmem->stride); + return -EINVAL; + } + + return 0; +} + +static int nvmem_add_cells(struct nvmem_device *nvmem, + struct nvmem_config *cfg) +{ + struct nvmem_cell **cells; + struct nvmem_cell_info *info = cfg->cells; + int i, rval; + + cells = kzalloc(sizeof(*cells) * cfg->ncells, GFP_KERNEL); + if (!cells) + return -ENOMEM; + + for (i = 0; i < cfg->ncells; i++) { + cells[i] = kzalloc(sizeof(struct nvmem_cell), GFP_KERNEL); + if (!cells[i]) { + rval = -ENOMEM; + goto err; + } + + rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); + if (IS_ERR_VALUE(rval)) { + kfree(cells[i]); + goto err; + } + + nvmem_cell_add(cells[i]); + } + + nvmem->ncells = cfg->ncells; + /* remove tmp array */ + kfree(cells); + + return 0; +err: + while (--i) + nvmem_cell_drop(cells[i]); + + return rval; +} + +/** + * nvmem_register(): Register a nvmem device for given nvmem. + * Also creates an binary entry in /sys/class/nvmem/dev-name/nvmem + * + * @nvmem: nvmem device that needs to be created + * + * The return value will be an ERR_PTR() on error or a valid pointer + * to nvmem_device. + */ + +struct nvmem_device *nvmem_register(struct nvmem_config *config) +{ + struct nvmem_device *nvmem; + struct regmap *rm; + int rval; + + if (!config->dev) + return ERR_PTR(-EINVAL); + + rm = dev_get_regmap(config->dev, NULL); + if (!rm) { + dev_err(config->dev, "Regmap not found\n"); + return ERR_PTR(-EINVAL); + } + + nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); + if (!nvmem) + return ERR_PTR(-ENOMEM); + + nvmem->id = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); + if (nvmem->id < 0) { + kfree(nvmem); + return ERR_PTR(nvmem->id); + } + + nvmem->regmap = rm; + nvmem->owner = config->owner; + nvmem->stride = regmap_get_reg_stride(rm); + nvmem->word_size = regmap_get_val_bytes(rm); + nvmem->size = regmap_get_max_register(rm) + nvmem->stride; + nvmem->dev.class = &nvmem_class; + nvmem->dev.parent = config->dev; + nvmem->dev.of_node = config->dev->of_node; + dev_set_name(&nvmem->dev, "%s%d", + config->name ? : "nvmem", config->id); + + nvmem->read_only = of_property_read_bool(nvmem->dev.of_node, + "read-only"); + + device_initialize(&nvmem->dev); + + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", + dev_name(&nvmem->dev)); + + rval = device_add(&nvmem->dev); + if (rval) { + ida_simple_remove(&nvmem_ida, nvmem->id); + kfree(nvmem); + return ERR_PTR(rval); + } + + /* update sysfs attributes */ + if (nvmem->read_only) + sysfs_update_group(&nvmem->dev.kobj, &nvmem_bin_ro_group); + + if (config->cells) + nvmem_add_cells(nvmem, config); + + return nvmem; +} +EXPORT_SYMBOL_GPL(nvmem_register); + +/** + * nvmem_unregister(): Unregister previously registered nvmem device + * + * @nvmem: Pointer to previously registered nvmem device. + * + * The return value will be an non zero on error or a zero on success. + */ +int nvmem_unregister(struct nvmem_device *nvmem) +{ + mutex_lock(&nvmem_mutex); + if (nvmem->users) { + mutex_unlock(&nvmem_mutex); + return -EBUSY; + } + mutex_unlock(&nvmem_mutex); + + nvmem_device_remove_all_cells(nvmem); + device_del(&nvmem->dev); + + return 0; +} +EXPORT_SYMBOL_GPL(nvmem_unregister); + +static int nvmem_init(void) +{ + return class_register(&nvmem_class); +} + +static void nvmem_exit(void) +{ + class_unregister(&nvmem_class); +} + +subsys_initcall(nvmem_init); +module_exit(nvmem_exit); + +MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); +MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); +MODULE_DESCRIPTION("nvmem Driver Core"); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h new file mode 100644 index 0000000..4908b37 --- /dev/null +++ b/include/linux/nvmem-provider.h @@ -0,0 +1,53 @@ +/* + * nvmem framework provider. + * + * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> + * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#ifndef _LINUX_NVMEM_PROVIDER_H +#define _LINUX_NVMEM_PROVIDER_H + +struct nvmem_device; + +struct nvmem_cell_info { + const char *name; + int offset; + int bytes; + int bit_offset; + int nbits; +}; + +struct nvmem_config { + struct device *dev; + const char *name; + int id; + struct module *owner; + struct nvmem_cell_info *cells; + int ncells; +}; + +#if IS_ENABLED(CONFIG_NVMEM) + +struct nvmem_device *nvmem_register(struct nvmem_config *cfg); +int nvmem_unregister(struct nvmem_device *nvmem); + +#else + +static inline struct nvmem_device *nvmem_register(struct nvmem_config *cfg) +{ + return ERR_PTR(-ENOSYS); +} + +static inline int nvmem_unregister(struct nvmem_device *nvmem) +{ + return -ENOSYS; +} + +#endif /* CONFIG_NVMEM */ + +#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */