xref: /openbmc/linux/drivers/nvmem/core.c (revision dc6a81c3)
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 #include "nvmem.h"
22 
23 struct nvmem_cell {
24 	const char		*name;
25 	int			offset;
26 	int			bytes;
27 	int			bit_offset;
28 	int			nbits;
29 	struct device_node	*np;
30 	struct nvmem_device	*nvmem;
31 	struct list_head	node;
32 };
33 
34 static DEFINE_MUTEX(nvmem_mutex);
35 static DEFINE_IDA(nvmem_ida);
36 
37 static DEFINE_MUTEX(nvmem_cell_mutex);
38 static LIST_HEAD(nvmem_cell_tables);
39 
40 static DEFINE_MUTEX(nvmem_lookup_mutex);
41 static LIST_HEAD(nvmem_lookup_list);
42 
43 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
44 
45 
46 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
47 			  void *val, size_t bytes)
48 {
49 	if (nvmem->reg_read)
50 		return nvmem->reg_read(nvmem->priv, offset, val, bytes);
51 
52 	return -EINVAL;
53 }
54 
55 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
56 			   void *val, size_t bytes)
57 {
58 	int ret;
59 
60 	if (nvmem->reg_write) {
61 		gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
62 		ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
63 		gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
64 		return ret;
65 	}
66 
67 	return -EINVAL;
68 }
69 
70 static void nvmem_release(struct device *dev)
71 {
72 	struct nvmem_device *nvmem = to_nvmem_device(dev);
73 
74 	ida_simple_remove(&nvmem_ida, nvmem->id);
75 	kfree(nvmem);
76 }
77 
78 static const struct device_type nvmem_provider_type = {
79 	.release	= nvmem_release,
80 };
81 
82 static struct bus_type nvmem_bus_type = {
83 	.name		= "nvmem",
84 };
85 
86 static void nvmem_cell_drop(struct nvmem_cell *cell)
87 {
88 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
89 	mutex_lock(&nvmem_mutex);
90 	list_del(&cell->node);
91 	mutex_unlock(&nvmem_mutex);
92 	of_node_put(cell->np);
93 	kfree_const(cell->name);
94 	kfree(cell);
95 }
96 
97 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
98 {
99 	struct nvmem_cell *cell, *p;
100 
101 	list_for_each_entry_safe(cell, p, &nvmem->cells, node)
102 		nvmem_cell_drop(cell);
103 }
104 
105 static void nvmem_cell_add(struct nvmem_cell *cell)
106 {
107 	mutex_lock(&nvmem_mutex);
108 	list_add_tail(&cell->node, &cell->nvmem->cells);
109 	mutex_unlock(&nvmem_mutex);
110 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
111 }
112 
113 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
114 				   const struct nvmem_cell_info *info,
115 				   struct nvmem_cell *cell)
116 {
117 	cell->nvmem = nvmem;
118 	cell->offset = info->offset;
119 	cell->bytes = info->bytes;
120 	cell->name = kstrdup_const(info->name, GFP_KERNEL);
121 	if (!cell->name)
122 		return -ENOMEM;
123 
124 	cell->bit_offset = info->bit_offset;
125 	cell->nbits = info->nbits;
126 
127 	if (cell->nbits)
128 		cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
129 					   BITS_PER_BYTE);
130 
131 	if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
132 		dev_err(&nvmem->dev,
133 			"cell %s unaligned to nvmem stride %d\n",
134 			cell->name, nvmem->stride);
135 		return -EINVAL;
136 	}
137 
138 	return 0;
139 }
140 
141 /**
142  * nvmem_add_cells() - Add cell information to an nvmem device
143  *
144  * @nvmem: nvmem device to add cells to.
145  * @info: nvmem cell info to add to the device
146  * @ncells: number of cells in info
147  *
148  * Return: 0 or negative error code on failure.
149  */
150 static int nvmem_add_cells(struct nvmem_device *nvmem,
151 		    const struct nvmem_cell_info *info,
152 		    int ncells)
153 {
154 	struct nvmem_cell **cells;
155 	int i, rval;
156 
157 	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
158 	if (!cells)
159 		return -ENOMEM;
160 
161 	for (i = 0; i < ncells; i++) {
162 		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
163 		if (!cells[i]) {
164 			rval = -ENOMEM;
165 			goto err;
166 		}
167 
168 		rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
169 		if (rval) {
170 			kfree(cells[i]);
171 			goto err;
172 		}
173 
174 		nvmem_cell_add(cells[i]);
175 	}
176 
177 	/* remove tmp array */
178 	kfree(cells);
179 
180 	return 0;
181 err:
182 	while (i--)
183 		nvmem_cell_drop(cells[i]);
184 
185 	kfree(cells);
186 
187 	return rval;
188 }
189 
190 /**
191  * nvmem_register_notifier() - Register a notifier block for nvmem events.
192  *
193  * @nb: notifier block to be called on nvmem events.
194  *
195  * Return: 0 on success, negative error number on failure.
196  */
197 int nvmem_register_notifier(struct notifier_block *nb)
198 {
199 	return blocking_notifier_chain_register(&nvmem_notifier, nb);
200 }
201 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
202 
203 /**
204  * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
205  *
206  * @nb: notifier block to be unregistered.
207  *
208  * Return: 0 on success, negative error number on failure.
209  */
210 int nvmem_unregister_notifier(struct notifier_block *nb)
211 {
212 	return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
213 }
214 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
215 
216 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
217 {
218 	const struct nvmem_cell_info *info;
219 	struct nvmem_cell_table *table;
220 	struct nvmem_cell *cell;
221 	int rval = 0, i;
222 
223 	mutex_lock(&nvmem_cell_mutex);
224 	list_for_each_entry(table, &nvmem_cell_tables, node) {
225 		if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
226 			for (i = 0; i < table->ncells; i++) {
227 				info = &table->cells[i];
228 
229 				cell = kzalloc(sizeof(*cell), GFP_KERNEL);
230 				if (!cell) {
231 					rval = -ENOMEM;
232 					goto out;
233 				}
234 
235 				rval = nvmem_cell_info_to_nvmem_cell(nvmem,
236 								     info,
237 								     cell);
238 				if (rval) {
239 					kfree(cell);
240 					goto out;
241 				}
242 
243 				nvmem_cell_add(cell);
244 			}
245 		}
246 	}
247 
248 out:
249 	mutex_unlock(&nvmem_cell_mutex);
250 	return rval;
251 }
252 
253 static struct nvmem_cell *
254 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
255 {
256 	struct nvmem_cell *iter, *cell = NULL;
257 
258 	mutex_lock(&nvmem_mutex);
259 	list_for_each_entry(iter, &nvmem->cells, node) {
260 		if (strcmp(cell_id, iter->name) == 0) {
261 			cell = iter;
262 			break;
263 		}
264 	}
265 	mutex_unlock(&nvmem_mutex);
266 
267 	return cell;
268 }
269 
270 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
271 {
272 	struct device_node *parent, *child;
273 	struct device *dev = &nvmem->dev;
274 	struct nvmem_cell *cell;
275 	const __be32 *addr;
276 	int len;
277 
278 	parent = dev->of_node;
279 
280 	for_each_child_of_node(parent, child) {
281 		addr = of_get_property(child, "reg", &len);
282 		if (!addr || (len < 2 * sizeof(u32))) {
283 			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
284 			return -EINVAL;
285 		}
286 
287 		cell = kzalloc(sizeof(*cell), GFP_KERNEL);
288 		if (!cell)
289 			return -ENOMEM;
290 
291 		cell->nvmem = nvmem;
292 		cell->np = of_node_get(child);
293 		cell->offset = be32_to_cpup(addr++);
294 		cell->bytes = be32_to_cpup(addr);
295 		cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
296 
297 		addr = of_get_property(child, "bits", &len);
298 		if (addr && len == (2 * sizeof(u32))) {
299 			cell->bit_offset = be32_to_cpup(addr++);
300 			cell->nbits = be32_to_cpup(addr);
301 		}
302 
303 		if (cell->nbits)
304 			cell->bytes = DIV_ROUND_UP(
305 					cell->nbits + cell->bit_offset,
306 					BITS_PER_BYTE);
307 
308 		if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
309 			dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
310 				cell->name, nvmem->stride);
311 			/* Cells already added will be freed later. */
312 			kfree_const(cell->name);
313 			kfree(cell);
314 			return -EINVAL;
315 		}
316 
317 		nvmem_cell_add(cell);
318 	}
319 
320 	return 0;
321 }
322 
323 /**
324  * nvmem_register() - Register a nvmem device for given nvmem_config.
325  * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
326  *
327  * @config: nvmem device configuration with which nvmem device is created.
328  *
329  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
330  * on success.
331  */
332 
333 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
334 {
335 	struct nvmem_device *nvmem;
336 	int rval;
337 
338 	if (!config->dev)
339 		return ERR_PTR(-EINVAL);
340 
341 	nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
342 	if (!nvmem)
343 		return ERR_PTR(-ENOMEM);
344 
345 	rval  = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
346 	if (rval < 0) {
347 		kfree(nvmem);
348 		return ERR_PTR(rval);
349 	}
350 	if (config->wp_gpio)
351 		nvmem->wp_gpio = config->wp_gpio;
352 	else
353 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
354 						    GPIOD_OUT_HIGH);
355 	if (IS_ERR(nvmem->wp_gpio))
356 		return ERR_CAST(nvmem->wp_gpio);
357 
358 
359 	kref_init(&nvmem->refcnt);
360 	INIT_LIST_HEAD(&nvmem->cells);
361 
362 	nvmem->id = rval;
363 	nvmem->owner = config->owner;
364 	if (!nvmem->owner && config->dev->driver)
365 		nvmem->owner = config->dev->driver->owner;
366 	nvmem->stride = config->stride ?: 1;
367 	nvmem->word_size = config->word_size ?: 1;
368 	nvmem->size = config->size;
369 	nvmem->dev.type = &nvmem_provider_type;
370 	nvmem->dev.bus = &nvmem_bus_type;
371 	nvmem->dev.parent = config->dev;
372 	nvmem->priv = config->priv;
373 	nvmem->type = config->type;
374 	nvmem->reg_read = config->reg_read;
375 	nvmem->reg_write = config->reg_write;
376 	if (!config->no_of_node)
377 		nvmem->dev.of_node = config->dev->of_node;
378 
379 	if (config->id == -1 && config->name) {
380 		dev_set_name(&nvmem->dev, "%s", config->name);
381 	} else {
382 		dev_set_name(&nvmem->dev, "%s%d",
383 			     config->name ? : "nvmem",
384 			     config->name ? config->id : nvmem->id);
385 	}
386 
387 	nvmem->read_only = device_property_present(config->dev, "read-only") ||
388 			   config->read_only || !nvmem->reg_write;
389 
390 	nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
391 
392 	device_initialize(&nvmem->dev);
393 
394 	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
395 
396 	rval = device_add(&nvmem->dev);
397 	if (rval)
398 		goto err_put_device;
399 
400 	if (config->compat) {
401 		rval = nvmem_sysfs_setup_compat(nvmem, config);
402 		if (rval)
403 			goto err_device_del;
404 	}
405 
406 	if (config->cells) {
407 		rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
408 		if (rval)
409 			goto err_teardown_compat;
410 	}
411 
412 	rval = nvmem_add_cells_from_table(nvmem);
413 	if (rval)
414 		goto err_remove_cells;
415 
416 	rval = nvmem_add_cells_from_of(nvmem);
417 	if (rval)
418 		goto err_remove_cells;
419 
420 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
421 
422 	return nvmem;
423 
424 err_remove_cells:
425 	nvmem_device_remove_all_cells(nvmem);
426 err_teardown_compat:
427 	if (config->compat)
428 		nvmem_sysfs_remove_compat(nvmem, config);
429 err_device_del:
430 	device_del(&nvmem->dev);
431 err_put_device:
432 	put_device(&nvmem->dev);
433 
434 	return ERR_PTR(rval);
435 }
436 EXPORT_SYMBOL_GPL(nvmem_register);
437 
438 static void nvmem_device_release(struct kref *kref)
439 {
440 	struct nvmem_device *nvmem;
441 
442 	nvmem = container_of(kref, struct nvmem_device, refcnt);
443 
444 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
445 
446 	if (nvmem->flags & FLAG_COMPAT)
447 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
448 
449 	nvmem_device_remove_all_cells(nvmem);
450 	device_del(&nvmem->dev);
451 	put_device(&nvmem->dev);
452 }
453 
454 /**
455  * nvmem_unregister() - Unregister previously registered nvmem device
456  *
457  * @nvmem: Pointer to previously registered nvmem device.
458  */
459 void nvmem_unregister(struct nvmem_device *nvmem)
460 {
461 	kref_put(&nvmem->refcnt, nvmem_device_release);
462 }
463 EXPORT_SYMBOL_GPL(nvmem_unregister);
464 
465 static void devm_nvmem_release(struct device *dev, void *res)
466 {
467 	nvmem_unregister(*(struct nvmem_device **)res);
468 }
469 
470 /**
471  * devm_nvmem_register() - Register a managed nvmem device for given
472  * nvmem_config.
473  * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
474  *
475  * @dev: Device that uses the nvmem device.
476  * @config: nvmem device configuration with which nvmem device is created.
477  *
478  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
479  * on success.
480  */
481 struct nvmem_device *devm_nvmem_register(struct device *dev,
482 					 const struct nvmem_config *config)
483 {
484 	struct nvmem_device **ptr, *nvmem;
485 
486 	ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
487 	if (!ptr)
488 		return ERR_PTR(-ENOMEM);
489 
490 	nvmem = nvmem_register(config);
491 
492 	if (!IS_ERR(nvmem)) {
493 		*ptr = nvmem;
494 		devres_add(dev, ptr);
495 	} else {
496 		devres_free(ptr);
497 	}
498 
499 	return nvmem;
500 }
501 EXPORT_SYMBOL_GPL(devm_nvmem_register);
502 
503 static int devm_nvmem_match(struct device *dev, void *res, void *data)
504 {
505 	struct nvmem_device **r = res;
506 
507 	return *r == data;
508 }
509 
510 /**
511  * devm_nvmem_unregister() - Unregister previously registered managed nvmem
512  * device.
513  *
514  * @dev: Device that uses the nvmem device.
515  * @nvmem: Pointer to previously registered nvmem device.
516  *
517  * Return: Will be an negative on error or a zero on success.
518  */
519 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
520 {
521 	return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
522 }
523 EXPORT_SYMBOL(devm_nvmem_unregister);
524 
525 static struct nvmem_device *__nvmem_device_get(void *data,
526 			int (*match)(struct device *dev, const void *data))
527 {
528 	struct nvmem_device *nvmem = NULL;
529 	struct device *dev;
530 
531 	mutex_lock(&nvmem_mutex);
532 	dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
533 	if (dev)
534 		nvmem = to_nvmem_device(dev);
535 	mutex_unlock(&nvmem_mutex);
536 	if (!nvmem)
537 		return ERR_PTR(-EPROBE_DEFER);
538 
539 	if (!try_module_get(nvmem->owner)) {
540 		dev_err(&nvmem->dev,
541 			"could not increase module refcount for cell %s\n",
542 			nvmem_dev_name(nvmem));
543 
544 		put_device(&nvmem->dev);
545 		return ERR_PTR(-EINVAL);
546 	}
547 
548 	kref_get(&nvmem->refcnt);
549 
550 	return nvmem;
551 }
552 
553 static void __nvmem_device_put(struct nvmem_device *nvmem)
554 {
555 	put_device(&nvmem->dev);
556 	module_put(nvmem->owner);
557 	kref_put(&nvmem->refcnt, nvmem_device_release);
558 }
559 
560 #if IS_ENABLED(CONFIG_OF)
561 /**
562  * of_nvmem_device_get() - Get nvmem device from a given id
563  *
564  * @np: Device tree node that uses the nvmem device.
565  * @id: nvmem name from nvmem-names property.
566  *
567  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
568  * on success.
569  */
570 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
571 {
572 
573 	struct device_node *nvmem_np;
574 	int index = 0;
575 
576 	if (id)
577 		index = of_property_match_string(np, "nvmem-names", id);
578 
579 	nvmem_np = of_parse_phandle(np, "nvmem", index);
580 	if (!nvmem_np)
581 		return ERR_PTR(-ENOENT);
582 
583 	return __nvmem_device_get(nvmem_np, device_match_of_node);
584 }
585 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
586 #endif
587 
588 /**
589  * nvmem_device_get() - Get nvmem device from a given id
590  *
591  * @dev: Device that uses the nvmem device.
592  * @dev_name: name of the requested nvmem device.
593  *
594  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
595  * on success.
596  */
597 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
598 {
599 	if (dev->of_node) { /* try dt first */
600 		struct nvmem_device *nvmem;
601 
602 		nvmem = of_nvmem_device_get(dev->of_node, dev_name);
603 
604 		if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
605 			return nvmem;
606 
607 	}
608 
609 	return __nvmem_device_get((void *)dev_name, device_match_name);
610 }
611 EXPORT_SYMBOL_GPL(nvmem_device_get);
612 
613 /**
614  * nvmem_device_find() - Find nvmem device with matching function
615  *
616  * @data: Data to pass to match function
617  * @match: Callback function to check device
618  *
619  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
620  * on success.
621  */
622 struct nvmem_device *nvmem_device_find(void *data,
623 			int (*match)(struct device *dev, const void *data))
624 {
625 	return __nvmem_device_get(data, match);
626 }
627 EXPORT_SYMBOL_GPL(nvmem_device_find);
628 
629 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
630 {
631 	struct nvmem_device **nvmem = res;
632 
633 	if (WARN_ON(!nvmem || !*nvmem))
634 		return 0;
635 
636 	return *nvmem == data;
637 }
638 
639 static void devm_nvmem_device_release(struct device *dev, void *res)
640 {
641 	nvmem_device_put(*(struct nvmem_device **)res);
642 }
643 
644 /**
645  * devm_nvmem_device_put() - put alredy got nvmem device
646  *
647  * @dev: Device that uses the nvmem device.
648  * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
649  * that needs to be released.
650  */
651 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
652 {
653 	int ret;
654 
655 	ret = devres_release(dev, devm_nvmem_device_release,
656 			     devm_nvmem_device_match, nvmem);
657 
658 	WARN_ON(ret);
659 }
660 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
661 
662 /**
663  * nvmem_device_put() - put alredy got nvmem device
664  *
665  * @nvmem: pointer to nvmem device that needs to be released.
666  */
667 void nvmem_device_put(struct nvmem_device *nvmem)
668 {
669 	__nvmem_device_put(nvmem);
670 }
671 EXPORT_SYMBOL_GPL(nvmem_device_put);
672 
673 /**
674  * devm_nvmem_device_get() - Get nvmem cell of device form a given id
675  *
676  * @dev: Device that requests the nvmem device.
677  * @id: name id for the requested nvmem device.
678  *
679  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
680  * on success.  The nvmem_cell will be freed by the automatically once the
681  * device is freed.
682  */
683 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
684 {
685 	struct nvmem_device **ptr, *nvmem;
686 
687 	ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
688 	if (!ptr)
689 		return ERR_PTR(-ENOMEM);
690 
691 	nvmem = nvmem_device_get(dev, id);
692 	if (!IS_ERR(nvmem)) {
693 		*ptr = nvmem;
694 		devres_add(dev, ptr);
695 	} else {
696 		devres_free(ptr);
697 	}
698 
699 	return nvmem;
700 }
701 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
702 
703 static struct nvmem_cell *
704 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
705 {
706 	struct nvmem_cell *cell = ERR_PTR(-ENOENT);
707 	struct nvmem_cell_lookup *lookup;
708 	struct nvmem_device *nvmem;
709 	const char *dev_id;
710 
711 	if (!dev)
712 		return ERR_PTR(-EINVAL);
713 
714 	dev_id = dev_name(dev);
715 
716 	mutex_lock(&nvmem_lookup_mutex);
717 
718 	list_for_each_entry(lookup, &nvmem_lookup_list, node) {
719 		if ((strcmp(lookup->dev_id, dev_id) == 0) &&
720 		    (strcmp(lookup->con_id, con_id) == 0)) {
721 			/* This is the right entry. */
722 			nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
723 						   device_match_name);
724 			if (IS_ERR(nvmem)) {
725 				/* Provider may not be registered yet. */
726 				cell = ERR_CAST(nvmem);
727 				break;
728 			}
729 
730 			cell = nvmem_find_cell_by_name(nvmem,
731 						       lookup->cell_name);
732 			if (!cell) {
733 				__nvmem_device_put(nvmem);
734 				cell = ERR_PTR(-ENOENT);
735 			}
736 			break;
737 		}
738 	}
739 
740 	mutex_unlock(&nvmem_lookup_mutex);
741 	return cell;
742 }
743 
744 #if IS_ENABLED(CONFIG_OF)
745 static struct nvmem_cell *
746 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
747 {
748 	struct nvmem_cell *iter, *cell = NULL;
749 
750 	mutex_lock(&nvmem_mutex);
751 	list_for_each_entry(iter, &nvmem->cells, node) {
752 		if (np == iter->np) {
753 			cell = iter;
754 			break;
755 		}
756 	}
757 	mutex_unlock(&nvmem_mutex);
758 
759 	return cell;
760 }
761 
762 /**
763  * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
764  *
765  * @np: Device tree node that uses the nvmem cell.
766  * @id: nvmem cell name from nvmem-cell-names property, or NULL
767  *      for the cell at index 0 (the lone cell with no accompanying
768  *      nvmem-cell-names property).
769  *
770  * Return: Will be an ERR_PTR() on error or a valid pointer
771  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
772  * nvmem_cell_put().
773  */
774 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
775 {
776 	struct device_node *cell_np, *nvmem_np;
777 	struct nvmem_device *nvmem;
778 	struct nvmem_cell *cell;
779 	int index = 0;
780 
781 	/* if cell name exists, find index to the name */
782 	if (id)
783 		index = of_property_match_string(np, "nvmem-cell-names", id);
784 
785 	cell_np = of_parse_phandle(np, "nvmem-cells", index);
786 	if (!cell_np)
787 		return ERR_PTR(-ENOENT);
788 
789 	nvmem_np = of_get_next_parent(cell_np);
790 	if (!nvmem_np)
791 		return ERR_PTR(-EINVAL);
792 
793 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
794 	of_node_put(nvmem_np);
795 	if (IS_ERR(nvmem))
796 		return ERR_CAST(nvmem);
797 
798 	cell = nvmem_find_cell_by_node(nvmem, cell_np);
799 	if (!cell) {
800 		__nvmem_device_put(nvmem);
801 		return ERR_PTR(-ENOENT);
802 	}
803 
804 	return cell;
805 }
806 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
807 #endif
808 
809 /**
810  * nvmem_cell_get() - Get nvmem cell of device form a given cell name
811  *
812  * @dev: Device that requests the nvmem cell.
813  * @id: nvmem cell name to get (this corresponds with the name from the
814  *      nvmem-cell-names property for DT systems and with the con_id from
815  *      the lookup entry for non-DT systems).
816  *
817  * Return: Will be an ERR_PTR() on error or a valid pointer
818  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
819  * nvmem_cell_put().
820  */
821 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
822 {
823 	struct nvmem_cell *cell;
824 
825 	if (dev->of_node) { /* try dt first */
826 		cell = of_nvmem_cell_get(dev->of_node, id);
827 		if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
828 			return cell;
829 	}
830 
831 	/* NULL cell id only allowed for device tree; invalid otherwise */
832 	if (!id)
833 		return ERR_PTR(-EINVAL);
834 
835 	return nvmem_cell_get_from_lookup(dev, id);
836 }
837 EXPORT_SYMBOL_GPL(nvmem_cell_get);
838 
839 static void devm_nvmem_cell_release(struct device *dev, void *res)
840 {
841 	nvmem_cell_put(*(struct nvmem_cell **)res);
842 }
843 
844 /**
845  * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
846  *
847  * @dev: Device that requests the nvmem cell.
848  * @id: nvmem cell name id to get.
849  *
850  * Return: Will be an ERR_PTR() on error or a valid pointer
851  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
852  * automatically once the device is freed.
853  */
854 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
855 {
856 	struct nvmem_cell **ptr, *cell;
857 
858 	ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
859 	if (!ptr)
860 		return ERR_PTR(-ENOMEM);
861 
862 	cell = nvmem_cell_get(dev, id);
863 	if (!IS_ERR(cell)) {
864 		*ptr = cell;
865 		devres_add(dev, ptr);
866 	} else {
867 		devres_free(ptr);
868 	}
869 
870 	return cell;
871 }
872 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
873 
874 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
875 {
876 	struct nvmem_cell **c = res;
877 
878 	if (WARN_ON(!c || !*c))
879 		return 0;
880 
881 	return *c == data;
882 }
883 
884 /**
885  * devm_nvmem_cell_put() - Release previously allocated nvmem cell
886  * from devm_nvmem_cell_get.
887  *
888  * @dev: Device that requests the nvmem cell.
889  * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
890  */
891 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
892 {
893 	int ret;
894 
895 	ret = devres_release(dev, devm_nvmem_cell_release,
896 				devm_nvmem_cell_match, cell);
897 
898 	WARN_ON(ret);
899 }
900 EXPORT_SYMBOL(devm_nvmem_cell_put);
901 
902 /**
903  * nvmem_cell_put() - Release previously allocated nvmem cell.
904  *
905  * @cell: Previously allocated nvmem cell by nvmem_cell_get().
906  */
907 void nvmem_cell_put(struct nvmem_cell *cell)
908 {
909 	struct nvmem_device *nvmem = cell->nvmem;
910 
911 	__nvmem_device_put(nvmem);
912 }
913 EXPORT_SYMBOL_GPL(nvmem_cell_put);
914 
915 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
916 {
917 	u8 *p, *b;
918 	int i, extra, bit_offset = cell->bit_offset;
919 
920 	p = b = buf;
921 	if (bit_offset) {
922 		/* First shift */
923 		*b++ >>= bit_offset;
924 
925 		/* setup rest of the bytes if any */
926 		for (i = 1; i < cell->bytes; i++) {
927 			/* Get bits from next byte and shift them towards msb */
928 			*p |= *b << (BITS_PER_BYTE - bit_offset);
929 
930 			p = b;
931 			*b++ >>= bit_offset;
932 		}
933 	} else {
934 		/* point to the msb */
935 		p += cell->bytes - 1;
936 	}
937 
938 	/* result fits in less bytes */
939 	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
940 	while (--extra >= 0)
941 		*p-- = 0;
942 
943 	/* clear msb bits if any leftover in the last byte */
944 	*p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
945 }
946 
947 static int __nvmem_cell_read(struct nvmem_device *nvmem,
948 		      struct nvmem_cell *cell,
949 		      void *buf, size_t *len)
950 {
951 	int rc;
952 
953 	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
954 
955 	if (rc)
956 		return rc;
957 
958 	/* shift bits in-place */
959 	if (cell->bit_offset || cell->nbits)
960 		nvmem_shift_read_buffer_in_place(cell, buf);
961 
962 	if (len)
963 		*len = cell->bytes;
964 
965 	return 0;
966 }
967 
968 /**
969  * nvmem_cell_read() - Read a given nvmem cell
970  *
971  * @cell: nvmem cell to be read.
972  * @len: pointer to length of cell which will be populated on successful read;
973  *	 can be NULL.
974  *
975  * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
976  * buffer should be freed by the consumer with a kfree().
977  */
978 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
979 {
980 	struct nvmem_device *nvmem = cell->nvmem;
981 	u8 *buf;
982 	int rc;
983 
984 	if (!nvmem)
985 		return ERR_PTR(-EINVAL);
986 
987 	buf = kzalloc(cell->bytes, GFP_KERNEL);
988 	if (!buf)
989 		return ERR_PTR(-ENOMEM);
990 
991 	rc = __nvmem_cell_read(nvmem, cell, buf, len);
992 	if (rc) {
993 		kfree(buf);
994 		return ERR_PTR(rc);
995 	}
996 
997 	return buf;
998 }
999 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1000 
1001 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1002 					     u8 *_buf, int len)
1003 {
1004 	struct nvmem_device *nvmem = cell->nvmem;
1005 	int i, rc, nbits, bit_offset = cell->bit_offset;
1006 	u8 v, *p, *buf, *b, pbyte, pbits;
1007 
1008 	nbits = cell->nbits;
1009 	buf = kzalloc(cell->bytes, GFP_KERNEL);
1010 	if (!buf)
1011 		return ERR_PTR(-ENOMEM);
1012 
1013 	memcpy(buf, _buf, len);
1014 	p = b = buf;
1015 
1016 	if (bit_offset) {
1017 		pbyte = *b;
1018 		*b <<= bit_offset;
1019 
1020 		/* setup the first byte with lsb bits from nvmem */
1021 		rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1022 		if (rc)
1023 			goto err;
1024 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
1025 
1026 		/* setup rest of the byte if any */
1027 		for (i = 1; i < cell->bytes; i++) {
1028 			/* Get last byte bits and shift them towards lsb */
1029 			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1030 			pbyte = *b;
1031 			p = b;
1032 			*b <<= bit_offset;
1033 			*b++ |= pbits;
1034 		}
1035 	}
1036 
1037 	/* if it's not end on byte boundary */
1038 	if ((nbits + bit_offset) % BITS_PER_BYTE) {
1039 		/* setup the last byte with msb bits from nvmem */
1040 		rc = nvmem_reg_read(nvmem,
1041 				    cell->offset + cell->bytes - 1, &v, 1);
1042 		if (rc)
1043 			goto err;
1044 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1045 
1046 	}
1047 
1048 	return buf;
1049 err:
1050 	kfree(buf);
1051 	return ERR_PTR(rc);
1052 }
1053 
1054 /**
1055  * nvmem_cell_write() - Write to a given nvmem cell
1056  *
1057  * @cell: nvmem cell to be written.
1058  * @buf: Buffer to be written.
1059  * @len: length of buffer to be written to nvmem cell.
1060  *
1061  * Return: length of bytes written or negative on failure.
1062  */
1063 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1064 {
1065 	struct nvmem_device *nvmem = cell->nvmem;
1066 	int rc;
1067 
1068 	if (!nvmem || nvmem->read_only ||
1069 	    (cell->bit_offset == 0 && len != cell->bytes))
1070 		return -EINVAL;
1071 
1072 	if (cell->bit_offset || cell->nbits) {
1073 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1074 		if (IS_ERR(buf))
1075 			return PTR_ERR(buf);
1076 	}
1077 
1078 	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1079 
1080 	/* free the tmp buffer */
1081 	if (cell->bit_offset || cell->nbits)
1082 		kfree(buf);
1083 
1084 	if (rc)
1085 		return rc;
1086 
1087 	return len;
1088 }
1089 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1090 
1091 /**
1092  * nvmem_cell_read_u16() - Read a cell value as an u16
1093  *
1094  * @dev: Device that requests the nvmem cell.
1095  * @cell_id: Name of nvmem cell to read.
1096  * @val: pointer to output value.
1097  *
1098  * Return: 0 on success or negative errno.
1099  */
1100 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1101 {
1102 	struct nvmem_cell *cell;
1103 	void *buf;
1104 	size_t len;
1105 
1106 	cell = nvmem_cell_get(dev, cell_id);
1107 	if (IS_ERR(cell))
1108 		return PTR_ERR(cell);
1109 
1110 	buf = nvmem_cell_read(cell, &len);
1111 	if (IS_ERR(buf)) {
1112 		nvmem_cell_put(cell);
1113 		return PTR_ERR(buf);
1114 	}
1115 	if (len != sizeof(*val)) {
1116 		kfree(buf);
1117 		nvmem_cell_put(cell);
1118 		return -EINVAL;
1119 	}
1120 	memcpy(val, buf, sizeof(*val));
1121 	kfree(buf);
1122 	nvmem_cell_put(cell);
1123 
1124 	return 0;
1125 }
1126 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1127 
1128 /**
1129  * nvmem_cell_read_u32() - Read a cell value as an u32
1130  *
1131  * @dev: Device that requests the nvmem cell.
1132  * @cell_id: Name of nvmem cell to read.
1133  * @val: pointer to output value.
1134  *
1135  * Return: 0 on success or negative errno.
1136  */
1137 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1138 {
1139 	struct nvmem_cell *cell;
1140 	void *buf;
1141 	size_t len;
1142 
1143 	cell = nvmem_cell_get(dev, cell_id);
1144 	if (IS_ERR(cell))
1145 		return PTR_ERR(cell);
1146 
1147 	buf = nvmem_cell_read(cell, &len);
1148 	if (IS_ERR(buf)) {
1149 		nvmem_cell_put(cell);
1150 		return PTR_ERR(buf);
1151 	}
1152 	if (len != sizeof(*val)) {
1153 		kfree(buf);
1154 		nvmem_cell_put(cell);
1155 		return -EINVAL;
1156 	}
1157 	memcpy(val, buf, sizeof(*val));
1158 
1159 	kfree(buf);
1160 	nvmem_cell_put(cell);
1161 	return 0;
1162 }
1163 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1164 
1165 /**
1166  * nvmem_device_cell_read() - Read a given nvmem device and cell
1167  *
1168  * @nvmem: nvmem device to read from.
1169  * @info: nvmem cell info to be read.
1170  * @buf: buffer pointer which will be populated on successful read.
1171  *
1172  * Return: length of successful bytes read on success and negative
1173  * error code on error.
1174  */
1175 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1176 			   struct nvmem_cell_info *info, void *buf)
1177 {
1178 	struct nvmem_cell cell;
1179 	int rc;
1180 	ssize_t len;
1181 
1182 	if (!nvmem)
1183 		return -EINVAL;
1184 
1185 	rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1186 	if (rc)
1187 		return rc;
1188 
1189 	rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1190 	if (rc)
1191 		return rc;
1192 
1193 	return len;
1194 }
1195 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1196 
1197 /**
1198  * nvmem_device_cell_write() - Write cell to a given nvmem device
1199  *
1200  * @nvmem: nvmem device to be written to.
1201  * @info: nvmem cell info to be written.
1202  * @buf: buffer to be written to cell.
1203  *
1204  * Return: length of bytes written or negative error code on failure.
1205  */
1206 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1207 			    struct nvmem_cell_info *info, void *buf)
1208 {
1209 	struct nvmem_cell cell;
1210 	int rc;
1211 
1212 	if (!nvmem)
1213 		return -EINVAL;
1214 
1215 	rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1216 	if (rc)
1217 		return rc;
1218 
1219 	return nvmem_cell_write(&cell, buf, cell.bytes);
1220 }
1221 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1222 
1223 /**
1224  * nvmem_device_read() - Read from a given nvmem device
1225  *
1226  * @nvmem: nvmem device to read from.
1227  * @offset: offset in nvmem device.
1228  * @bytes: number of bytes to read.
1229  * @buf: buffer pointer which will be populated on successful read.
1230  *
1231  * Return: length of successful bytes read on success and negative
1232  * error code on error.
1233  */
1234 int nvmem_device_read(struct nvmem_device *nvmem,
1235 		      unsigned int offset,
1236 		      size_t bytes, void *buf)
1237 {
1238 	int rc;
1239 
1240 	if (!nvmem)
1241 		return -EINVAL;
1242 
1243 	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1244 
1245 	if (rc)
1246 		return rc;
1247 
1248 	return bytes;
1249 }
1250 EXPORT_SYMBOL_GPL(nvmem_device_read);
1251 
1252 /**
1253  * nvmem_device_write() - Write cell to a given nvmem device
1254  *
1255  * @nvmem: nvmem device to be written to.
1256  * @offset: offset in nvmem device.
1257  * @bytes: number of bytes to write.
1258  * @buf: buffer to be written.
1259  *
1260  * Return: length of bytes written or negative error code on failure.
1261  */
1262 int nvmem_device_write(struct nvmem_device *nvmem,
1263 		       unsigned int offset,
1264 		       size_t bytes, void *buf)
1265 {
1266 	int rc;
1267 
1268 	if (!nvmem)
1269 		return -EINVAL;
1270 
1271 	rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1272 
1273 	if (rc)
1274 		return rc;
1275 
1276 
1277 	return bytes;
1278 }
1279 EXPORT_SYMBOL_GPL(nvmem_device_write);
1280 
1281 /**
1282  * nvmem_add_cell_table() - register a table of cell info entries
1283  *
1284  * @table: table of cell info entries
1285  */
1286 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1287 {
1288 	mutex_lock(&nvmem_cell_mutex);
1289 	list_add_tail(&table->node, &nvmem_cell_tables);
1290 	mutex_unlock(&nvmem_cell_mutex);
1291 }
1292 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1293 
1294 /**
1295  * nvmem_del_cell_table() - remove a previously registered cell info table
1296  *
1297  * @table: table of cell info entries
1298  */
1299 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1300 {
1301 	mutex_lock(&nvmem_cell_mutex);
1302 	list_del(&table->node);
1303 	mutex_unlock(&nvmem_cell_mutex);
1304 }
1305 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1306 
1307 /**
1308  * nvmem_add_cell_lookups() - register a list of cell lookup entries
1309  *
1310  * @entries: array of cell lookup entries
1311  * @nentries: number of cell lookup entries in the array
1312  */
1313 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1314 {
1315 	int i;
1316 
1317 	mutex_lock(&nvmem_lookup_mutex);
1318 	for (i = 0; i < nentries; i++)
1319 		list_add_tail(&entries[i].node, &nvmem_lookup_list);
1320 	mutex_unlock(&nvmem_lookup_mutex);
1321 }
1322 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1323 
1324 /**
1325  * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1326  *                            entries
1327  *
1328  * @entries: array of cell lookup entries
1329  * @nentries: number of cell lookup entries in the array
1330  */
1331 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1332 {
1333 	int i;
1334 
1335 	mutex_lock(&nvmem_lookup_mutex);
1336 	for (i = 0; i < nentries; i++)
1337 		list_del(&entries[i].node);
1338 	mutex_unlock(&nvmem_lookup_mutex);
1339 }
1340 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1341 
1342 /**
1343  * nvmem_dev_name() - Get the name of a given nvmem device.
1344  *
1345  * @nvmem: nvmem device.
1346  *
1347  * Return: name of the nvmem device.
1348  */
1349 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1350 {
1351 	return dev_name(&nvmem->dev);
1352 }
1353 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1354 
1355 static int __init nvmem_init(void)
1356 {
1357 	return bus_register(&nvmem_bus_type);
1358 }
1359 
1360 static void __exit nvmem_exit(void)
1361 {
1362 	bus_unregister(&nvmem_bus_type);
1363 }
1364 
1365 subsys_initcall(nvmem_init);
1366 module_exit(nvmem_exit);
1367 
1368 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1369 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1370 MODULE_DESCRIPTION("nvmem Driver Core");
1371 MODULE_LICENSE("GPL v2");
1372