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