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