gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / nvmem / core.c
blob05c6ae4b0b97365ce70a586bcd8a7a8d3b9a0f9b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * nvmem framework core.
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 */
9 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/fs.h>
12 #include <linux/idr.h>
13 #include <linux/init.h>
14 #include <linux/kref.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
22 struct nvmem_device {
23 struct module *owner;
24 struct device dev;
25 int stride;
26 int word_size;
27 int id;
28 struct kref refcnt;
29 size_t size;
30 bool read_only;
31 bool root_only;
32 int flags;
33 enum nvmem_type type;
34 struct bin_attribute eeprom;
35 struct device *base_dev;
36 struct list_head cells;
37 nvmem_reg_read_t reg_read;
38 nvmem_reg_write_t reg_write;
39 struct gpio_desc *wp_gpio;
40 void *priv;
43 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
45 #define FLAG_COMPAT BIT(0)
47 struct nvmem_cell {
48 const char *name;
49 int offset;
50 int bytes;
51 int bit_offset;
52 int nbits;
53 struct device_node *np;
54 struct nvmem_device *nvmem;
55 struct list_head node;
58 static DEFINE_MUTEX(nvmem_mutex);
59 static DEFINE_IDA(nvmem_ida);
61 static DEFINE_MUTEX(nvmem_cell_mutex);
62 static LIST_HEAD(nvmem_cell_tables);
64 static DEFINE_MUTEX(nvmem_lookup_mutex);
65 static LIST_HEAD(nvmem_lookup_list);
67 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
69 #ifdef CONFIG_NVMEM_SYSFS
70 static const char * const nvmem_type_str[] = {
71 [NVMEM_TYPE_UNKNOWN] = "Unknown",
72 [NVMEM_TYPE_EEPROM] = "EEPROM",
73 [NVMEM_TYPE_OTP] = "OTP",
74 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
77 #ifdef CONFIG_DEBUG_LOCK_ALLOC
78 static struct lock_class_key eeprom_lock_key;
79 #endif
81 static ssize_t type_show(struct device *dev,
82 struct device_attribute *attr, char *buf)
84 struct nvmem_device *nvmem = to_nvmem_device(dev);
86 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
89 static DEVICE_ATTR_RO(type);
91 static struct attribute *nvmem_attrs[] = {
92 &dev_attr_type.attr,
93 NULL,
96 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
97 struct bin_attribute *attr, char *buf,
98 loff_t pos, size_t count)
100 struct device *dev;
101 struct nvmem_device *nvmem;
102 int rc;
104 if (attr->private)
105 dev = attr->private;
106 else
107 dev = container_of(kobj, struct device, kobj);
108 nvmem = to_nvmem_device(dev);
110 /* Stop the user from reading */
111 if (pos >= nvmem->size)
112 return 0;
114 if (count < nvmem->word_size)
115 return -EINVAL;
117 if (pos + count > nvmem->size)
118 count = nvmem->size - pos;
120 count = round_down(count, nvmem->word_size);
122 if (!nvmem->reg_read)
123 return -EPERM;
125 rc = nvmem->reg_read(nvmem->priv, pos, buf, count);
127 if (rc)
128 return rc;
130 return count;
133 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
134 struct bin_attribute *attr, char *buf,
135 loff_t pos, size_t count)
137 struct device *dev;
138 struct nvmem_device *nvmem;
139 int rc;
141 if (attr->private)
142 dev = attr->private;
143 else
144 dev = container_of(kobj, struct device, kobj);
145 nvmem = to_nvmem_device(dev);
147 /* Stop the user from writing */
148 if (pos >= nvmem->size)
149 return -EFBIG;
151 if (count < nvmem->word_size)
152 return -EINVAL;
154 if (pos + count > nvmem->size)
155 count = nvmem->size - pos;
157 count = round_down(count, nvmem->word_size);
159 if (!nvmem->reg_write)
160 return -EPERM;
162 rc = nvmem->reg_write(nvmem->priv, pos, buf, count);
164 if (rc)
165 return rc;
167 return count;
170 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
171 struct bin_attribute *attr, int i)
173 struct device *dev = container_of(kobj, struct device, kobj);
174 struct nvmem_device *nvmem = to_nvmem_device(dev);
175 umode_t mode = 0400;
177 if (!nvmem->root_only)
178 mode |= 0044;
180 if (!nvmem->read_only)
181 mode |= 0200;
183 if (!nvmem->reg_write)
184 mode &= ~0200;
186 if (!nvmem->reg_read)
187 mode &= ~0444;
189 return mode;
192 /* default read/write permissions */
193 static struct bin_attribute bin_attr_rw_nvmem = {
194 .attr = {
195 .name = "nvmem",
196 .mode = 0644,
198 .read = bin_attr_nvmem_read,
199 .write = bin_attr_nvmem_write,
202 static struct bin_attribute *nvmem_bin_attributes[] = {
203 &bin_attr_rw_nvmem,
204 NULL,
207 static const struct attribute_group nvmem_bin_group = {
208 .bin_attrs = nvmem_bin_attributes,
209 .attrs = nvmem_attrs,
210 .is_bin_visible = nvmem_bin_attr_is_visible,
213 static const struct attribute_group *nvmem_dev_groups[] = {
214 &nvmem_bin_group,
215 NULL,
218 /* read only permission */
219 static struct bin_attribute bin_attr_ro_nvmem = {
220 .attr = {
221 .name = "nvmem",
222 .mode = 0444,
224 .read = bin_attr_nvmem_read,
227 /* default read/write permissions, root only */
228 static struct bin_attribute bin_attr_rw_root_nvmem = {
229 .attr = {
230 .name = "nvmem",
231 .mode = 0600,
233 .read = bin_attr_nvmem_read,
234 .write = bin_attr_nvmem_write,
237 /* read only permission, root only */
238 static struct bin_attribute bin_attr_ro_root_nvmem = {
239 .attr = {
240 .name = "nvmem",
241 .mode = 0400,
243 .read = bin_attr_nvmem_read,
247 * nvmem_setup_compat() - Create an additional binary entry in
248 * drivers sys directory, to be backwards compatible with the older
249 * drivers/misc/eeprom drivers.
251 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
252 const struct nvmem_config *config)
254 int rval;
256 if (!config->compat)
257 return 0;
259 if (!config->base_dev)
260 return -EINVAL;
262 if (nvmem->read_only) {
263 if (config->root_only)
264 nvmem->eeprom = bin_attr_ro_root_nvmem;
265 else
266 nvmem->eeprom = bin_attr_ro_nvmem;
267 } else {
268 if (config->root_only)
269 nvmem->eeprom = bin_attr_rw_root_nvmem;
270 else
271 nvmem->eeprom = bin_attr_rw_nvmem;
273 nvmem->eeprom.attr.name = "eeprom";
274 nvmem->eeprom.size = nvmem->size;
275 #ifdef CONFIG_DEBUG_LOCK_ALLOC
276 nvmem->eeprom.attr.key = &eeprom_lock_key;
277 #endif
278 nvmem->eeprom.private = &nvmem->dev;
279 nvmem->base_dev = config->base_dev;
281 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
282 if (rval) {
283 dev_err(&nvmem->dev,
284 "Failed to create eeprom binary file %d\n", rval);
285 return rval;
288 nvmem->flags |= FLAG_COMPAT;
290 return 0;
293 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
294 const struct nvmem_config *config)
296 if (config->compat)
297 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
300 #else /* CONFIG_NVMEM_SYSFS */
302 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
303 const struct nvmem_config *config)
305 return -ENOSYS;
307 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
308 const struct nvmem_config *config)
312 #endif /* CONFIG_NVMEM_SYSFS */
314 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
315 void *val, size_t bytes)
317 if (nvmem->reg_read)
318 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
320 return -EINVAL;
323 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
324 void *val, size_t bytes)
326 int ret;
328 if (nvmem->reg_write) {
329 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
330 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
331 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
332 return ret;
335 return -EINVAL;
338 static void nvmem_release(struct device *dev)
340 struct nvmem_device *nvmem = to_nvmem_device(dev);
342 ida_simple_remove(&nvmem_ida, nvmem->id);
343 gpiod_put(nvmem->wp_gpio);
344 kfree(nvmem);
347 static const struct device_type nvmem_provider_type = {
348 .release = nvmem_release,
351 static struct bus_type nvmem_bus_type = {
352 .name = "nvmem",
355 static void nvmem_cell_drop(struct nvmem_cell *cell)
357 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
358 mutex_lock(&nvmem_mutex);
359 list_del(&cell->node);
360 mutex_unlock(&nvmem_mutex);
361 of_node_put(cell->np);
362 kfree_const(cell->name);
363 kfree(cell);
366 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
368 struct nvmem_cell *cell, *p;
370 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
371 nvmem_cell_drop(cell);
374 static void nvmem_cell_add(struct nvmem_cell *cell)
376 mutex_lock(&nvmem_mutex);
377 list_add_tail(&cell->node, &cell->nvmem->cells);
378 mutex_unlock(&nvmem_mutex);
379 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
382 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
383 const struct nvmem_cell_info *info,
384 struct nvmem_cell *cell)
386 cell->nvmem = nvmem;
387 cell->offset = info->offset;
388 cell->bytes = info->bytes;
389 cell->name = kstrdup_const(info->name, GFP_KERNEL);
390 if (!cell->name)
391 return -ENOMEM;
393 cell->bit_offset = info->bit_offset;
394 cell->nbits = info->nbits;
396 if (cell->nbits)
397 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
398 BITS_PER_BYTE);
400 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
401 dev_err(&nvmem->dev,
402 "cell %s unaligned to nvmem stride %d\n",
403 cell->name, nvmem->stride);
404 return -EINVAL;
407 return 0;
411 * nvmem_add_cells() - Add cell information to an nvmem device
413 * @nvmem: nvmem device to add cells to.
414 * @info: nvmem cell info to add to the device
415 * @ncells: number of cells in info
417 * Return: 0 or negative error code on failure.
419 static int nvmem_add_cells(struct nvmem_device *nvmem,
420 const struct nvmem_cell_info *info,
421 int ncells)
423 struct nvmem_cell **cells;
424 int i, rval;
426 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
427 if (!cells)
428 return -ENOMEM;
430 for (i = 0; i < ncells; i++) {
431 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
432 if (!cells[i]) {
433 rval = -ENOMEM;
434 goto err;
437 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
438 if (rval) {
439 kfree(cells[i]);
440 goto err;
443 nvmem_cell_add(cells[i]);
446 /* remove tmp array */
447 kfree(cells);
449 return 0;
450 err:
451 while (i--)
452 nvmem_cell_drop(cells[i]);
454 kfree(cells);
456 return rval;
460 * nvmem_register_notifier() - Register a notifier block for nvmem events.
462 * @nb: notifier block to be called on nvmem events.
464 * Return: 0 on success, negative error number on failure.
466 int nvmem_register_notifier(struct notifier_block *nb)
468 return blocking_notifier_chain_register(&nvmem_notifier, nb);
470 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
473 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
475 * @nb: notifier block to be unregistered.
477 * Return: 0 on success, negative error number on failure.
479 int nvmem_unregister_notifier(struct notifier_block *nb)
481 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
483 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
485 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
487 const struct nvmem_cell_info *info;
488 struct nvmem_cell_table *table;
489 struct nvmem_cell *cell;
490 int rval = 0, i;
492 mutex_lock(&nvmem_cell_mutex);
493 list_for_each_entry(table, &nvmem_cell_tables, node) {
494 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
495 for (i = 0; i < table->ncells; i++) {
496 info = &table->cells[i];
498 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
499 if (!cell) {
500 rval = -ENOMEM;
501 goto out;
504 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
505 info,
506 cell);
507 if (rval) {
508 kfree(cell);
509 goto out;
512 nvmem_cell_add(cell);
517 out:
518 mutex_unlock(&nvmem_cell_mutex);
519 return rval;
522 static struct nvmem_cell *
523 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
525 struct nvmem_cell *iter, *cell = NULL;
527 mutex_lock(&nvmem_mutex);
528 list_for_each_entry(iter, &nvmem->cells, node) {
529 if (strcmp(cell_id, iter->name) == 0) {
530 cell = iter;
531 break;
534 mutex_unlock(&nvmem_mutex);
536 return cell;
539 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
541 struct device_node *parent, *child;
542 struct device *dev = &nvmem->dev;
543 struct nvmem_cell *cell;
544 const __be32 *addr;
545 int len;
547 parent = dev->of_node;
549 for_each_child_of_node(parent, child) {
550 addr = of_get_property(child, "reg", &len);
551 if (!addr || (len < 2 * sizeof(u32))) {
552 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
553 return -EINVAL;
556 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
557 if (!cell)
558 return -ENOMEM;
560 cell->nvmem = nvmem;
561 cell->np = of_node_get(child);
562 cell->offset = be32_to_cpup(addr++);
563 cell->bytes = be32_to_cpup(addr);
564 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
566 addr = of_get_property(child, "bits", &len);
567 if (addr && len == (2 * sizeof(u32))) {
568 cell->bit_offset = be32_to_cpup(addr++);
569 cell->nbits = be32_to_cpup(addr);
572 if (cell->nbits)
573 cell->bytes = DIV_ROUND_UP(
574 cell->nbits + cell->bit_offset,
575 BITS_PER_BYTE);
577 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
578 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
579 cell->name, nvmem->stride);
580 /* Cells already added will be freed later. */
581 kfree_const(cell->name);
582 kfree(cell);
583 return -EINVAL;
586 nvmem_cell_add(cell);
589 return 0;
593 * nvmem_register() - Register a nvmem device for given nvmem_config.
594 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
596 * @config: nvmem device configuration with which nvmem device is created.
598 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
599 * on success.
602 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
604 struct nvmem_device *nvmem;
605 int rval;
607 if (!config->dev)
608 return ERR_PTR(-EINVAL);
610 if (!config->reg_read && !config->reg_write)
611 return ERR_PTR(-EINVAL);
613 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
614 if (!nvmem)
615 return ERR_PTR(-ENOMEM);
617 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
618 if (rval < 0) {
619 kfree(nvmem);
620 return ERR_PTR(rval);
623 if (config->wp_gpio)
624 nvmem->wp_gpio = config->wp_gpio;
625 else
626 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
627 GPIOD_OUT_HIGH);
628 if (IS_ERR(nvmem->wp_gpio)) {
629 ida_simple_remove(&nvmem_ida, nvmem->id);
630 rval = PTR_ERR(nvmem->wp_gpio);
631 kfree(nvmem);
632 return ERR_PTR(rval);
635 kref_init(&nvmem->refcnt);
636 INIT_LIST_HEAD(&nvmem->cells);
638 nvmem->id = rval;
639 nvmem->owner = config->owner;
640 if (!nvmem->owner && config->dev->driver)
641 nvmem->owner = config->dev->driver->owner;
642 nvmem->stride = config->stride ?: 1;
643 nvmem->word_size = config->word_size ?: 1;
644 nvmem->size = config->size;
645 nvmem->dev.type = &nvmem_provider_type;
646 nvmem->dev.bus = &nvmem_bus_type;
647 nvmem->dev.parent = config->dev;
648 nvmem->root_only = config->root_only;
649 nvmem->priv = config->priv;
650 nvmem->type = config->type;
651 nvmem->reg_read = config->reg_read;
652 nvmem->reg_write = config->reg_write;
653 if (!config->no_of_node)
654 nvmem->dev.of_node = config->dev->of_node;
656 if (config->id == -1 && config->name) {
657 dev_set_name(&nvmem->dev, "%s", config->name);
658 } else {
659 dev_set_name(&nvmem->dev, "%s%d",
660 config->name ? : "nvmem",
661 config->name ? config->id : nvmem->id);
664 nvmem->read_only = device_property_present(config->dev, "read-only") ||
665 config->read_only || !nvmem->reg_write;
667 #ifdef CONFIG_NVMEM_SYSFS
668 nvmem->dev.groups = nvmem_dev_groups;
669 #endif
671 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
673 rval = device_register(&nvmem->dev);
674 if (rval)
675 goto err_put_device;
677 if (config->compat) {
678 rval = nvmem_sysfs_setup_compat(nvmem, config);
679 if (rval)
680 goto err_device_del;
683 if (config->cells) {
684 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
685 if (rval)
686 goto err_teardown_compat;
689 rval = nvmem_add_cells_from_table(nvmem);
690 if (rval)
691 goto err_remove_cells;
693 rval = nvmem_add_cells_from_of(nvmem);
694 if (rval)
695 goto err_remove_cells;
697 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
699 return nvmem;
701 err_remove_cells:
702 nvmem_device_remove_all_cells(nvmem);
703 err_teardown_compat:
704 if (config->compat)
705 nvmem_sysfs_remove_compat(nvmem, config);
706 err_device_del:
707 device_del(&nvmem->dev);
708 err_put_device:
709 put_device(&nvmem->dev);
711 return ERR_PTR(rval);
713 EXPORT_SYMBOL_GPL(nvmem_register);
715 static void nvmem_device_release(struct kref *kref)
717 struct nvmem_device *nvmem;
719 nvmem = container_of(kref, struct nvmem_device, refcnt);
721 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
723 if (nvmem->flags & FLAG_COMPAT)
724 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
726 nvmem_device_remove_all_cells(nvmem);
727 device_unregister(&nvmem->dev);
731 * nvmem_unregister() - Unregister previously registered nvmem device
733 * @nvmem: Pointer to previously registered nvmem device.
735 void nvmem_unregister(struct nvmem_device *nvmem)
737 kref_put(&nvmem->refcnt, nvmem_device_release);
739 EXPORT_SYMBOL_GPL(nvmem_unregister);
741 static void devm_nvmem_release(struct device *dev, void *res)
743 nvmem_unregister(*(struct nvmem_device **)res);
747 * devm_nvmem_register() - Register a managed nvmem device for given
748 * nvmem_config.
749 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
751 * @dev: Device that uses the nvmem device.
752 * @config: nvmem device configuration with which nvmem device is created.
754 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
755 * on success.
757 struct nvmem_device *devm_nvmem_register(struct device *dev,
758 const struct nvmem_config *config)
760 struct nvmem_device **ptr, *nvmem;
762 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
763 if (!ptr)
764 return ERR_PTR(-ENOMEM);
766 nvmem = nvmem_register(config);
768 if (!IS_ERR(nvmem)) {
769 *ptr = nvmem;
770 devres_add(dev, ptr);
771 } else {
772 devres_free(ptr);
775 return nvmem;
777 EXPORT_SYMBOL_GPL(devm_nvmem_register);
779 static int devm_nvmem_match(struct device *dev, void *res, void *data)
781 struct nvmem_device **r = res;
783 return *r == data;
787 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
788 * device.
790 * @dev: Device that uses the nvmem device.
791 * @nvmem: Pointer to previously registered nvmem device.
793 * Return: Will be an negative on error or a zero on success.
795 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
797 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
799 EXPORT_SYMBOL(devm_nvmem_unregister);
801 static struct nvmem_device *__nvmem_device_get(void *data,
802 int (*match)(struct device *dev, const void *data))
804 struct nvmem_device *nvmem = NULL;
805 struct device *dev;
807 mutex_lock(&nvmem_mutex);
808 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
809 if (dev)
810 nvmem = to_nvmem_device(dev);
811 mutex_unlock(&nvmem_mutex);
812 if (!nvmem)
813 return ERR_PTR(-EPROBE_DEFER);
815 if (!try_module_get(nvmem->owner)) {
816 dev_err(&nvmem->dev,
817 "could not increase module refcount for cell %s\n",
818 nvmem_dev_name(nvmem));
820 put_device(&nvmem->dev);
821 return ERR_PTR(-EINVAL);
824 kref_get(&nvmem->refcnt);
826 return nvmem;
829 static void __nvmem_device_put(struct nvmem_device *nvmem)
831 put_device(&nvmem->dev);
832 module_put(nvmem->owner);
833 kref_put(&nvmem->refcnt, nvmem_device_release);
836 #if IS_ENABLED(CONFIG_OF)
838 * of_nvmem_device_get() - Get nvmem device from a given id
840 * @np: Device tree node that uses the nvmem device.
841 * @id: nvmem name from nvmem-names property.
843 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
844 * on success.
846 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
849 struct device_node *nvmem_np;
850 int index = 0;
852 if (id)
853 index = of_property_match_string(np, "nvmem-names", id);
855 nvmem_np = of_parse_phandle(np, "nvmem", index);
856 if (!nvmem_np)
857 return ERR_PTR(-ENOENT);
859 return __nvmem_device_get(nvmem_np, device_match_of_node);
861 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
862 #endif
865 * nvmem_device_get() - Get nvmem device from a given id
867 * @dev: Device that uses the nvmem device.
868 * @dev_name: name of the requested nvmem device.
870 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
871 * on success.
873 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
875 if (dev->of_node) { /* try dt first */
876 struct nvmem_device *nvmem;
878 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
880 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
881 return nvmem;
885 return __nvmem_device_get((void *)dev_name, device_match_name);
887 EXPORT_SYMBOL_GPL(nvmem_device_get);
890 * nvmem_device_find() - Find nvmem device with matching function
892 * @data: Data to pass to match function
893 * @match: Callback function to check device
895 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
896 * on success.
898 struct nvmem_device *nvmem_device_find(void *data,
899 int (*match)(struct device *dev, const void *data))
901 return __nvmem_device_get(data, match);
903 EXPORT_SYMBOL_GPL(nvmem_device_find);
905 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
907 struct nvmem_device **nvmem = res;
909 if (WARN_ON(!nvmem || !*nvmem))
910 return 0;
912 return *nvmem == data;
915 static void devm_nvmem_device_release(struct device *dev, void *res)
917 nvmem_device_put(*(struct nvmem_device **)res);
921 * devm_nvmem_device_put() - put alredy got nvmem device
923 * @dev: Device that uses the nvmem device.
924 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
925 * that needs to be released.
927 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
929 int ret;
931 ret = devres_release(dev, devm_nvmem_device_release,
932 devm_nvmem_device_match, nvmem);
934 WARN_ON(ret);
936 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
939 * nvmem_device_put() - put alredy got nvmem device
941 * @nvmem: pointer to nvmem device that needs to be released.
943 void nvmem_device_put(struct nvmem_device *nvmem)
945 __nvmem_device_put(nvmem);
947 EXPORT_SYMBOL_GPL(nvmem_device_put);
950 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
952 * @dev: Device that requests the nvmem device.
953 * @id: name id for the requested nvmem device.
955 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
956 * on success. The nvmem_cell will be freed by the automatically once the
957 * device is freed.
959 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
961 struct nvmem_device **ptr, *nvmem;
963 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
964 if (!ptr)
965 return ERR_PTR(-ENOMEM);
967 nvmem = nvmem_device_get(dev, id);
968 if (!IS_ERR(nvmem)) {
969 *ptr = nvmem;
970 devres_add(dev, ptr);
971 } else {
972 devres_free(ptr);
975 return nvmem;
977 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
979 static struct nvmem_cell *
980 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
982 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
983 struct nvmem_cell_lookup *lookup;
984 struct nvmem_device *nvmem;
985 const char *dev_id;
987 if (!dev)
988 return ERR_PTR(-EINVAL);
990 dev_id = dev_name(dev);
992 mutex_lock(&nvmem_lookup_mutex);
994 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
995 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
996 (strcmp(lookup->con_id, con_id) == 0)) {
997 /* This is the right entry. */
998 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
999 device_match_name);
1000 if (IS_ERR(nvmem)) {
1001 /* Provider may not be registered yet. */
1002 cell = ERR_CAST(nvmem);
1003 break;
1006 cell = nvmem_find_cell_by_name(nvmem,
1007 lookup->cell_name);
1008 if (!cell) {
1009 __nvmem_device_put(nvmem);
1010 cell = ERR_PTR(-ENOENT);
1012 break;
1016 mutex_unlock(&nvmem_lookup_mutex);
1017 return cell;
1020 #if IS_ENABLED(CONFIG_OF)
1021 static struct nvmem_cell *
1022 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1024 struct nvmem_cell *iter, *cell = NULL;
1026 mutex_lock(&nvmem_mutex);
1027 list_for_each_entry(iter, &nvmem->cells, node) {
1028 if (np == iter->np) {
1029 cell = iter;
1030 break;
1033 mutex_unlock(&nvmem_mutex);
1035 return cell;
1039 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1041 * @np: Device tree node that uses the nvmem cell.
1042 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1043 * for the cell at index 0 (the lone cell with no accompanying
1044 * nvmem-cell-names property).
1046 * Return: Will be an ERR_PTR() on error or a valid pointer
1047 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1048 * nvmem_cell_put().
1050 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1052 struct device_node *cell_np, *nvmem_np;
1053 struct nvmem_device *nvmem;
1054 struct nvmem_cell *cell;
1055 int index = 0;
1057 /* if cell name exists, find index to the name */
1058 if (id)
1059 index = of_property_match_string(np, "nvmem-cell-names", id);
1061 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1062 if (!cell_np)
1063 return ERR_PTR(-ENOENT);
1065 nvmem_np = of_get_next_parent(cell_np);
1066 if (!nvmem_np)
1067 return ERR_PTR(-EINVAL);
1069 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1070 of_node_put(nvmem_np);
1071 if (IS_ERR(nvmem))
1072 return ERR_CAST(nvmem);
1074 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1075 if (!cell) {
1076 __nvmem_device_put(nvmem);
1077 return ERR_PTR(-ENOENT);
1080 return cell;
1082 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1083 #endif
1086 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1088 * @dev: Device that requests the nvmem cell.
1089 * @id: nvmem cell name to get (this corresponds with the name from the
1090 * nvmem-cell-names property for DT systems and with the con_id from
1091 * the lookup entry for non-DT systems).
1093 * Return: Will be an ERR_PTR() on error or a valid pointer
1094 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1095 * nvmem_cell_put().
1097 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1099 struct nvmem_cell *cell;
1101 if (dev->of_node) { /* try dt first */
1102 cell = of_nvmem_cell_get(dev->of_node, id);
1103 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1104 return cell;
1107 /* NULL cell id only allowed for device tree; invalid otherwise */
1108 if (!id)
1109 return ERR_PTR(-EINVAL);
1111 return nvmem_cell_get_from_lookup(dev, id);
1113 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1115 static void devm_nvmem_cell_release(struct device *dev, void *res)
1117 nvmem_cell_put(*(struct nvmem_cell **)res);
1121 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1123 * @dev: Device that requests the nvmem cell.
1124 * @id: nvmem cell name id to get.
1126 * Return: Will be an ERR_PTR() on error or a valid pointer
1127 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1128 * automatically once the device is freed.
1130 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1132 struct nvmem_cell **ptr, *cell;
1134 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1135 if (!ptr)
1136 return ERR_PTR(-ENOMEM);
1138 cell = nvmem_cell_get(dev, id);
1139 if (!IS_ERR(cell)) {
1140 *ptr = cell;
1141 devres_add(dev, ptr);
1142 } else {
1143 devres_free(ptr);
1146 return cell;
1148 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1150 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1152 struct nvmem_cell **c = res;
1154 if (WARN_ON(!c || !*c))
1155 return 0;
1157 return *c == data;
1161 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1162 * from devm_nvmem_cell_get.
1164 * @dev: Device that requests the nvmem cell.
1165 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1167 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1169 int ret;
1171 ret = devres_release(dev, devm_nvmem_cell_release,
1172 devm_nvmem_cell_match, cell);
1174 WARN_ON(ret);
1176 EXPORT_SYMBOL(devm_nvmem_cell_put);
1179 * nvmem_cell_put() - Release previously allocated nvmem cell.
1181 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1183 void nvmem_cell_put(struct nvmem_cell *cell)
1185 struct nvmem_device *nvmem = cell->nvmem;
1187 __nvmem_device_put(nvmem);
1189 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1191 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1193 u8 *p, *b;
1194 int i, extra, bit_offset = cell->bit_offset;
1196 p = b = buf;
1197 if (bit_offset) {
1198 /* First shift */
1199 *b++ >>= bit_offset;
1201 /* setup rest of the bytes if any */
1202 for (i = 1; i < cell->bytes; i++) {
1203 /* Get bits from next byte and shift them towards msb */
1204 *p |= *b << (BITS_PER_BYTE - bit_offset);
1206 p = b;
1207 *b++ >>= bit_offset;
1209 } else {
1210 /* point to the msb */
1211 p += cell->bytes - 1;
1214 /* result fits in less bytes */
1215 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1216 while (--extra >= 0)
1217 *p-- = 0;
1219 /* clear msb bits if any leftover in the last byte */
1220 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1223 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1224 struct nvmem_cell *cell,
1225 void *buf, size_t *len)
1227 int rc;
1229 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1231 if (rc)
1232 return rc;
1234 /* shift bits in-place */
1235 if (cell->bit_offset || cell->nbits)
1236 nvmem_shift_read_buffer_in_place(cell, buf);
1238 if (len)
1239 *len = cell->bytes;
1241 return 0;
1245 * nvmem_cell_read() - Read a given nvmem cell
1247 * @cell: nvmem cell to be read.
1248 * @len: pointer to length of cell which will be populated on successful read;
1249 * can be NULL.
1251 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1252 * buffer should be freed by the consumer with a kfree().
1254 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1256 struct nvmem_device *nvmem = cell->nvmem;
1257 u8 *buf;
1258 int rc;
1260 if (!nvmem)
1261 return ERR_PTR(-EINVAL);
1263 buf = kzalloc(cell->bytes, GFP_KERNEL);
1264 if (!buf)
1265 return ERR_PTR(-ENOMEM);
1267 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1268 if (rc) {
1269 kfree(buf);
1270 return ERR_PTR(rc);
1273 return buf;
1275 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1277 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1278 u8 *_buf, int len)
1280 struct nvmem_device *nvmem = cell->nvmem;
1281 int i, rc, nbits, bit_offset = cell->bit_offset;
1282 u8 v, *p, *buf, *b, pbyte, pbits;
1284 nbits = cell->nbits;
1285 buf = kzalloc(cell->bytes, GFP_KERNEL);
1286 if (!buf)
1287 return ERR_PTR(-ENOMEM);
1289 memcpy(buf, _buf, len);
1290 p = b = buf;
1292 if (bit_offset) {
1293 pbyte = *b;
1294 *b <<= bit_offset;
1296 /* setup the first byte with lsb bits from nvmem */
1297 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1298 if (rc)
1299 goto err;
1300 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1302 /* setup rest of the byte if any */
1303 for (i = 1; i < cell->bytes; i++) {
1304 /* Get last byte bits and shift them towards lsb */
1305 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1306 pbyte = *b;
1307 p = b;
1308 *b <<= bit_offset;
1309 *b++ |= pbits;
1313 /* if it's not end on byte boundary */
1314 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1315 /* setup the last byte with msb bits from nvmem */
1316 rc = nvmem_reg_read(nvmem,
1317 cell->offset + cell->bytes - 1, &v, 1);
1318 if (rc)
1319 goto err;
1320 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1324 return buf;
1325 err:
1326 kfree(buf);
1327 return ERR_PTR(rc);
1331 * nvmem_cell_write() - Write to a given nvmem cell
1333 * @cell: nvmem cell to be written.
1334 * @buf: Buffer to be written.
1335 * @len: length of buffer to be written to nvmem cell.
1337 * Return: length of bytes written or negative on failure.
1339 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1341 struct nvmem_device *nvmem = cell->nvmem;
1342 int rc;
1344 if (!nvmem || nvmem->read_only ||
1345 (cell->bit_offset == 0 && len != cell->bytes))
1346 return -EINVAL;
1348 if (cell->bit_offset || cell->nbits) {
1349 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1350 if (IS_ERR(buf))
1351 return PTR_ERR(buf);
1354 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1356 /* free the tmp buffer */
1357 if (cell->bit_offset || cell->nbits)
1358 kfree(buf);
1360 if (rc)
1361 return rc;
1363 return len;
1365 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1367 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1368 void *val, size_t count)
1370 struct nvmem_cell *cell;
1371 void *buf;
1372 size_t len;
1374 cell = nvmem_cell_get(dev, cell_id);
1375 if (IS_ERR(cell))
1376 return PTR_ERR(cell);
1378 buf = nvmem_cell_read(cell, &len);
1379 if (IS_ERR(buf)) {
1380 nvmem_cell_put(cell);
1381 return PTR_ERR(buf);
1383 if (len != count) {
1384 kfree(buf);
1385 nvmem_cell_put(cell);
1386 return -EINVAL;
1388 memcpy(val, buf, count);
1389 kfree(buf);
1390 nvmem_cell_put(cell);
1392 return 0;
1396 * nvmem_cell_read_u16() - Read a cell value as an u16
1398 * @dev: Device that requests the nvmem cell.
1399 * @cell_id: Name of nvmem cell to read.
1400 * @val: pointer to output value.
1402 * Return: 0 on success or negative errno.
1404 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1406 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1408 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1411 * nvmem_cell_read_u32() - Read a cell value as an u32
1413 * @dev: Device that requests the nvmem cell.
1414 * @cell_id: Name of nvmem cell to read.
1415 * @val: pointer to output value.
1417 * Return: 0 on success or negative errno.
1419 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1421 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1423 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1426 * nvmem_cell_read_u64() - Read a cell value as an u64
1428 * @dev: Device that requests the nvmem cell.
1429 * @cell_id: Name of nvmem cell to read.
1430 * @val: pointer to output value.
1432 * Return: 0 on success or negative errno.
1434 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1436 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1438 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1441 * nvmem_device_cell_read() - Read a given nvmem device and cell
1443 * @nvmem: nvmem device to read from.
1444 * @info: nvmem cell info to be read.
1445 * @buf: buffer pointer which will be populated on successful read.
1447 * Return: length of successful bytes read on success and negative
1448 * error code on error.
1450 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1451 struct nvmem_cell_info *info, void *buf)
1453 struct nvmem_cell cell;
1454 int rc;
1455 ssize_t len;
1457 if (!nvmem)
1458 return -EINVAL;
1460 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1461 if (rc)
1462 return rc;
1464 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1465 if (rc)
1466 return rc;
1468 return len;
1470 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1473 * nvmem_device_cell_write() - Write cell to a given nvmem device
1475 * @nvmem: nvmem device to be written to.
1476 * @info: nvmem cell info to be written.
1477 * @buf: buffer to be written to cell.
1479 * Return: length of bytes written or negative error code on failure.
1481 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1482 struct nvmem_cell_info *info, void *buf)
1484 struct nvmem_cell cell;
1485 int rc;
1487 if (!nvmem)
1488 return -EINVAL;
1490 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1491 if (rc)
1492 return rc;
1494 return nvmem_cell_write(&cell, buf, cell.bytes);
1496 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1499 * nvmem_device_read() - Read from a given nvmem device
1501 * @nvmem: nvmem device to read from.
1502 * @offset: offset in nvmem device.
1503 * @bytes: number of bytes to read.
1504 * @buf: buffer pointer which will be populated on successful read.
1506 * Return: length of successful bytes read on success and negative
1507 * error code on error.
1509 int nvmem_device_read(struct nvmem_device *nvmem,
1510 unsigned int offset,
1511 size_t bytes, void *buf)
1513 int rc;
1515 if (!nvmem)
1516 return -EINVAL;
1518 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1520 if (rc)
1521 return rc;
1523 return bytes;
1525 EXPORT_SYMBOL_GPL(nvmem_device_read);
1528 * nvmem_device_write() - Write cell to a given nvmem device
1530 * @nvmem: nvmem device to be written to.
1531 * @offset: offset in nvmem device.
1532 * @bytes: number of bytes to write.
1533 * @buf: buffer to be written.
1535 * Return: length of bytes written or negative error code on failure.
1537 int nvmem_device_write(struct nvmem_device *nvmem,
1538 unsigned int offset,
1539 size_t bytes, void *buf)
1541 int rc;
1543 if (!nvmem)
1544 return -EINVAL;
1546 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1548 if (rc)
1549 return rc;
1552 return bytes;
1554 EXPORT_SYMBOL_GPL(nvmem_device_write);
1557 * nvmem_add_cell_table() - register a table of cell info entries
1559 * @table: table of cell info entries
1561 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1563 mutex_lock(&nvmem_cell_mutex);
1564 list_add_tail(&table->node, &nvmem_cell_tables);
1565 mutex_unlock(&nvmem_cell_mutex);
1567 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1570 * nvmem_del_cell_table() - remove a previously registered cell info table
1572 * @table: table of cell info entries
1574 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1576 mutex_lock(&nvmem_cell_mutex);
1577 list_del(&table->node);
1578 mutex_unlock(&nvmem_cell_mutex);
1580 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1583 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1585 * @entries: array of cell lookup entries
1586 * @nentries: number of cell lookup entries in the array
1588 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1590 int i;
1592 mutex_lock(&nvmem_lookup_mutex);
1593 for (i = 0; i < nentries; i++)
1594 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1595 mutex_unlock(&nvmem_lookup_mutex);
1597 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1600 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1601 * entries
1603 * @entries: array of cell lookup entries
1604 * @nentries: number of cell lookup entries in the array
1606 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1608 int i;
1610 mutex_lock(&nvmem_lookup_mutex);
1611 for (i = 0; i < nentries; i++)
1612 list_del(&entries[i].node);
1613 mutex_unlock(&nvmem_lookup_mutex);
1615 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1618 * nvmem_dev_name() - Get the name of a given nvmem device.
1620 * @nvmem: nvmem device.
1622 * Return: name of the nvmem device.
1624 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1626 return dev_name(&nvmem->dev);
1628 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1630 static int __init nvmem_init(void)
1632 return bus_register(&nvmem_bus_type);
1635 static void __exit nvmem_exit(void)
1637 bus_unregister(&nvmem_bus_type);
1640 subsys_initcall(nvmem_init);
1641 module_exit(nvmem_exit);
1643 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1644 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1645 MODULE_DESCRIPTION("nvmem Driver Core");
1646 MODULE_LICENSE("GPL v2");