• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * nvmem framework core.
4  *
5  * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6  * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/fs.h>
12 #include <linux/idr.h>
13 #include <linux/init.h>
14 #include <linux/kref.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 
22 struct nvmem_device {
23 	struct module		*owner;
24 	struct device		dev;
25 	int			stride;
26 	int			word_size;
27 	int			id;
28 	struct kref		refcnt;
29 	size_t			size;
30 	bool			read_only;
31 	bool			root_only;
32 	int			flags;
33 	enum nvmem_type		type;
34 	struct bin_attribute	eeprom;
35 	struct device		*base_dev;
36 	struct list_head	cells;
37 	nvmem_reg_read_t	reg_read;
38 	nvmem_reg_write_t	reg_write;
39 	struct gpio_desc	*wp_gpio;
40 	void *priv;
41 };
42 
43 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
44 
45 #define FLAG_COMPAT		BIT(0)
46 
47 struct nvmem_cell {
48 	const char		*name;
49 	int			offset;
50 	int			bytes;
51 	int			bit_offset;
52 	int			nbits;
53 	struct device_node	*np;
54 	struct nvmem_device	*nvmem;
55 	struct list_head	node;
56 };
57 
58 static DEFINE_MUTEX(nvmem_mutex);
59 static DEFINE_IDA(nvmem_ida);
60 
61 static DEFINE_MUTEX(nvmem_cell_mutex);
62 static LIST_HEAD(nvmem_cell_tables);
63 
64 static DEFINE_MUTEX(nvmem_lookup_mutex);
65 static LIST_HEAD(nvmem_lookup_list);
66 
67 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
68 
nvmem_reg_read(struct nvmem_device * nvmem,unsigned int offset,void * val,size_t bytes)69 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
70 			  void *val, size_t bytes)
71 {
72 	if (nvmem->reg_read)
73 		return nvmem->reg_read(nvmem->priv, offset, val, bytes);
74 
75 	return -EINVAL;
76 }
77 
nvmem_reg_write(struct nvmem_device * nvmem,unsigned int offset,void * val,size_t bytes)78 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
79 			   void *val, size_t bytes)
80 {
81 	int ret;
82 
83 	if (nvmem->reg_write) {
84 		gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
85 		ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
86 		gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
87 		return ret;
88 	}
89 
90 	return -EINVAL;
91 }
92 
93 #ifdef CONFIG_NVMEM_SYSFS
94 static const char * const nvmem_type_str[] = {
95 	[NVMEM_TYPE_UNKNOWN] = "Unknown",
96 	[NVMEM_TYPE_EEPROM] = "EEPROM",
97 	[NVMEM_TYPE_OTP] = "OTP",
98 	[NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
99 };
100 
101 #ifdef CONFIG_DEBUG_LOCK_ALLOC
102 static struct lock_class_key eeprom_lock_key;
103 #endif
104 
type_show(struct device * dev,struct device_attribute * attr,char * buf)105 static ssize_t type_show(struct device *dev,
106 			 struct device_attribute *attr, char *buf)
107 {
108 	struct nvmem_device *nvmem = to_nvmem_device(dev);
109 
110 	return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
111 }
112 
113 static DEVICE_ATTR_RO(type);
114 
115 static struct attribute *nvmem_attrs[] = {
116 	&dev_attr_type.attr,
117 	NULL,
118 };
119 
bin_attr_nvmem_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)120 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
121 				   struct bin_attribute *attr, char *buf,
122 				   loff_t pos, size_t count)
123 {
124 	struct device *dev;
125 	struct nvmem_device *nvmem;
126 	int rc;
127 
128 	if (attr->private)
129 		dev = attr->private;
130 	else
131 		dev = kobj_to_dev(kobj);
132 	nvmem = to_nvmem_device(dev);
133 
134 	/* Stop the user from reading */
135 	if (pos >= nvmem->size)
136 		return 0;
137 
138 	if (!IS_ALIGNED(pos, nvmem->stride))
139 		return -EINVAL;
140 
141 	if (count < nvmem->word_size)
142 		return -EINVAL;
143 
144 	if (pos + count > nvmem->size)
145 		count = nvmem->size - pos;
146 
147 	count = round_down(count, nvmem->word_size);
148 
149 	if (!nvmem->reg_read)
150 		return -EPERM;
151 
152 	rc = nvmem_reg_read(nvmem, pos, buf, count);
153 
154 	if (rc)
155 		return rc;
156 
157 	return count;
158 }
159 
bin_attr_nvmem_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)160 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
161 				    struct bin_attribute *attr, char *buf,
162 				    loff_t pos, size_t count)
163 {
164 	struct device *dev;
165 	struct nvmem_device *nvmem;
166 	int rc;
167 
168 	if (attr->private)
169 		dev = attr->private;
170 	else
171 		dev = kobj_to_dev(kobj);
172 	nvmem = to_nvmem_device(dev);
173 
174 	/* Stop the user from writing */
175 	if (pos >= nvmem->size)
176 		return -EFBIG;
177 
178 	if (!IS_ALIGNED(pos, nvmem->stride))
179 		return -EINVAL;
180 
181 	if (count < nvmem->word_size)
182 		return -EINVAL;
183 
184 	if (pos + count > nvmem->size)
185 		count = nvmem->size - pos;
186 
187 	count = round_down(count, nvmem->word_size);
188 
189 	if (!nvmem->reg_write)
190 		return -EPERM;
191 
192 	rc = nvmem_reg_write(nvmem, pos, buf, count);
193 
194 	if (rc)
195 		return rc;
196 
197 	return count;
198 }
199 
nvmem_bin_attr_get_umode(struct nvmem_device * nvmem)200 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
201 {
202 	umode_t mode = 0400;
203 
204 	if (!nvmem->root_only)
205 		mode |= 0044;
206 
207 	if (!nvmem->read_only)
208 		mode |= 0200;
209 
210 	if (!nvmem->reg_write)
211 		mode &= ~0200;
212 
213 	if (!nvmem->reg_read)
214 		mode &= ~0444;
215 
216 	return mode;
217 }
218 
nvmem_bin_attr_is_visible(struct kobject * kobj,struct bin_attribute * attr,int i)219 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
220 					 struct bin_attribute *attr, int i)
221 {
222 	struct device *dev = kobj_to_dev(kobj);
223 	struct nvmem_device *nvmem = to_nvmem_device(dev);
224 
225 	attr->size = nvmem->size;
226 
227 	return nvmem_bin_attr_get_umode(nvmem);
228 }
229 
230 /* default read/write permissions */
231 static struct bin_attribute bin_attr_rw_nvmem = {
232 	.attr	= {
233 		.name	= "nvmem",
234 		.mode	= 0644,
235 	},
236 	.read	= bin_attr_nvmem_read,
237 	.write	= bin_attr_nvmem_write,
238 };
239 
240 static struct bin_attribute *nvmem_bin_attributes[] = {
241 	&bin_attr_rw_nvmem,
242 	NULL,
243 };
244 
245 static const struct attribute_group nvmem_bin_group = {
246 	.bin_attrs	= nvmem_bin_attributes,
247 	.attrs		= nvmem_attrs,
248 	.is_bin_visible = nvmem_bin_attr_is_visible,
249 };
250 
251 static const struct attribute_group *nvmem_dev_groups[] = {
252 	&nvmem_bin_group,
253 	NULL,
254 };
255 
256 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
257 	.attr	= {
258 		.name	= "eeprom",
259 	},
260 	.read	= bin_attr_nvmem_read,
261 	.write	= bin_attr_nvmem_write,
262 };
263 
264 /*
265  * nvmem_setup_compat() - Create an additional binary entry in
266  * drivers sys directory, to be backwards compatible with the older
267  * drivers/misc/eeprom drivers.
268  */
nvmem_sysfs_setup_compat(struct nvmem_device * nvmem,const struct nvmem_config * config)269 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
270 				    const struct nvmem_config *config)
271 {
272 	int rval;
273 
274 	if (!config->compat)
275 		return 0;
276 
277 	if (!config->base_dev)
278 		return -EINVAL;
279 
280 	nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
281 	nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
282 	nvmem->eeprom.size = nvmem->size;
283 #ifdef CONFIG_DEBUG_LOCK_ALLOC
284 	nvmem->eeprom.attr.key = &eeprom_lock_key;
285 #endif
286 	nvmem->eeprom.private = &nvmem->dev;
287 	nvmem->base_dev = config->base_dev;
288 
289 	rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
290 	if (rval) {
291 		dev_err(&nvmem->dev,
292 			"Failed to create eeprom binary file %d\n", rval);
293 		return rval;
294 	}
295 
296 	nvmem->flags |= FLAG_COMPAT;
297 
298 	return 0;
299 }
300 
nvmem_sysfs_remove_compat(struct nvmem_device * nvmem,const struct nvmem_config * config)301 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
302 			      const struct nvmem_config *config)
303 {
304 	if (config->compat)
305 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
306 }
307 
308 #else /* CONFIG_NVMEM_SYSFS */
309 
nvmem_sysfs_setup_compat(struct nvmem_device * nvmem,const struct nvmem_config * config)310 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
311 				    const struct nvmem_config *config)
312 {
313 	return -ENOSYS;
314 }
nvmem_sysfs_remove_compat(struct nvmem_device * nvmem,const struct nvmem_config * config)315 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
316 				      const struct nvmem_config *config)
317 {
318 }
319 
320 #endif /* CONFIG_NVMEM_SYSFS */
321 
nvmem_release(struct device * dev)322 static void nvmem_release(struct device *dev)
323 {
324 	struct nvmem_device *nvmem = to_nvmem_device(dev);
325 
326 	ida_free(&nvmem_ida, nvmem->id);
327 	gpiod_put(nvmem->wp_gpio);
328 	kfree(nvmem);
329 }
330 
331 static const struct device_type nvmem_provider_type = {
332 	.release	= nvmem_release,
333 };
334 
335 static struct bus_type nvmem_bus_type = {
336 	.name		= "nvmem",
337 };
338 
nvmem_cell_drop(struct nvmem_cell * cell)339 static void nvmem_cell_drop(struct nvmem_cell *cell)
340 {
341 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
342 	mutex_lock(&nvmem_mutex);
343 	list_del(&cell->node);
344 	mutex_unlock(&nvmem_mutex);
345 	of_node_put(cell->np);
346 	kfree_const(cell->name);
347 	kfree(cell);
348 }
349 
nvmem_device_remove_all_cells(const struct nvmem_device * nvmem)350 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
351 {
352 	struct nvmem_cell *cell, *p;
353 
354 	list_for_each_entry_safe(cell, p, &nvmem->cells, node)
355 		nvmem_cell_drop(cell);
356 }
357 
nvmem_cell_add(struct nvmem_cell * cell)358 static void nvmem_cell_add(struct nvmem_cell *cell)
359 {
360 	mutex_lock(&nvmem_mutex);
361 	list_add_tail(&cell->node, &cell->nvmem->cells);
362 	mutex_unlock(&nvmem_mutex);
363 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
364 }
365 
nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device * nvmem,const struct nvmem_cell_info * info,struct nvmem_cell * cell)366 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
367 					const struct nvmem_cell_info *info,
368 					struct nvmem_cell *cell)
369 {
370 	cell->nvmem = nvmem;
371 	cell->offset = info->offset;
372 	cell->bytes = info->bytes;
373 	cell->name = info->name;
374 
375 	cell->bit_offset = info->bit_offset;
376 	cell->nbits = info->nbits;
377 
378 	if (cell->nbits)
379 		cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
380 					   BITS_PER_BYTE);
381 
382 	if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
383 		dev_err(&nvmem->dev,
384 			"cell %s unaligned to nvmem stride %d\n",
385 			cell->name ?: "<unknown>", nvmem->stride);
386 		return -EINVAL;
387 	}
388 
389 	return 0;
390 }
391 
nvmem_cell_info_to_nvmem_cell(struct nvmem_device * nvmem,const struct nvmem_cell_info * info,struct nvmem_cell * cell)392 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
393 				const struct nvmem_cell_info *info,
394 				struct nvmem_cell *cell)
395 {
396 	int err;
397 
398 	err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
399 	if (err)
400 		return err;
401 
402 	cell->name = kstrdup_const(info->name, GFP_KERNEL);
403 	if (!cell->name)
404 		return -ENOMEM;
405 
406 	return 0;
407 }
408 
409 /**
410  * nvmem_add_cells() - Add cell information to an nvmem device
411  *
412  * @nvmem: nvmem device to add cells to.
413  * @info: nvmem cell info to add to the device
414  * @ncells: number of cells in info
415  *
416  * Return: 0 or negative error code on failure.
417  */
nvmem_add_cells(struct nvmem_device * nvmem,const struct nvmem_cell_info * info,int ncells)418 static int nvmem_add_cells(struct nvmem_device *nvmem,
419 		    const struct nvmem_cell_info *info,
420 		    int ncells)
421 {
422 	struct nvmem_cell **cells;
423 	int i, rval;
424 
425 	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
426 	if (!cells)
427 		return -ENOMEM;
428 
429 	for (i = 0; i < ncells; i++) {
430 		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
431 		if (!cells[i]) {
432 			rval = -ENOMEM;
433 			goto err;
434 		}
435 
436 		rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
437 		if (rval) {
438 			kfree(cells[i]);
439 			goto err;
440 		}
441 
442 		nvmem_cell_add(cells[i]);
443 	}
444 
445 	/* remove tmp array */
446 	kfree(cells);
447 
448 	return 0;
449 err:
450 	while (i--)
451 		nvmem_cell_drop(cells[i]);
452 
453 	kfree(cells);
454 
455 	return rval;
456 }
457 
458 /**
459  * nvmem_register_notifier() - Register a notifier block for nvmem events.
460  *
461  * @nb: notifier block to be called on nvmem events.
462  *
463  * Return: 0 on success, negative error number on failure.
464  */
nvmem_register_notifier(struct notifier_block * nb)465 int nvmem_register_notifier(struct notifier_block *nb)
466 {
467 	return blocking_notifier_chain_register(&nvmem_notifier, nb);
468 }
469 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
470 
471 /**
472  * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
473  *
474  * @nb: notifier block to be unregistered.
475  *
476  * Return: 0 on success, negative error number on failure.
477  */
nvmem_unregister_notifier(struct notifier_block * nb)478 int nvmem_unregister_notifier(struct notifier_block *nb)
479 {
480 	return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
481 }
482 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
483 
nvmem_add_cells_from_table(struct nvmem_device * nvmem)484 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
485 {
486 	const struct nvmem_cell_info *info;
487 	struct nvmem_cell_table *table;
488 	struct nvmem_cell *cell;
489 	int rval = 0, i;
490 
491 	mutex_lock(&nvmem_cell_mutex);
492 	list_for_each_entry(table, &nvmem_cell_tables, node) {
493 		if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
494 			for (i = 0; i < table->ncells; i++) {
495 				info = &table->cells[i];
496 
497 				cell = kzalloc(sizeof(*cell), GFP_KERNEL);
498 				if (!cell) {
499 					rval = -ENOMEM;
500 					goto out;
501 				}
502 
503 				rval = nvmem_cell_info_to_nvmem_cell(nvmem,
504 								     info,
505 								     cell);
506 				if (rval) {
507 					kfree(cell);
508 					goto out;
509 				}
510 
511 				nvmem_cell_add(cell);
512 			}
513 		}
514 	}
515 
516 out:
517 	mutex_unlock(&nvmem_cell_mutex);
518 	return rval;
519 }
520 
521 static struct nvmem_cell *
nvmem_find_cell_by_name(struct nvmem_device * nvmem,const char * cell_id)522 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
523 {
524 	struct nvmem_cell *iter, *cell = NULL;
525 
526 	mutex_lock(&nvmem_mutex);
527 	list_for_each_entry(iter, &nvmem->cells, node) {
528 		if (strcmp(cell_id, iter->name) == 0) {
529 			cell = iter;
530 			break;
531 		}
532 	}
533 	mutex_unlock(&nvmem_mutex);
534 
535 	return cell;
536 }
537 
nvmem_add_cells_from_of(struct nvmem_device * nvmem)538 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
539 {
540 	struct device_node *parent, *child;
541 	struct device *dev = &nvmem->dev;
542 	struct nvmem_cell *cell;
543 	const __be32 *addr;
544 	int len;
545 
546 	parent = dev->of_node;
547 
548 	for_each_child_of_node(parent, child) {
549 		addr = of_get_property(child, "reg", &len);
550 		if (!addr)
551 			continue;
552 		if (len < 2 * sizeof(u32)) {
553 			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
554 			of_node_put(child);
555 			return -EINVAL;
556 		}
557 
558 		cell = kzalloc(sizeof(*cell), GFP_KERNEL);
559 		if (!cell) {
560 			of_node_put(child);
561 			return -ENOMEM;
562 		}
563 
564 		cell->nvmem = nvmem;
565 		cell->offset = be32_to_cpup(addr++);
566 		cell->bytes = be32_to_cpup(addr);
567 		cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
568 
569 		addr = of_get_property(child, "bits", &len);
570 		if (addr && len == (2 * sizeof(u32))) {
571 			cell->bit_offset = be32_to_cpup(addr++);
572 			cell->nbits = be32_to_cpup(addr);
573 		}
574 
575 		if (cell->nbits)
576 			cell->bytes = DIV_ROUND_UP(
577 					cell->nbits + cell->bit_offset,
578 					BITS_PER_BYTE);
579 
580 		if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
581 			dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
582 				cell->name, nvmem->stride);
583 			/* Cells already added will be freed later. */
584 			kfree_const(cell->name);
585 			kfree(cell);
586 			of_node_put(child);
587 			return -EINVAL;
588 		}
589 
590 		cell->np = of_node_get(child);
591 		nvmem_cell_add(cell);
592 	}
593 
594 	return 0;
595 }
596 
597 /**
598  * nvmem_register() - Register a nvmem device for given nvmem_config.
599  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
600  *
601  * @config: nvmem device configuration with which nvmem device is created.
602  *
603  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
604  * on success.
605  */
606 
nvmem_register(const struct nvmem_config * config)607 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
608 {
609 	struct nvmem_device *nvmem;
610 	int rval;
611 
612 	if (!config->dev)
613 		return ERR_PTR(-EINVAL);
614 
615 	if (!config->reg_read && !config->reg_write)
616 		return ERR_PTR(-EINVAL);
617 
618 	nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
619 	if (!nvmem)
620 		return ERR_PTR(-ENOMEM);
621 
622 	rval  = ida_alloc(&nvmem_ida, GFP_KERNEL);
623 	if (rval < 0) {
624 		kfree(nvmem);
625 		return ERR_PTR(rval);
626 	}
627 
628 	nvmem->id = rval;
629 
630 	if (config->wp_gpio)
631 		nvmem->wp_gpio = config->wp_gpio;
632 	else
633 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
634 						    GPIOD_OUT_HIGH);
635 	if (IS_ERR(nvmem->wp_gpio)) {
636 		ida_free(&nvmem_ida, nvmem->id);
637 		rval = PTR_ERR(nvmem->wp_gpio);
638 		kfree(nvmem);
639 		return ERR_PTR(rval);
640 	}
641 
642 	kref_init(&nvmem->refcnt);
643 	INIT_LIST_HEAD(&nvmem->cells);
644 
645 	nvmem->owner = config->owner;
646 	if (!nvmem->owner && config->dev->driver)
647 		nvmem->owner = config->dev->driver->owner;
648 	nvmem->stride = config->stride ?: 1;
649 	nvmem->word_size = config->word_size ?: 1;
650 	nvmem->size = config->size;
651 	nvmem->dev.type = &nvmem_provider_type;
652 	nvmem->dev.bus = &nvmem_bus_type;
653 	nvmem->dev.parent = config->dev;
654 	nvmem->root_only = config->root_only;
655 	nvmem->priv = config->priv;
656 	nvmem->type = config->type;
657 	nvmem->reg_read = config->reg_read;
658 	nvmem->reg_write = config->reg_write;
659 	if (!config->no_of_node)
660 		nvmem->dev.of_node = config->dev->of_node;
661 
662 	switch (config->id) {
663 	case NVMEM_DEVID_NONE:
664 		rval = dev_set_name(&nvmem->dev, "%s", config->name);
665 		break;
666 	case NVMEM_DEVID_AUTO:
667 		rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
668 		break;
669 	default:
670 		rval = dev_set_name(&nvmem->dev, "%s%d",
671 			     config->name ? : "nvmem",
672 			     config->name ? config->id : nvmem->id);
673 		break;
674 	}
675 
676 	if (rval) {
677 		ida_free(&nvmem_ida, nvmem->id);
678 		kfree(nvmem);
679 		return ERR_PTR(rval);
680 	}
681 
682 	nvmem->read_only = device_property_present(config->dev, "read-only") ||
683 			   config->read_only || !nvmem->reg_write;
684 
685 #ifdef CONFIG_NVMEM_SYSFS
686 	nvmem->dev.groups = nvmem_dev_groups;
687 #endif
688 
689 	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
690 
691 	rval = device_register(&nvmem->dev);
692 	if (rval)
693 		goto err_put_device;
694 
695 	if (config->compat) {
696 		rval = nvmem_sysfs_setup_compat(nvmem, config);
697 		if (rval)
698 			goto err_device_del;
699 	}
700 
701 	if (config->cells) {
702 		rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
703 		if (rval)
704 			goto err_remove_cells;
705 	}
706 
707 	rval = nvmem_add_cells_from_table(nvmem);
708 	if (rval)
709 		goto err_remove_cells;
710 
711 	rval = nvmem_add_cells_from_of(nvmem);
712 	if (rval)
713 		goto err_remove_cells;
714 
715 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
716 
717 	return nvmem;
718 
719 err_remove_cells:
720 	nvmem_device_remove_all_cells(nvmem);
721 	if (config->compat)
722 		nvmem_sysfs_remove_compat(nvmem, config);
723 err_device_del:
724 	device_del(&nvmem->dev);
725 err_put_device:
726 	put_device(&nvmem->dev);
727 
728 	return ERR_PTR(rval);
729 }
730 EXPORT_SYMBOL_GPL(nvmem_register);
731 
nvmem_device_release(struct kref * kref)732 static void nvmem_device_release(struct kref *kref)
733 {
734 	struct nvmem_device *nvmem;
735 
736 	nvmem = container_of(kref, struct nvmem_device, refcnt);
737 
738 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
739 
740 	if (nvmem->flags & FLAG_COMPAT)
741 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
742 
743 	nvmem_device_remove_all_cells(nvmem);
744 	device_unregister(&nvmem->dev);
745 }
746 
747 /**
748  * nvmem_unregister() - Unregister previously registered nvmem device
749  *
750  * @nvmem: Pointer to previously registered nvmem device.
751  */
nvmem_unregister(struct nvmem_device * nvmem)752 void nvmem_unregister(struct nvmem_device *nvmem)
753 {
754 	kref_put(&nvmem->refcnt, nvmem_device_release);
755 }
756 EXPORT_SYMBOL_GPL(nvmem_unregister);
757 
devm_nvmem_release(struct device * dev,void * res)758 static void devm_nvmem_release(struct device *dev, void *res)
759 {
760 	nvmem_unregister(*(struct nvmem_device **)res);
761 }
762 
763 /**
764  * devm_nvmem_register() - Register a managed nvmem device for given
765  * nvmem_config.
766  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
767  *
768  * @dev: Device that uses the nvmem device.
769  * @config: nvmem device configuration with which nvmem device is created.
770  *
771  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
772  * on success.
773  */
devm_nvmem_register(struct device * dev,const struct nvmem_config * config)774 struct nvmem_device *devm_nvmem_register(struct device *dev,
775 					 const struct nvmem_config *config)
776 {
777 	struct nvmem_device **ptr, *nvmem;
778 
779 	ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
780 	if (!ptr)
781 		return ERR_PTR(-ENOMEM);
782 
783 	nvmem = nvmem_register(config);
784 
785 	if (!IS_ERR(nvmem)) {
786 		*ptr = nvmem;
787 		devres_add(dev, ptr);
788 	} else {
789 		devres_free(ptr);
790 	}
791 
792 	return nvmem;
793 }
794 EXPORT_SYMBOL_GPL(devm_nvmem_register);
795 
devm_nvmem_match(struct device * dev,void * res,void * data)796 static int devm_nvmem_match(struct device *dev, void *res, void *data)
797 {
798 	struct nvmem_device **r = res;
799 
800 	return *r == data;
801 }
802 
803 /**
804  * devm_nvmem_unregister() - Unregister previously registered managed nvmem
805  * device.
806  *
807  * @dev: Device that uses the nvmem device.
808  * @nvmem: Pointer to previously registered nvmem device.
809  *
810  * Return: Will be negative on error or zero on success.
811  */
devm_nvmem_unregister(struct device * dev,struct nvmem_device * nvmem)812 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
813 {
814 	return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
815 }
816 EXPORT_SYMBOL(devm_nvmem_unregister);
817 
__nvmem_device_get(void * data,int (* match)(struct device * dev,const void * data))818 static struct nvmem_device *__nvmem_device_get(void *data,
819 			int (*match)(struct device *dev, const void *data))
820 {
821 	struct nvmem_device *nvmem = NULL;
822 	struct device *dev;
823 
824 	mutex_lock(&nvmem_mutex);
825 	dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
826 	if (dev)
827 		nvmem = to_nvmem_device(dev);
828 	mutex_unlock(&nvmem_mutex);
829 	if (!nvmem)
830 		return ERR_PTR(-EPROBE_DEFER);
831 
832 	if (!try_module_get(nvmem->owner)) {
833 		dev_err(&nvmem->dev,
834 			"could not increase module refcount for cell %s\n",
835 			nvmem_dev_name(nvmem));
836 
837 		put_device(&nvmem->dev);
838 		return ERR_PTR(-EINVAL);
839 	}
840 
841 	kref_get(&nvmem->refcnt);
842 
843 	return nvmem;
844 }
845 
__nvmem_device_put(struct nvmem_device * nvmem)846 static void __nvmem_device_put(struct nvmem_device *nvmem)
847 {
848 	put_device(&nvmem->dev);
849 	module_put(nvmem->owner);
850 	kref_put(&nvmem->refcnt, nvmem_device_release);
851 }
852 
853 #if IS_ENABLED(CONFIG_OF)
854 /**
855  * of_nvmem_device_get() - Get nvmem device from a given id
856  *
857  * @np: Device tree node that uses the nvmem device.
858  * @id: nvmem name from nvmem-names property.
859  *
860  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
861  * on success.
862  */
of_nvmem_device_get(struct device_node * np,const char * id)863 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
864 {
865 
866 	struct device_node *nvmem_np;
867 	struct nvmem_device *nvmem;
868 	int index = 0;
869 
870 	if (id)
871 		index = of_property_match_string(np, "nvmem-names", id);
872 
873 	nvmem_np = of_parse_phandle(np, "nvmem", index);
874 	if (!nvmem_np)
875 		return ERR_PTR(-ENOENT);
876 
877 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
878 	of_node_put(nvmem_np);
879 	return nvmem;
880 }
881 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
882 #endif
883 
884 /**
885  * nvmem_device_get() - Get nvmem device from a given id
886  *
887  * @dev: Device that uses the nvmem device.
888  * @dev_name: name of the requested nvmem device.
889  *
890  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
891  * on success.
892  */
nvmem_device_get(struct device * dev,const char * dev_name)893 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
894 {
895 	if (dev->of_node) { /* try dt first */
896 		struct nvmem_device *nvmem;
897 
898 		nvmem = of_nvmem_device_get(dev->of_node, dev_name);
899 
900 		if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
901 			return nvmem;
902 
903 	}
904 
905 	return __nvmem_device_get((void *)dev_name, device_match_name);
906 }
907 EXPORT_SYMBOL_GPL(nvmem_device_get);
908 
909 /**
910  * nvmem_device_find() - Find nvmem device with matching function
911  *
912  * @data: Data to pass to match function
913  * @match: Callback function to check device
914  *
915  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
916  * on success.
917  */
nvmem_device_find(void * data,int (* match)(struct device * dev,const void * data))918 struct nvmem_device *nvmem_device_find(void *data,
919 			int (*match)(struct device *dev, const void *data))
920 {
921 	return __nvmem_device_get(data, match);
922 }
923 EXPORT_SYMBOL_GPL(nvmem_device_find);
924 
devm_nvmem_device_match(struct device * dev,void * res,void * data)925 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
926 {
927 	struct nvmem_device **nvmem = res;
928 
929 	if (WARN_ON(!nvmem || !*nvmem))
930 		return 0;
931 
932 	return *nvmem == data;
933 }
934 
devm_nvmem_device_release(struct device * dev,void * res)935 static void devm_nvmem_device_release(struct device *dev, void *res)
936 {
937 	nvmem_device_put(*(struct nvmem_device **)res);
938 }
939 
940 /**
941  * devm_nvmem_device_put() - put alredy got nvmem device
942  *
943  * @dev: Device that uses the nvmem device.
944  * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
945  * that needs to be released.
946  */
devm_nvmem_device_put(struct device * dev,struct nvmem_device * nvmem)947 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
948 {
949 	int ret;
950 
951 	ret = devres_release(dev, devm_nvmem_device_release,
952 			     devm_nvmem_device_match, nvmem);
953 
954 	WARN_ON(ret);
955 }
956 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
957 
958 /**
959  * nvmem_device_put() - put alredy got nvmem device
960  *
961  * @nvmem: pointer to nvmem device that needs to be released.
962  */
nvmem_device_put(struct nvmem_device * nvmem)963 void nvmem_device_put(struct nvmem_device *nvmem)
964 {
965 	__nvmem_device_put(nvmem);
966 }
967 EXPORT_SYMBOL_GPL(nvmem_device_put);
968 
969 /**
970  * devm_nvmem_device_get() - Get nvmem cell of device form a given id
971  *
972  * @dev: Device that requests the nvmem device.
973  * @id: name id for the requested nvmem device.
974  *
975  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
976  * on success.  The nvmem_cell will be freed by the automatically once the
977  * device is freed.
978  */
devm_nvmem_device_get(struct device * dev,const char * id)979 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
980 {
981 	struct nvmem_device **ptr, *nvmem;
982 
983 	ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
984 	if (!ptr)
985 		return ERR_PTR(-ENOMEM);
986 
987 	nvmem = nvmem_device_get(dev, id);
988 	if (!IS_ERR(nvmem)) {
989 		*ptr = nvmem;
990 		devres_add(dev, ptr);
991 	} else {
992 		devres_free(ptr);
993 	}
994 
995 	return nvmem;
996 }
997 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
998 
999 static struct nvmem_cell *
nvmem_cell_get_from_lookup(struct device * dev,const char * con_id)1000 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1001 {
1002 	struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1003 	struct nvmem_cell_lookup *lookup;
1004 	struct nvmem_device *nvmem;
1005 	const char *dev_id;
1006 
1007 	if (!dev)
1008 		return ERR_PTR(-EINVAL);
1009 
1010 	dev_id = dev_name(dev);
1011 
1012 	mutex_lock(&nvmem_lookup_mutex);
1013 
1014 	list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1015 		if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1016 		    (strcmp(lookup->con_id, con_id) == 0)) {
1017 			/* This is the right entry. */
1018 			nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1019 						   device_match_name);
1020 			if (IS_ERR(nvmem)) {
1021 				/* Provider may not be registered yet. */
1022 				cell = ERR_CAST(nvmem);
1023 				break;
1024 			}
1025 
1026 			cell = nvmem_find_cell_by_name(nvmem,
1027 						       lookup->cell_name);
1028 			if (!cell) {
1029 				__nvmem_device_put(nvmem);
1030 				cell = ERR_PTR(-ENOENT);
1031 			}
1032 			break;
1033 		}
1034 	}
1035 
1036 	mutex_unlock(&nvmem_lookup_mutex);
1037 	return cell;
1038 }
1039 
1040 #if IS_ENABLED(CONFIG_OF)
1041 static struct nvmem_cell *
nvmem_find_cell_by_node(struct nvmem_device * nvmem,struct device_node * np)1042 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1043 {
1044 	struct nvmem_cell *iter, *cell = NULL;
1045 
1046 	mutex_lock(&nvmem_mutex);
1047 	list_for_each_entry(iter, &nvmem->cells, node) {
1048 		if (np == iter->np) {
1049 			cell = iter;
1050 			break;
1051 		}
1052 	}
1053 	mutex_unlock(&nvmem_mutex);
1054 
1055 	return cell;
1056 }
1057 
1058 /**
1059  * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1060  *
1061  * @np: Device tree node that uses the nvmem cell.
1062  * @id: nvmem cell name from nvmem-cell-names property, or NULL
1063  *      for the cell at index 0 (the lone cell with no accompanying
1064  *      nvmem-cell-names property).
1065  *
1066  * Return: Will be an ERR_PTR() on error or a valid pointer
1067  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1068  * nvmem_cell_put().
1069  */
of_nvmem_cell_get(struct device_node * np,const char * id)1070 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1071 {
1072 	struct device_node *cell_np, *nvmem_np;
1073 	struct nvmem_device *nvmem;
1074 	struct nvmem_cell *cell;
1075 	int index = 0;
1076 
1077 	/* if cell name exists, find index to the name */
1078 	if (id)
1079 		index = of_property_match_string(np, "nvmem-cell-names", id);
1080 
1081 	cell_np = of_parse_phandle(np, "nvmem-cells", index);
1082 	if (!cell_np)
1083 		return ERR_PTR(-ENOENT);
1084 
1085 	nvmem_np = of_get_next_parent(cell_np);
1086 	if (!nvmem_np)
1087 		return ERR_PTR(-EINVAL);
1088 
1089 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1090 	of_node_put(nvmem_np);
1091 	if (IS_ERR(nvmem))
1092 		return ERR_CAST(nvmem);
1093 
1094 	cell = nvmem_find_cell_by_node(nvmem, cell_np);
1095 	if (!cell) {
1096 		__nvmem_device_put(nvmem);
1097 		return ERR_PTR(-ENOENT);
1098 	}
1099 
1100 	return cell;
1101 }
1102 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1103 #endif
1104 
1105 /**
1106  * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1107  *
1108  * @dev: Device that requests the nvmem cell.
1109  * @id: nvmem cell name to get (this corresponds with the name from the
1110  *      nvmem-cell-names property for DT systems and with the con_id from
1111  *      the lookup entry for non-DT systems).
1112  *
1113  * Return: Will be an ERR_PTR() on error or a valid pointer
1114  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1115  * nvmem_cell_put().
1116  */
nvmem_cell_get(struct device * dev,const char * id)1117 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1118 {
1119 	struct nvmem_cell *cell;
1120 
1121 	if (dev->of_node) { /* try dt first */
1122 		cell = of_nvmem_cell_get(dev->of_node, id);
1123 		if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1124 			return cell;
1125 	}
1126 
1127 	/* NULL cell id only allowed for device tree; invalid otherwise */
1128 	if (!id)
1129 		return ERR_PTR(-EINVAL);
1130 
1131 	return nvmem_cell_get_from_lookup(dev, id);
1132 }
1133 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1134 
devm_nvmem_cell_release(struct device * dev,void * res)1135 static void devm_nvmem_cell_release(struct device *dev, void *res)
1136 {
1137 	nvmem_cell_put(*(struct nvmem_cell **)res);
1138 }
1139 
1140 /**
1141  * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1142  *
1143  * @dev: Device that requests the nvmem cell.
1144  * @id: nvmem cell name id to get.
1145  *
1146  * Return: Will be an ERR_PTR() on error or a valid pointer
1147  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1148  * automatically once the device is freed.
1149  */
devm_nvmem_cell_get(struct device * dev,const char * id)1150 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1151 {
1152 	struct nvmem_cell **ptr, *cell;
1153 
1154 	ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1155 	if (!ptr)
1156 		return ERR_PTR(-ENOMEM);
1157 
1158 	cell = nvmem_cell_get(dev, id);
1159 	if (!IS_ERR(cell)) {
1160 		*ptr = cell;
1161 		devres_add(dev, ptr);
1162 	} else {
1163 		devres_free(ptr);
1164 	}
1165 
1166 	return cell;
1167 }
1168 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1169 
devm_nvmem_cell_match(struct device * dev,void * res,void * data)1170 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1171 {
1172 	struct nvmem_cell **c = res;
1173 
1174 	if (WARN_ON(!c || !*c))
1175 		return 0;
1176 
1177 	return *c == data;
1178 }
1179 
1180 /**
1181  * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1182  * from devm_nvmem_cell_get.
1183  *
1184  * @dev: Device that requests the nvmem cell.
1185  * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1186  */
devm_nvmem_cell_put(struct device * dev,struct nvmem_cell * cell)1187 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1188 {
1189 	int ret;
1190 
1191 	ret = devres_release(dev, devm_nvmem_cell_release,
1192 				devm_nvmem_cell_match, cell);
1193 
1194 	WARN_ON(ret);
1195 }
1196 EXPORT_SYMBOL(devm_nvmem_cell_put);
1197 
1198 /**
1199  * nvmem_cell_put() - Release previously allocated nvmem cell.
1200  *
1201  * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1202  */
nvmem_cell_put(struct nvmem_cell * cell)1203 void nvmem_cell_put(struct nvmem_cell *cell)
1204 {
1205 	struct nvmem_device *nvmem = cell->nvmem;
1206 
1207 	__nvmem_device_put(nvmem);
1208 }
1209 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1210 
nvmem_shift_read_buffer_in_place(struct nvmem_cell * cell,void * buf)1211 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1212 {
1213 	u8 *p, *b;
1214 	int i, extra, bit_offset = cell->bit_offset;
1215 
1216 	p = b = buf;
1217 	if (bit_offset) {
1218 		/* First shift */
1219 		*b++ >>= bit_offset;
1220 
1221 		/* setup rest of the bytes if any */
1222 		for (i = 1; i < cell->bytes; i++) {
1223 			/* Get bits from next byte and shift them towards msb */
1224 			*p |= *b << (BITS_PER_BYTE - bit_offset);
1225 
1226 			p = b;
1227 			*b++ >>= bit_offset;
1228 		}
1229 	} else {
1230 		/* point to the msb */
1231 		p += cell->bytes - 1;
1232 	}
1233 
1234 	/* result fits in less bytes */
1235 	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1236 	while (--extra >= 0)
1237 		*p-- = 0;
1238 
1239 	/* clear msb bits if any leftover in the last byte */
1240 	if (cell->nbits % BITS_PER_BYTE)
1241 		*p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1242 }
1243 
__nvmem_cell_read(struct nvmem_device * nvmem,struct nvmem_cell * cell,void * buf,size_t * len)1244 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1245 		      struct nvmem_cell *cell,
1246 		      void *buf, size_t *len)
1247 {
1248 	int rc;
1249 
1250 	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1251 
1252 	if (rc)
1253 		return rc;
1254 
1255 	/* shift bits in-place */
1256 	if (cell->bit_offset || cell->nbits)
1257 		nvmem_shift_read_buffer_in_place(cell, buf);
1258 
1259 	if (len)
1260 		*len = cell->bytes;
1261 
1262 	return 0;
1263 }
1264 
1265 /**
1266  * nvmem_cell_read() - Read a given nvmem cell
1267  *
1268  * @cell: nvmem cell to be read.
1269  * @len: pointer to length of cell which will be populated on successful read;
1270  *	 can be NULL.
1271  *
1272  * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1273  * buffer should be freed by the consumer with a kfree().
1274  */
nvmem_cell_read(struct nvmem_cell * cell,size_t * len)1275 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1276 {
1277 	struct nvmem_device *nvmem = cell->nvmem;
1278 	u8 *buf;
1279 	int rc;
1280 
1281 	if (!nvmem)
1282 		return ERR_PTR(-EINVAL);
1283 
1284 	buf = kzalloc(cell->bytes, GFP_KERNEL);
1285 	if (!buf)
1286 		return ERR_PTR(-ENOMEM);
1287 
1288 	rc = __nvmem_cell_read(nvmem, cell, buf, len);
1289 	if (rc) {
1290 		kfree(buf);
1291 		return ERR_PTR(rc);
1292 	}
1293 
1294 	return buf;
1295 }
1296 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1297 
nvmem_cell_prepare_write_buffer(struct nvmem_cell * cell,u8 * _buf,int len)1298 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1299 					     u8 *_buf, int len)
1300 {
1301 	struct nvmem_device *nvmem = cell->nvmem;
1302 	int i, rc, nbits, bit_offset = cell->bit_offset;
1303 	u8 v, *p, *buf, *b, pbyte, pbits;
1304 
1305 	nbits = cell->nbits;
1306 	buf = kzalloc(cell->bytes, GFP_KERNEL);
1307 	if (!buf)
1308 		return ERR_PTR(-ENOMEM);
1309 
1310 	memcpy(buf, _buf, len);
1311 	p = b = buf;
1312 
1313 	if (bit_offset) {
1314 		pbyte = *b;
1315 		*b <<= bit_offset;
1316 
1317 		/* setup the first byte with lsb bits from nvmem */
1318 		rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1319 		if (rc)
1320 			goto err;
1321 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
1322 
1323 		/* setup rest of the byte if any */
1324 		for (i = 1; i < cell->bytes; i++) {
1325 			/* Get last byte bits and shift them towards lsb */
1326 			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1327 			pbyte = *b;
1328 			p = b;
1329 			*b <<= bit_offset;
1330 			*b++ |= pbits;
1331 		}
1332 	}
1333 
1334 	/* if it's not end on byte boundary */
1335 	if ((nbits + bit_offset) % BITS_PER_BYTE) {
1336 		/* setup the last byte with msb bits from nvmem */
1337 		rc = nvmem_reg_read(nvmem,
1338 				    cell->offset + cell->bytes - 1, &v, 1);
1339 		if (rc)
1340 			goto err;
1341 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1342 
1343 	}
1344 
1345 	return buf;
1346 err:
1347 	kfree(buf);
1348 	return ERR_PTR(rc);
1349 }
1350 
1351 /**
1352  * nvmem_cell_write() - Write to a given nvmem cell
1353  *
1354  * @cell: nvmem cell to be written.
1355  * @buf: Buffer to be written.
1356  * @len: length of buffer to be written to nvmem cell.
1357  *
1358  * Return: length of bytes written or negative on failure.
1359  */
nvmem_cell_write(struct nvmem_cell * cell,void * buf,size_t len)1360 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1361 {
1362 	struct nvmem_device *nvmem = cell->nvmem;
1363 	int rc;
1364 
1365 	if (!nvmem || nvmem->read_only ||
1366 	    (cell->bit_offset == 0 && len != cell->bytes))
1367 		return -EINVAL;
1368 
1369 	if (cell->bit_offset || cell->nbits) {
1370 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1371 		if (IS_ERR(buf))
1372 			return PTR_ERR(buf);
1373 	}
1374 
1375 	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1376 
1377 	/* free the tmp buffer */
1378 	if (cell->bit_offset || cell->nbits)
1379 		kfree(buf);
1380 
1381 	if (rc)
1382 		return rc;
1383 
1384 	return len;
1385 }
1386 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1387 
nvmem_cell_read_common(struct device * dev,const char * cell_id,void * val,size_t count)1388 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1389 				  void *val, size_t count)
1390 {
1391 	struct nvmem_cell *cell;
1392 	void *buf;
1393 	size_t len;
1394 
1395 	cell = nvmem_cell_get(dev, cell_id);
1396 	if (IS_ERR(cell))
1397 		return PTR_ERR(cell);
1398 
1399 	buf = nvmem_cell_read(cell, &len);
1400 	if (IS_ERR(buf)) {
1401 		nvmem_cell_put(cell);
1402 		return PTR_ERR(buf);
1403 	}
1404 	if (len != count) {
1405 		kfree(buf);
1406 		nvmem_cell_put(cell);
1407 		return -EINVAL;
1408 	}
1409 	memcpy(val, buf, count);
1410 	kfree(buf);
1411 	nvmem_cell_put(cell);
1412 
1413 	return 0;
1414 }
1415 
1416 /**
1417  * nvmem_cell_read_u8() - Read a cell value as a u8
1418  *
1419  * @dev: Device that requests the nvmem cell.
1420  * @cell_id: Name of nvmem cell to read.
1421  * @val: pointer to output value.
1422  *
1423  * Return: 0 on success or negative errno.
1424  */
nvmem_cell_read_u8(struct device * dev,const char * cell_id,u8 * val)1425 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1426 {
1427 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1428 }
1429 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1430 
1431 /**
1432  * nvmem_cell_read_u16() - Read a cell value as a u16
1433  *
1434  * @dev: Device that requests the nvmem cell.
1435  * @cell_id: Name of nvmem cell to read.
1436  * @val: pointer to output value.
1437  *
1438  * Return: 0 on success or negative errno.
1439  */
nvmem_cell_read_u16(struct device * dev,const char * cell_id,u16 * val)1440 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1441 {
1442 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1443 }
1444 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1445 
1446 /**
1447  * nvmem_cell_read_u32() - Read a cell value as a u32
1448  *
1449  * @dev: Device that requests the nvmem cell.
1450  * @cell_id: Name of nvmem cell to read.
1451  * @val: pointer to output value.
1452  *
1453  * Return: 0 on success or negative errno.
1454  */
nvmem_cell_read_u32(struct device * dev,const char * cell_id,u32 * val)1455 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1456 {
1457 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1458 }
1459 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1460 
1461 /**
1462  * nvmem_cell_read_u64() - Read a cell value as a u64
1463  *
1464  * @dev: Device that requests the nvmem cell.
1465  * @cell_id: Name of nvmem cell to read.
1466  * @val: pointer to output value.
1467  *
1468  * Return: 0 on success or negative errno.
1469  */
nvmem_cell_read_u64(struct device * dev,const char * cell_id,u64 * val)1470 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1471 {
1472 	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1473 }
1474 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1475 
1476 /**
1477  * nvmem_device_cell_read() - Read a given nvmem device and cell
1478  *
1479  * @nvmem: nvmem device to read from.
1480  * @info: nvmem cell info to be read.
1481  * @buf: buffer pointer which will be populated on successful read.
1482  *
1483  * Return: length of successful bytes read on success and negative
1484  * error code on error.
1485  */
nvmem_device_cell_read(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)1486 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1487 			   struct nvmem_cell_info *info, void *buf)
1488 {
1489 	struct nvmem_cell cell;
1490 	int rc;
1491 	ssize_t len;
1492 
1493 	if (!nvmem)
1494 		return -EINVAL;
1495 
1496 	rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1497 	if (rc)
1498 		return rc;
1499 
1500 	rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1501 	if (rc)
1502 		return rc;
1503 
1504 	return len;
1505 }
1506 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1507 
1508 /**
1509  * nvmem_device_cell_write() - Write cell to a given nvmem device
1510  *
1511  * @nvmem: nvmem device to be written to.
1512  * @info: nvmem cell info to be written.
1513  * @buf: buffer to be written to cell.
1514  *
1515  * Return: length of bytes written or negative error code on failure.
1516  */
nvmem_device_cell_write(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)1517 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1518 			    struct nvmem_cell_info *info, void *buf)
1519 {
1520 	struct nvmem_cell cell;
1521 	int rc;
1522 
1523 	if (!nvmem)
1524 		return -EINVAL;
1525 
1526 	rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1527 	if (rc)
1528 		return rc;
1529 
1530 	return nvmem_cell_write(&cell, buf, cell.bytes);
1531 }
1532 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1533 
1534 /**
1535  * nvmem_device_read() - Read from a given nvmem device
1536  *
1537  * @nvmem: nvmem device to read from.
1538  * @offset: offset in nvmem device.
1539  * @bytes: number of bytes to read.
1540  * @buf: buffer pointer which will be populated on successful read.
1541  *
1542  * Return: length of successful bytes read on success and negative
1543  * error code on error.
1544  */
nvmem_device_read(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)1545 int nvmem_device_read(struct nvmem_device *nvmem,
1546 		      unsigned int offset,
1547 		      size_t bytes, void *buf)
1548 {
1549 	int rc;
1550 
1551 	if (!nvmem)
1552 		return -EINVAL;
1553 
1554 	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1555 
1556 	if (rc)
1557 		return rc;
1558 
1559 	return bytes;
1560 }
1561 EXPORT_SYMBOL_GPL(nvmem_device_read);
1562 
1563 /**
1564  * nvmem_device_write() - Write cell to a given nvmem device
1565  *
1566  * @nvmem: nvmem device to be written to.
1567  * @offset: offset in nvmem device.
1568  * @bytes: number of bytes to write.
1569  * @buf: buffer to be written.
1570  *
1571  * Return: length of bytes written or negative error code on failure.
1572  */
nvmem_device_write(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)1573 int nvmem_device_write(struct nvmem_device *nvmem,
1574 		       unsigned int offset,
1575 		       size_t bytes, void *buf)
1576 {
1577 	int rc;
1578 
1579 	if (!nvmem)
1580 		return -EINVAL;
1581 
1582 	rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1583 
1584 	if (rc)
1585 		return rc;
1586 
1587 
1588 	return bytes;
1589 }
1590 EXPORT_SYMBOL_GPL(nvmem_device_write);
1591 
1592 /**
1593  * nvmem_add_cell_table() - register a table of cell info entries
1594  *
1595  * @table: table of cell info entries
1596  */
nvmem_add_cell_table(struct nvmem_cell_table * table)1597 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1598 {
1599 	mutex_lock(&nvmem_cell_mutex);
1600 	list_add_tail(&table->node, &nvmem_cell_tables);
1601 	mutex_unlock(&nvmem_cell_mutex);
1602 }
1603 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1604 
1605 /**
1606  * nvmem_del_cell_table() - remove a previously registered cell info table
1607  *
1608  * @table: table of cell info entries
1609  */
nvmem_del_cell_table(struct nvmem_cell_table * table)1610 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1611 {
1612 	mutex_lock(&nvmem_cell_mutex);
1613 	list_del(&table->node);
1614 	mutex_unlock(&nvmem_cell_mutex);
1615 }
1616 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1617 
1618 /**
1619  * nvmem_add_cell_lookups() - register a list of cell lookup entries
1620  *
1621  * @entries: array of cell lookup entries
1622  * @nentries: number of cell lookup entries in the array
1623  */
nvmem_add_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)1624 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1625 {
1626 	int i;
1627 
1628 	mutex_lock(&nvmem_lookup_mutex);
1629 	for (i = 0; i < nentries; i++)
1630 		list_add_tail(&entries[i].node, &nvmem_lookup_list);
1631 	mutex_unlock(&nvmem_lookup_mutex);
1632 }
1633 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1634 
1635 /**
1636  * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1637  *                            entries
1638  *
1639  * @entries: array of cell lookup entries
1640  * @nentries: number of cell lookup entries in the array
1641  */
nvmem_del_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)1642 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1643 {
1644 	int i;
1645 
1646 	mutex_lock(&nvmem_lookup_mutex);
1647 	for (i = 0; i < nentries; i++)
1648 		list_del(&entries[i].node);
1649 	mutex_unlock(&nvmem_lookup_mutex);
1650 }
1651 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1652 
1653 /**
1654  * nvmem_dev_name() - Get the name of a given nvmem device.
1655  *
1656  * @nvmem: nvmem device.
1657  *
1658  * Return: name of the nvmem device.
1659  */
nvmem_dev_name(struct nvmem_device * nvmem)1660 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1661 {
1662 	return dev_name(&nvmem->dev);
1663 }
1664 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1665 
nvmem_init(void)1666 static int __init nvmem_init(void)
1667 {
1668 	return bus_register(&nvmem_bus_type);
1669 }
1670 
nvmem_exit(void)1671 static void __exit nvmem_exit(void)
1672 {
1673 	bus_unregister(&nvmem_bus_type);
1674 }
1675 
1676 subsys_initcall(nvmem_init);
1677 module_exit(nvmem_exit);
1678 
1679 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1680 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1681 MODULE_DESCRIPTION("nvmem Driver Core");
1682 MODULE_LICENSE("GPL v2");
1683