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 nvmem->dev.type = &nvmem_provider_type;
631 nvmem->dev.bus = &nvmem_bus_type;
632 nvmem->dev.parent = config->dev;
633
634 device_initialize(&nvmem->dev);
635
636 if (!config->ignore_wp)
637 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
638 GPIOD_OUT_HIGH);
639 if (IS_ERR(nvmem->wp_gpio)) {
640 rval = PTR_ERR(nvmem->wp_gpio);
641 nvmem->wp_gpio = NULL;
642 goto err_put_device;
643 }
644
645 kref_init(&nvmem->refcnt);
646 INIT_LIST_HEAD(&nvmem->cells);
647
648 nvmem->owner = config->owner;
649 if (!nvmem->owner && config->dev->driver)
650 nvmem->owner = config->dev->driver->owner;
651 nvmem->stride = config->stride ?: 1;
652 nvmem->word_size = config->word_size ?: 1;
653 nvmem->size = config->size;
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 goto err_put_device;
678
679 nvmem->read_only = device_property_present(config->dev, "read-only") ||
680 config->read_only || !nvmem->reg_write;
681
682 #ifdef CONFIG_NVMEM_SYSFS
683 nvmem->dev.groups = nvmem_dev_groups;
684 #endif
685
686 if (config->compat) {
687 rval = nvmem_sysfs_setup_compat(nvmem, config);
688 if (rval)
689 goto err_put_device;
690 }
691
692 if (config->cells) {
693 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
694 if (rval)
695 goto err_remove_cells;
696 }
697
698 rval = nvmem_add_cells_from_table(nvmem);
699 if (rval)
700 goto err_remove_cells;
701
702 rval = nvmem_add_cells_from_of(nvmem);
703 if (rval)
704 goto err_remove_cells;
705
706 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
707
708 rval = device_add(&nvmem->dev);
709 if (rval)
710 goto err_remove_cells;
711
712 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
713
714 return nvmem;
715
716 err_remove_cells:
717 nvmem_device_remove_all_cells(nvmem);
718 if (config->compat)
719 nvmem_sysfs_remove_compat(nvmem, config);
720 err_put_device:
721 put_device(&nvmem->dev);
722
723 return ERR_PTR(rval);
724 }
725 EXPORT_SYMBOL_GPL(nvmem_register);
726
nvmem_device_release(struct kref *kref)727 static void nvmem_device_release(struct kref *kref)
728 {
729 struct nvmem_device *nvmem;
730
731 nvmem = container_of(kref, struct nvmem_device, refcnt);
732
733 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
734
735 if (nvmem->flags & FLAG_COMPAT)
736 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
737
738 nvmem_device_remove_all_cells(nvmem);
739 device_unregister(&nvmem->dev);
740 }
741
742 /**
743 * nvmem_unregister() - Unregister previously registered nvmem device
744 *
745 * @nvmem: Pointer to previously registered nvmem device.
746 */
nvmem_unregister(struct nvmem_device *nvmem)747 void nvmem_unregister(struct nvmem_device *nvmem)
748 {
749 kref_put(&nvmem->refcnt, nvmem_device_release);
750 }
751 EXPORT_SYMBOL_GPL(nvmem_unregister);
752
devm_nvmem_release(struct device *dev, void *res)753 static void devm_nvmem_release(struct device *dev, void *res)
754 {
755 nvmem_unregister(*(struct nvmem_device **)res);
756 }
757
758 /**
759 * devm_nvmem_register() - Register a managed nvmem device for given
760 * nvmem_config.
761 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
762 *
763 * @dev: Device that uses the nvmem device.
764 * @config: nvmem device configuration with which nvmem device is created.
765 *
766 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
767 * on success.
768 */
devm_nvmem_register(struct device *dev, const struct nvmem_config *config)769 struct nvmem_device *devm_nvmem_register(struct device *dev,
770 const struct nvmem_config *config)
771 {
772 struct nvmem_device **ptr, *nvmem;
773
774 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
775 if (!ptr)
776 return ERR_PTR(-ENOMEM);
777
778 nvmem = nvmem_register(config);
779
780 if (!IS_ERR(nvmem)) {
781 *ptr = nvmem;
782 devres_add(dev, ptr);
783 } else {
784 devres_free(ptr);
785 }
786
787 return nvmem;
788 }
789 EXPORT_SYMBOL_GPL(devm_nvmem_register);
790
devm_nvmem_match(struct device *dev, void *res, void *data)791 static int devm_nvmem_match(struct device *dev, void *res, void *data)
792 {
793 struct nvmem_device **r = res;
794
795 return *r == data;
796 }
797
798 /**
799 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
800 * device.
801 *
802 * @dev: Device that uses the nvmem device.
803 * @nvmem: Pointer to previously registered nvmem device.
804 *
805 * Return: Will be negative on error or zero on success.
806 */
devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)807 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
808 {
809 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
810 }
811 EXPORT_SYMBOL(devm_nvmem_unregister);
812
__nvmem_device_get(void *data, int (*match)(struct device *dev, const void *data))813 static struct nvmem_device *__nvmem_device_get(void *data,
814 int (*match)(struct device *dev, const void *data))
815 {
816 struct nvmem_device *nvmem = NULL;
817 struct device *dev;
818
819 mutex_lock(&nvmem_mutex);
820 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
821 if (dev)
822 nvmem = to_nvmem_device(dev);
823 mutex_unlock(&nvmem_mutex);
824 if (!nvmem)
825 return ERR_PTR(-EPROBE_DEFER);
826
827 if (!try_module_get(nvmem->owner)) {
828 dev_err(&nvmem->dev,
829 "could not increase module refcount for cell %s\n",
830 nvmem_dev_name(nvmem));
831
832 put_device(&nvmem->dev);
833 return ERR_PTR(-EINVAL);
834 }
835
836 kref_get(&nvmem->refcnt);
837
838 return nvmem;
839 }
840
__nvmem_device_put(struct nvmem_device *nvmem)841 static void __nvmem_device_put(struct nvmem_device *nvmem)
842 {
843 put_device(&nvmem->dev);
844 module_put(nvmem->owner);
845 kref_put(&nvmem->refcnt, nvmem_device_release);
846 }
847
848 #if IS_ENABLED(CONFIG_OF)
849 /**
850 * of_nvmem_device_get() - Get nvmem device from a given id
851 *
852 * @np: Device tree node that uses the nvmem device.
853 * @id: nvmem name from nvmem-names property.
854 *
855 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
856 * on success.
857 */
of_nvmem_device_get(struct device_node *np, const char *id)858 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
859 {
860
861 struct device_node *nvmem_np;
862 struct nvmem_device *nvmem;
863 int index = 0;
864
865 if (id)
866 index = of_property_match_string(np, "nvmem-names", id);
867
868 nvmem_np = of_parse_phandle(np, "nvmem", index);
869 if (!nvmem_np)
870 return ERR_PTR(-ENOENT);
871
872 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
873 of_node_put(nvmem_np);
874 return nvmem;
875 }
876 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
877 #endif
878
879 /**
880 * nvmem_device_get() - Get nvmem device from a given id
881 *
882 * @dev: Device that uses the nvmem device.
883 * @dev_name: name of the requested nvmem device.
884 *
885 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
886 * on success.
887 */
nvmem_device_get(struct device *dev, const char *dev_name)888 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
889 {
890 if (dev->of_node) { /* try dt first */
891 struct nvmem_device *nvmem;
892
893 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
894
895 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
896 return nvmem;
897
898 }
899
900 return __nvmem_device_get((void *)dev_name, device_match_name);
901 }
902 EXPORT_SYMBOL_GPL(nvmem_device_get);
903
904 /**
905 * nvmem_device_find() - Find nvmem device with matching function
906 *
907 * @data: Data to pass to match function
908 * @match: Callback function to check device
909 *
910 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
911 * on success.
912 */
nvmem_device_find(void *data, int (*match)(struct device *dev, const void *data))913 struct nvmem_device *nvmem_device_find(void *data,
914 int (*match)(struct device *dev, const void *data))
915 {
916 return __nvmem_device_get(data, match);
917 }
918 EXPORT_SYMBOL_GPL(nvmem_device_find);
919
devm_nvmem_device_match(struct device *dev, void *res, void *data)920 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
921 {
922 struct nvmem_device **nvmem = res;
923
924 if (WARN_ON(!nvmem || !*nvmem))
925 return 0;
926
927 return *nvmem == data;
928 }
929
devm_nvmem_device_release(struct device *dev, void *res)930 static void devm_nvmem_device_release(struct device *dev, void *res)
931 {
932 nvmem_device_put(*(struct nvmem_device **)res);
933 }
934
935 /**
936 * devm_nvmem_device_put() - put alredy got nvmem device
937 *
938 * @dev: Device that uses the nvmem device.
939 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
940 * that needs to be released.
941 */
devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)942 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
943 {
944 int ret;
945
946 ret = devres_release(dev, devm_nvmem_device_release,
947 devm_nvmem_device_match, nvmem);
948
949 WARN_ON(ret);
950 }
951 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
952
953 /**
954 * nvmem_device_put() - put alredy got nvmem device
955 *
956 * @nvmem: pointer to nvmem device that needs to be released.
957 */
nvmem_device_put(struct nvmem_device *nvmem)958 void nvmem_device_put(struct nvmem_device *nvmem)
959 {
960 __nvmem_device_put(nvmem);
961 }
962 EXPORT_SYMBOL_GPL(nvmem_device_put);
963
964 /**
965 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
966 *
967 * @dev: Device that requests the nvmem device.
968 * @id: name id for the requested nvmem device.
969 *
970 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
971 * on success. The nvmem_cell will be freed by the automatically once the
972 * device is freed.
973 */
devm_nvmem_device_get(struct device *dev, const char *id)974 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
975 {
976 struct nvmem_device **ptr, *nvmem;
977
978 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
979 if (!ptr)
980 return ERR_PTR(-ENOMEM);
981
982 nvmem = nvmem_device_get(dev, id);
983 if (!IS_ERR(nvmem)) {
984 *ptr = nvmem;
985 devres_add(dev, ptr);
986 } else {
987 devres_free(ptr);
988 }
989
990 return nvmem;
991 }
992 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
993
994 static struct nvmem_cell *
nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)995 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
996 {
997 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
998 struct nvmem_cell_lookup *lookup;
999 struct nvmem_device *nvmem;
1000 const char *dev_id;
1001
1002 if (!dev)
1003 return ERR_PTR(-EINVAL);
1004
1005 dev_id = dev_name(dev);
1006
1007 mutex_lock(&nvmem_lookup_mutex);
1008
1009 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1010 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1011 (strcmp(lookup->con_id, con_id) == 0)) {
1012 /* This is the right entry. */
1013 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1014 device_match_name);
1015 if (IS_ERR(nvmem)) {
1016 /* Provider may not be registered yet. */
1017 cell = ERR_CAST(nvmem);
1018 break;
1019 }
1020
1021 cell = nvmem_find_cell_by_name(nvmem,
1022 lookup->cell_name);
1023 if (!cell) {
1024 __nvmem_device_put(nvmem);
1025 cell = ERR_PTR(-ENOENT);
1026 }
1027 break;
1028 }
1029 }
1030
1031 mutex_unlock(&nvmem_lookup_mutex);
1032 return cell;
1033 }
1034
1035 #if IS_ENABLED(CONFIG_OF)
1036 static struct nvmem_cell *
nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)1037 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1038 {
1039 struct nvmem_cell *iter, *cell = NULL;
1040
1041 mutex_lock(&nvmem_mutex);
1042 list_for_each_entry(iter, &nvmem->cells, node) {
1043 if (np == iter->np) {
1044 cell = iter;
1045 break;
1046 }
1047 }
1048 mutex_unlock(&nvmem_mutex);
1049
1050 return cell;
1051 }
1052
1053 /**
1054 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1055 *
1056 * @np: Device tree node that uses the nvmem cell.
1057 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1058 * for the cell at index 0 (the lone cell with no accompanying
1059 * nvmem-cell-names property).
1060 *
1061 * Return: Will be an ERR_PTR() on error or a valid pointer
1062 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1063 * nvmem_cell_put().
1064 */
of_nvmem_cell_get(struct device_node *np, const char *id)1065 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1066 {
1067 struct device_node *cell_np, *nvmem_np;
1068 struct nvmem_device *nvmem;
1069 struct nvmem_cell *cell;
1070 int index = 0;
1071
1072 /* if cell name exists, find index to the name */
1073 if (id)
1074 index = of_property_match_string(np, "nvmem-cell-names", id);
1075
1076 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1077 if (!cell_np)
1078 return ERR_PTR(-ENOENT);
1079
1080 nvmem_np = of_get_next_parent(cell_np);
1081 if (!nvmem_np)
1082 return ERR_PTR(-EINVAL);
1083
1084 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1085 of_node_put(nvmem_np);
1086 if (IS_ERR(nvmem))
1087 return ERR_CAST(nvmem);
1088
1089 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1090 if (!cell) {
1091 __nvmem_device_put(nvmem);
1092 return ERR_PTR(-ENOENT);
1093 }
1094
1095 return cell;
1096 }
1097 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1098 #endif
1099
1100 /**
1101 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1102 *
1103 * @dev: Device that requests the nvmem cell.
1104 * @id: nvmem cell name to get (this corresponds with the name from the
1105 * nvmem-cell-names property for DT systems and with the con_id from
1106 * the lookup entry for non-DT systems).
1107 *
1108 * Return: Will be an ERR_PTR() on error or a valid pointer
1109 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1110 * nvmem_cell_put().
1111 */
nvmem_cell_get(struct device *dev, const char *id)1112 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1113 {
1114 struct nvmem_cell *cell;
1115
1116 if (dev->of_node) { /* try dt first */
1117 cell = of_nvmem_cell_get(dev->of_node, id);
1118 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1119 return cell;
1120 }
1121
1122 /* NULL cell id only allowed for device tree; invalid otherwise */
1123 if (!id)
1124 return ERR_PTR(-EINVAL);
1125
1126 return nvmem_cell_get_from_lookup(dev, id);
1127 }
1128 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1129
devm_nvmem_cell_release(struct device *dev, void *res)1130 static void devm_nvmem_cell_release(struct device *dev, void *res)
1131 {
1132 nvmem_cell_put(*(struct nvmem_cell **)res);
1133 }
1134
1135 /**
1136 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1137 *
1138 * @dev: Device that requests the nvmem cell.
1139 * @id: nvmem cell name id to get.
1140 *
1141 * Return: Will be an ERR_PTR() on error or a valid pointer
1142 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1143 * automatically once the device is freed.
1144 */
devm_nvmem_cell_get(struct device *dev, const char *id)1145 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1146 {
1147 struct nvmem_cell **ptr, *cell;
1148
1149 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1150 if (!ptr)
1151 return ERR_PTR(-ENOMEM);
1152
1153 cell = nvmem_cell_get(dev, id);
1154 if (!IS_ERR(cell)) {
1155 *ptr = cell;
1156 devres_add(dev, ptr);
1157 } else {
1158 devres_free(ptr);
1159 }
1160
1161 return cell;
1162 }
1163 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1164
devm_nvmem_cell_match(struct device *dev, void *res, void *data)1165 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1166 {
1167 struct nvmem_cell **c = res;
1168
1169 if (WARN_ON(!c || !*c))
1170 return 0;
1171
1172 return *c == data;
1173 }
1174
1175 /**
1176 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1177 * from devm_nvmem_cell_get.
1178 *
1179 * @dev: Device that requests the nvmem cell.
1180 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1181 */
devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)1182 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1183 {
1184 int ret;
1185
1186 ret = devres_release(dev, devm_nvmem_cell_release,
1187 devm_nvmem_cell_match, cell);
1188
1189 WARN_ON(ret);
1190 }
1191 EXPORT_SYMBOL(devm_nvmem_cell_put);
1192
1193 /**
1194 * nvmem_cell_put() - Release previously allocated nvmem cell.
1195 *
1196 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1197 */
nvmem_cell_put(struct nvmem_cell *cell)1198 void nvmem_cell_put(struct nvmem_cell *cell)
1199 {
1200 struct nvmem_device *nvmem = cell->nvmem;
1201
1202 __nvmem_device_put(nvmem);
1203 }
1204 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1205
nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)1206 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1207 {
1208 u8 *p, *b;
1209 int i, extra, bit_offset = cell->bit_offset;
1210
1211 p = b = buf;
1212 if (bit_offset) {
1213 /* First shift */
1214 *b++ >>= bit_offset;
1215
1216 /* setup rest of the bytes if any */
1217 for (i = 1; i < cell->bytes; i++) {
1218 /* Get bits from next byte and shift them towards msb */
1219 *p |= *b << (BITS_PER_BYTE - bit_offset);
1220
1221 p = b;
1222 *b++ >>= bit_offset;
1223 }
1224 } else {
1225 /* point to the msb */
1226 p += cell->bytes - 1;
1227 }
1228
1229 /* result fits in less bytes */
1230 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1231 while (--extra >= 0)
1232 *p-- = 0;
1233
1234 /* clear msb bits if any leftover in the last byte */
1235 if (cell->nbits % BITS_PER_BYTE)
1236 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1237 }
1238
__nvmem_cell_read(struct nvmem_device *nvmem, struct nvmem_cell *cell, void *buf, size_t *len)1239 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1240 struct nvmem_cell *cell,
1241 void *buf, size_t *len)
1242 {
1243 int rc;
1244
1245 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1246
1247 if (rc)
1248 return rc;
1249
1250 /* shift bits in-place */
1251 if (cell->bit_offset || cell->nbits)
1252 nvmem_shift_read_buffer_in_place(cell, buf);
1253
1254 if (len)
1255 *len = cell->bytes;
1256
1257 return 0;
1258 }
1259
1260 /**
1261 * nvmem_cell_read() - Read a given nvmem cell
1262 *
1263 * @cell: nvmem cell to be read.
1264 * @len: pointer to length of cell which will be populated on successful read;
1265 * can be NULL.
1266 *
1267 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1268 * buffer should be freed by the consumer with a kfree().
1269 */
nvmem_cell_read(struct nvmem_cell *cell, size_t *len)1270 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1271 {
1272 struct nvmem_device *nvmem = cell->nvmem;
1273 u8 *buf;
1274 int rc;
1275
1276 if (!nvmem)
1277 return ERR_PTR(-EINVAL);
1278
1279 buf = kzalloc(cell->bytes, GFP_KERNEL);
1280 if (!buf)
1281 return ERR_PTR(-ENOMEM);
1282
1283 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1284 if (rc) {
1285 kfree(buf);
1286 return ERR_PTR(rc);
1287 }
1288
1289 return buf;
1290 }
1291 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1292
nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, u8 *_buf, int len)1293 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1294 u8 *_buf, int len)
1295 {
1296 struct nvmem_device *nvmem = cell->nvmem;
1297 int i, rc, nbits, bit_offset = cell->bit_offset;
1298 u8 v, *p, *buf, *b, pbyte, pbits;
1299
1300 nbits = cell->nbits;
1301 buf = kzalloc(cell->bytes, GFP_KERNEL);
1302 if (!buf)
1303 return ERR_PTR(-ENOMEM);
1304
1305 memcpy(buf, _buf, len);
1306 p = b = buf;
1307
1308 if (bit_offset) {
1309 pbyte = *b;
1310 *b <<= bit_offset;
1311
1312 /* setup the first byte with lsb bits from nvmem */
1313 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1314 if (rc)
1315 goto err;
1316 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1317
1318 /* setup rest of the byte if any */
1319 for (i = 1; i < cell->bytes; i++) {
1320 /* Get last byte bits and shift them towards lsb */
1321 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1322 pbyte = *b;
1323 p = b;
1324 *b <<= bit_offset;
1325 *b++ |= pbits;
1326 }
1327 }
1328
1329 /* if it's not end on byte boundary */
1330 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1331 /* setup the last byte with msb bits from nvmem */
1332 rc = nvmem_reg_read(nvmem,
1333 cell->offset + cell->bytes - 1, &v, 1);
1334 if (rc)
1335 goto err;
1336 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1337
1338 }
1339
1340 return buf;
1341 err:
1342 kfree(buf);
1343 return ERR_PTR(rc);
1344 }
1345
1346 /**
1347 * nvmem_cell_write() - Write to a given nvmem cell
1348 *
1349 * @cell: nvmem cell to be written.
1350 * @buf: Buffer to be written.
1351 * @len: length of buffer to be written to nvmem cell.
1352 *
1353 * Return: length of bytes written or negative on failure.
1354 */
nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)1355 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1356 {
1357 struct nvmem_device *nvmem = cell->nvmem;
1358 int rc;
1359
1360 if (!nvmem || nvmem->read_only ||
1361 (cell->bit_offset == 0 && len != cell->bytes))
1362 return -EINVAL;
1363
1364 if (cell->bit_offset || cell->nbits) {
1365 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1366 if (IS_ERR(buf))
1367 return PTR_ERR(buf);
1368 }
1369
1370 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1371
1372 /* free the tmp buffer */
1373 if (cell->bit_offset || cell->nbits)
1374 kfree(buf);
1375
1376 if (rc)
1377 return rc;
1378
1379 return len;
1380 }
1381 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1382
nvmem_cell_read_common(struct device *dev, const char *cell_id, void *val, size_t count)1383 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1384 void *val, size_t count)
1385 {
1386 struct nvmem_cell *cell;
1387 void *buf;
1388 size_t len;
1389
1390 cell = nvmem_cell_get(dev, cell_id);
1391 if (IS_ERR(cell))
1392 return PTR_ERR(cell);
1393
1394 buf = nvmem_cell_read(cell, &len);
1395 if (IS_ERR(buf)) {
1396 nvmem_cell_put(cell);
1397 return PTR_ERR(buf);
1398 }
1399 if (len != count) {
1400 kfree(buf);
1401 nvmem_cell_put(cell);
1402 return -EINVAL;
1403 }
1404 memcpy(val, buf, count);
1405 kfree(buf);
1406 nvmem_cell_put(cell);
1407
1408 return 0;
1409 }
1410
1411 /**
1412 * nvmem_cell_read_u8() - Read a cell value as a u8
1413 *
1414 * @dev: Device that requests the nvmem cell.
1415 * @cell_id: Name of nvmem cell to read.
1416 * @val: pointer to output value.
1417 *
1418 * Return: 0 on success or negative errno.
1419 */
nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)1420 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1421 {
1422 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1423 }
1424 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1425
1426 /**
1427 * nvmem_cell_read_u16() - Read a cell value as a u16
1428 *
1429 * @dev: Device that requests the nvmem cell.
1430 * @cell_id: Name of nvmem cell to read.
1431 * @val: pointer to output value.
1432 *
1433 * Return: 0 on success or negative errno.
1434 */
nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)1435 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1436 {
1437 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1438 }
1439 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1440
1441 /**
1442 * nvmem_cell_read_u32() - Read a cell value as a u32
1443 *
1444 * @dev: Device that requests the nvmem cell.
1445 * @cell_id: Name of nvmem cell to read.
1446 * @val: pointer to output value.
1447 *
1448 * Return: 0 on success or negative errno.
1449 */
nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)1450 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1451 {
1452 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1453 }
1454 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1455
1456 /**
1457 * nvmem_cell_read_u64() - Read a cell value as a u64
1458 *
1459 * @dev: Device that requests the nvmem cell.
1460 * @cell_id: Name of nvmem cell to read.
1461 * @val: pointer to output value.
1462 *
1463 * Return: 0 on success or negative errno.
1464 */
nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)1465 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1466 {
1467 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1468 }
1469 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1470
1471 /**
1472 * nvmem_device_cell_read() - Read a given nvmem device and cell
1473 *
1474 * @nvmem: nvmem device to read from.
1475 * @info: nvmem cell info to be read.
1476 * @buf: buffer pointer which will be populated on successful read.
1477 *
1478 * Return: length of successful bytes read on success and negative
1479 * error code on error.
1480 */
nvmem_device_cell_read(struct nvmem_device *nvmem, struct nvmem_cell_info *info, void *buf)1481 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1482 struct nvmem_cell_info *info, void *buf)
1483 {
1484 struct nvmem_cell cell;
1485 int rc;
1486 ssize_t len;
1487
1488 if (!nvmem)
1489 return -EINVAL;
1490
1491 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1492 if (rc)
1493 return rc;
1494
1495 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1496 if (rc)
1497 return rc;
1498
1499 return len;
1500 }
1501 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1502
1503 /**
1504 * nvmem_device_cell_write() - Write cell to a given nvmem device
1505 *
1506 * @nvmem: nvmem device to be written to.
1507 * @info: nvmem cell info to be written.
1508 * @buf: buffer to be written to cell.
1509 *
1510 * Return: length of bytes written or negative error code on failure.
1511 */
nvmem_device_cell_write(struct nvmem_device *nvmem, struct nvmem_cell_info *info, void *buf)1512 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1513 struct nvmem_cell_info *info, void *buf)
1514 {
1515 struct nvmem_cell cell;
1516 int rc;
1517
1518 if (!nvmem)
1519 return -EINVAL;
1520
1521 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1522 if (rc)
1523 return rc;
1524
1525 return nvmem_cell_write(&cell, buf, cell.bytes);
1526 }
1527 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1528
1529 /**
1530 * nvmem_device_read() - Read from a given nvmem device
1531 *
1532 * @nvmem: nvmem device to read from.
1533 * @offset: offset in nvmem device.
1534 * @bytes: number of bytes to read.
1535 * @buf: buffer pointer which will be populated on successful read.
1536 *
1537 * Return: length of successful bytes read on success and negative
1538 * error code on error.
1539 */
nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset, size_t bytes, void *buf)1540 int nvmem_device_read(struct nvmem_device *nvmem,
1541 unsigned int offset,
1542 size_t bytes, void *buf)
1543 {
1544 int rc;
1545
1546 if (!nvmem)
1547 return -EINVAL;
1548
1549 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1550
1551 if (rc)
1552 return rc;
1553
1554 return bytes;
1555 }
1556 EXPORT_SYMBOL_GPL(nvmem_device_read);
1557
1558 /**
1559 * nvmem_device_write() - Write cell to a given nvmem device
1560 *
1561 * @nvmem: nvmem device to be written to.
1562 * @offset: offset in nvmem device.
1563 * @bytes: number of bytes to write.
1564 * @buf: buffer to be written.
1565 *
1566 * Return: length of bytes written or negative error code on failure.
1567 */
nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset, size_t bytes, void *buf)1568 int nvmem_device_write(struct nvmem_device *nvmem,
1569 unsigned int offset,
1570 size_t bytes, void *buf)
1571 {
1572 int rc;
1573
1574 if (!nvmem)
1575 return -EINVAL;
1576
1577 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1578
1579 if (rc)
1580 return rc;
1581
1582
1583 return bytes;
1584 }
1585 EXPORT_SYMBOL_GPL(nvmem_device_write);
1586
1587 /**
1588 * nvmem_add_cell_table() - register a table of cell info entries
1589 *
1590 * @table: table of cell info entries
1591 */
nvmem_add_cell_table(struct nvmem_cell_table *table)1592 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1593 {
1594 mutex_lock(&nvmem_cell_mutex);
1595 list_add_tail(&table->node, &nvmem_cell_tables);
1596 mutex_unlock(&nvmem_cell_mutex);
1597 }
1598 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1599
1600 /**
1601 * nvmem_del_cell_table() - remove a previously registered cell info table
1602 *
1603 * @table: table of cell info entries
1604 */
nvmem_del_cell_table(struct nvmem_cell_table *table)1605 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1606 {
1607 mutex_lock(&nvmem_cell_mutex);
1608 list_del(&table->node);
1609 mutex_unlock(&nvmem_cell_mutex);
1610 }
1611 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1612
1613 /**
1614 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1615 *
1616 * @entries: array of cell lookup entries
1617 * @nentries: number of cell lookup entries in the array
1618 */
nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)1619 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1620 {
1621 int i;
1622
1623 mutex_lock(&nvmem_lookup_mutex);
1624 for (i = 0; i < nentries; i++)
1625 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1626 mutex_unlock(&nvmem_lookup_mutex);
1627 }
1628 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1629
1630 /**
1631 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1632 * entries
1633 *
1634 * @entries: array of cell lookup entries
1635 * @nentries: number of cell lookup entries in the array
1636 */
nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)1637 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1638 {
1639 int i;
1640
1641 mutex_lock(&nvmem_lookup_mutex);
1642 for (i = 0; i < nentries; i++)
1643 list_del(&entries[i].node);
1644 mutex_unlock(&nvmem_lookup_mutex);
1645 }
1646 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1647
1648 /**
1649 * nvmem_dev_name() - Get the name of a given nvmem device.
1650 *
1651 * @nvmem: nvmem device.
1652 *
1653 * Return: name of the nvmem device.
1654 */
nvmem_dev_name(struct nvmem_device *nvmem)1655 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1656 {
1657 return dev_name(&nvmem->dev);
1658 }
1659 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1660
nvmem_init(void)1661 static int __init nvmem_init(void)
1662 {
1663 return bus_register(&nvmem_bus_type);
1664 }
1665
nvmem_exit(void)1666 static void __exit nvmem_exit(void)
1667 {
1668 bus_unregister(&nvmem_bus_type);
1669 }
1670
1671 subsys_initcall(nvmem_init);
1672 module_exit(nvmem_exit);
1673
1674 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1675 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1676 MODULE_DESCRIPTION("nvmem Driver Core");
1677 MODULE_LICENSE("GPL v2");
1678