xref: /linux/drivers/nvmem/core.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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)
27 struct 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 
41 struct nvmem_cell {
42 	struct nvmem_cell_entry *entry;
43 	const char		*id;
44 	int			index;
45 };
46 
47 static DEFINE_MUTEX(nvmem_mutex);
48 static DEFINE_IDA(nvmem_ida);
49 
50 static DEFINE_MUTEX(nvmem_lookup_mutex);
51 static LIST_HEAD(nvmem_lookup_list);
52 
53 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
54 
55 static 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 
64 static 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 
79 static 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 
143 static 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 
152 static 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
162 static 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
171 static struct lock_class_key eeprom_lock_key;
172 #endif
173 
174 static 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 
182 static DEVICE_ATTR_RO(type);
183 
184 static 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 
192 static 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 
204 static DEVICE_ATTR_RW(force_ro);
205 
206 static struct attribute *nvmem_attrs[] = {
207 	&dev_attr_force_ro.attr,
208 	&dev_attr_type.attr,
209 	NULL,
210 };
211 
212 static 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 
245 static 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 
278 static 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 
297 static 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 
307 static 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 
317 static 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 
336 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
337 					    const char *id, int index);
338 
339 static 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 
366 destroy_cell:
367 	kfree_const(cell->id);
368 	kfree(cell);
369 
370 	return read_len;
371 }
372 
373 /* default read/write permissions */
374 static 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 
383 static const struct bin_attribute *const nvmem_bin_attributes[] = {
384 	&bin_attr_rw_nvmem,
385 	NULL,
386 };
387 
388 static 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 
396 static const struct attribute_group *nvmem_dev_groups[] = {
397 	&nvmem_bin_group,
398 	NULL,
399 };
400 
401 static 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  */
414 static 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 
448 static 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 
455 static 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 
514 unlock_mutex:
515 	mutex_unlock(&nvmem_mutex);
516 
517 	return ret;
518 }
519 
520 #else /* CONFIG_NVMEM_SYSFS */
521 
522 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
523 				    const struct nvmem_config *config)
524 {
525 	return -ENOSYS;
526 }
527 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
528 				      const struct nvmem_config *config)
529 {
530 }
531 
532 #endif /* CONFIG_NVMEM_SYSFS */
533 
534 static 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 
543 static const struct device_type nvmem_provider_type = {
544 	.release	= nvmem_release,
545 };
546 
547 static const struct bus_type nvmem_bus_type = {
548 	.name		= "nvmem",
549 };
550 
551 static 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 
562 static 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 
570 static 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 
578 static 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 
622 static 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  */
647 int 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_obj(*cell);
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 }
667 EXPORT_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  */
678 static 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  */
700 int nvmem_register_notifier(struct notifier_block *nb)
701 {
702 	return blocking_notifier_chain_register(&nvmem_notifier, nb);
703 }
704 EXPORT_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  */
713 int nvmem_unregister_notifier(struct notifier_block *nb)
714 {
715 	return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
716 }
717 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
718 
719 static struct nvmem_cell_entry *
720 nvmem_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 
736 static 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 
789 int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
790 {
791 	struct device *dev = &nvmem->dev;
792 	const __be32 *addr;
793 	int len, ret;
794 
795 	for_each_child_of_node_scoped(np, child) {
796 		struct nvmem_cell_info info = {0};
797 
798 		addr = of_get_property(child, "reg", &len);
799 		if (!addr)
800 			continue;
801 		if (len < 2 * sizeof(u32)) {
802 			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
803 			return -EINVAL;
804 		}
805 
806 		info.offset = be32_to_cpup(addr++);
807 		info.bytes = be32_to_cpup(addr);
808 		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
809 
810 		addr = of_get_property(child, "bits", &len);
811 		if (addr && len == (2 * sizeof(u32))) {
812 			info.bit_offset = be32_to_cpup(addr++);
813 			info.nbits = be32_to_cpup(addr);
814 			if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
815 			    info.nbits < 1 ||
816 			    info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
817 				dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
818 				return -EINVAL;
819 			}
820 		}
821 
822 		info.np = of_node_get(child);
823 
824 		if (nvmem->fixup_dt_cell_info)
825 			nvmem->fixup_dt_cell_info(nvmem, &info);
826 
827 		ret = nvmem_add_one_cell(nvmem, &info);
828 		kfree(info.name);
829 		if (ret) {
830 			of_node_put(info.np);
831 			return ret;
832 		}
833 	}
834 
835 	return 0;
836 }
837 EXPORT_SYMBOL_GPL(nvmem_add_cells_from_dt);
838 
839 static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
840 {
841 	return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
842 }
843 
844 int nvmem_layout_register(struct nvmem_layout *layout)
845 {
846 	int ret;
847 
848 	if (!layout->add_cells)
849 		return -EINVAL;
850 
851 	/* Populate the cells */
852 	ret = layout->add_cells(layout);
853 	if (ret)
854 		return ret;
855 
856 #ifdef CONFIG_NVMEM_SYSFS
857 	ret = nvmem_populate_sysfs_cells(layout->nvmem);
858 	if (ret) {
859 		nvmem_device_remove_all_cells(layout->nvmem);
860 		return ret;
861 	}
862 #endif
863 
864 	return 0;
865 }
866 EXPORT_SYMBOL_GPL(nvmem_layout_register);
867 
868 void nvmem_layout_unregister(struct nvmem_layout *layout)
869 {
870 	/* Keep the API even with an empty stub in case we need it later */
871 }
872 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
873 
874 /**
875  * nvmem_register() - Register a nvmem device for given nvmem_config.
876  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
877  *
878  * @config: nvmem device configuration with which nvmem device is created.
879  *
880  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
881  * on success.
882  */
883 
884 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
885 {
886 	struct nvmem_device *nvmem;
887 	int rval;
888 
889 	if (!config->dev)
890 		return ERR_PTR(-EINVAL);
891 
892 	if (!config->reg_read && !config->reg_write)
893 		return ERR_PTR(-EINVAL);
894 
895 	nvmem = kzalloc_obj(*nvmem);
896 	if (!nvmem)
897 		return ERR_PTR(-ENOMEM);
898 
899 	rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
900 	if (rval < 0) {
901 		kfree(nvmem);
902 		return ERR_PTR(rval);
903 	}
904 
905 	nvmem->id = rval;
906 
907 	nvmem->dev.type = &nvmem_provider_type;
908 	nvmem->dev.bus = &nvmem_bus_type;
909 	nvmem->dev.parent = config->dev;
910 
911 	device_initialize(&nvmem->dev);
912 
913 	if (!config->ignore_wp)
914 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
915 						    GPIOD_OUT_HIGH);
916 	if (IS_ERR(nvmem->wp_gpio)) {
917 		rval = PTR_ERR(nvmem->wp_gpio);
918 		nvmem->wp_gpio = NULL;
919 		goto err_put_device;
920 	}
921 
922 	kref_init(&nvmem->refcnt);
923 	INIT_LIST_HEAD(&nvmem->cells);
924 	nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
925 
926 	nvmem->owner = config->owner;
927 	if (!nvmem->owner && config->dev->driver)
928 		nvmem->owner = config->dev->driver->owner;
929 	nvmem->stride = config->stride ?: 1;
930 	nvmem->word_size = config->word_size ?: 1;
931 	nvmem->size = config->size;
932 	nvmem->root_only = config->root_only;
933 	nvmem->priv = config->priv;
934 	nvmem->type = config->type;
935 	nvmem->reg_read = config->reg_read;
936 	nvmem->reg_write = config->reg_write;
937 	nvmem->keepout = config->keepout;
938 	nvmem->nkeepout = config->nkeepout;
939 	if (config->of_node)
940 		nvmem->dev.of_node = config->of_node;
941 	else
942 		nvmem->dev.of_node = config->dev->of_node;
943 
944 	switch (config->id) {
945 	case NVMEM_DEVID_NONE:
946 		rval = dev_set_name(&nvmem->dev, "%s", config->name);
947 		break;
948 	case NVMEM_DEVID_AUTO:
949 		rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
950 		break;
951 	default:
952 		rval = dev_set_name(&nvmem->dev, "%s%d",
953 			     config->name ? : "nvmem",
954 			     config->name ? config->id : nvmem->id);
955 		break;
956 	}
957 
958 	if (rval)
959 		goto err_put_device;
960 
961 	nvmem->read_only = device_property_present(config->dev, "read-only") ||
962 			   config->read_only || !nvmem->reg_write;
963 
964 #ifdef CONFIG_NVMEM_SYSFS
965 	nvmem->dev.groups = nvmem_dev_groups;
966 #endif
967 
968 	if (nvmem->nkeepout) {
969 		rval = nvmem_validate_keepouts(nvmem);
970 		if (rval)
971 			goto err_put_device;
972 	}
973 
974 	if (config->compat) {
975 		rval = nvmem_sysfs_setup_compat(nvmem, config);
976 		if (rval)
977 			goto err_put_device;
978 	}
979 
980 	if (config->cells) {
981 		rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
982 		if (rval)
983 			goto err_remove_cells;
984 	}
985 
986 	if (config->add_legacy_fixed_of_cells) {
987 		rval = nvmem_add_cells_from_legacy_of(nvmem);
988 		if (rval)
989 			goto err_remove_cells;
990 	}
991 
992 	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
993 
994 	rval = device_add(&nvmem->dev);
995 	if (rval)
996 		goto err_remove_cells;
997 
998 	rval = nvmem_populate_layout(nvmem);
999 	if (rval)
1000 		goto err_remove_dev;
1001 
1002 #ifdef CONFIG_NVMEM_SYSFS
1003 	rval = nvmem_populate_sysfs_cells(nvmem);
1004 	if (rval)
1005 		goto err_destroy_layout;
1006 #endif
1007 
1008 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1009 
1010 	return nvmem;
1011 
1012 #ifdef CONFIG_NVMEM_SYSFS
1013 err_destroy_layout:
1014 	nvmem_destroy_layout(nvmem);
1015 #endif
1016 err_remove_dev:
1017 	device_del(&nvmem->dev);
1018 err_remove_cells:
1019 	nvmem_device_remove_all_cells(nvmem);
1020 	if (config->compat)
1021 		nvmem_sysfs_remove_compat(nvmem, config);
1022 err_put_device:
1023 	put_device(&nvmem->dev);
1024 
1025 	return ERR_PTR(rval);
1026 }
1027 EXPORT_SYMBOL_GPL(nvmem_register);
1028 
1029 static void nvmem_device_release(struct kref *kref)
1030 {
1031 	struct nvmem_device *nvmem;
1032 
1033 	nvmem = container_of(kref, struct nvmem_device, refcnt);
1034 
1035 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1036 
1037 	if (nvmem->flags & FLAG_COMPAT)
1038 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1039 
1040 	nvmem_device_remove_all_cells(nvmem);
1041 	nvmem_destroy_layout(nvmem);
1042 	device_unregister(&nvmem->dev);
1043 }
1044 
1045 /**
1046  * nvmem_unregister() - Unregister previously registered nvmem device
1047  *
1048  * @nvmem: Pointer to previously registered nvmem device.
1049  */
1050 void nvmem_unregister(struct nvmem_device *nvmem)
1051 {
1052 	if (nvmem)
1053 		kref_put(&nvmem->refcnt, nvmem_device_release);
1054 }
1055 EXPORT_SYMBOL_GPL(nvmem_unregister);
1056 
1057 static void devm_nvmem_unregister(void *nvmem)
1058 {
1059 	nvmem_unregister(nvmem);
1060 }
1061 
1062 /**
1063  * devm_nvmem_register() - Register a managed nvmem device for given
1064  * nvmem_config.
1065  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1066  *
1067  * @dev: Device that uses the nvmem device.
1068  * @config: nvmem device configuration with which nvmem device is created.
1069  *
1070  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1071  * on success.
1072  */
1073 struct nvmem_device *devm_nvmem_register(struct device *dev,
1074 					 const struct nvmem_config *config)
1075 {
1076 	struct nvmem_device *nvmem;
1077 	int ret;
1078 
1079 	nvmem = nvmem_register(config);
1080 	if (IS_ERR(nvmem))
1081 		return nvmem;
1082 
1083 	ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1084 	if (ret)
1085 		return ERR_PTR(ret);
1086 
1087 	return nvmem;
1088 }
1089 EXPORT_SYMBOL_GPL(devm_nvmem_register);
1090 
1091 static struct nvmem_device *__nvmem_device_get(void *data,
1092 			int (*match)(struct device *dev, const void *data))
1093 {
1094 	struct nvmem_device *nvmem = NULL;
1095 	struct device *dev;
1096 
1097 	mutex_lock(&nvmem_mutex);
1098 	dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1099 	if (dev)
1100 		nvmem = to_nvmem_device(dev);
1101 	mutex_unlock(&nvmem_mutex);
1102 	if (!nvmem)
1103 		return ERR_PTR(-EPROBE_DEFER);
1104 
1105 	if (!try_module_get(nvmem->owner)) {
1106 		dev_err(&nvmem->dev,
1107 			"could not increase module refcount for cell %s\n",
1108 			nvmem_dev_name(nvmem));
1109 
1110 		put_device(&nvmem->dev);
1111 		return ERR_PTR(-EINVAL);
1112 	}
1113 
1114 	kref_get(&nvmem->refcnt);
1115 
1116 	return nvmem;
1117 }
1118 
1119 static void __nvmem_device_put(struct nvmem_device *nvmem)
1120 {
1121 	put_device(&nvmem->dev);
1122 	module_put(nvmem->owner);
1123 	kref_put(&nvmem->refcnt, nvmem_device_release);
1124 }
1125 
1126 #if IS_ENABLED(CONFIG_OF)
1127 /**
1128  * of_nvmem_device_get() - Get nvmem device from a given id
1129  *
1130  * @np: Device tree node that uses the nvmem device.
1131  * @id: nvmem name from nvmem-names property.
1132  *
1133  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1134  * on success.
1135  */
1136 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1137 {
1138 
1139 	struct device_node *nvmem_np;
1140 	struct nvmem_device *nvmem;
1141 	int index = 0;
1142 
1143 	if (id)
1144 		index = of_property_match_string(np, "nvmem-names", id);
1145 
1146 	nvmem_np = of_parse_phandle(np, "nvmem", index);
1147 	if (!nvmem_np)
1148 		return ERR_PTR(-ENOENT);
1149 
1150 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1151 	of_node_put(nvmem_np);
1152 	return nvmem;
1153 }
1154 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1155 #endif
1156 
1157 /**
1158  * nvmem_device_get() - Get nvmem device from a given id
1159  *
1160  * @dev: Device that uses the nvmem device.
1161  * @dev_name: name of the requested nvmem device.
1162  *
1163  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1164  * on success.
1165  */
1166 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1167 {
1168 	if (dev->of_node) { /* try dt first */
1169 		struct nvmem_device *nvmem;
1170 
1171 		nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1172 
1173 		if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1174 			return nvmem;
1175 
1176 	}
1177 
1178 	return __nvmem_device_get((void *)dev_name, device_match_name);
1179 }
1180 EXPORT_SYMBOL_GPL(nvmem_device_get);
1181 
1182 /**
1183  * nvmem_device_find() - Find nvmem device with matching function
1184  *
1185  * @data: Data to pass to match function
1186  * @match: Callback function to check device
1187  *
1188  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1189  * on success.
1190  */
1191 struct nvmem_device *nvmem_device_find(void *data,
1192 			int (*match)(struct device *dev, const void *data))
1193 {
1194 	return __nvmem_device_get(data, match);
1195 }
1196 EXPORT_SYMBOL_GPL(nvmem_device_find);
1197 
1198 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1199 {
1200 	struct nvmem_device **nvmem = res;
1201 
1202 	if (WARN_ON(!nvmem || !*nvmem))
1203 		return 0;
1204 
1205 	return *nvmem == data;
1206 }
1207 
1208 static void devm_nvmem_device_release(struct device *dev, void *res)
1209 {
1210 	nvmem_device_put(*(struct nvmem_device **)res);
1211 }
1212 
1213 /**
1214  * devm_nvmem_device_put() - put already got nvmem device
1215  *
1216  * @dev: Device that uses the nvmem device.
1217  * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1218  * that needs to be released.
1219  */
1220 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1221 {
1222 	int ret;
1223 
1224 	ret = devres_release(dev, devm_nvmem_device_release,
1225 			     devm_nvmem_device_match, nvmem);
1226 
1227 	WARN_ON(ret);
1228 }
1229 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1230 
1231 /**
1232  * nvmem_device_put() - put already got nvmem device
1233  *
1234  * @nvmem: pointer to nvmem device that needs to be released.
1235  */
1236 void nvmem_device_put(struct nvmem_device *nvmem)
1237 {
1238 	__nvmem_device_put(nvmem);
1239 }
1240 EXPORT_SYMBOL_GPL(nvmem_device_put);
1241 
1242 /**
1243  * devm_nvmem_device_get() - Get nvmem device of device from a given id
1244  *
1245  * @dev: Device that requests the nvmem device.
1246  * @id: name id for the requested nvmem device.
1247  *
1248  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1249  * on success.  The nvmem_device will be freed by the automatically once the
1250  * device is freed.
1251  */
1252 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1253 {
1254 	struct nvmem_device **ptr, *nvmem;
1255 
1256 	ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1257 	if (!ptr)
1258 		return ERR_PTR(-ENOMEM);
1259 
1260 	nvmem = nvmem_device_get(dev, id);
1261 	if (!IS_ERR(nvmem)) {
1262 		*ptr = nvmem;
1263 		devres_add(dev, ptr);
1264 	} else {
1265 		devres_free(ptr);
1266 	}
1267 
1268 	return nvmem;
1269 }
1270 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1271 
1272 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1273 					    const char *id, int index)
1274 {
1275 	struct nvmem_cell *cell;
1276 	const char *name = NULL;
1277 
1278 	cell = kzalloc_obj(*cell);
1279 	if (!cell)
1280 		return ERR_PTR(-ENOMEM);
1281 
1282 	if (id) {
1283 		name = kstrdup_const(id, GFP_KERNEL);
1284 		if (!name) {
1285 			kfree(cell);
1286 			return ERR_PTR(-ENOMEM);
1287 		}
1288 	}
1289 
1290 	cell->id = name;
1291 	cell->entry = entry;
1292 	cell->index = index;
1293 
1294 	return cell;
1295 }
1296 
1297 static struct nvmem_cell *
1298 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1299 {
1300 	struct nvmem_cell_entry *cell_entry;
1301 	struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1302 	struct nvmem_cell_lookup *lookup;
1303 	struct nvmem_device *nvmem;
1304 	const char *dev_id;
1305 
1306 	if (!dev)
1307 		return ERR_PTR(-EINVAL);
1308 
1309 	dev_id = dev_name(dev);
1310 
1311 	mutex_lock(&nvmem_lookup_mutex);
1312 
1313 	list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1314 		if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1315 		    (strcmp(lookup->con_id, con_id) == 0)) {
1316 			/* This is the right entry. */
1317 			nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1318 						   device_match_name);
1319 			if (IS_ERR(nvmem)) {
1320 				/* Provider may not be registered yet. */
1321 				cell = ERR_CAST(nvmem);
1322 				break;
1323 			}
1324 
1325 			cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1326 								   lookup->cell_name);
1327 			if (!cell_entry) {
1328 				__nvmem_device_put(nvmem);
1329 				cell = ERR_PTR(-ENOENT);
1330 			} else {
1331 				cell = nvmem_create_cell(cell_entry, con_id, 0);
1332 				if (IS_ERR(cell))
1333 					__nvmem_device_put(nvmem);
1334 			}
1335 			break;
1336 		}
1337 	}
1338 
1339 	mutex_unlock(&nvmem_lookup_mutex);
1340 	return cell;
1341 }
1342 
1343 static void nvmem_layout_module_put(struct nvmem_device *nvmem)
1344 {
1345 	if (nvmem->layout && nvmem->layout->dev.driver)
1346 		module_put(nvmem->layout->dev.driver->owner);
1347 }
1348 
1349 #if IS_ENABLED(CONFIG_OF)
1350 static struct nvmem_cell_entry *
1351 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1352 {
1353 	struct nvmem_cell_entry *iter, *cell = NULL;
1354 
1355 	mutex_lock(&nvmem_mutex);
1356 	list_for_each_entry(iter, &nvmem->cells, node) {
1357 		if (np == iter->np) {
1358 			cell = iter;
1359 			break;
1360 		}
1361 	}
1362 	mutex_unlock(&nvmem_mutex);
1363 
1364 	return cell;
1365 }
1366 
1367 static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
1368 {
1369 	if (!nvmem->layout)
1370 		return 0;
1371 
1372 	if (!nvmem->layout->dev.driver ||
1373 	    !try_module_get(nvmem->layout->dev.driver->owner))
1374 		return -EPROBE_DEFER;
1375 
1376 	return 0;
1377 }
1378 
1379 /**
1380  * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1381  *
1382  * @np: Device tree node that uses the nvmem cell.
1383  * @id: nvmem cell name from nvmem-cell-names property, or NULL
1384  *      for the cell at index 0 (the lone cell with no accompanying
1385  *      nvmem-cell-names property).
1386  *
1387  * Return: Will be an ERR_PTR() on error or a valid pointer
1388  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1389  * nvmem_cell_put().
1390  */
1391 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1392 {
1393 	struct device_node *cell_np, *nvmem_np;
1394 	struct nvmem_device *nvmem;
1395 	struct nvmem_cell_entry *cell_entry;
1396 	struct nvmem_cell *cell;
1397 	struct of_phandle_args cell_spec;
1398 	int index = 0;
1399 	int cell_index = 0;
1400 	int ret;
1401 
1402 	/* if cell name exists, find index to the name */
1403 	if (id)
1404 		index = of_property_match_string(np, "nvmem-cell-names", id);
1405 
1406 	ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1407 						  "#nvmem-cell-cells",
1408 						  index, &cell_spec);
1409 	if (ret)
1410 		return ERR_PTR(-ENOENT);
1411 
1412 	if (cell_spec.args_count > 1)
1413 		return ERR_PTR(-EINVAL);
1414 
1415 	cell_np = cell_spec.np;
1416 	if (cell_spec.args_count)
1417 		cell_index = cell_spec.args[0];
1418 
1419 	nvmem_np = of_get_parent(cell_np);
1420 	if (!nvmem_np) {
1421 		of_node_put(cell_np);
1422 		return ERR_PTR(-EINVAL);
1423 	}
1424 
1425 	/* nvmem layouts produce cells within the nvmem-layout container */
1426 	if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1427 		nvmem_np = of_get_next_parent(nvmem_np);
1428 		if (!nvmem_np) {
1429 			of_node_put(cell_np);
1430 			return ERR_PTR(-EINVAL);
1431 		}
1432 	}
1433 
1434 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1435 	of_node_put(nvmem_np);
1436 	if (IS_ERR(nvmem)) {
1437 		of_node_put(cell_np);
1438 		return ERR_CAST(nvmem);
1439 	}
1440 
1441 	ret = nvmem_layout_module_get_optional(nvmem);
1442 	if (ret) {
1443 		of_node_put(cell_np);
1444 		__nvmem_device_put(nvmem);
1445 		return ERR_PTR(ret);
1446 	}
1447 
1448 	cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1449 	of_node_put(cell_np);
1450 	if (!cell_entry) {
1451 		nvmem_layout_module_put(nvmem);
1452 		ret = nvmem->layout ? -EPROBE_DEFER : -ENOENT;
1453 		__nvmem_device_put(nvmem);
1454 		return ERR_PTR(ret);
1455 	}
1456 
1457 	cell = nvmem_create_cell(cell_entry, id, cell_index);
1458 	if (IS_ERR(cell)) {
1459 		nvmem_layout_module_put(nvmem);
1460 		__nvmem_device_put(nvmem);
1461 	}
1462 
1463 	return cell;
1464 }
1465 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1466 #endif
1467 
1468 /**
1469  * nvmem_cell_get() - Get nvmem cell of device from a given cell name
1470  *
1471  * @dev: Device that requests the nvmem cell.
1472  * @id: nvmem cell name to get (this corresponds with the name from the
1473  *      nvmem-cell-names property for DT systems and with the con_id from
1474  *      the lookup entry for non-DT systems).
1475  *
1476  * Return: Will be an ERR_PTR() on error or a valid pointer
1477  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1478  * nvmem_cell_put().
1479  */
1480 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1481 {
1482 	struct nvmem_cell *cell;
1483 
1484 	if (dev->of_node) { /* try dt first */
1485 		cell = of_nvmem_cell_get(dev->of_node, id);
1486 		if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1487 			return cell;
1488 	}
1489 
1490 	/* NULL cell id only allowed for device tree; invalid otherwise */
1491 	if (!id)
1492 		return ERR_PTR(-EINVAL);
1493 
1494 	return nvmem_cell_get_from_lookup(dev, id);
1495 }
1496 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1497 
1498 static void devm_nvmem_cell_release(struct device *dev, void *res)
1499 {
1500 	nvmem_cell_put(*(struct nvmem_cell **)res);
1501 }
1502 
1503 /**
1504  * devm_nvmem_cell_get() - Get nvmem cell of device from a given id
1505  *
1506  * @dev: Device that requests the nvmem cell.
1507  * @id: nvmem cell name id to get.
1508  *
1509  * Return: Will be an ERR_PTR() on error or a valid pointer
1510  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1511  * automatically once the device is freed.
1512  */
1513 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1514 {
1515 	struct nvmem_cell **ptr, *cell;
1516 
1517 	ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1518 	if (!ptr)
1519 		return ERR_PTR(-ENOMEM);
1520 
1521 	cell = nvmem_cell_get(dev, id);
1522 	if (!IS_ERR(cell)) {
1523 		*ptr = cell;
1524 		devres_add(dev, ptr);
1525 	} else {
1526 		devres_free(ptr);
1527 	}
1528 
1529 	return cell;
1530 }
1531 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1532 
1533 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1534 {
1535 	struct nvmem_cell **c = res;
1536 
1537 	if (WARN_ON(!c || !*c))
1538 		return 0;
1539 
1540 	return *c == data;
1541 }
1542 
1543 /**
1544  * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1545  * from devm_nvmem_cell_get.
1546  *
1547  * @dev: Device that requests the nvmem cell.
1548  * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1549  */
1550 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1551 {
1552 	int ret;
1553 
1554 	ret = devres_release(dev, devm_nvmem_cell_release,
1555 				devm_nvmem_cell_match, cell);
1556 
1557 	WARN_ON(ret);
1558 }
1559 EXPORT_SYMBOL(devm_nvmem_cell_put);
1560 
1561 /**
1562  * nvmem_cell_put() - Release previously allocated nvmem cell.
1563  *
1564  * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1565  */
1566 void nvmem_cell_put(struct nvmem_cell *cell)
1567 {
1568 	struct nvmem_device *nvmem = cell->entry->nvmem;
1569 
1570 	if (cell->id)
1571 		kfree_const(cell->id);
1572 
1573 	kfree(cell);
1574 	nvmem_layout_module_put(nvmem);
1575 	__nvmem_device_put(nvmem);
1576 }
1577 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1578 
1579 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1580 {
1581 	u8 *p, *b;
1582 	int i, extra, bytes_offset;
1583 	int bit_offset = cell->bit_offset;
1584 
1585 	p = b = buf;
1586 
1587 	bytes_offset = bit_offset / BITS_PER_BYTE;
1588 	b += bytes_offset;
1589 	bit_offset %= BITS_PER_BYTE;
1590 
1591 	if (bit_offset % BITS_PER_BYTE) {
1592 		/* First shift */
1593 		*p = *b++ >> bit_offset;
1594 
1595 		/* setup rest of the bytes if any */
1596 		for (i = 1; i < cell->bytes; i++) {
1597 			/* Get bits from next byte and shift them towards msb */
1598 			*p++ |= *b << (BITS_PER_BYTE - bit_offset);
1599 
1600 			*p = *b++ >> bit_offset;
1601 		}
1602 	} else if (p != b) {
1603 		memmove(p, b, cell->bytes - bytes_offset);
1604 		p += cell->bytes - 1;
1605 	} else {
1606 		/* point to the msb */
1607 		p += cell->bytes - 1;
1608 	}
1609 
1610 	/* result fits in less bytes */
1611 	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1612 	while (--extra >= 0)
1613 		*p-- = 0;
1614 
1615 	/* clear msb bits if any leftover in the last byte */
1616 	if (cell->nbits % BITS_PER_BYTE)
1617 		*p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1618 }
1619 
1620 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1621 			     struct nvmem_cell_entry *cell,
1622 			     void *buf, size_t *len, const char *id, int index)
1623 {
1624 	int rc;
1625 
1626 	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1627 
1628 	if (rc)
1629 		return rc;
1630 
1631 	/* shift bits in-place */
1632 	if (cell->bit_offset || cell->nbits)
1633 		nvmem_shift_read_buffer_in_place(cell, buf);
1634 
1635 	if (cell->read_post_process) {
1636 		rc = cell->read_post_process(cell->priv, id, index,
1637 					     cell->offset, buf, cell->raw_len);
1638 		if (rc)
1639 			return rc;
1640 	}
1641 
1642 	if (len)
1643 		*len = cell->bytes;
1644 
1645 	return 0;
1646 }
1647 
1648 /**
1649  * nvmem_cell_read() - Read a given nvmem cell
1650  *
1651  * @cell: nvmem cell to be read.
1652  * @len: pointer to length of cell which will be populated on successful read;
1653  *	 can be NULL.
1654  *
1655  * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1656  * buffer should be freed by the consumer with a kfree().
1657  */
1658 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1659 {
1660 	struct nvmem_cell_entry *entry = cell->entry;
1661 	struct nvmem_device *nvmem = entry->nvmem;
1662 	u8 *buf;
1663 	int rc;
1664 
1665 	if (!nvmem)
1666 		return ERR_PTR(-EINVAL);
1667 
1668 	buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1669 	if (!buf)
1670 		return ERR_PTR(-ENOMEM);
1671 
1672 	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1673 	if (rc) {
1674 		kfree(buf);
1675 		return ERR_PTR(rc);
1676 	}
1677 
1678 	return buf;
1679 }
1680 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1681 
1682 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1683 					     u8 *_buf, int len)
1684 {
1685 	struct nvmem_device *nvmem = cell->nvmem;
1686 	int i, rc, nbits, bit_offset = cell->bit_offset;
1687 	u8 v, *p, *buf, *b, pbyte, pbits;
1688 
1689 	nbits = cell->nbits;
1690 	buf = kzalloc(cell->bytes, GFP_KERNEL);
1691 	if (!buf)
1692 		return ERR_PTR(-ENOMEM);
1693 
1694 	memcpy(buf, _buf, len);
1695 	p = b = buf;
1696 
1697 	if (bit_offset) {
1698 		pbyte = *b;
1699 		*b <<= bit_offset;
1700 
1701 		/* setup the first byte with lsb bits from nvmem */
1702 		rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1703 		if (rc)
1704 			goto err;
1705 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
1706 
1707 		/* setup rest of the byte if any */
1708 		for (i = 1; i < cell->bytes; i++) {
1709 			/* Get last byte bits and shift them towards lsb */
1710 			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1711 			pbyte = *b;
1712 			p = b;
1713 			*b <<= bit_offset;
1714 			*b++ |= pbits;
1715 		}
1716 	}
1717 
1718 	/* if it's not end on byte boundary */
1719 	if ((nbits + bit_offset) % BITS_PER_BYTE) {
1720 		/* setup the last byte with msb bits from nvmem */
1721 		rc = nvmem_reg_read(nvmem,
1722 				    cell->offset + cell->bytes - 1, &v, 1);
1723 		if (rc)
1724 			goto err;
1725 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1726 
1727 	}
1728 
1729 	return buf;
1730 err:
1731 	kfree(buf);
1732 	return ERR_PTR(rc);
1733 }
1734 
1735 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1736 {
1737 	struct nvmem_device *nvmem = cell->nvmem;
1738 	int rc;
1739 
1740 	if (!nvmem || nvmem->read_only ||
1741 	    (cell->bit_offset == 0 && len != cell->bytes))
1742 		return -EINVAL;
1743 
1744 	/*
1745 	 * Any cells which have a read_post_process hook are read-only because
1746 	 * we cannot reverse the operation and it might affect other cells,
1747 	 * too.
1748 	 */
1749 	if (cell->read_post_process)
1750 		return -EINVAL;
1751 
1752 	if (cell->bit_offset || cell->nbits) {
1753 		if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
1754 			return -EINVAL;
1755 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1756 		if (IS_ERR(buf))
1757 			return PTR_ERR(buf);
1758 	}
1759 
1760 	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1761 
1762 	/* free the tmp buffer */
1763 	if (cell->bit_offset || cell->nbits)
1764 		kfree(buf);
1765 
1766 	if (rc)
1767 		return rc;
1768 
1769 	return len;
1770 }
1771 
1772 /**
1773  * nvmem_cell_write() - Write to a given nvmem cell
1774  *
1775  * @cell: nvmem cell to be written.
1776  * @buf: Buffer to be written.
1777  * @len: length of buffer to be written to nvmem cell.
1778  *
1779  * Return: length of bytes written or negative on failure.
1780  */
1781 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1782 {
1783 	return __nvmem_cell_entry_write(cell->entry, buf, len);
1784 }
1785 
1786 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1787 
1788 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1789 				  void *val, size_t count)
1790 {
1791 	struct nvmem_cell *cell;
1792 	void *buf;
1793 	size_t len;
1794 
1795 	cell = nvmem_cell_get(dev, cell_id);
1796 	if (IS_ERR(cell))
1797 		return PTR_ERR(cell);
1798 
1799 	buf = nvmem_cell_read(cell, &len);
1800 	if (IS_ERR(buf)) {
1801 		nvmem_cell_put(cell);
1802 		return PTR_ERR(buf);
1803 	}
1804 	if (len != count) {
1805 		kfree(buf);
1806 		nvmem_cell_put(cell);
1807 		return -EINVAL;
1808 	}
1809 	memcpy(val, buf, count);
1810 	kfree(buf);
1811 	nvmem_cell_put(cell);
1812 
1813 	return 0;
1814 }
1815 
1816 /**
1817  * nvmem_cell_read_u8() - Read a cell value as a u8
1818  *
1819  * @dev: Device that requests the nvmem cell.
1820  * @cell_id: Name of nvmem cell to read.
1821  * @val: pointer to output value.
1822  *
1823  * Return: 0 on success or negative errno.
1824  */
1825 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1826 {
1827 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1828 }
1829 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1830 
1831 /**
1832  * nvmem_cell_read_u16() - Read a cell value as a u16
1833  *
1834  * @dev: Device that requests the nvmem cell.
1835  * @cell_id: Name of nvmem cell to read.
1836  * @val: pointer to output value.
1837  *
1838  * Return: 0 on success or negative errno.
1839  */
1840 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1841 {
1842 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1843 }
1844 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1845 
1846 /**
1847  * nvmem_cell_read_u32() - Read a cell value as a u32
1848  *
1849  * @dev: Device that requests the nvmem cell.
1850  * @cell_id: Name of nvmem cell to read.
1851  * @val: pointer to output value.
1852  *
1853  * Return: 0 on success or negative errno.
1854  */
1855 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1856 {
1857 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1858 }
1859 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1860 
1861 /**
1862  * nvmem_cell_read_u64() - Read a cell value as a u64
1863  *
1864  * @dev: Device that requests the nvmem cell.
1865  * @cell_id: Name of nvmem cell to read.
1866  * @val: pointer to output value.
1867  *
1868  * Return: 0 on success or negative errno.
1869  */
1870 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1871 {
1872 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1873 }
1874 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1875 
1876 static const void *nvmem_cell_read_variable_common(struct device *dev,
1877 						   const char *cell_id,
1878 						   size_t max_len, size_t *len)
1879 {
1880 	struct nvmem_cell *cell;
1881 	int nbits;
1882 	void *buf;
1883 
1884 	cell = nvmem_cell_get(dev, cell_id);
1885 	if (IS_ERR(cell))
1886 		return cell;
1887 
1888 	nbits = cell->entry->nbits;
1889 	buf = nvmem_cell_read(cell, len);
1890 	nvmem_cell_put(cell);
1891 	if (IS_ERR(buf))
1892 		return buf;
1893 
1894 	/*
1895 	 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1896 	 * the length of the real data. Throw away the extra junk.
1897 	 */
1898 	if (nbits)
1899 		*len = DIV_ROUND_UP(nbits, 8);
1900 
1901 	if (*len > max_len) {
1902 		kfree(buf);
1903 		return ERR_PTR(-ERANGE);
1904 	}
1905 
1906 	return buf;
1907 }
1908 
1909 /**
1910  * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1911  *
1912  * @dev: Device that requests the nvmem cell.
1913  * @cell_id: Name of nvmem cell to read.
1914  * @val: pointer to output value.
1915  *
1916  * Return: 0 on success or negative errno.
1917  */
1918 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1919 				    u32 *val)
1920 {
1921 	size_t len;
1922 	const u8 *buf;
1923 	int i;
1924 
1925 	buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1926 	if (IS_ERR(buf))
1927 		return PTR_ERR(buf);
1928 
1929 	/* Copy w/ implicit endian conversion */
1930 	*val = 0;
1931 	for (i = 0; i < len; i++)
1932 		*val |= buf[i] << (8 * i);
1933 
1934 	kfree(buf);
1935 
1936 	return 0;
1937 }
1938 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1939 
1940 /**
1941  * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1942  *
1943  * @dev: Device that requests the nvmem cell.
1944  * @cell_id: Name of nvmem cell to read.
1945  * @val: pointer to output value.
1946  *
1947  * Return: 0 on success or negative errno.
1948  */
1949 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1950 				    u64 *val)
1951 {
1952 	size_t len;
1953 	const u8 *buf;
1954 	int i;
1955 
1956 	buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1957 	if (IS_ERR(buf))
1958 		return PTR_ERR(buf);
1959 
1960 	/* Copy w/ implicit endian conversion */
1961 	*val = 0;
1962 	for (i = 0; i < len; i++)
1963 		*val |= (uint64_t)buf[i] << (8 * i);
1964 
1965 	kfree(buf);
1966 
1967 	return 0;
1968 }
1969 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1970 
1971 /**
1972  * nvmem_device_cell_read() - Read a given nvmem device and cell
1973  *
1974  * @nvmem: nvmem device to read from.
1975  * @info: nvmem cell info to be read.
1976  * @buf: buffer pointer which will be populated on successful read.
1977  *
1978  * Return: length of successful bytes read on success and negative
1979  * error code on error.
1980  */
1981 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1982 			   struct nvmem_cell_info *info, void *buf)
1983 {
1984 	struct nvmem_cell_entry cell;
1985 	int rc;
1986 	ssize_t len;
1987 
1988 	if (!nvmem)
1989 		return -EINVAL;
1990 
1991 	rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1992 	if (rc)
1993 		return rc;
1994 
1995 	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1996 	if (rc)
1997 		return rc;
1998 
1999 	return len;
2000 }
2001 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
2002 
2003 /**
2004  * nvmem_device_cell_write() - Write cell to a given nvmem device
2005  *
2006  * @nvmem: nvmem device to be written to.
2007  * @info: nvmem cell info to be written.
2008  * @buf: buffer to be written to cell.
2009  *
2010  * Return: length of bytes written or negative error code on failure.
2011  */
2012 int nvmem_device_cell_write(struct nvmem_device *nvmem,
2013 			    struct nvmem_cell_info *info, void *buf)
2014 {
2015 	struct nvmem_cell_entry cell;
2016 	int rc;
2017 
2018 	if (!nvmem)
2019 		return -EINVAL;
2020 
2021 	rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2022 	if (rc)
2023 		return rc;
2024 
2025 	return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
2026 }
2027 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
2028 
2029 /**
2030  * nvmem_device_read() - Read from a given nvmem device
2031  *
2032  * @nvmem: nvmem device to read from.
2033  * @offset: offset in nvmem device.
2034  * @bytes: number of bytes to read.
2035  * @buf: buffer pointer which will be populated on successful read.
2036  *
2037  * Return: length of successful bytes read on success and negative
2038  * error code on error.
2039  */
2040 int nvmem_device_read(struct nvmem_device *nvmem,
2041 		      unsigned int offset,
2042 		      size_t bytes, void *buf)
2043 {
2044 	int rc;
2045 
2046 	if (!nvmem)
2047 		return -EINVAL;
2048 
2049 	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2050 
2051 	if (rc)
2052 		return rc;
2053 
2054 	return bytes;
2055 }
2056 EXPORT_SYMBOL_GPL(nvmem_device_read);
2057 
2058 /**
2059  * nvmem_device_write() - Write cell to a given nvmem device
2060  *
2061  * @nvmem: nvmem device to be written to.
2062  * @offset: offset in nvmem device.
2063  * @bytes: number of bytes to write.
2064  * @buf: buffer to be written.
2065  *
2066  * Return: length of bytes written or negative error code on failure.
2067  */
2068 int nvmem_device_write(struct nvmem_device *nvmem,
2069 		       unsigned int offset,
2070 		       size_t bytes, void *buf)
2071 {
2072 	int rc;
2073 
2074 	if (!nvmem)
2075 		return -EINVAL;
2076 
2077 	rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2078 
2079 	if (rc)
2080 		return rc;
2081 
2082 
2083 	return bytes;
2084 }
2085 EXPORT_SYMBOL_GPL(nvmem_device_write);
2086 
2087 /**
2088  * nvmem_add_cell_lookups() - register a list of cell lookup entries
2089  *
2090  * @entries: array of cell lookup entries
2091  * @nentries: number of cell lookup entries in the array
2092  */
2093 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2094 {
2095 	int i;
2096 
2097 	mutex_lock(&nvmem_lookup_mutex);
2098 	for (i = 0; i < nentries; i++)
2099 		list_add_tail(&entries[i].node, &nvmem_lookup_list);
2100 	mutex_unlock(&nvmem_lookup_mutex);
2101 }
2102 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2103 
2104 /**
2105  * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2106  *                            entries
2107  *
2108  * @entries: array of cell lookup entries
2109  * @nentries: number of cell lookup entries in the array
2110  */
2111 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2112 {
2113 	int i;
2114 
2115 	mutex_lock(&nvmem_lookup_mutex);
2116 	for (i = 0; i < nentries; i++)
2117 		list_del(&entries[i].node);
2118 	mutex_unlock(&nvmem_lookup_mutex);
2119 }
2120 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2121 
2122 /**
2123  * nvmem_dev_name() - Get the name of a given nvmem device.
2124  *
2125  * @nvmem: nvmem device.
2126  *
2127  * Return: name of the nvmem device.
2128  */
2129 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2130 {
2131 	return dev_name(&nvmem->dev);
2132 }
2133 EXPORT_SYMBOL_GPL(nvmem_dev_name);
2134 
2135 /**
2136  * nvmem_dev_size() - Get the size of a given nvmem device.
2137  *
2138  * @nvmem: nvmem device.
2139  *
2140  * Return: size of the nvmem device.
2141  */
2142 size_t nvmem_dev_size(struct nvmem_device *nvmem)
2143 {
2144 	return nvmem->size;
2145 }
2146 EXPORT_SYMBOL_GPL(nvmem_dev_size);
2147 
2148 static int __init nvmem_init(void)
2149 {
2150 	int ret;
2151 
2152 	ret = bus_register(&nvmem_bus_type);
2153 	if (ret)
2154 		return ret;
2155 
2156 	ret = nvmem_layout_bus_register();
2157 	if (ret)
2158 		bus_unregister(&nvmem_bus_type);
2159 
2160 	return ret;
2161 }
2162 
2163 static void __exit nvmem_exit(void)
2164 {
2165 	nvmem_layout_bus_unregister();
2166 	bus_unregister(&nvmem_bus_type);
2167 }
2168 
2169 subsys_initcall(nvmem_init);
2170 module_exit(nvmem_exit);
2171 
2172 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
2173 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
2174 MODULE_DESCRIPTION("nvmem Driver Core");
2175