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