File Index
Files and directories
Sign in
FILE

core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/nvmem/core.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * nvmem framework core.
   4 *
   5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
   6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
   7 */
   8
   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>
  21
  22#include "internals.h"
  23
  24#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  25
  26#define FLAG_COMPAT		BIT(0)
  27struct nvmem_cell_entry {
  28	const char		*name;
  29	int			offset;
  30	size_t			raw_len;
  31	int			bytes;
  32	int			bit_offset;
  33	int			nbits;
  34	nvmem_cell_post_process_t read_post_process;
  35	void			*priv;
  36	struct device_node	*np;
  37	struct nvmem_device	*nvmem;
  38	struct list_head	node;
  39};
  40
  41struct nvmem_cell {
  42	struct nvmem_cell_entry *entry;
  43	const char		*id;
  44	int			index;
  45};
  46
  47static DEFINE_MUTEX(nvmem_mutex);
  48static DEFINE_IDA(nvmem_ida);
  49
  50static DEFINE_MUTEX(nvmem_lookup_mutex);
  51static LIST_HEAD(nvmem_lookup_list);
  52
  53static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
  54
  55static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  56			    void *val, size_t bytes)
  57{
  58	if (nvmem->reg_read)
  59		return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  60
  61	return -EINVAL;
  62}
  63
  64static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  65			     void *val, size_t bytes)
  66{
  67	int ret;
  68
  69	if (nvmem->reg_write) {
  70		gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
  71		ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
  72		gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
  73		return ret;
  74	}
  75
  76	return -EINVAL;
  77}
  78
  79static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
  80				      unsigned int offset, void *val,
  81				      size_t bytes, int write)
  82{
  83
  84	unsigned int end = offset + bytes;
  85	unsigned int kend, ksize;
  86	const struct nvmem_keepout *keepout = nvmem->keepout;
  87	const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  88	int rc;
  89
  90	/*
  91	 * Skip all keepouts before the range being accessed.
  92	 * Keepouts are sorted.
  93	 */
  94	while ((keepout < keepoutend) && (keepout->end <= offset))
  95		keepout++;
  96
  97	while ((offset < end) && (keepout < keepoutend)) {
  98		/* Access the valid portion before the keepout. */
  99		if (offset < keepout->start) {
 100			kend = min(end, keepout->start);
 101			ksize = kend - offset;
 102			if (write)
 103				rc = __nvmem_reg_write(nvmem, offset, val, ksize);
 104			else
 105				rc = __nvmem_reg_read(nvmem, offset, val, ksize);
 106
 107			if (rc)
 108				return rc;
 109
 110			offset += ksize;
 111			val += ksize;
 112		}
 113
 114		/*
 115		 * Now we're aligned to the start of this keepout zone. Go
 116		 * through it.
 117		 */
 118		kend = min(end, keepout->end);
 119		ksize = kend - offset;
 120		if (!write)
 121			memset(val, keepout->value, ksize);
 122
 123		val += ksize;
 124		offset += ksize;
 125		keepout++;
 126	}
 127
 128	/*
 129	 * If we ran out of keepouts but there's still stuff to do, send it
 130	 * down directly
 131	 */
 132	if (offset < end) {
 133		ksize = end - offset;
 134		if (write)
 135			return __nvmem_reg_write(nvmem, offset, val, ksize);
 136		else
 137			return __nvmem_reg_read(nvmem, offset, val, ksize);
 138	}
 139
 140	return 0;
 141}
 142
 143static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
 144			  void *val, size_t bytes)
 145{
 146	if (!nvmem->nkeepout)
 147		return __nvmem_reg_read(nvmem, offset, val, bytes);
 148
 149	return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
 150}
 151
 152static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
 153			   void *val, size_t bytes)
 154{
 155	if (!nvmem->nkeepout)
 156		return __nvmem_reg_write(nvmem, offset, val, bytes);
 157
 158	return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
 159}
 160
 161#ifdef CONFIG_NVMEM_SYSFS
 162static const char * const nvmem_type_str[] = {
 163	[NVMEM_TYPE_UNKNOWN] = "Unknown",
 164	[NVMEM_TYPE_EEPROM] = "EEPROM",
 165	[NVMEM_TYPE_OTP] = "OTP",
 166	[NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
 167	[NVMEM_TYPE_FRAM] = "FRAM",
 168};
 169
 170#ifdef CONFIG_DEBUG_LOCK_ALLOC
 171static struct lock_class_key eeprom_lock_key;
 172#endif
 173
 174static ssize_t type_show(struct device *dev,
 175			 struct device_attribute *attr, char *buf)
 176{
 177	struct nvmem_device *nvmem = to_nvmem_device(dev);
 178
 179	return sysfs_emit(buf, "%s\n", nvmem_type_str[nvmem->type]);
 180}
 181
 182static DEVICE_ATTR_RO(type);
 183
 184static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
 185			     char *buf)
 186{
 187	struct nvmem_device *nvmem = to_nvmem_device(dev);
 188
 189	return sysfs_emit(buf, "%d\n", nvmem->read_only);
 190}
 191
 192static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
 193			      const char *buf, size_t count)
 194{
 195	struct nvmem_device *nvmem = to_nvmem_device(dev);
 196	int ret = kstrtobool(buf, &nvmem->read_only);
 197
 198	if (ret < 0)
 199		return ret;
 200
 201	return count;
 202}
 203
 204static DEVICE_ATTR_RW(force_ro);
 205
 206static struct attribute *nvmem_attrs[] = {
 207	&dev_attr_force_ro.attr,
 208	&dev_attr_type.attr,
 209	NULL,
 210};
 211
 212static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
 213				   const struct bin_attribute *attr, char *buf,
 214				   loff_t pos, size_t count)
 215{
 216	struct device *dev;
 217	struct nvmem_device *nvmem;
 218	int rc;
 219
 220	if (attr->private)
 221		dev = attr->private;
 222	else
 223		dev = kobj_to_dev(kobj);
 224	nvmem = to_nvmem_device(dev);
 225
 226	if (!IS_ALIGNED(pos, nvmem->stride))
 227		return -EINVAL;
 228
 229	if (count < nvmem->word_size)
 230		return -EINVAL;
 231
 232	count = round_down(count, nvmem->word_size);
 233
 234	if (!nvmem->reg_read)
 235		return -EPERM;
 236
 237	rc = nvmem_reg_read(nvmem, pos, buf, count);
 238
 239	if (rc)
 240		return rc;
 241
 242	return count;
 243}
 244
 245static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
 246				    const struct bin_attribute *attr, char *buf,
 247				    loff_t pos, size_t count)
 248{
 249	struct device *dev;
 250	struct nvmem_device *nvmem;
 251	int rc;
 252
 253	if (attr->private)
 254		dev = attr->private;
 255	else
 256		dev = kobj_to_dev(kobj);
 257	nvmem = to_nvmem_device(dev);
 258
 259	if (!IS_ALIGNED(pos, nvmem->stride))
 260		return -EINVAL;
 261
 262	if (count < nvmem->word_size)
 263		return -EINVAL;
 264
 265	count = round_down(count, nvmem->word_size);
 266
 267	if (!nvmem->reg_write || nvmem->read_only)
 268		return -EPERM;
 269
 270	rc = nvmem_reg_write(nvmem, pos, buf, count);
 271
 272	if (rc)
 273		return rc;
 274
 275	return count;
 276}
 277
 278static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 279{
 280	umode_t mode = 0400;
 281
 282	if (!nvmem->root_only)
 283		mode |= 0044;
 284
 285	if (!nvmem->read_only)
 286		mode |= 0200;
 287
 288	if (!nvmem->reg_write)
 289		mode &= ~0200;
 290
 291	if (!nvmem->reg_read)
 292		mode &= ~0444;
 293
 294	return mode;
 295}
 296
 297static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
 298					 const struct bin_attribute *attr,
 299					 int i)
 300{
 301	struct device *dev = kobj_to_dev(kobj);
 302	struct nvmem_device *nvmem = to_nvmem_device(dev);
 303
 304	return nvmem_bin_attr_get_umode(nvmem);
 305}
 306
 307static size_t nvmem_bin_attr_size(struct kobject *kobj,
 308				  const struct bin_attribute *attr,
 309				  int i)
 310{
 311	struct device *dev = kobj_to_dev(kobj);
 312	struct nvmem_device *nvmem = to_nvmem_device(dev);
 313
 314	return nvmem->size;
 315}
 316
 317static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 318				     struct attribute *attr, int i)
 319{
 320	struct device *dev = kobj_to_dev(kobj);
 321	struct nvmem_device *nvmem = to_nvmem_device(dev);
 322
 323	/*
 324	 * If the device has no .reg_write operation, do not allow
 325	 * configuration as read-write.
 326	 * If the device is set as read-only by configuration, it
 327	 * can be forced into read-write mode using the 'force_ro'
 328	 * attribute.
 329	 */
 330	if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
 331		return 0;	/* Attribute not visible */
 332
 333	return attr->mode;
 334}
 335
 336static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
 337					    const char *id, int index);
 338
 339static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
 340				    const struct bin_attribute *attr, char *buf,
 341				    loff_t pos, size_t count)
 342{
 343	struct nvmem_cell_entry *entry;
 344	struct nvmem_cell *cell = NULL;
 345	size_t cell_sz, read_len;
 346	void *content;
 347
 348	entry = attr->private;
 349	cell = nvmem_create_cell(entry, entry->name, 0);
 350	if (IS_ERR(cell))
 351		return PTR_ERR(cell);
 352
 353	if (!cell)
 354		return -EINVAL;
 355
 356	content = nvmem_cell_read(cell, &cell_sz);
 357	if (IS_ERR(content)) {
 358		read_len = PTR_ERR(content);
 359		goto destroy_cell;
 360	}
 361
 362	read_len = min_t(unsigned int, cell_sz - pos, count);
 363	memcpy(buf, content + pos, read_len);
 364	kfree(content);
 365
 366destroy_cell:
 367	kfree_const(cell->id);
 368	kfree(cell);
 369
 370	return read_len;
 371}
 372
 373/* default read/write permissions */
 374static const struct bin_attribute bin_attr_rw_nvmem = {
 375	.attr	= {
 376		.name	= "nvmem",
 377		.mode	= 0644,
 378	},
 379	.read	= bin_attr_nvmem_read,
 380	.write	= bin_attr_nvmem_write,
 381};
 382
 383static const struct bin_attribute *const nvmem_bin_attributes[] = {
 384	&bin_attr_rw_nvmem,
 385	NULL,
 386};
 387
 388static const struct attribute_group nvmem_bin_group = {
 389	.bin_attrs	= nvmem_bin_attributes,
 390	.attrs		= nvmem_attrs,
 391	.is_bin_visible = nvmem_bin_attr_is_visible,
 392	.bin_size	= nvmem_bin_attr_size,
 393	.is_visible	= nvmem_attr_is_visible,
 394};
 395
 396static const struct attribute_group *nvmem_dev_groups[] = {
 397	&nvmem_bin_group,
 398	NULL,
 399};
 400
 401static const struct bin_attribute bin_attr_nvmem_eeprom_compat = {
 402	.attr	= {
 403		.name	= "eeprom",
 404	},
 405	.read	= bin_attr_nvmem_read,
 406	.write	= bin_attr_nvmem_write,
 407};
 408
 409/*
 410 * nvmem_setup_compat() - Create an additional binary entry in
 411 * drivers sys directory, to be backwards compatible with the older
 412 * drivers/misc/eeprom drivers.
 413 */
 414static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
 415				    const struct nvmem_config *config)
 416{
 417	int rval;
 418
 419	if (!config->compat)
 420		return 0;
 421
 422	if (!config->base_dev)
 423		return -EINVAL;
 424
 425	nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
 426	if (config->type == NVMEM_TYPE_FRAM)
 427		nvmem->eeprom.attr.name = "fram";
 428	nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
 429	nvmem->eeprom.size = nvmem->size;
 430#ifdef CONFIG_DEBUG_LOCK_ALLOC
 431	nvmem->eeprom.attr.key = &eeprom_lock_key;
 432#endif
 433	nvmem->eeprom.private = &nvmem->dev;
 434	nvmem->base_dev = config->base_dev;
 435
 436	rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
 437	if (rval) {
 438		dev_err(&nvmem->dev,
 439			"Failed to create eeprom binary file %d\n", rval);
 440		return rval;
 441	}
 442
 443	nvmem->flags |= FLAG_COMPAT;
 444
 445	return 0;
 446}
 447
 448static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
 449			      const struct nvmem_config *config)
 450{
 451	if (config->compat)
 452		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 453}
 454
 455static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
 456{
 457	struct attribute_group group = {
 458		.name	= "cells",
 459	};
 460	struct nvmem_cell_entry *entry;
 461	const struct bin_attribute **pattrs;
 462	struct bin_attribute *attrs;
 463	unsigned int ncells = 0, i = 0;
 464	int ret = 0;
 465
 466	mutex_lock(&nvmem_mutex);
 467
 468	if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
 469		goto unlock_mutex;
 470
 471	/* Allocate an array of attributes with a sentinel */
 472	ncells = list_count_nodes(&nvmem->cells);
 473	pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
 474			      sizeof(struct bin_attribute *), GFP_KERNEL);
 475	if (!pattrs) {
 476		ret = -ENOMEM;
 477		goto unlock_mutex;
 478	}
 479
 480	attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
 481	if (!attrs) {
 482		ret = -ENOMEM;
 483		goto unlock_mutex;
 484	}
 485
 486	/* Initialize each attribute to take the name and size of the cell */
 487	list_for_each_entry(entry, &nvmem->cells, node) {
 488		sysfs_bin_attr_init(&attrs[i]);
 489		attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
 490						    "%s@%x,%x", entry->name,
 491						    entry->offset,
 492						    entry->bit_offset);
 493		attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
 494		attrs[i].size = entry->bytes;
 495		attrs[i].read = &nvmem_cell_attr_read;
 496		attrs[i].private = entry;
 497		if (!attrs[i].attr.name) {
 498			ret = -ENOMEM;
 499			goto unlock_mutex;
 500		}
 501
 502		pattrs[i] = &attrs[i];
 503		i++;
 504	}
 505
 506	group.bin_attrs = pattrs;
 507
 508	ret = device_add_group(&nvmem->dev, &group);
 509	if (ret)
 510		goto unlock_mutex;
 511
 512	nvmem->sysfs_cells_populated = true;
 513
 514unlock_mutex:
 515	mutex_unlock(&nvmem_mutex);
 516
 517	return ret;
 518}
 519
 520#else /* CONFIG_NVMEM_SYSFS */
 521
 522static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
 523				    const struct nvmem_config *config)
 524{
 525	return -ENOSYS;
 526}
 527static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
 528				      const struct nvmem_config *config)
 529{
 530}
 531
 532#endif /* CONFIG_NVMEM_SYSFS */
 533
 534static void nvmem_release(struct device *dev)
 535{
 536	struct nvmem_device *nvmem = to_nvmem_device(dev);
 537
 538	ida_free(&nvmem_ida, nvmem->id);
 539	gpiod_put(nvmem->wp_gpio);
 540	kfree(nvmem);
 541}
 542
 543static const struct device_type nvmem_provider_type = {
 544	.release	= nvmem_release,
 545};
 546
 547static const struct bus_type nvmem_bus_type = {
 548	.name		= "nvmem",
 549};
 550
 551static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
 552{
 553	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
 554	mutex_lock(&nvmem_mutex);
 555	list_del(&cell->node);
 556	mutex_unlock(&nvmem_mutex);
 557	of_node_put(cell->np);
 558	kfree_const(cell->name);
 559	kfree(cell);
 560}
 561
 562static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
 563{
 564	struct nvmem_cell_entry *cell, *p;
 565
 566	list_for_each_entry_safe(cell, p, &nvmem->cells, node)
 567		nvmem_cell_entry_drop(cell);
 568}
 569
 570static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
 571{
 572	mutex_lock(&nvmem_mutex);
 573	list_add_tail(&cell->node, &cell->nvmem->cells);
 574	mutex_unlock(&nvmem_mutex);
 575	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
 576}
 577
 578static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
 579						     const struct nvmem_cell_info *info,
 580						     struct nvmem_cell_entry *cell)
 581{
 582	cell->nvmem = nvmem;
 583	cell->offset = info->offset;
 584	cell->raw_len = info->raw_len ?: info->bytes;
 585	cell->bytes = info->bytes;
 586	cell->name = info->name;
 587	cell->read_post_process = info->read_post_process;
 588	cell->priv = info->priv;
 589
 590	cell->bit_offset = info->bit_offset;
 591	cell->nbits = info->nbits;
 592	cell->np = info->np;
 593
 594	if (cell->nbits) {
 595		cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
 596					   BITS_PER_BYTE);
 597		cell->raw_len = ALIGN(cell->bytes, nvmem->word_size);
 598	}
 599
 600	if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
 601		dev_err(&nvmem->dev,
 602			"cell %s unaligned to nvmem stride %d\n",
 603			cell->name ?: "<unknown>", nvmem->stride);
 604		return -EINVAL;
 605	}
 606
 607	if (!IS_ALIGNED(cell->raw_len, nvmem->word_size)) {
 608		dev_err(&nvmem->dev,
 609			"cell %s raw len %zd unaligned to nvmem word size %d\n",
 610			cell->name ?: "<unknown>", cell->raw_len,
 611			nvmem->word_size);
 612
 613		if (info->raw_len)
 614			return -EINVAL;
 615
 616		cell->raw_len = ALIGN(cell->raw_len, nvmem->word_size);
 617	}
 618
 619	return 0;
 620}
 621
 622static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
 623					       const struct nvmem_cell_info *info,
 624					       struct nvmem_cell_entry *cell)
 625{
 626	int err;
 627
 628	err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
 629	if (err)
 630		return err;
 631
 632	cell->name = kstrdup_const(info->name, GFP_KERNEL);
 633	if (!cell->name)
 634		return -ENOMEM;
 635
 636	return 0;
 637}
 638
 639/**
 640 * nvmem_add_one_cell() - Add one cell information to an nvmem device
 641 *
 642 * @nvmem: nvmem device to add cells to.
 643 * @info: nvmem cell info to add to the device
 644 *
 645 * Return: 0 or negative error code on failure.
 646 */
 647int nvmem_add_one_cell(struct nvmem_device *nvmem,
 648		       const struct nvmem_cell_info *info)
 649{
 650	struct nvmem_cell_entry *cell;
 651	int rval;
 652
 653	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
 654	if (!cell)
 655		return -ENOMEM;
 656
 657	rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
 658	if (rval) {
 659		kfree(cell);
 660		return rval;
 661	}
 662
 663	nvmem_cell_entry_add(cell);
 664
 665	return 0;
 666}
 667EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
 668
 669/**
 670 * nvmem_add_cells() - Add cell information to an nvmem device
 671 *
 672 * @nvmem: nvmem device to add cells to.
 673 * @info: nvmem cell info to add to the device
 674 * @ncells: number of cells in info
 675 *
 676 * Return: 0 or negative error code on failure.
 677 */
 678static int nvmem_add_cells(struct nvmem_device *nvmem,
 679		    const struct nvmem_cell_info *info,
 680		    int ncells)
 681{
 682	int i, rval;
 683
 684	for (i = 0; i < ncells; i++) {
 685		rval = nvmem_add_one_cell(nvmem, &info[i]);
 686		if (rval)
 687			return rval;
 688	}
 689
 690	return 0;
 691}
 692
 693/**
 694 * nvmem_register_notifier() - Register a notifier block for nvmem events.
 695 *
 696 * @nb: notifier block to be called on nvmem events.
 697 *
 698 * Return: 0 on success, negative error number on failure.
 699 */
 700int nvmem_register_notifier(struct notifier_block *nb)
 701{
 702	return blocking_notifier_chain_register(&nvmem_notifier, nb);
 703}
 704EXPORT_SYMBOL_GPL(nvmem_register_notifier);
 705
 706/**
 707 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
 708 *
 709 * @nb: notifier block to be unregistered.
 710 *
 711 * Return: 0 on success, negative error number on failure.
 712 */
 713int nvmem_unregister_notifier(struct notifier_block *nb)
 714{
 715	return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
 716}
 717EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
 718
 719static struct nvmem_cell_entry *
 720nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
 721{
 722	struct nvmem_cell_entry *iter, *cell = NULL;
 723
 724	mutex_lock(&nvmem_mutex);
 725	list_for_each_entry(iter, &nvmem->cells, node) {
 726		if (strcmp(cell_id, iter->name) == 0) {
 727			cell = iter;
 728			break;
 729		}
 730	}
 731	mutex_unlock(&nvmem_mutex);
 732
 733	return cell;
 734}
 735
 736static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
 737{
 738	unsigned int cur = 0;
 739	const struct nvmem_keepout *keepout = nvmem->keepout;
 740	const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
 741
 742	while (keepout < keepoutend) {
 743		/* Ensure keepouts are sorted and don't overlap. */
 744		if (keepout->start < cur) {
 745			dev_err(&nvmem->dev,
 746				"Keepout regions aren't sorted or overlap.\n");
 747
 748			return -ERANGE;
 749		}
 750
 751		if (keepout->end < keepout->start) {
 752			dev_err(&nvmem->dev,
 753				"Invalid keepout region.\n");
 754
 755			return -EINVAL;
 756		}
 757
 758		/*
 759		 * Validate keepouts (and holes between) don't violate
 760		 * word_size constraints.
 761		 */
 762		if ((keepout->end - keepout->start < nvmem->word_size) ||
 763		    ((keepout->start != cur) &&
 764		     (keepout->start - cur < nvmem->word_size))) {
 765
 766			dev_err(&nvmem->dev,
 767				"Keepout regions violate word_size constraints.\n");
 768
 769			return -ERANGE;
 770		}
 771
 772		/* Validate keepouts don't violate stride (alignment). */
 773		if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
 774		    !IS_ALIGNED(keepout->end, nvmem->stride)) {
 775
 776			dev_err(&nvmem->dev,
 777				"Keepout regions violate stride.\n");
 778
 779			return -EINVAL;
 780		}
 781
 782		cur = keepout->end;
 783		keepout++;
 784	}
 785
 786	return 0;
 787}
 788
 789static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
 790{
 791	struct device *dev = &nvmem->dev;
 792	struct device_node *child;
 793	const __be32 *addr;
 794	int len, ret;
 795
 796	for_each_child_of_node(np, child) {
 797		struct nvmem_cell_info info = {0};
 798
 799		addr = of_get_property(child, "reg", &len);
 800		if (!addr)
 801			continue;
 802		if (len < 2 * sizeof(u32)) {
 803			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
 804			of_node_put(child);
 805			return -EINVAL;
 806		}
 807
 808		info.offset = be32_to_cpup(addr++);
 809		info.bytes = be32_to_cpup(addr);
 810		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
 811
 812		addr = of_get_property(child, "bits", &len);
 813		if (addr && len == (2 * sizeof(u32))) {
 814			info.bit_offset = be32_to_cpup(addr++);
 815			info.nbits = be32_to_cpup(addr);
 816			if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
 817			    info.nbits < 1 ||
 818			    info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
 819				dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
 820				of_node_put(child);
 821				return -EINVAL;
 822			}
 823		}
 824
 825		info.np = of_node_get(child);
 826
 827		if (nvmem->fixup_dt_cell_info)
 828			nvmem->fixup_dt_cell_info(nvmem, &info);
 829
 830		ret = nvmem_add_one_cell(nvmem, &info);
 831		kfree(info.name);
 832		if (ret) {
 833			of_node_put(child);
 834			of_node_put(info.np);
 835			return ret;
 836		}
 837	}
 838
 839	return 0;
 840}
 841
 842static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
 843{
 844	return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
 845}
 846
 847static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
 848{
 849	struct device_node *layout_np;
 850	int err = 0;
 851
 852	layout_np = of_nvmem_layout_get_container(nvmem);
 853	if (!layout_np)
 854		return 0;
 855
 856	if (of_device_is_compatible(layout_np, "fixed-layout"))
 857		err = nvmem_add_cells_from_dt(nvmem, layout_np);
 858
 859	of_node_put(layout_np);
 860
 861	return err;
 862}
 863
 864int nvmem_layout_register(struct nvmem_layout *layout)
 865{
 866	int ret;
 867
 868	if (!layout->add_cells)
 869		return -EINVAL;
 870
 871	/* Populate the cells */
 872	ret = layout->add_cells(layout);
 873	if (ret)
 874		return ret;
 875
 876#ifdef CONFIG_NVMEM_SYSFS
 877	ret = nvmem_populate_sysfs_cells(layout->nvmem);
 878	if (ret) {
 879		nvmem_device_remove_all_cells(layout->nvmem);
 880		return ret;
 881	}
 882#endif
 883
 884	return 0;
 885}
 886EXPORT_SYMBOL_GPL(nvmem_layout_register);
 887
 888void nvmem_layout_unregister(struct nvmem_layout *layout)
 889{
 890	/* Keep the API even with an empty stub in case we need it later */
 891}
 892EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
 893
 894/**
 895 * nvmem_register() - Register a nvmem device for given nvmem_config.
 896 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
 897 *
 898 * @config: nvmem device configuration with which nvmem device is created.
 899 *
 900 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
 901 * on success.
 902 */
 903
 904struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 905{
 906	struct nvmem_device *nvmem;
 907	int rval;
 908
 909	if (!config->dev)
 910		return ERR_PTR(-EINVAL);
 911
 912	if (!config->reg_read && !config->reg_write)
 913		return ERR_PTR(-EINVAL);
 914
 915	nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
 916	if (!nvmem)
 917		return ERR_PTR(-ENOMEM);
 918
 919	rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
 920	if (rval < 0) {
 921		kfree(nvmem);
 922		return ERR_PTR(rval);
 923	}
 924
 925	nvmem->id = rval;
 926
 927	nvmem->dev.type = &nvmem_provider_type;
 928	nvmem->dev.bus = &nvmem_bus_type;
 929	nvmem->dev.parent = config->dev;
 930
 931	device_initialize(&nvmem->dev);
 932
 933	if (!config->ignore_wp)
 934		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
 935						    GPIOD_OUT_HIGH);
 936	if (IS_ERR(nvmem->wp_gpio)) {
 937		rval = PTR_ERR(nvmem->wp_gpio);
 938		nvmem->wp_gpio = NULL;
 939		goto err_put_device;
 940	}
 941
 942	kref_init(&nvmem->refcnt);
 943	INIT_LIST_HEAD(&nvmem->cells);
 944	nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
 945
 946	nvmem->owner = config->owner;
 947	if (!nvmem->owner && config->dev->driver)
 948		nvmem->owner = config->dev->driver->owner;
 949	nvmem->stride = config->stride ?: 1;
 950	nvmem->word_size = config->word_size ?: 1;
 951	nvmem->size = config->size;
 952	nvmem->root_only = config->root_only;
 953	nvmem->priv = config->priv;
 954	nvmem->type = config->type;
 955	nvmem->reg_read = config->reg_read;
 956	nvmem->reg_write = config->reg_write;
 957	nvmem->keepout = config->keepout;
 958	nvmem->nkeepout = config->nkeepout;
 959	if (config->of_node)
 960		nvmem->dev.of_node = config->of_node;
 961	else
 962		nvmem->dev.of_node = config->dev->of_node;
 963
 964	switch (config->id) {
 965	case NVMEM_DEVID_NONE:
 966		rval = dev_set_name(&nvmem->dev, "%s", config->name);
 967		break;
 968	case NVMEM_DEVID_AUTO:
 969		rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
 970		break;
 971	default:
 972		rval = dev_set_name(&nvmem->dev, "%s%d",
 973			     config->name ? : "nvmem",
 974			     config->name ? config->id : nvmem->id);
 975		break;
 976	}
 977
 978	if (rval)
 979		goto err_put_device;
 980
 981	nvmem->read_only = device_property_present(config->dev, "read-only") ||
 982			   config->read_only || !nvmem->reg_write;
 983
 984#ifdef CONFIG_NVMEM_SYSFS
 985	nvmem->dev.groups = nvmem_dev_groups;
 986#endif
 987
 988	if (nvmem->nkeepout) {
 989		rval = nvmem_validate_keepouts(nvmem);
 990		if (rval)
 991			goto err_put_device;
 992	}
 993
 994	if (config->compat) {
 995		rval = nvmem_sysfs_setup_compat(nvmem, config);
 996		if (rval)
 997			goto err_put_device;
 998	}
 999
1000	if (config->cells) {
1001		rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
1002		if (rval)
1003			goto err_remove_cells;
1004	}
1005
1006	if (config->add_legacy_fixed_of_cells) {
1007		rval = nvmem_add_cells_from_legacy_of(nvmem);
1008		if (rval)
1009			goto err_remove_cells;
1010	}
1011
1012	rval = nvmem_add_cells_from_fixed_layout(nvmem);
1013	if (rval)
1014		goto err_remove_cells;
1015
1016	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1017
1018	rval = device_add(&nvmem->dev);
1019	if (rval)
1020		goto err_remove_cells;
1021
1022	rval = nvmem_populate_layout(nvmem);
1023	if (rval)
1024		goto err_remove_dev;
1025
1026#ifdef CONFIG_NVMEM_SYSFS
1027	rval = nvmem_populate_sysfs_cells(nvmem);
1028	if (rval)
1029		goto err_destroy_layout;
1030#endif
1031
1032	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1033
1034	return nvmem;
1035
1036#ifdef CONFIG_NVMEM_SYSFS
1037err_destroy_layout:
1038	nvmem_destroy_layout(nvmem);
1039#endif
1040err_remove_dev:
1041	device_del(&nvmem->dev);
1042err_remove_cells:
1043	nvmem_device_remove_all_cells(nvmem);
1044	if (config->compat)
1045		nvmem_sysfs_remove_compat(nvmem, config);
1046err_put_device:
1047	put_device(&nvmem->dev);
1048
1049	return ERR_PTR(rval);
1050}
1051EXPORT_SYMBOL_GPL(nvmem_register);
1052
1053static void nvmem_device_release(struct kref *kref)
1054{
1055	struct nvmem_device *nvmem;
1056
1057	nvmem = container_of(kref, struct nvmem_device, refcnt);
1058
1059	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1060
1061	if (nvmem->flags & FLAG_COMPAT)
1062		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1063
1064	nvmem_device_remove_all_cells(nvmem);
1065	nvmem_destroy_layout(nvmem);
1066	device_unregister(&nvmem->dev);
1067}
1068
1069/**
1070 * nvmem_unregister() - Unregister previously registered nvmem device
1071 *
1072 * @nvmem: Pointer to previously registered nvmem device.
1073 */
1074void nvmem_unregister(struct nvmem_device *nvmem)
1075{
1076	if (nvmem)
1077		kref_put(&nvmem->refcnt, nvmem_device_release);
1078}
1079EXPORT_SYMBOL_GPL(nvmem_unregister);
1080
1081static void devm_nvmem_unregister(void *nvmem)
1082{
1083	nvmem_unregister(nvmem);
1084}
1085
1086/**
1087 * devm_nvmem_register() - Register a managed nvmem device for given
1088 * nvmem_config.
1089 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1090 *
1091 * @dev: Device that uses the nvmem device.
1092 * @config: nvmem device configuration with which nvmem device is created.
1093 *
1094 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1095 * on success.
1096 */
1097struct nvmem_device *devm_nvmem_register(struct device *dev,
1098					 const struct nvmem_config *config)
1099{
1100	struct nvmem_device *nvmem;
1101	int ret;
1102
1103	nvmem = nvmem_register(config);
1104	if (IS_ERR(nvmem))
1105		return nvmem;
1106
1107	ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1108	if (ret)
1109		return ERR_PTR(ret);
1110
1111	return nvmem;
1112}
1113EXPORT_SYMBOL_GPL(devm_nvmem_register);
1114
1115static struct nvmem_device *__nvmem_device_get(void *data,
1116			int (*match)(struct device *dev, const void *data))
1117{
1118	struct nvmem_device *nvmem = NULL;
1119	struct device *dev;
1120
1121	mutex_lock(&nvmem_mutex);
1122	dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1123	if (dev)
1124		nvmem = to_nvmem_device(dev);
1125	mutex_unlock(&nvmem_mutex);
1126	if (!nvmem)
1127		return ERR_PTR(-EPROBE_DEFER);
1128
1129	if (!try_module_get(nvmem->owner)) {
1130		dev_err(&nvmem->dev,
1131			"could not increase module refcount for cell %s\n",
1132			nvmem_dev_name(nvmem));
1133
1134		put_device(&nvmem->dev);
1135		return ERR_PTR(-EINVAL);
1136	}
1137
1138	kref_get(&nvmem->refcnt);
1139
1140	return nvmem;
1141}
1142
1143static void __nvmem_device_put(struct nvmem_device *nvmem)
1144{
1145	put_device(&nvmem->dev);
1146	module_put(nvmem->owner);
1147	kref_put(&nvmem->refcnt, nvmem_device_release);
1148}
1149
1150#if IS_ENABLED(CONFIG_OF)
1151/**
1152 * of_nvmem_device_get() - Get nvmem device from a given id
1153 *
1154 * @np: Device tree node that uses the nvmem device.
1155 * @id: nvmem name from nvmem-names property.
1156 *
1157 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1158 * on success.
1159 */
1160struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1161{
1162
1163	struct device_node *nvmem_np;
1164	struct nvmem_device *nvmem;
1165	int index = 0;
1166
1167	if (id)
1168		index = of_property_match_string(np, "nvmem-names", id);
1169
1170	nvmem_np = of_parse_phandle(np, "nvmem", index);
1171	if (!nvmem_np)
1172		return ERR_PTR(-ENOENT);
1173
1174	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1175	of_node_put(nvmem_np);
1176	return nvmem;
1177}
1178EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1179#endif
1180
1181/**
1182 * nvmem_device_get() - Get nvmem device from a given id
1183 *
1184 * @dev: Device that uses the nvmem device.
1185 * @dev_name: name of the requested nvmem device.
1186 *
1187 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1188 * on success.
1189 */
1190struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1191{
1192	if (dev->of_node) { /* try dt first */
1193		struct nvmem_device *nvmem;
1194
1195		nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1196
1197		if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1198			return nvmem;
1199
1200	}
1201
1202	return __nvmem_device_get((void *)dev_name, device_match_name);
1203}
1204EXPORT_SYMBOL_GPL(nvmem_device_get);
1205
1206/**
1207 * nvmem_device_find() - Find nvmem device with matching function
1208 *
1209 * @data: Data to pass to match function
1210 * @match: Callback function to check device
1211 *
1212 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1213 * on success.
1214 */
1215struct nvmem_device *nvmem_device_find(void *data,
1216			int (*match)(struct device *dev, const void *data))
1217{
1218	return __nvmem_device_get(data, match);
1219}
1220EXPORT_SYMBOL_GPL(nvmem_device_find);
1221
1222static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1223{
1224	struct nvmem_device **nvmem = res;
1225
1226	if (WARN_ON(!nvmem || !*nvmem))
1227		return 0;
1228
1229	return *nvmem == data;
1230}
1231
1232static void devm_nvmem_device_release(struct device *dev, void *res)
1233{
1234	nvmem_device_put(*(struct nvmem_device **)res);
1235}
1236
1237/**
1238 * devm_nvmem_device_put() - put already got nvmem device
1239 *
1240 * @dev: Device that uses the nvmem device.
1241 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1242 * that needs to be released.
1243 */
1244void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1245{
1246	int ret;
1247
1248	ret = devres_release(dev, devm_nvmem_device_release,
1249			     devm_nvmem_device_match, nvmem);
1250
1251	WARN_ON(ret);
1252}
1253EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1254
1255/**
1256 * nvmem_device_put() - put already got nvmem device
1257 *
1258 * @nvmem: pointer to nvmem device that needs to be released.
1259 */
1260void nvmem_device_put(struct nvmem_device *nvmem)
1261{
1262	__nvmem_device_put(nvmem);
1263}
1264EXPORT_SYMBOL_GPL(nvmem_device_put);
1265
1266/**
1267 * devm_nvmem_device_get() - Get nvmem device of device from a given id
1268 *
1269 * @dev: Device that requests the nvmem device.
1270 * @id: name id for the requested nvmem device.
1271 *
1272 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1273 * on success.  The nvmem_device will be freed by the automatically once the
1274 * device is freed.
1275 */
1276struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1277{
1278	struct nvmem_device **ptr, *nvmem;
1279
1280	ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1281	if (!ptr)
1282		return ERR_PTR(-ENOMEM);
1283
1284	nvmem = nvmem_device_get(dev, id);
1285	if (!IS_ERR(nvmem)) {
1286		*ptr = nvmem;
1287		devres_add(dev, ptr);
1288	} else {
1289		devres_free(ptr);
1290	}
1291
1292	return nvmem;
1293}
1294EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1295
1296static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1297					    const char *id, int index)
1298{
1299	struct nvmem_cell *cell;
1300	const char *name = NULL;
1301
1302	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1303	if (!cell)
1304		return ERR_PTR(-ENOMEM);
1305
1306	if (id) {
1307		name = kstrdup_const(id, GFP_KERNEL);
1308		if (!name) {
1309			kfree(cell);
1310			return ERR_PTR(-ENOMEM);
1311		}
1312	}
1313
1314	cell->id = name;
1315	cell->entry = entry;
1316	cell->index = index;
1317
1318	return cell;
1319}
1320
1321static struct nvmem_cell *
1322nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1323{
1324	struct nvmem_cell_entry *cell_entry;
1325	struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1326	struct nvmem_cell_lookup *lookup;
1327	struct nvmem_device *nvmem;
1328	const char *dev_id;
1329
1330	if (!dev)
1331		return ERR_PTR(-EINVAL);
1332
1333	dev_id = dev_name(dev);
1334
1335	mutex_lock(&nvmem_lookup_mutex);
1336
1337	list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1338		if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1339		    (strcmp(lookup->con_id, con_id) == 0)) {
1340			/* This is the right entry. */
1341			nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1342						   device_match_name);
1343			if (IS_ERR(nvmem)) {
1344				/* Provider may not be registered yet. */
1345				cell = ERR_CAST(nvmem);
1346				break;
1347			}
1348
1349			cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1350								   lookup->cell_name);
1351			if (!cell_entry) {
1352				__nvmem_device_put(nvmem);
1353				cell = ERR_PTR(-ENOENT);
1354			} else {
1355				cell = nvmem_create_cell(cell_entry, con_id, 0);
1356				if (IS_ERR(cell))
1357					__nvmem_device_put(nvmem);
1358			}
1359			break;
1360		}
1361	}
1362
1363	mutex_unlock(&nvmem_lookup_mutex);
1364	return cell;
1365}
1366
1367static void nvmem_layout_module_put(struct nvmem_device *nvmem)
1368{
1369	if (nvmem->layout && nvmem->layout->dev.driver)
1370		module_put(nvmem->layout->dev.driver->owner);
1371}
1372
1373#if IS_ENABLED(CONFIG_OF)
1374static struct nvmem_cell_entry *
1375nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1376{
1377	struct nvmem_cell_entry *iter, *cell = NULL;
1378
1379	mutex_lock(&nvmem_mutex);
1380	list_for_each_entry(iter, &nvmem->cells, node) {
1381		if (np == iter->np) {
1382			cell = iter;
1383			break;
1384		}
1385	}
1386	mutex_unlock(&nvmem_mutex);
1387
1388	return cell;
1389}
1390
1391static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
1392{
1393	if (!nvmem->layout)
1394		return 0;
1395
1396	if (!nvmem->layout->dev.driver ||
1397	    !try_module_get(nvmem->layout->dev.driver->owner))
1398		return -EPROBE_DEFER;
1399
1400	return 0;
1401}
1402
1403/**
1404 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1405 *
1406 * @np: Device tree node that uses the nvmem cell.
1407 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1408 *      for the cell at index 0 (the lone cell with no accompanying
1409 *      nvmem-cell-names property).
1410 *
1411 * Return: Will be an ERR_PTR() on error or a valid pointer
1412 * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1413 * nvmem_cell_put().
1414 */
1415struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1416{
1417	struct device_node *cell_np, *nvmem_np;
1418	struct nvmem_device *nvmem;
1419	struct nvmem_cell_entry *cell_entry;
1420	struct nvmem_cell *cell;
1421	struct of_phandle_args cell_spec;
1422	int index = 0;
1423	int cell_index = 0;
1424	int ret;
1425
1426	/* if cell name exists, find index to the name */
1427	if (id)
1428		index = of_property_match_string(np, "nvmem-cell-names", id);
1429
1430	ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1431						  "#nvmem-cell-cells",
1432						  index, &cell_spec);
1433	if (ret)
1434		return ERR_PTR(-ENOENT);
1435
1436	if (cell_spec.args_count > 1)
1437		return ERR_PTR(-EINVAL);
1438
1439	cell_np = cell_spec.np;
1440	if (cell_spec.args_count)
1441		cell_index = cell_spec.args[0];
1442
1443	nvmem_np = of_get_parent(cell_np);
1444	if (!nvmem_np) {
1445		of_node_put(cell_np);
1446		return ERR_PTR(-EINVAL);
1447	}
1448
1449	/* nvmem layouts produce cells within the nvmem-layout container */
1450	if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1451		nvmem_np = of_get_next_parent(nvmem_np);
1452		if (!nvmem_np) {
1453			of_node_put(cell_np);
1454			return ERR_PTR(-EINVAL);
1455		}
1456	}
1457
1458	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1459	of_node_put(nvmem_np);
1460	if (IS_ERR(nvmem)) {
1461		of_node_put(cell_np);
1462		return ERR_CAST(nvmem);
1463	}
1464
1465	ret = nvmem_layout_module_get_optional(nvmem);
1466	if (ret) {
1467		of_node_put(cell_np);
1468		__nvmem_device_put(nvmem);
1469		return ERR_PTR(ret);
1470	}
1471
1472	cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1473	of_node_put(cell_np);
1474	if (!cell_entry) {
1475		__nvmem_device_put(nvmem);
1476		nvmem_layout_module_put(nvmem);
1477		if (nvmem->layout)
1478			return ERR_PTR(-EPROBE_DEFER);
1479		else
1480			return ERR_PTR(-ENOENT);
1481	}
1482
1483	cell = nvmem_create_cell(cell_entry, id, cell_index);
1484	if (IS_ERR(cell)) {
1485		__nvmem_device_put(nvmem);
1486		nvmem_layout_module_put(nvmem);
1487	}
1488
1489	return cell;
1490}
1491EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1492#endif
1493
1494/**
1495 * nvmem_cell_get() - Get nvmem cell of device from a given cell name
1496 *
1497 * @dev: Device that requests the nvmem cell.
1498 * @id: nvmem cell name to get (this corresponds with the name from the
1499 *      nvmem-cell-names property for DT systems and with the con_id from
1500 *      the lookup entry for non-DT systems).
1501 *
1502 * Return: Will be an ERR_PTR() on error or a valid pointer
1503 * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1504 * nvmem_cell_put().
1505 */
1506struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1507{
1508	struct nvmem_cell *cell;
1509
1510	if (dev->of_node) { /* try dt first */
1511		cell = of_nvmem_cell_get(dev->of_node, id);
1512		if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1513			return cell;
1514	}
1515
1516	/* NULL cell id only allowed for device tree; invalid otherwise */
1517	if (!id)
1518		return ERR_PTR(-EINVAL);
1519
1520	return nvmem_cell_get_from_lookup(dev, id);
1521}
1522EXPORT_SYMBOL_GPL(nvmem_cell_get);
1523
1524static void devm_nvmem_cell_release(struct device *dev, void *res)
1525{
1526	nvmem_cell_put(*(struct nvmem_cell **)res);
1527}
1528
1529/**
1530 * devm_nvmem_cell_get() - Get nvmem cell of device from a given id
1531 *
1532 * @dev: Device that requests the nvmem cell.
1533 * @id: nvmem cell name id to get.
1534 *
1535 * Return: Will be an ERR_PTR() on error or a valid pointer
1536 * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1537 * automatically once the device is freed.
1538 */
1539struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1540{
1541	struct nvmem_cell **ptr, *cell;
1542
1543	ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1544	if (!ptr)
1545		return ERR_PTR(-ENOMEM);
1546
1547	cell = nvmem_cell_get(dev, id);
1548	if (!IS_ERR(cell)) {
1549		*ptr = cell;
1550		devres_add(dev, ptr);
1551	} else {
1552		devres_free(ptr);
1553	}
1554
1555	return cell;
1556}
1557EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1558
1559static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1560{
1561	struct nvmem_cell **c = res;
1562
1563	if (WARN_ON(!c || !*c))
1564		return 0;
1565
1566	return *c == data;
1567}
1568
1569/**
1570 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1571 * from devm_nvmem_cell_get.
1572 *
1573 * @dev: Device that requests the nvmem cell.
1574 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1575 */
1576void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1577{
1578	int ret;
1579
1580	ret = devres_release(dev, devm_nvmem_cell_release,
1581				devm_nvmem_cell_match, cell);
1582
1583	WARN_ON(ret);
1584}
1585EXPORT_SYMBOL(devm_nvmem_cell_put);
1586
1587/**
1588 * nvmem_cell_put() - Release previously allocated nvmem cell.
1589 *
1590 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1591 */
1592void nvmem_cell_put(struct nvmem_cell *cell)
1593{
1594	struct nvmem_device *nvmem = cell->entry->nvmem;
1595
1596	if (cell->id)
1597		kfree_const(cell->id);
1598
1599	kfree(cell);
1600	__nvmem_device_put(nvmem);
1601	nvmem_layout_module_put(nvmem);
1602}
1603EXPORT_SYMBOL_GPL(nvmem_cell_put);
1604
1605static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1606{
1607	u8 *p, *b;
1608	int i, extra, bytes_offset;
1609	int bit_offset = cell->bit_offset;
1610
1611	p = b = buf;
1612
1613	bytes_offset = bit_offset / BITS_PER_BYTE;
1614	b += bytes_offset;
1615	bit_offset %= BITS_PER_BYTE;
1616
1617	if (bit_offset % BITS_PER_BYTE) {
1618		/* First shift */
1619		*p = *b++ >> bit_offset;
1620
1621		/* setup rest of the bytes if any */
1622		for (i = 1; i < cell->bytes; i++) {
1623			/* Get bits from next byte and shift them towards msb */
1624			*p++ |= *b << (BITS_PER_BYTE - bit_offset);
1625
1626			*p = *b++ >> bit_offset;
1627		}
1628	} else if (p != b) {
1629		memmove(p, b, cell->bytes - bytes_offset);
1630		p += cell->bytes - 1;
1631	} else {
1632		/* point to the msb */
1633		p += cell->bytes - 1;
1634	}
1635
1636	/* result fits in less bytes */
1637	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1638	while (--extra >= 0)
1639		*p-- = 0;
1640
1641	/* clear msb bits if any leftover in the last byte */
1642	if (cell->nbits % BITS_PER_BYTE)
1643		*p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1644}
1645
1646static int __nvmem_cell_read(struct nvmem_device *nvmem,
1647			     struct nvmem_cell_entry *cell,
1648			     void *buf, size_t *len, const char *id, int index)
1649{
1650	int rc;
1651
1652	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1653
1654	if (rc)
1655		return rc;
1656
1657	/* shift bits in-place */
1658	if (cell->bit_offset || cell->nbits)
1659		nvmem_shift_read_buffer_in_place(cell, buf);
1660
1661	if (cell->read_post_process) {
1662		rc = cell->read_post_process(cell->priv, id, index,
1663					     cell->offset, buf, cell->raw_len);
1664		if (rc)
1665			return rc;
1666	}
1667
1668	if (len)
1669		*len = cell->bytes;
1670
1671	return 0;
1672}
1673
1674/**
1675 * nvmem_cell_read() - Read a given nvmem cell
1676 *
1677 * @cell: nvmem cell to be read.
1678 * @len: pointer to length of cell which will be populated on successful read;
1679 *	 can be NULL.
1680 *
1681 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1682 * buffer should be freed by the consumer with a kfree().
1683 */
1684void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1685{
1686	struct nvmem_cell_entry *entry = cell->entry;
1687	struct nvmem_device *nvmem = entry->nvmem;
1688	u8 *buf;
1689	int rc;
1690
1691	if (!nvmem)
1692		return ERR_PTR(-EINVAL);
1693
1694	buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1695	if (!buf)
1696		return ERR_PTR(-ENOMEM);
1697
1698	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1699	if (rc) {
1700		kfree(buf);
1701		return ERR_PTR(rc);
1702	}
1703
1704	return buf;
1705}
1706EXPORT_SYMBOL_GPL(nvmem_cell_read);
1707
1708static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1709					     u8 *_buf, int len)
1710{
1711	struct nvmem_device *nvmem = cell->nvmem;
1712	int i, rc, nbits, bit_offset = cell->bit_offset;
1713	u8 v, *p, *buf, *b, pbyte, pbits;
1714
1715	nbits = cell->nbits;
1716	buf = kzalloc(cell->bytes, GFP_KERNEL);
1717	if (!buf)
1718		return ERR_PTR(-ENOMEM);
1719
1720	memcpy(buf, _buf, len);
1721	p = b = buf;
1722
1723	if (bit_offset) {
1724		pbyte = *b;
1725		*b <<= bit_offset;
1726
1727		/* setup the first byte with lsb bits from nvmem */
1728		rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1729		if (rc)
1730			goto err;
1731		*b++ |= GENMASK(bit_offset - 1, 0) & v;
1732
1733		/* setup rest of the byte if any */
1734		for (i = 1; i < cell->bytes; i++) {
1735			/* Get last byte bits and shift them towards lsb */
1736			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1737			pbyte = *b;
1738			p = b;
1739			*b <<= bit_offset;
1740			*b++ |= pbits;
1741		}
1742	}
1743
1744	/* if it's not end on byte boundary */
1745	if ((nbits + bit_offset) % BITS_PER_BYTE) {
1746		/* setup the last byte with msb bits from nvmem */
1747		rc = nvmem_reg_read(nvmem,
1748				    cell->offset + cell->bytes - 1, &v, 1);
1749		if (rc)
1750			goto err;
1751		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1752
1753	}
1754
1755	return buf;
1756err:
1757	kfree(buf);
1758	return ERR_PTR(rc);
1759}
1760
1761static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1762{
1763	struct nvmem_device *nvmem = cell->nvmem;
1764	int rc;
1765
1766	if (!nvmem || nvmem->read_only ||
1767	    (cell->bit_offset == 0 && len != cell->bytes))
1768		return -EINVAL;
1769
1770	/*
1771	 * Any cells which have a read_post_process hook are read-only because
1772	 * we cannot reverse the operation and it might affect other cells,
1773	 * too.
1774	 */
1775	if (cell->read_post_process)
1776		return -EINVAL;
1777
1778	if (cell->bit_offset || cell->nbits) {
1779		if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
1780			return -EINVAL;
1781		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1782		if (IS_ERR(buf))
1783			return PTR_ERR(buf);
1784	}
1785
1786	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1787
1788	/* free the tmp buffer */
1789	if (cell->bit_offset || cell->nbits)
1790		kfree(buf);
1791
1792	if (rc)
1793		return rc;
1794
1795	return len;
1796}
1797
1798/**
1799 * nvmem_cell_write() - Write to a given nvmem cell
1800 *
1801 * @cell: nvmem cell to be written.
1802 * @buf: Buffer to be written.
1803 * @len: length of buffer to be written to nvmem cell.
1804 *
1805 * Return: length of bytes written or negative on failure.
1806 */
1807int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1808{
1809	return __nvmem_cell_entry_write(cell->entry, buf, len);
1810}
1811
1812EXPORT_SYMBOL_GPL(nvmem_cell_write);
1813
1814static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1815				  void *val, size_t count)
1816{
1817	struct nvmem_cell *cell;
1818	void *buf;
1819	size_t len;
1820
1821	cell = nvmem_cell_get(dev, cell_id);
1822	if (IS_ERR(cell))
1823		return PTR_ERR(cell);
1824
1825	buf = nvmem_cell_read(cell, &len);
1826	if (IS_ERR(buf)) {
1827		nvmem_cell_put(cell);
1828		return PTR_ERR(buf);
1829	}
1830	if (len != count) {
1831		kfree(buf);
1832		nvmem_cell_put(cell);
1833		return -EINVAL;
1834	}
1835	memcpy(val, buf, count);
1836	kfree(buf);
1837	nvmem_cell_put(cell);
1838
1839	return 0;
1840}
1841
1842/**
1843 * nvmem_cell_read_u8() - Read a cell value as a u8
1844 *
1845 * @dev: Device that requests the nvmem cell.
1846 * @cell_id: Name of nvmem cell to read.
1847 * @val: pointer to output value.
1848 *
1849 * Return: 0 on success or negative errno.
1850 */
1851int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1852{
1853	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1854}
1855EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1856
1857/**
1858 * nvmem_cell_read_u16() - Read a cell value as a u16
1859 *
1860 * @dev: Device that requests the nvmem cell.
1861 * @cell_id: Name of nvmem cell to read.
1862 * @val: pointer to output value.
1863 *
1864 * Return: 0 on success or negative errno.
1865 */
1866int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1867{
1868	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1869}
1870EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1871
1872/**
1873 * nvmem_cell_read_u32() - Read a cell value as a u32
1874 *
1875 * @dev: Device that requests the nvmem cell.
1876 * @cell_id: Name of nvmem cell to read.
1877 * @val: pointer to output value.
1878 *
1879 * Return: 0 on success or negative errno.
1880 */
1881int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1882{
1883	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1884}
1885EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1886
1887/**
1888 * nvmem_cell_read_u64() - Read a cell value as a u64
1889 *
1890 * @dev: Device that requests the nvmem cell.
1891 * @cell_id: Name of nvmem cell to read.
1892 * @val: pointer to output value.
1893 *
1894 * Return: 0 on success or negative errno.
1895 */
1896int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1897{
1898	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1899}
1900EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1901
1902static const void *nvmem_cell_read_variable_common(struct device *dev,
1903						   const char *cell_id,
1904						   size_t max_len, size_t *len)
1905{
1906	struct nvmem_cell *cell;
1907	int nbits;
1908	void *buf;
1909
1910	cell = nvmem_cell_get(dev, cell_id);
1911	if (IS_ERR(cell))
1912		return cell;
1913
1914	nbits = cell->entry->nbits;
1915	buf = nvmem_cell_read(cell, len);
1916	nvmem_cell_put(cell);
1917	if (IS_ERR(buf))
1918		return buf;
1919
1920	/*
1921	 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1922	 * the length of the real data. Throw away the extra junk.
1923	 */
1924	if (nbits)
1925		*len = DIV_ROUND_UP(nbits, 8);
1926
1927	if (*len > max_len) {
1928		kfree(buf);
1929		return ERR_PTR(-ERANGE);
1930	}
1931
1932	return buf;
1933}
1934
1935/**
1936 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1937 *
1938 * @dev: Device that requests the nvmem cell.
1939 * @cell_id: Name of nvmem cell to read.
1940 * @val: pointer to output value.
1941 *
1942 * Return: 0 on success or negative errno.
1943 */
1944int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1945				    u32 *val)
1946{
1947	size_t len;
1948	const u8 *buf;
1949	int i;
1950
1951	buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1952	if (IS_ERR(buf))
1953		return PTR_ERR(buf);
1954
1955	/* Copy w/ implicit endian conversion */
1956	*val = 0;
1957	for (i = 0; i < len; i++)
1958		*val |= buf[i] << (8 * i);
1959
1960	kfree(buf);
1961
1962	return 0;
1963}
1964EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1965
1966/**
1967 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1968 *
1969 * @dev: Device that requests the nvmem cell.
1970 * @cell_id: Name of nvmem cell to read.
1971 * @val: pointer to output value.
1972 *
1973 * Return: 0 on success or negative errno.
1974 */
1975int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1976				    u64 *val)
1977{
1978	size_t len;
1979	const u8 *buf;
1980	int i;
1981
1982	buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1983	if (IS_ERR(buf))
1984		return PTR_ERR(buf);
1985
1986	/* Copy w/ implicit endian conversion */
1987	*val = 0;
1988	for (i = 0; i < len; i++)
1989		*val |= (uint64_t)buf[i] << (8 * i);
1990
1991	kfree(buf);
1992
1993	return 0;
1994}
1995EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1996
1997/**
1998 * nvmem_device_cell_read() - Read a given nvmem device and cell
1999 *
2000 * @nvmem: nvmem device to read from.
2001 * @info: nvmem cell info to be read.
2002 * @buf: buffer pointer which will be populated on successful read.
2003 *
2004 * Return: length of successful bytes read on success and negative
2005 * error code on error.
2006 */
2007ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
2008			   struct nvmem_cell_info *info, void *buf)
2009{
2010	struct nvmem_cell_entry cell;
2011	int rc;
2012	ssize_t len;
2013
2014	if (!nvmem)
2015		return -EINVAL;
2016
2017	rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2018	if (rc)
2019		return rc;
2020
2021	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
2022	if (rc)
2023		return rc;
2024
2025	return len;
2026}
2027EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
2028
2029/**
2030 * nvmem_device_cell_write() - Write cell to a given nvmem device
2031 *
2032 * @nvmem: nvmem device to be written to.
2033 * @info: nvmem cell info to be written.
2034 * @buf: buffer to be written to cell.
2035 *
2036 * Return: length of bytes written or negative error code on failure.
2037 */
2038int nvmem_device_cell_write(struct nvmem_device *nvmem,
2039			    struct nvmem_cell_info *info, void *buf)
2040{
2041	struct nvmem_cell_entry cell;
2042	int rc;
2043
2044	if (!nvmem)
2045		return -EINVAL;
2046
2047	rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2048	if (rc)
2049		return rc;
2050
2051	return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
2052}
2053EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
2054
2055/**
2056 * nvmem_device_read() - Read from a given nvmem device
2057 *
2058 * @nvmem: nvmem device to read from.
2059 * @offset: offset in nvmem device.
2060 * @bytes: number of bytes to read.
2061 * @buf: buffer pointer which will be populated on successful read.
2062 *
2063 * Return: length of successful bytes read on success and negative
2064 * error code on error.
2065 */
2066int nvmem_device_read(struct nvmem_device *nvmem,
2067		      unsigned int offset,
2068		      size_t bytes, void *buf)
2069{
2070	int rc;
2071
2072	if (!nvmem)
2073		return -EINVAL;
2074
2075	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2076
2077	if (rc)
2078		return rc;
2079
2080	return bytes;
2081}
2082EXPORT_SYMBOL_GPL(nvmem_device_read);
2083
2084/**
2085 * nvmem_device_write() - Write cell to a given nvmem device
2086 *
2087 * @nvmem: nvmem device to be written to.
2088 * @offset: offset in nvmem device.
2089 * @bytes: number of bytes to write.
2090 * @buf: buffer to be written.
2091 *
2092 * Return: length of bytes written or negative error code on failure.
2093 */
2094int nvmem_device_write(struct nvmem_device *nvmem,
2095		       unsigned int offset,
2096		       size_t bytes, void *buf)
2097{
2098	int rc;
2099
2100	if (!nvmem)
2101		return -EINVAL;
2102
2103	rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2104
2105	if (rc)
2106		return rc;
2107
2108
2109	return bytes;
2110}
2111EXPORT_SYMBOL_GPL(nvmem_device_write);
2112
2113/**
2114 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2115 *
2116 * @entries: array of cell lookup entries
2117 * @nentries: number of cell lookup entries in the array
2118 */
2119void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2120{
2121	int i;
2122
2123	mutex_lock(&nvmem_lookup_mutex);
2124	for (i = 0; i < nentries; i++)
2125		list_add_tail(&entries[i].node, &nvmem_lookup_list);
2126	mutex_unlock(&nvmem_lookup_mutex);
2127}
2128EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2129
2130/**
2131 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2132 *                            entries
2133 *
2134 * @entries: array of cell lookup entries
2135 * @nentries: number of cell lookup entries in the array
2136 */
2137void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2138{
2139	int i;
2140
2141	mutex_lock(&nvmem_lookup_mutex);
2142	for (i = 0; i < nentries; i++)
2143		list_del(&entries[i].node);
2144	mutex_unlock(&nvmem_lookup_mutex);
2145}
2146EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2147
2148/**
2149 * nvmem_dev_name() - Get the name of a given nvmem device.
2150 *
2151 * @nvmem: nvmem device.
2152 *
2153 * Return: name of the nvmem device.
2154 */
2155const char *nvmem_dev_name(struct nvmem_device *nvmem)
2156{
2157	return dev_name(&nvmem->dev);
2158}
2159EXPORT_SYMBOL_GPL(nvmem_dev_name);
2160
2161/**
2162 * nvmem_dev_size() - Get the size of a given nvmem device.
2163 *
2164 * @nvmem: nvmem device.
2165 *
2166 * Return: size of the nvmem device.
2167 */
2168size_t nvmem_dev_size(struct nvmem_device *nvmem)
2169{
2170	return nvmem->size;
2171}
2172EXPORT_SYMBOL_GPL(nvmem_dev_size);
2173
2174static int __init nvmem_init(void)
2175{
2176	int ret;
2177
2178	ret = bus_register(&nvmem_bus_type);
2179	if (ret)
2180		return ret;
2181
2182	ret = nvmem_layout_bus_register();
2183	if (ret)
2184		bus_unregister(&nvmem_bus_type);
2185
2186	return ret;
2187}
2188
2189static void __exit nvmem_exit(void)
2190{
2191	nvmem_layout_bus_unregister();
2192	bus_unregister(&nvmem_bus_type);
2193}
2194
2195subsys_initcall(nvmem_init);
2196module_exit(nvmem_exit);
2197
2198MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
2199MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
2200MODULE_DESCRIPTION("nvmem Driver Core");
~/ Read-only file preview.