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