1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Register map access API 4 // 5 // Copyright 2011 Wolfson Microelectronics plc 6 // 7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 9 #include <linux/device.h> 10 #include <linux/slab.h> 11 #include <linux/export.h> 12 #include <linux/mutex.h> 13 #include <linux/err.h> 14 #include <linux/property.h> 15 #include <linux/rbtree.h> 16 #include <linux/sched.h> 17 #include <linux/delay.h> 18 #include <linux/log2.h> 19 #include <linux/hwspinlock.h> 20 #include <linux/unaligned.h> 21 22 #define CREATE_TRACE_POINTS 23 #include "trace.h" 24 25 #include "internal.h" 26 27 /* 28 * Sometimes for failures during very early init the trace 29 * infrastructure isn't available early enough to be used. For this 30 * sort of problem defining LOG_DEVICE will add printks for basic 31 * register I/O on a specific device. 32 */ 33 #undef LOG_DEVICE 34 35 #ifdef LOG_DEVICE 36 static inline bool regmap_should_log(struct regmap *map) 37 { 38 return (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0); 39 } 40 #else 41 static inline bool regmap_should_log(struct regmap *map) { return false; } 42 #endif 43 44 45 static int _regmap_update_bits(struct regmap *map, unsigned int reg, 46 unsigned int mask, unsigned int val, 47 bool *change, bool force_write); 48 49 static int _regmap_bus_reg_read(void *context, unsigned int reg, 50 unsigned int *val); 51 static int _regmap_bus_read(void *context, unsigned int reg, 52 unsigned int *val); 53 static int _regmap_bus_formatted_write(void *context, unsigned int reg, 54 unsigned int val); 55 static int _regmap_bus_reg_write(void *context, unsigned int reg, 56 unsigned int val); 57 static int _regmap_bus_raw_write(void *context, unsigned int reg, 58 unsigned int val); 59 60 bool regmap_reg_in_ranges(unsigned int reg, 61 const struct regmap_range *ranges, 62 unsigned int nranges) 63 { 64 const struct regmap_range *r; 65 int i; 66 67 for (i = 0, r = ranges; i < nranges; i++, r++) 68 if (regmap_reg_in_range(reg, r)) 69 return true; 70 return false; 71 } 72 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges); 73 74 bool regmap_check_range_table(struct regmap *map, unsigned int reg, 75 const struct regmap_access_table *table) 76 { 77 /* Check "no ranges" first */ 78 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges)) 79 return false; 80 81 /* In case zero "yes ranges" are supplied, any reg is OK */ 82 if (!table->n_yes_ranges) 83 return true; 84 85 return regmap_reg_in_ranges(reg, table->yes_ranges, 86 table->n_yes_ranges); 87 } 88 EXPORT_SYMBOL_GPL(regmap_check_range_table); 89 90 bool regmap_writeable(struct regmap *map, unsigned int reg) 91 { 92 if (map->max_register_is_set && reg > map->max_register) 93 return false; 94 95 if (map->writeable_reg) 96 return map->writeable_reg(map->dev, reg); 97 98 if (map->wr_table) 99 return regmap_check_range_table(map, reg, map->wr_table); 100 101 return true; 102 } 103 104 bool regmap_cached(struct regmap *map, unsigned int reg) 105 { 106 int ret; 107 unsigned int val; 108 109 if (map->cache_type == REGCACHE_NONE) 110 return false; 111 112 if (!map->cache_ops) 113 return false; 114 115 if (map->max_register_is_set && reg > map->max_register) 116 return false; 117 118 scoped_guard(regmap, map) 119 ret = regcache_read(map, reg, &val); 120 if (ret) 121 return false; 122 123 return true; 124 } 125 126 bool regmap_readable(struct regmap *map, unsigned int reg) 127 { 128 if (!map->reg_read) 129 return false; 130 131 if (map->max_register_is_set && reg > map->max_register) 132 return false; 133 134 if (map->format.format_write) 135 return false; 136 137 if (map->readable_reg) 138 return map->readable_reg(map->dev, reg); 139 140 if (map->rd_table) 141 return regmap_check_range_table(map, reg, map->rd_table); 142 143 return true; 144 } 145 146 bool regmap_volatile(struct regmap *map, unsigned int reg) 147 { 148 if (!map->format.format_write && !regmap_readable(map, reg)) 149 return false; 150 151 if (map->volatile_reg) 152 return map->volatile_reg(map->dev, reg); 153 154 if (map->volatile_table) 155 return regmap_check_range_table(map, reg, map->volatile_table); 156 157 if (map->cache_ops) 158 return false; 159 else 160 return true; 161 } 162 163 bool regmap_precious(struct regmap *map, unsigned int reg) 164 { 165 if (!regmap_readable(map, reg)) 166 return false; 167 168 if (map->precious_reg) 169 return map->precious_reg(map->dev, reg); 170 171 if (map->precious_table) 172 return regmap_check_range_table(map, reg, map->precious_table); 173 174 return false; 175 } 176 177 bool regmap_writeable_noinc(struct regmap *map, unsigned int reg) 178 { 179 if (map->writeable_noinc_reg) 180 return map->writeable_noinc_reg(map->dev, reg); 181 182 if (map->wr_noinc_table) 183 return regmap_check_range_table(map, reg, map->wr_noinc_table); 184 185 return true; 186 } 187 188 bool regmap_readable_noinc(struct regmap *map, unsigned int reg) 189 { 190 if (map->readable_noinc_reg) 191 return map->readable_noinc_reg(map->dev, reg); 192 193 if (map->rd_noinc_table) 194 return regmap_check_range_table(map, reg, map->rd_noinc_table); 195 196 return true; 197 } 198 199 static bool regmap_volatile_range(struct regmap *map, unsigned int reg, 200 size_t num) 201 { 202 unsigned int i; 203 204 for (i = 0; i < num; i++) 205 if (!regmap_volatile(map, reg + regmap_get_offset(map, i))) 206 return false; 207 208 return true; 209 } 210 211 static void regmap_format_12_20_write(struct regmap *map, 212 unsigned int reg, unsigned int val) 213 { 214 u8 *out = map->work_buf; 215 216 out[0] = reg >> 4; 217 out[1] = (reg << 4) | (val >> 16); 218 out[2] = val >> 8; 219 out[3] = val; 220 } 221 222 223 static void regmap_format_2_6_write(struct regmap *map, 224 unsigned int reg, unsigned int val) 225 { 226 u8 *out = map->work_buf; 227 228 *out = (reg << 6) | val; 229 } 230 231 static void regmap_format_4_12_write(struct regmap *map, 232 unsigned int reg, unsigned int val) 233 { 234 __be16 *out = map->work_buf; 235 *out = cpu_to_be16((reg << 12) | val); 236 } 237 238 static void regmap_format_7_9_write(struct regmap *map, 239 unsigned int reg, unsigned int val) 240 { 241 __be16 *out = map->work_buf; 242 *out = cpu_to_be16((reg << 9) | val); 243 } 244 245 static void regmap_format_7_17_write(struct regmap *map, 246 unsigned int reg, unsigned int val) 247 { 248 u8 *out = map->work_buf; 249 250 out[2] = val; 251 out[1] = val >> 8; 252 out[0] = (val >> 16) | (reg << 1); 253 } 254 255 static void regmap_format_10_14_write(struct regmap *map, 256 unsigned int reg, unsigned int val) 257 { 258 u8 *out = map->work_buf; 259 260 out[2] = val; 261 out[1] = (val >> 8) | (reg << 6); 262 out[0] = reg >> 2; 263 } 264 265 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift) 266 { 267 u8 *b = buf; 268 269 b[0] = val << shift; 270 } 271 272 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift) 273 { 274 put_unaligned_be16(val << shift, buf); 275 } 276 277 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift) 278 { 279 put_unaligned_le16(val << shift, buf); 280 } 281 282 static void regmap_format_16_native(void *buf, unsigned int val, 283 unsigned int shift) 284 { 285 u16 v = val << shift; 286 287 memcpy(buf, &v, sizeof(v)); 288 } 289 290 static void regmap_format_24_be(void *buf, unsigned int val, unsigned int shift) 291 { 292 put_unaligned_be24(val << shift, buf); 293 } 294 295 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift) 296 { 297 put_unaligned_be32(val << shift, buf); 298 } 299 300 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift) 301 { 302 put_unaligned_le32(val << shift, buf); 303 } 304 305 static void regmap_format_32_native(void *buf, unsigned int val, 306 unsigned int shift) 307 { 308 u32 v = val << shift; 309 310 memcpy(buf, &v, sizeof(v)); 311 } 312 313 static void regmap_parse_inplace_noop(void *buf) 314 { 315 } 316 317 static unsigned int regmap_parse_8(const void *buf) 318 { 319 const u8 *b = buf; 320 321 return b[0]; 322 } 323 324 static unsigned int regmap_parse_16_be(const void *buf) 325 { 326 return get_unaligned_be16(buf); 327 } 328 329 static unsigned int regmap_parse_16_le(const void *buf) 330 { 331 return get_unaligned_le16(buf); 332 } 333 334 static void regmap_parse_16_be_inplace(void *buf) 335 { 336 u16 v = get_unaligned_be16(buf); 337 338 memcpy(buf, &v, sizeof(v)); 339 } 340 341 static void regmap_parse_16_le_inplace(void *buf) 342 { 343 u16 v = get_unaligned_le16(buf); 344 345 memcpy(buf, &v, sizeof(v)); 346 } 347 348 static unsigned int regmap_parse_16_native(const void *buf) 349 { 350 u16 v; 351 352 memcpy(&v, buf, sizeof(v)); 353 return v; 354 } 355 356 static unsigned int regmap_parse_24_be(const void *buf) 357 { 358 return get_unaligned_be24(buf); 359 } 360 361 static unsigned int regmap_parse_32_be(const void *buf) 362 { 363 return get_unaligned_be32(buf); 364 } 365 366 static unsigned int regmap_parse_32_le(const void *buf) 367 { 368 return get_unaligned_le32(buf); 369 } 370 371 static void regmap_parse_32_be_inplace(void *buf) 372 { 373 u32 v = get_unaligned_be32(buf); 374 375 memcpy(buf, &v, sizeof(v)); 376 } 377 378 static void regmap_parse_32_le_inplace(void *buf) 379 { 380 u32 v = get_unaligned_le32(buf); 381 382 memcpy(buf, &v, sizeof(v)); 383 } 384 385 static unsigned int regmap_parse_32_native(const void *buf) 386 { 387 u32 v; 388 389 memcpy(&v, buf, sizeof(v)); 390 return v; 391 } 392 393 static void regmap_lock_hwlock(void *__map) 394 { 395 struct regmap *map = __map; 396 397 hwspin_lock_timeout(map->hwlock, UINT_MAX); 398 } 399 400 static void regmap_lock_hwlock_irq(void *__map) 401 { 402 struct regmap *map = __map; 403 404 hwspin_lock_timeout_irq(map->hwlock, UINT_MAX); 405 } 406 407 static void regmap_lock_hwlock_irqsave(void *__map) 408 { 409 struct regmap *map = __map; 410 unsigned long flags = 0; 411 412 hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX, 413 &flags); 414 map->spinlock_flags = flags; 415 } 416 417 static void regmap_unlock_hwlock(void *__map) 418 { 419 struct regmap *map = __map; 420 421 hwspin_unlock(map->hwlock); 422 } 423 424 static void regmap_unlock_hwlock_irq(void *__map) 425 { 426 struct regmap *map = __map; 427 428 hwspin_unlock_irq(map->hwlock); 429 } 430 431 static void regmap_unlock_hwlock_irqrestore(void *__map) 432 { 433 struct regmap *map = __map; 434 435 hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags); 436 } 437 438 static void regmap_lock_unlock_none(void *__map) 439 { 440 441 } 442 443 static void regmap_lock_mutex(void *__map) 444 { 445 struct regmap *map = __map; 446 mutex_lock(&map->mutex); 447 } 448 449 static void regmap_unlock_mutex(void *__map) 450 { 451 struct regmap *map = __map; 452 mutex_unlock(&map->mutex); 453 } 454 455 static void regmap_lock_spinlock(void *__map) 456 __acquires(&map->spinlock) 457 { 458 struct regmap *map = __map; 459 unsigned long flags; 460 461 spin_lock_irqsave(&map->spinlock, flags); 462 map->spinlock_flags = flags; 463 } 464 465 static void regmap_unlock_spinlock(void *__map) 466 __releases(&map->spinlock) 467 { 468 struct regmap *map = __map; 469 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags); 470 } 471 472 static void regmap_lock_raw_spinlock(void *__map) 473 __acquires(&map->raw_spinlock) 474 { 475 struct regmap *map = __map; 476 unsigned long flags; 477 478 raw_spin_lock_irqsave(&map->raw_spinlock, flags); 479 map->raw_spinlock_flags = flags; 480 } 481 482 static void regmap_unlock_raw_spinlock(void *__map) 483 __releases(&map->raw_spinlock) 484 { 485 struct regmap *map = __map; 486 raw_spin_unlock_irqrestore(&map->raw_spinlock, map->raw_spinlock_flags); 487 } 488 489 static void dev_get_regmap_release(struct device *dev, void *res) 490 { 491 /* 492 * We don't actually have anything to do here; the goal here 493 * is not to manage the regmap but to provide a simple way to 494 * get the regmap back given a struct device. 495 */ 496 } 497 498 static bool _regmap_range_add(struct regmap *map, 499 struct regmap_range_node *data) 500 { 501 struct rb_root *root = &map->range_tree; 502 struct rb_node **new = &(root->rb_node), *parent = NULL; 503 504 while (*new) { 505 struct regmap_range_node *this = 506 rb_entry(*new, struct regmap_range_node, node); 507 508 parent = *new; 509 if (data->range_max < this->range_min) 510 new = &((*new)->rb_left); 511 else if (data->range_min > this->range_max) 512 new = &((*new)->rb_right); 513 else 514 return false; 515 } 516 517 rb_link_node(&data->node, parent, new); 518 rb_insert_color(&data->node, root); 519 520 return true; 521 } 522 523 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map, 524 unsigned int reg) 525 { 526 struct rb_node *node = map->range_tree.rb_node; 527 528 while (node) { 529 struct regmap_range_node *this = 530 rb_entry(node, struct regmap_range_node, node); 531 532 if (reg < this->range_min) 533 node = node->rb_left; 534 else if (reg > this->range_max) 535 node = node->rb_right; 536 else 537 return this; 538 } 539 540 return NULL; 541 } 542 543 static void regmap_range_exit(struct regmap *map) 544 { 545 struct rb_node *next; 546 struct regmap_range_node *range_node; 547 548 next = rb_first(&map->range_tree); 549 while (next) { 550 range_node = rb_entry(next, struct regmap_range_node, node); 551 next = rb_next(&range_node->node); 552 rb_erase(&range_node->node, &map->range_tree); 553 kfree(range_node); 554 } 555 556 kfree(map->selector_work_buf); 557 } 558 559 static int regmap_set_name(struct regmap *map, const struct regmap_config *config) 560 { 561 if (config->name) { 562 const char *name = kstrdup_const(config->name, GFP_KERNEL); 563 564 if (!name) 565 return -ENOMEM; 566 567 kfree_const(map->name); 568 map->name = name; 569 } 570 571 return 0; 572 } 573 574 int regmap_attach_dev(struct device *dev, struct regmap *map, 575 const struct regmap_config *config) 576 { 577 struct regmap **m; 578 int ret; 579 580 map->dev = dev; 581 582 ret = regmap_set_name(map, config); 583 if (ret) 584 return ret; 585 586 regmap_debugfs_exit(map); 587 regmap_debugfs_init(map); 588 589 /* Add a devres resource for dev_get_regmap() */ 590 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); 591 if (!m) { 592 regmap_debugfs_exit(map); 593 return -ENOMEM; 594 } 595 *m = map; 596 devres_add(dev, m); 597 598 return 0; 599 } 600 EXPORT_SYMBOL_GPL(regmap_attach_dev); 601 602 static int dev_get_regmap_match(struct device *dev, void *res, void *data); 603 604 static int regmap_detach_dev(struct device *dev, struct regmap *map) 605 { 606 if (!dev) 607 return 0; 608 609 return devres_release(dev, dev_get_regmap_release, 610 dev_get_regmap_match, (void *)map->name); 611 } 612 613 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus, 614 const struct regmap_config *config) 615 { 616 enum regmap_endian endian; 617 618 /* Retrieve the endianness specification from the regmap config */ 619 endian = config->reg_format_endian; 620 621 /* If the regmap config specified a non-default value, use that */ 622 if (endian != REGMAP_ENDIAN_DEFAULT) 623 return endian; 624 625 /* Retrieve the endianness specification from the bus config */ 626 if (bus && bus->reg_format_endian_default) 627 endian = bus->reg_format_endian_default; 628 629 /* If the bus specified a non-default value, use that */ 630 if (endian != REGMAP_ENDIAN_DEFAULT) 631 return endian; 632 633 /* Use this if no other value was found */ 634 return REGMAP_ENDIAN_BIG; 635 } 636 637 enum regmap_endian regmap_get_val_endian(struct device *dev, 638 const struct regmap_bus *bus, 639 const struct regmap_config *config) 640 { 641 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 642 enum regmap_endian endian; 643 644 /* Retrieve the endianness specification from the regmap config */ 645 endian = config->val_format_endian; 646 647 /* If the regmap config specified a non-default value, use that */ 648 if (endian != REGMAP_ENDIAN_DEFAULT) 649 return endian; 650 651 /* If the firmware node exist try to get endianness from it */ 652 if (fwnode_property_read_bool(fwnode, "big-endian")) 653 endian = REGMAP_ENDIAN_BIG; 654 else if (fwnode_property_read_bool(fwnode, "little-endian")) 655 endian = REGMAP_ENDIAN_LITTLE; 656 else if (fwnode_property_read_bool(fwnode, "native-endian")) 657 endian = REGMAP_ENDIAN_NATIVE; 658 659 /* If the endianness was specified in fwnode, use that */ 660 if (endian != REGMAP_ENDIAN_DEFAULT) 661 return endian; 662 663 /* Retrieve the endianness specification from the bus config */ 664 if (bus && bus->val_format_endian_default) 665 endian = bus->val_format_endian_default; 666 667 /* If the bus specified a non-default value, use that */ 668 if (endian != REGMAP_ENDIAN_DEFAULT) 669 return endian; 670 671 /* Use this if no other value was found */ 672 return REGMAP_ENDIAN_BIG; 673 } 674 EXPORT_SYMBOL_GPL(regmap_get_val_endian); 675 676 struct regmap *__regmap_init(struct device *dev, 677 const struct regmap_bus *bus, 678 void *bus_context, 679 const struct regmap_config *config, 680 struct lock_class_key *lock_key, 681 const char *lock_name) 682 { 683 struct regmap *map; 684 int ret = -EINVAL; 685 enum regmap_endian reg_endian, val_endian; 686 int i, j; 687 688 if (!config) 689 goto err; 690 691 map = kzalloc_obj(*map); 692 if (map == NULL) { 693 ret = -ENOMEM; 694 goto err; 695 } 696 697 ret = regmap_set_name(map, config); 698 if (ret) 699 goto err_map; 700 701 ret = -EINVAL; /* Later error paths rely on this */ 702 703 if (config->disable_locking) { 704 map->lock = map->unlock = regmap_lock_unlock_none; 705 map->can_sleep = config->can_sleep; 706 regmap_debugfs_disable(map); 707 } else if (config->lock && config->unlock) { 708 map->lock = config->lock; 709 map->unlock = config->unlock; 710 map->lock_arg = config->lock_arg; 711 map->can_sleep = config->can_sleep; 712 } else if (config->use_hwlock) { 713 map->hwlock = hwspin_lock_request_specific(config->hwlock_id); 714 if (!map->hwlock) { 715 ret = -ENXIO; 716 goto err_name; 717 } 718 719 switch (config->hwlock_mode) { 720 case HWLOCK_IRQSTATE: 721 map->lock = regmap_lock_hwlock_irqsave; 722 map->unlock = regmap_unlock_hwlock_irqrestore; 723 break; 724 case HWLOCK_IRQ: 725 map->lock = regmap_lock_hwlock_irq; 726 map->unlock = regmap_unlock_hwlock_irq; 727 break; 728 default: 729 map->lock = regmap_lock_hwlock; 730 map->unlock = regmap_unlock_hwlock; 731 break; 732 } 733 734 map->lock_arg = map; 735 } else { 736 if ((bus && bus->fast_io) || 737 config->fast_io) { 738 if (config->use_raw_spinlock) { 739 raw_spin_lock_init(&map->raw_spinlock); 740 map->lock = regmap_lock_raw_spinlock; 741 map->unlock = regmap_unlock_raw_spinlock; 742 lockdep_set_class_and_name(&map->raw_spinlock, 743 lock_key, lock_name); 744 } else { 745 spin_lock_init(&map->spinlock); 746 map->lock = regmap_lock_spinlock; 747 map->unlock = regmap_unlock_spinlock; 748 lockdep_set_class_and_name(&map->spinlock, 749 lock_key, lock_name); 750 } 751 } else { 752 mutex_init(&map->mutex); 753 map->lock = regmap_lock_mutex; 754 map->unlock = regmap_unlock_mutex; 755 map->can_sleep = true; 756 lockdep_set_class_and_name(&map->mutex, 757 lock_key, lock_name); 758 } 759 map->lock_arg = map; 760 map->lock_key = lock_key; 761 } 762 763 /* 764 * When we write in fast-paths with regmap_bulk_write() don't allocate 765 * scratch buffers with sleeping allocations. 766 */ 767 if ((bus && bus->fast_io) || config->fast_io) 768 map->alloc_flags = GFP_ATOMIC; 769 else 770 map->alloc_flags = GFP_KERNEL; 771 772 map->reg_base = config->reg_base; 773 map->reg_shift = config->pad_bits % 8; 774 775 map->format.pad_bytes = config->pad_bits / 8; 776 map->format.reg_shift = config->reg_shift; 777 map->format.reg_bytes = BITS_TO_BYTES(config->reg_bits); 778 map->format.val_bytes = BITS_TO_BYTES(config->val_bits); 779 map->format.buf_size = BITS_TO_BYTES(config->reg_bits + config->val_bits + config->pad_bits); 780 if (config->reg_stride) 781 map->reg_stride = config->reg_stride; 782 else 783 map->reg_stride = 1; 784 if (is_power_of_2(map->reg_stride)) 785 map->reg_stride_order = ilog2(map->reg_stride); 786 else 787 map->reg_stride_order = -1; 788 map->use_single_read = config->use_single_read || !(config->read || (bus && bus->read)); 789 map->use_single_write = config->use_single_write || !(config->write || (bus && bus->write)); 790 map->can_multi_write = config->can_multi_write && (config->write || (bus && bus->write)); 791 if (bus) { 792 map->max_raw_read = bus->max_raw_read; 793 map->max_raw_write = bus->max_raw_write; 794 } else if (config->max_raw_read && config->max_raw_write) { 795 map->max_raw_read = config->max_raw_read; 796 map->max_raw_write = config->max_raw_write; 797 } 798 map->dev = dev; 799 map->bus = bus; 800 map->bus_context = bus_context; 801 map->max_register = config->max_register; 802 map->max_register_is_set = map->max_register ?: config->max_register_is_0; 803 map->wr_table = config->wr_table; 804 map->rd_table = config->rd_table; 805 map->volatile_table = config->volatile_table; 806 map->precious_table = config->precious_table; 807 map->wr_noinc_table = config->wr_noinc_table; 808 map->rd_noinc_table = config->rd_noinc_table; 809 map->writeable_reg = config->writeable_reg; 810 map->readable_reg = config->readable_reg; 811 map->volatile_reg = config->volatile_reg; 812 map->precious_reg = config->precious_reg; 813 map->writeable_noinc_reg = config->writeable_noinc_reg; 814 map->readable_noinc_reg = config->readable_noinc_reg; 815 map->reg_default_cb = config->reg_default_cb; 816 map->cache_type = config->cache_type; 817 818 spin_lock_init(&map->async_lock); 819 INIT_LIST_HEAD(&map->async_list); 820 INIT_LIST_HEAD(&map->async_free); 821 init_waitqueue_head(&map->async_waitq); 822 823 if (config->read_flag_mask || 824 config->write_flag_mask || 825 config->zero_flag_mask) { 826 map->read_flag_mask = config->read_flag_mask; 827 map->write_flag_mask = config->write_flag_mask; 828 } else if (bus) { 829 map->read_flag_mask = bus->read_flag_mask; 830 } 831 832 if (config->read && config->write) { 833 map->reg_read = _regmap_bus_read; 834 if (config->reg_update_bits) 835 map->reg_update_bits = config->reg_update_bits; 836 837 /* Bulk read/write */ 838 map->read = config->read; 839 map->write = config->write; 840 841 reg_endian = REGMAP_ENDIAN_NATIVE; 842 val_endian = REGMAP_ENDIAN_NATIVE; 843 } else if (!bus) { 844 map->reg_read = config->reg_read; 845 map->reg_write = config->reg_write; 846 map->reg_update_bits = config->reg_update_bits; 847 848 map->defer_caching = false; 849 goto skip_format_initialization; 850 } else if (!bus->read || !bus->write) { 851 map->reg_read = _regmap_bus_reg_read; 852 map->reg_write = _regmap_bus_reg_write; 853 map->reg_update_bits = bus->reg_update_bits; 854 855 map->defer_caching = false; 856 goto skip_format_initialization; 857 } else { 858 map->reg_read = _regmap_bus_read; 859 map->reg_update_bits = bus->reg_update_bits; 860 /* Bulk read/write */ 861 map->read = bus->read; 862 map->write = bus->write; 863 864 reg_endian = regmap_get_reg_endian(bus, config); 865 val_endian = regmap_get_val_endian(dev, bus, config); 866 } 867 868 switch (config->reg_bits + map->reg_shift) { 869 case 2: 870 switch (config->val_bits) { 871 case 6: 872 map->format.format_write = regmap_format_2_6_write; 873 break; 874 default: 875 goto err_hwlock; 876 } 877 break; 878 879 case 4: 880 switch (config->val_bits) { 881 case 12: 882 map->format.format_write = regmap_format_4_12_write; 883 break; 884 default: 885 goto err_hwlock; 886 } 887 break; 888 889 case 7: 890 switch (config->val_bits) { 891 case 9: 892 map->format.format_write = regmap_format_7_9_write; 893 break; 894 case 17: 895 map->format.format_write = regmap_format_7_17_write; 896 break; 897 default: 898 goto err_hwlock; 899 } 900 break; 901 902 case 10: 903 switch (config->val_bits) { 904 case 14: 905 map->format.format_write = regmap_format_10_14_write; 906 break; 907 default: 908 goto err_hwlock; 909 } 910 break; 911 912 case 12: 913 switch (config->val_bits) { 914 case 20: 915 map->format.format_write = regmap_format_12_20_write; 916 break; 917 default: 918 goto err_hwlock; 919 } 920 break; 921 922 case 8: 923 map->format.format_reg = regmap_format_8; 924 break; 925 926 case 16: 927 switch (reg_endian) { 928 case REGMAP_ENDIAN_BIG: 929 map->format.format_reg = regmap_format_16_be; 930 break; 931 case REGMAP_ENDIAN_LITTLE: 932 map->format.format_reg = regmap_format_16_le; 933 break; 934 case REGMAP_ENDIAN_NATIVE: 935 map->format.format_reg = regmap_format_16_native; 936 break; 937 default: 938 goto err_hwlock; 939 } 940 break; 941 942 case 24: 943 switch (reg_endian) { 944 case REGMAP_ENDIAN_BIG: 945 map->format.format_reg = regmap_format_24_be; 946 break; 947 default: 948 goto err_hwlock; 949 } 950 break; 951 952 case 32: 953 switch (reg_endian) { 954 case REGMAP_ENDIAN_BIG: 955 map->format.format_reg = regmap_format_32_be; 956 break; 957 case REGMAP_ENDIAN_LITTLE: 958 map->format.format_reg = regmap_format_32_le; 959 break; 960 case REGMAP_ENDIAN_NATIVE: 961 map->format.format_reg = regmap_format_32_native; 962 break; 963 default: 964 goto err_hwlock; 965 } 966 break; 967 968 default: 969 goto err_hwlock; 970 } 971 972 if (val_endian == REGMAP_ENDIAN_NATIVE) 973 map->format.parse_inplace = regmap_parse_inplace_noop; 974 975 switch (config->val_bits) { 976 case 8: 977 map->format.format_val = regmap_format_8; 978 map->format.parse_val = regmap_parse_8; 979 map->format.parse_inplace = regmap_parse_inplace_noop; 980 break; 981 case 16: 982 switch (val_endian) { 983 case REGMAP_ENDIAN_BIG: 984 map->format.format_val = regmap_format_16_be; 985 map->format.parse_val = regmap_parse_16_be; 986 map->format.parse_inplace = regmap_parse_16_be_inplace; 987 break; 988 case REGMAP_ENDIAN_LITTLE: 989 map->format.format_val = regmap_format_16_le; 990 map->format.parse_val = regmap_parse_16_le; 991 map->format.parse_inplace = regmap_parse_16_le_inplace; 992 break; 993 case REGMAP_ENDIAN_NATIVE: 994 map->format.format_val = regmap_format_16_native; 995 map->format.parse_val = regmap_parse_16_native; 996 break; 997 default: 998 goto err_hwlock; 999 } 1000 break; 1001 case 24: 1002 switch (val_endian) { 1003 case REGMAP_ENDIAN_BIG: 1004 map->format.format_val = regmap_format_24_be; 1005 map->format.parse_val = regmap_parse_24_be; 1006 break; 1007 default: 1008 goto err_hwlock; 1009 } 1010 break; 1011 case 32: 1012 switch (val_endian) { 1013 case REGMAP_ENDIAN_BIG: 1014 map->format.format_val = regmap_format_32_be; 1015 map->format.parse_val = regmap_parse_32_be; 1016 map->format.parse_inplace = regmap_parse_32_be_inplace; 1017 break; 1018 case REGMAP_ENDIAN_LITTLE: 1019 map->format.format_val = regmap_format_32_le; 1020 map->format.parse_val = regmap_parse_32_le; 1021 map->format.parse_inplace = regmap_parse_32_le_inplace; 1022 break; 1023 case REGMAP_ENDIAN_NATIVE: 1024 map->format.format_val = regmap_format_32_native; 1025 map->format.parse_val = regmap_parse_32_native; 1026 break; 1027 default: 1028 goto err_hwlock; 1029 } 1030 break; 1031 } 1032 1033 if (map->format.format_write) { 1034 if ((reg_endian != REGMAP_ENDIAN_BIG) || 1035 (val_endian != REGMAP_ENDIAN_BIG)) 1036 goto err_hwlock; 1037 map->use_single_write = true; 1038 } 1039 1040 if (!map->format.format_write && 1041 !(map->format.format_reg && map->format.format_val)) 1042 goto err_hwlock; 1043 1044 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); 1045 if (map->work_buf == NULL) { 1046 ret = -ENOMEM; 1047 goto err_hwlock; 1048 } 1049 1050 if (map->format.format_write) { 1051 map->defer_caching = false; 1052 map->reg_write = _regmap_bus_formatted_write; 1053 } else if (map->format.format_val) { 1054 map->defer_caching = true; 1055 map->reg_write = _regmap_bus_raw_write; 1056 } 1057 1058 skip_format_initialization: 1059 1060 map->range_tree = RB_ROOT; 1061 for (i = 0; i < config->num_ranges; i++) { 1062 const struct regmap_range_cfg *range_cfg = &config->ranges[i]; 1063 struct regmap_range_node *new; 1064 1065 /* Sanity check */ 1066 if (range_cfg->range_max < range_cfg->range_min) { 1067 dev_err(map->dev, "Invalid range %d: %u < %u\n", i, 1068 range_cfg->range_max, range_cfg->range_min); 1069 goto err_range; 1070 } 1071 1072 if (range_cfg->range_max > map->max_register) { 1073 dev_err(map->dev, "Invalid range %d: %u > %u\n", i, 1074 range_cfg->range_max, map->max_register); 1075 goto err_range; 1076 } 1077 1078 if (range_cfg->selector_reg > map->max_register) { 1079 dev_err(map->dev, 1080 "Invalid range %d: selector out of map\n", i); 1081 goto err_range; 1082 } 1083 1084 if (range_cfg->window_len == 0) { 1085 dev_err(map->dev, "Invalid range %d: window_len 0\n", 1086 i); 1087 goto err_range; 1088 } 1089 1090 /* Make sure, that this register range has no selector 1091 or data window within its boundary */ 1092 for (j = 0; j < config->num_ranges; j++) { 1093 unsigned int sel_reg = config->ranges[j].selector_reg; 1094 unsigned int win_min = config->ranges[j].window_start; 1095 unsigned int win_max = win_min + 1096 config->ranges[j].window_len - 1; 1097 1098 /* Allow data window inside its own virtual range */ 1099 if (j == i) 1100 continue; 1101 1102 if (range_cfg->range_min <= sel_reg && 1103 sel_reg <= range_cfg->range_max) { 1104 dev_err(map->dev, 1105 "Range %d: selector for %d in window\n", 1106 i, j); 1107 goto err_range; 1108 } 1109 1110 if (!(win_max < range_cfg->range_min || 1111 win_min > range_cfg->range_max)) { 1112 dev_err(map->dev, 1113 "Range %d: window for %d in window\n", 1114 i, j); 1115 goto err_range; 1116 } 1117 } 1118 1119 new = kzalloc_obj(*new); 1120 if (new == NULL) { 1121 ret = -ENOMEM; 1122 goto err_range; 1123 } 1124 1125 new->map = map; 1126 new->name = range_cfg->name; 1127 new->range_min = range_cfg->range_min; 1128 new->range_max = range_cfg->range_max; 1129 new->selector_reg = range_cfg->selector_reg; 1130 new->selector_mask = range_cfg->selector_mask; 1131 new->selector_shift = range_cfg->selector_shift; 1132 new->window_start = range_cfg->window_start; 1133 new->window_len = range_cfg->window_len; 1134 1135 if (!_regmap_range_add(map, new)) { 1136 dev_err(map->dev, "Failed to add range %d\n", i); 1137 kfree(new); 1138 goto err_range; 1139 } 1140 1141 if (map->selector_work_buf == NULL) { 1142 map->selector_work_buf = 1143 kzalloc(map->format.buf_size, GFP_KERNEL); 1144 if (map->selector_work_buf == NULL) { 1145 ret = -ENOMEM; 1146 goto err_range; 1147 } 1148 } 1149 } 1150 1151 ret = regcache_init(map, config); 1152 if (ret != 0) 1153 goto err_range; 1154 1155 if (dev) { 1156 ret = regmap_attach_dev(dev, map, config); 1157 if (ret != 0) 1158 goto err_regcache; 1159 } else { 1160 regmap_debugfs_init(map); 1161 } 1162 1163 return map; 1164 1165 err_regcache: 1166 regcache_exit(map); 1167 err_range: 1168 regmap_range_exit(map); 1169 kfree(map->work_buf); 1170 err_hwlock: 1171 if (map->hwlock) 1172 hwspin_lock_free(map->hwlock); 1173 err_name: 1174 kfree_const(map->name); 1175 err_map: 1176 kfree(map); 1177 err: 1178 if (bus && bus->free_on_exit) 1179 kfree(bus); 1180 return ERR_PTR(ret); 1181 } 1182 EXPORT_SYMBOL_GPL(__regmap_init); 1183 1184 static void devm_regmap_release(void *regmap) 1185 { 1186 regmap_exit(regmap); 1187 } 1188 1189 struct regmap *__devm_regmap_init(struct device *dev, 1190 const struct regmap_bus *bus, 1191 void *bus_context, 1192 const struct regmap_config *config, 1193 struct lock_class_key *lock_key, 1194 const char *lock_name) 1195 { 1196 struct regmap *regmap; 1197 int ret; 1198 1199 regmap = __regmap_init(dev, bus, bus_context, config, 1200 lock_key, lock_name); 1201 if (IS_ERR(regmap)) 1202 return regmap; 1203 1204 ret = devm_add_action_or_reset(dev, devm_regmap_release, regmap); 1205 if (ret) 1206 return ERR_PTR(ret); 1207 1208 return regmap; 1209 } 1210 EXPORT_SYMBOL_GPL(__devm_regmap_init); 1211 1212 static void regmap_field_init(struct regmap_field *rm_field, 1213 struct regmap *regmap, struct reg_field reg_field) 1214 { 1215 rm_field->regmap = regmap; 1216 rm_field->reg = reg_field.reg; 1217 rm_field->shift = reg_field.lsb; 1218 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb); 1219 1220 WARN_ONCE(rm_field->mask == 0, "invalid empty mask defined\n"); 1221 1222 rm_field->id_size = reg_field.id_size; 1223 rm_field->id_offset = reg_field.id_offset; 1224 } 1225 1226 /** 1227 * devm_regmap_field_alloc() - Allocate and initialise a register field. 1228 * 1229 * @dev: Device that will be interacted with 1230 * @regmap: regmap bank in which this register field is located. 1231 * @reg_field: Register field with in the bank. 1232 * 1233 * The return value will be an ERR_PTR() on error or a valid pointer 1234 * to a struct regmap_field. The regmap_field will be automatically freed 1235 * by the device management code. 1236 */ 1237 struct regmap_field *devm_regmap_field_alloc(struct device *dev, 1238 struct regmap *regmap, struct reg_field reg_field) 1239 { 1240 struct regmap_field *rm_field = devm_kzalloc_obj(dev, *rm_field); 1241 if (!rm_field) 1242 return ERR_PTR(-ENOMEM); 1243 1244 regmap_field_init(rm_field, regmap, reg_field); 1245 1246 return rm_field; 1247 1248 } 1249 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc); 1250 1251 1252 /** 1253 * regmap_field_bulk_alloc() - Allocate and initialise a bulk register field. 1254 * 1255 * @regmap: regmap bank in which this register field is located. 1256 * @rm_field: regmap register fields within the bank. 1257 * @reg_field: Register fields within the bank. 1258 * @num_fields: Number of register fields. 1259 * 1260 * The return value will be an -ENOMEM on error or zero for success. 1261 * Newly allocated regmap_fields should be freed by calling 1262 * regmap_field_bulk_free() 1263 */ 1264 int regmap_field_bulk_alloc(struct regmap *regmap, 1265 struct regmap_field **rm_field, 1266 const struct reg_field *reg_field, 1267 int num_fields) 1268 { 1269 struct regmap_field *rf; 1270 int i; 1271 1272 rf = kzalloc_objs(*rf, num_fields); 1273 if (!rf) 1274 return -ENOMEM; 1275 1276 for (i = 0; i < num_fields; i++) { 1277 regmap_field_init(&rf[i], regmap, reg_field[i]); 1278 rm_field[i] = &rf[i]; 1279 } 1280 1281 return 0; 1282 } 1283 EXPORT_SYMBOL_GPL(regmap_field_bulk_alloc); 1284 1285 /** 1286 * devm_regmap_field_bulk_alloc() - Allocate and initialise a bulk register 1287 * fields. 1288 * 1289 * @dev: Device that will be interacted with 1290 * @regmap: regmap bank in which this register field is located. 1291 * @rm_field: regmap register fields within the bank. 1292 * @reg_field: Register fields within the bank. 1293 * @num_fields: Number of register fields. 1294 * 1295 * The return value will be an -ENOMEM on error or zero for success. 1296 * Newly allocated regmap_fields will be automatically freed by the 1297 * device management code. 1298 */ 1299 int devm_regmap_field_bulk_alloc(struct device *dev, 1300 struct regmap *regmap, 1301 struct regmap_field **rm_field, 1302 const struct reg_field *reg_field, 1303 int num_fields) 1304 { 1305 struct regmap_field *rf; 1306 int i; 1307 1308 rf = devm_kzalloc_objs(dev, *rf, num_fields); 1309 if (!rf) 1310 return -ENOMEM; 1311 1312 for (i = 0; i < num_fields; i++) { 1313 regmap_field_init(&rf[i], regmap, reg_field[i]); 1314 rm_field[i] = &rf[i]; 1315 } 1316 1317 return 0; 1318 } 1319 EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_alloc); 1320 1321 /** 1322 * regmap_field_bulk_free() - Free register field allocated using 1323 * regmap_field_bulk_alloc. 1324 * 1325 * @field: regmap fields which should be freed. 1326 */ 1327 void regmap_field_bulk_free(struct regmap_field *field) 1328 { 1329 kfree(field); 1330 } 1331 EXPORT_SYMBOL_GPL(regmap_field_bulk_free); 1332 1333 /** 1334 * devm_regmap_field_bulk_free() - Free a bulk register field allocated using 1335 * devm_regmap_field_bulk_alloc. 1336 * 1337 * @dev: Device that will be interacted with 1338 * @field: regmap field which should be freed. 1339 * 1340 * Free register field allocated using devm_regmap_field_bulk_alloc(). Usually 1341 * drivers need not call this function, as the memory allocated via devm 1342 * will be freed as per device-driver life-cycle. 1343 */ 1344 void devm_regmap_field_bulk_free(struct device *dev, 1345 struct regmap_field *field) 1346 { 1347 devm_kfree(dev, field); 1348 } 1349 EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_free); 1350 1351 /** 1352 * devm_regmap_field_free() - Free a register field allocated using 1353 * devm_regmap_field_alloc. 1354 * 1355 * @dev: Device that will be interacted with 1356 * @field: regmap field which should be freed. 1357 * 1358 * Free register field allocated using devm_regmap_field_alloc(). Usually 1359 * drivers need not call this function, as the memory allocated via devm 1360 * will be freed as per device-driver life-cyle. 1361 */ 1362 void devm_regmap_field_free(struct device *dev, 1363 struct regmap_field *field) 1364 { 1365 devm_kfree(dev, field); 1366 } 1367 EXPORT_SYMBOL_GPL(devm_regmap_field_free); 1368 1369 /** 1370 * regmap_field_alloc() - Allocate and initialise a register field. 1371 * 1372 * @regmap: regmap bank in which this register field is located. 1373 * @reg_field: Register field with in the bank. 1374 * 1375 * The return value will be an ERR_PTR() on error or a valid pointer 1376 * to a struct regmap_field. The regmap_field should be freed by the 1377 * user once its finished working with it using regmap_field_free(). 1378 */ 1379 struct regmap_field *regmap_field_alloc(struct regmap *regmap, 1380 struct reg_field reg_field) 1381 { 1382 struct regmap_field *rm_field = kzalloc_obj(*rm_field); 1383 1384 if (!rm_field) 1385 return ERR_PTR(-ENOMEM); 1386 1387 regmap_field_init(rm_field, regmap, reg_field); 1388 1389 return rm_field; 1390 } 1391 EXPORT_SYMBOL_GPL(regmap_field_alloc); 1392 1393 /** 1394 * regmap_field_free() - Free register field allocated using 1395 * regmap_field_alloc. 1396 * 1397 * @field: regmap field which should be freed. 1398 */ 1399 void regmap_field_free(struct regmap_field *field) 1400 { 1401 kfree(field); 1402 } 1403 EXPORT_SYMBOL_GPL(regmap_field_free); 1404 1405 /** 1406 * regmap_reinit_cache() - Reinitialise the current register cache 1407 * 1408 * @map: Register map to operate on. 1409 * @config: New configuration. Only the cache data will be used. 1410 * 1411 * Discard any existing register cache for the map and initialize a 1412 * new cache. This can be used to restore the cache to defaults or to 1413 * update the cache configuration to reflect runtime discovery of the 1414 * hardware. 1415 * 1416 * No explicit locking is done here, the user needs to ensure that 1417 * this function will not race with other calls to regmap. 1418 */ 1419 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) 1420 { 1421 int ret; 1422 1423 regmap_debugfs_exit(map); 1424 regcache_exit(map); 1425 1426 map->max_register = config->max_register; 1427 map->max_register_is_set = map->max_register ?: config->max_register_is_0; 1428 map->writeable_reg = config->writeable_reg; 1429 map->readable_reg = config->readable_reg; 1430 map->volatile_reg = config->volatile_reg; 1431 map->precious_reg = config->precious_reg; 1432 map->writeable_noinc_reg = config->writeable_noinc_reg; 1433 map->readable_noinc_reg = config->readable_noinc_reg; 1434 map->reg_default_cb = config->reg_default_cb; 1435 map->cache_type = config->cache_type; 1436 1437 ret = regmap_set_name(map, config); 1438 if (ret) 1439 return ret; 1440 1441 map->cache_bypass = false; 1442 map->cache_only = false; 1443 1444 ret = regcache_init(map, config); 1445 if (ret) 1446 return ret; 1447 1448 regmap_debugfs_init(map); 1449 1450 return 0; 1451 } 1452 EXPORT_SYMBOL_GPL(regmap_reinit_cache); 1453 1454 /** 1455 * regmap_exit() - Free a previously allocated register map 1456 * 1457 * @map: Register map to operate on. 1458 */ 1459 void regmap_exit(struct regmap *map) 1460 { 1461 struct regmap_async *async; 1462 1463 regmap_detach_dev(map->dev, map); 1464 regmap_debugfs_exit(map); 1465 regcache_exit(map); 1466 regmap_range_exit(map); 1467 if (map->bus && map->bus->free_context) 1468 map->bus->free_context(map->bus_context); 1469 kfree(map->work_buf); 1470 while (!list_empty(&map->async_free)) { 1471 async = list_first_entry_or_null(&map->async_free, 1472 struct regmap_async, 1473 list); 1474 list_del(&async->list); 1475 kfree(async->work_buf); 1476 kfree(async); 1477 } 1478 if (map->hwlock) 1479 hwspin_lock_free(map->hwlock); 1480 if (map->lock == regmap_lock_mutex) 1481 mutex_destroy(&map->mutex); 1482 kfree_const(map->name); 1483 kfree(map->patch); 1484 if (map->bus && map->bus->free_on_exit) 1485 kfree(map->bus); 1486 kfree(map); 1487 } 1488 EXPORT_SYMBOL_GPL(regmap_exit); 1489 1490 static int dev_get_regmap_match(struct device *dev, void *res, void *data) 1491 { 1492 struct regmap **r = res; 1493 if (!r || !*r) { 1494 WARN_ON(!r || !*r); 1495 return 0; 1496 } 1497 1498 /* If the user didn't specify a name match any */ 1499 if (data) 1500 return (*r)->name && !strcmp((*r)->name, data); 1501 else 1502 return 1; 1503 } 1504 1505 /** 1506 * dev_get_regmap() - Obtain the regmap (if any) for a device 1507 * 1508 * @dev: Device to retrieve the map for 1509 * @name: Optional name for the register map, usually NULL. 1510 * 1511 * Returns the regmap for the device if one is present, or NULL. If 1512 * name is specified then it must match the name specified when 1513 * registering the device, if it is NULL then the first regmap found 1514 * will be used. Devices with multiple register maps are very rare, 1515 * generic code should normally not need to specify a name. 1516 */ 1517 struct regmap *dev_get_regmap(struct device *dev, const char *name) 1518 { 1519 struct regmap **r = devres_find(dev, dev_get_regmap_release, 1520 dev_get_regmap_match, (void *)name); 1521 1522 if (!r) 1523 return NULL; 1524 return *r; 1525 } 1526 EXPORT_SYMBOL_GPL(dev_get_regmap); 1527 1528 /** 1529 * regmap_get_device() - Obtain the device from a regmap 1530 * 1531 * @map: Register map to operate on. 1532 * 1533 * Returns the underlying device that the regmap has been created for. 1534 */ 1535 struct device *regmap_get_device(struct regmap *map) 1536 { 1537 return map->dev; 1538 } 1539 EXPORT_SYMBOL_GPL(regmap_get_device); 1540 1541 static int _regmap_select_page(struct regmap *map, unsigned int *reg, 1542 struct regmap_range_node *range, 1543 unsigned int val_num) 1544 { 1545 void *orig_work_buf; 1546 unsigned int selector_reg; 1547 unsigned int win_offset; 1548 unsigned int win_page; 1549 bool page_chg; 1550 int ret; 1551 1552 win_offset = (*reg - range->range_min) % range->window_len; 1553 win_page = (*reg - range->range_min) / range->window_len; 1554 1555 if (val_num > 1) { 1556 /* Bulk write shouldn't cross range boundary */ 1557 if (*reg + val_num - 1 > range->range_max) 1558 return -EINVAL; 1559 1560 /* ... or single page boundary */ 1561 if (val_num > range->window_len - win_offset) 1562 return -EINVAL; 1563 } 1564 1565 /* 1566 * Calculate the address of the selector register in the corresponding 1567 * data window if it is located on every page. 1568 */ 1569 page_chg = in_range(range->selector_reg, range->window_start, range->window_len); 1570 if (page_chg) 1571 selector_reg = range->range_min + win_page * range->window_len + 1572 range->selector_reg - range->window_start; 1573 1574 /* 1575 * It is possible to have selector register inside data window. 1576 * In that case, selector register is located on every page and it 1577 * needs no page switching, when accessed alone. 1578 * 1579 * Nevertheless we should synchronize the cache values for it. 1580 * This can't be properly achieved if the selector register is 1581 * the first and the only one to be read inside the data window. 1582 * That's why we update it in that case as well. 1583 * 1584 * However, we specifically avoid updating it for the default page, 1585 * when it's overlapped with the real data window, to prevent from 1586 * infinite looping. 1587 */ 1588 if (val_num > 1 || 1589 (page_chg && selector_reg != range->selector_reg) || 1590 range->window_start + win_offset != range->selector_reg) { 1591 /* Use separate work_buf during page switching */ 1592 orig_work_buf = map->work_buf; 1593 map->work_buf = map->selector_work_buf; 1594 1595 ret = _regmap_update_bits(map, range->selector_reg, 1596 range->selector_mask, 1597 win_page << range->selector_shift, 1598 NULL, false); 1599 1600 map->work_buf = orig_work_buf; 1601 1602 if (ret != 0) 1603 return ret; 1604 } 1605 1606 *reg = range->window_start + win_offset; 1607 1608 return 0; 1609 } 1610 1611 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes, 1612 unsigned long mask) 1613 { 1614 u8 *buf; 1615 int i; 1616 1617 if (!mask || !map->work_buf) 1618 return; 1619 1620 buf = map->work_buf; 1621 1622 for (i = 0; i < max_bytes; i++) 1623 buf[i] |= (mask >> (8 * i)) & 0xff; 1624 } 1625 1626 static unsigned int regmap_reg_addr(struct regmap *map, unsigned int reg) 1627 { 1628 reg += map->reg_base; 1629 1630 if (map->format.reg_shift > 0) 1631 reg >>= map->format.reg_shift; 1632 else if (map->format.reg_shift < 0) 1633 reg <<= -(map->format.reg_shift); 1634 1635 return reg; 1636 } 1637 1638 static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg, 1639 const void *val, size_t val_len, bool noinc) 1640 { 1641 struct regmap_range_node *range; 1642 unsigned long flags; 1643 void *work_val = map->work_buf + map->format.reg_bytes + 1644 map->format.pad_bytes; 1645 void *buf; 1646 int ret = -ENOTSUPP; 1647 size_t len; 1648 int i; 1649 1650 /* Check for unwritable or noinc registers in range 1651 * before we start 1652 */ 1653 if (!regmap_writeable_noinc(map, reg)) { 1654 for (i = 0; i < val_len / map->format.val_bytes; i++) { 1655 unsigned int element = 1656 reg + regmap_get_offset(map, i); 1657 if (!regmap_writeable(map, element) || 1658 regmap_writeable_noinc(map, element)) 1659 return -EINVAL; 1660 } 1661 } 1662 1663 if (!map->cache_bypass && map->format.parse_val) { 1664 unsigned int ival, offset; 1665 int val_bytes = map->format.val_bytes; 1666 1667 /* Cache the last written value for noinc writes */ 1668 i = noinc ? val_len - val_bytes : 0; 1669 for (; i < val_len; i += val_bytes) { 1670 ival = map->format.parse_val(val + i); 1671 offset = noinc ? 0 : regmap_get_offset(map, i / val_bytes); 1672 ret = regcache_write(map, reg + offset, ival); 1673 if (ret) { 1674 dev_err(map->dev, 1675 "Error in caching of register: %x ret: %d\n", 1676 reg + offset, ret); 1677 return ret; 1678 } 1679 } 1680 if (map->cache_only) { 1681 map->cache_dirty = true; 1682 return 0; 1683 } 1684 } 1685 1686 range = _regmap_range_lookup(map, reg); 1687 if (range) { 1688 int val_num = val_len / map->format.val_bytes; 1689 int win_offset = (reg - range->range_min) % range->window_len; 1690 int win_residue = range->window_len - win_offset; 1691 1692 /* If the write goes beyond the end of the window split it */ 1693 while (val_num > win_residue) { 1694 dev_dbg(map->dev, "Writing window %d/%zu\n", 1695 win_residue, val_len / map->format.val_bytes); 1696 ret = _regmap_raw_write_impl(map, reg, val, 1697 win_residue * 1698 map->format.val_bytes, noinc); 1699 if (ret != 0) 1700 return ret; 1701 1702 reg += win_residue; 1703 val_num -= win_residue; 1704 val += win_residue * map->format.val_bytes; 1705 val_len -= win_residue * map->format.val_bytes; 1706 1707 win_offset = (reg - range->range_min) % 1708 range->window_len; 1709 win_residue = range->window_len - win_offset; 1710 } 1711 1712 ret = _regmap_select_page(map, ®, range, noinc ? 1 : val_num); 1713 if (ret != 0) 1714 return ret; 1715 } 1716 1717 reg = regmap_reg_addr(map, reg); 1718 map->format.format_reg(map->work_buf, reg, map->reg_shift); 1719 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes, 1720 map->write_flag_mask); 1721 1722 /* 1723 * Essentially all I/O mechanisms will be faster with a single 1724 * buffer to write. Since register syncs often generate raw 1725 * writes of single registers optimise that case. 1726 */ 1727 if (val != work_val && val_len == map->format.val_bytes) { 1728 memcpy(work_val, val, map->format.val_bytes); 1729 val = work_val; 1730 } 1731 1732 if (map->async && map->bus && map->bus->async_write) { 1733 struct regmap_async *async; 1734 1735 trace_regmap_async_write_start(map, reg, val_len); 1736 1737 spin_lock_irqsave(&map->async_lock, flags); 1738 async = list_first_entry_or_null(&map->async_free, 1739 struct regmap_async, 1740 list); 1741 if (async) 1742 list_del(&async->list); 1743 spin_unlock_irqrestore(&map->async_lock, flags); 1744 1745 if (!async) { 1746 async = map->bus->async_alloc(); 1747 if (!async) 1748 return -ENOMEM; 1749 1750 async->work_buf = kzalloc(map->format.buf_size, 1751 GFP_KERNEL | GFP_DMA); 1752 if (!async->work_buf) { 1753 kfree(async); 1754 return -ENOMEM; 1755 } 1756 } 1757 1758 async->map = map; 1759 1760 /* If the caller supplied the value we can use it safely. */ 1761 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes + 1762 map->format.reg_bytes + map->format.val_bytes); 1763 1764 spin_lock_irqsave(&map->async_lock, flags); 1765 list_add_tail(&async->list, &map->async_list); 1766 spin_unlock_irqrestore(&map->async_lock, flags); 1767 1768 if (val != work_val) 1769 ret = map->bus->async_write(map->bus_context, 1770 async->work_buf, 1771 map->format.reg_bytes + 1772 map->format.pad_bytes, 1773 val, val_len, async); 1774 else 1775 ret = map->bus->async_write(map->bus_context, 1776 async->work_buf, 1777 map->format.reg_bytes + 1778 map->format.pad_bytes + 1779 val_len, NULL, 0, async); 1780 1781 if (ret != 0) { 1782 dev_err(map->dev, "Failed to schedule write: %d\n", 1783 ret); 1784 1785 spin_lock_irqsave(&map->async_lock, flags); 1786 list_move(&async->list, &map->async_free); 1787 spin_unlock_irqrestore(&map->async_lock, flags); 1788 } 1789 1790 return ret; 1791 } 1792 1793 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes); 1794 1795 /* If we're doing a single register write we can probably just 1796 * send the work_buf directly, otherwise try to do a gather 1797 * write. 1798 */ 1799 if (val == work_val) 1800 ret = map->write(map->bus_context, map->work_buf, 1801 map->format.reg_bytes + 1802 map->format.pad_bytes + 1803 val_len); 1804 else if (map->bus && map->bus->gather_write) 1805 ret = map->bus->gather_write(map->bus_context, map->work_buf, 1806 map->format.reg_bytes + 1807 map->format.pad_bytes, 1808 val, val_len); 1809 else 1810 ret = -ENOTSUPP; 1811 1812 /* If that didn't work fall back on linearising by hand. */ 1813 if (ret == -ENOTSUPP) { 1814 len = map->format.reg_bytes + map->format.pad_bytes + val_len; 1815 buf = kzalloc(len, GFP_KERNEL); 1816 if (!buf) 1817 return -ENOMEM; 1818 1819 memcpy(buf, map->work_buf, map->format.reg_bytes); 1820 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, 1821 val, val_len); 1822 ret = map->write(map->bus_context, buf, len); 1823 1824 kfree(buf); 1825 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) { 1826 /* regcache_drop_region() takes lock that we already have, 1827 * thus call map->cache_ops->drop() directly 1828 */ 1829 if (map->cache_ops && map->cache_ops->drop) 1830 map->cache_ops->drop(map, reg, reg + 1); 1831 } 1832 1833 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes); 1834 1835 return ret; 1836 } 1837 1838 /** 1839 * regmap_can_raw_write - Test if regmap_raw_write() is supported 1840 * 1841 * @map: Map to check. 1842 */ 1843 bool regmap_can_raw_write(struct regmap *map) 1844 { 1845 return map->write && map->format.format_val && map->format.format_reg; 1846 } 1847 EXPORT_SYMBOL_GPL(regmap_can_raw_write); 1848 1849 /** 1850 * regmap_get_raw_read_max - Get the maximum size we can read 1851 * 1852 * @map: Map to check. 1853 */ 1854 size_t regmap_get_raw_read_max(struct regmap *map) 1855 { 1856 return map->max_raw_read; 1857 } 1858 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max); 1859 1860 /** 1861 * regmap_get_raw_write_max - Get the maximum size we can read 1862 * 1863 * @map: Map to check. 1864 */ 1865 size_t regmap_get_raw_write_max(struct regmap *map) 1866 { 1867 return map->max_raw_write; 1868 } 1869 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max); 1870 1871 static int _regmap_bus_formatted_write(void *context, unsigned int reg, 1872 unsigned int val) 1873 { 1874 int ret; 1875 struct regmap_range_node *range; 1876 struct regmap *map = context; 1877 1878 WARN_ON(!map->format.format_write); 1879 1880 range = _regmap_range_lookup(map, reg); 1881 if (range) { 1882 ret = _regmap_select_page(map, ®, range, 1); 1883 if (ret != 0) 1884 return ret; 1885 } 1886 1887 reg = regmap_reg_addr(map, reg); 1888 map->format.format_write(map, reg, val); 1889 1890 trace_regmap_hw_write_start(map, reg, 1); 1891 1892 ret = map->write(map->bus_context, map->work_buf, map->format.buf_size); 1893 1894 trace_regmap_hw_write_done(map, reg, 1); 1895 1896 return ret; 1897 } 1898 1899 static int _regmap_bus_reg_write(void *context, unsigned int reg, 1900 unsigned int val) 1901 { 1902 struct regmap *map = context; 1903 struct regmap_range_node *range; 1904 int ret; 1905 1906 range = _regmap_range_lookup(map, reg); 1907 if (range) { 1908 ret = _regmap_select_page(map, ®, range, 1); 1909 if (ret != 0) 1910 return ret; 1911 } 1912 1913 reg = regmap_reg_addr(map, reg); 1914 return map->bus->reg_write(map->bus_context, reg, val); 1915 } 1916 1917 static int _regmap_bus_raw_write(void *context, unsigned int reg, 1918 unsigned int val) 1919 { 1920 struct regmap *map = context; 1921 1922 WARN_ON(!map->format.format_val); 1923 1924 map->format.format_val(map->work_buf + map->format.reg_bytes 1925 + map->format.pad_bytes, val, 0); 1926 return _regmap_raw_write_impl(map, reg, 1927 map->work_buf + 1928 map->format.reg_bytes + 1929 map->format.pad_bytes, 1930 map->format.val_bytes, 1931 false); 1932 } 1933 1934 static inline void *_regmap_map_get_context(struct regmap *map) 1935 { 1936 return (map->bus || (!map->bus && map->read)) ? map : map->bus_context; 1937 } 1938 1939 int _regmap_write(struct regmap *map, unsigned int reg, 1940 unsigned int val) 1941 { 1942 int ret; 1943 void *context = _regmap_map_get_context(map); 1944 1945 if (!regmap_writeable(map, reg)) 1946 return -EIO; 1947 1948 if (!map->cache_bypass && !map->defer_caching) { 1949 ret = regcache_write(map, reg, val); 1950 if (ret != 0) 1951 return ret; 1952 if (map->cache_only) { 1953 map->cache_dirty = true; 1954 return 0; 1955 } 1956 } 1957 1958 ret = map->reg_write(context, reg, val); 1959 if (ret == 0) { 1960 if (regmap_should_log(map)) 1961 dev_info(map->dev, "%x <= %x\n", reg, val); 1962 1963 trace_regmap_reg_write(map, reg, val); 1964 } 1965 1966 return ret; 1967 } 1968 1969 /** 1970 * regmap_write() - Write a value to a single register 1971 * 1972 * @map: Register map to write to 1973 * @reg: Register to write to 1974 * @val: Value to be written 1975 * 1976 * A value of zero will be returned on success, a negative errno will 1977 * be returned in error cases. 1978 */ 1979 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) 1980 { 1981 if (!IS_ALIGNED(reg, map->reg_stride)) 1982 return -EINVAL; 1983 1984 guard(regmap)(map); 1985 1986 return _regmap_write(map, reg, val); 1987 } 1988 EXPORT_SYMBOL_GPL(regmap_write); 1989 1990 /** 1991 * regmap_write_async() - Write a value to a single register asynchronously 1992 * 1993 * @map: Register map to write to 1994 * @reg: Register to write to 1995 * @val: Value to be written 1996 * 1997 * A value of zero will be returned on success, a negative errno will 1998 * be returned in error cases. 1999 */ 2000 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val) 2001 { 2002 int ret; 2003 2004 if (!IS_ALIGNED(reg, map->reg_stride)) 2005 return -EINVAL; 2006 2007 guard(regmap)(map); 2008 2009 map->async = true; 2010 2011 ret = _regmap_write(map, reg, val); 2012 2013 map->async = false; 2014 2015 return ret; 2016 } 2017 EXPORT_SYMBOL_GPL(regmap_write_async); 2018 2019 int _regmap_raw_write(struct regmap *map, unsigned int reg, 2020 const void *val, size_t val_len, bool noinc) 2021 { 2022 size_t val_bytes = map->format.val_bytes; 2023 size_t val_count = val_len / val_bytes; 2024 size_t chunk_count, chunk_bytes; 2025 size_t chunk_regs = val_count; 2026 int ret, i; 2027 2028 if (!val_count) 2029 return -EINVAL; 2030 2031 if (map->use_single_write) 2032 chunk_regs = 1; 2033 else if (map->max_raw_write && val_len > map->max_raw_write) 2034 chunk_regs = map->max_raw_write / val_bytes; 2035 2036 chunk_count = val_count / chunk_regs; 2037 chunk_bytes = chunk_regs * val_bytes; 2038 2039 /* Write as many bytes as possible with chunk_size */ 2040 for (i = 0; i < chunk_count; i++) { 2041 ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc); 2042 if (ret) 2043 return ret; 2044 2045 reg += regmap_get_offset(map, chunk_regs); 2046 val += chunk_bytes; 2047 val_len -= chunk_bytes; 2048 } 2049 2050 /* Write remaining bytes */ 2051 if (val_len) 2052 ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc); 2053 2054 return ret; 2055 } 2056 2057 /** 2058 * regmap_raw_write() - Write raw values to one or more registers 2059 * 2060 * @map: Register map to write to 2061 * @reg: Initial register to write to 2062 * @val: Block of data to be written, laid out for direct transmission to the 2063 * device 2064 * @val_len: Length of data pointed to by val. 2065 * 2066 * This function is intended to be used for things like firmware 2067 * download where a large block of data needs to be transferred to the 2068 * device. No formatting will be done on the data provided. 2069 * 2070 * A value of zero will be returned on success, a negative errno will 2071 * be returned in error cases. 2072 */ 2073 int regmap_raw_write(struct regmap *map, unsigned int reg, 2074 const void *val, size_t val_len) 2075 { 2076 if (!regmap_can_raw_write(map)) 2077 return -EINVAL; 2078 if (val_len % map->format.val_bytes) 2079 return -EINVAL; 2080 2081 guard(regmap)(map); 2082 2083 return _regmap_raw_write(map, reg, val, val_len, false); 2084 } 2085 EXPORT_SYMBOL_GPL(regmap_raw_write); 2086 2087 static int regmap_noinc_readwrite(struct regmap *map, unsigned int reg, 2088 void *val, unsigned int val_len, bool write) 2089 { 2090 size_t val_bytes = map->format.val_bytes; 2091 size_t val_count = val_len / val_bytes; 2092 unsigned int lastval; 2093 u8 *u8p; 2094 u16 *u16p; 2095 u32 *u32p; 2096 int ret; 2097 int i; 2098 2099 switch (val_bytes) { 2100 case 1: 2101 u8p = val; 2102 if (write) 2103 lastval = (unsigned int)u8p[val_count - 1]; 2104 break; 2105 case 2: 2106 u16p = val; 2107 if (write) 2108 lastval = (unsigned int)u16p[val_count - 1]; 2109 break; 2110 case 4: 2111 u32p = val; 2112 if (write) 2113 lastval = (unsigned int)u32p[val_count - 1]; 2114 break; 2115 default: 2116 return -EINVAL; 2117 } 2118 2119 /* 2120 * Update the cache with the last value we write, the rest is just 2121 * gone down in the hardware FIFO. We can't cache FIFOs. This makes 2122 * sure a single read from the cache will work. 2123 */ 2124 if (write) { 2125 if (!map->cache_bypass && !map->defer_caching) { 2126 ret = regcache_write(map, reg, lastval); 2127 if (ret != 0) 2128 return ret; 2129 if (map->cache_only) { 2130 map->cache_dirty = true; 2131 return 0; 2132 } 2133 } 2134 ret = map->bus->reg_noinc_write(map->bus_context, reg, val, val_count); 2135 } else { 2136 ret = map->bus->reg_noinc_read(map->bus_context, reg, val, val_count); 2137 } 2138 2139 if (!ret && regmap_should_log(map)) { 2140 dev_info(map->dev, "%x %s [", reg, write ? "<=" : "=>"); 2141 for (i = 0; i < val_count; i++) { 2142 switch (val_bytes) { 2143 case 1: 2144 pr_cont("%x", u8p[i]); 2145 break; 2146 case 2: 2147 pr_cont("%x", u16p[i]); 2148 break; 2149 case 4: 2150 pr_cont("%x", u32p[i]); 2151 break; 2152 default: 2153 break; 2154 } 2155 if (i == (val_count - 1)) 2156 pr_cont("]\n"); 2157 else 2158 pr_cont(","); 2159 } 2160 } 2161 2162 return 0; 2163 } 2164 2165 /** 2166 * regmap_noinc_write(): Write data to a register without incrementing the 2167 * register number 2168 * 2169 * @map: Register map to write to 2170 * @reg: Register to write to 2171 * @val: Pointer to data buffer 2172 * @val_len: Length of output buffer in bytes. 2173 * 2174 * The regmap API usually assumes that bulk bus write operations will write a 2175 * range of registers. Some devices have certain registers for which a write 2176 * operation can write to an internal FIFO. 2177 * 2178 * The target register must be volatile but registers after it can be 2179 * completely unrelated cacheable registers. 2180 * 2181 * This will attempt multiple writes as required to write val_len bytes. 2182 * 2183 * A value of zero will be returned on success, a negative errno will be 2184 * returned in error cases. 2185 */ 2186 int regmap_noinc_write(struct regmap *map, unsigned int reg, 2187 const void *val, size_t val_len) 2188 { 2189 size_t write_len; 2190 int ret; 2191 2192 if (!map->write && !(map->bus && map->bus->reg_noinc_write)) 2193 return -EINVAL; 2194 if (val_len % map->format.val_bytes) 2195 return -EINVAL; 2196 if (!IS_ALIGNED(reg, map->reg_stride)) 2197 return -EINVAL; 2198 if (val_len == 0) 2199 return -EINVAL; 2200 2201 guard(regmap)(map); 2202 2203 if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) 2204 return -EINVAL; 2205 2206 /* 2207 * Use the accelerated operation if we can. The val drops the const 2208 * typing in order to facilitate code reuse in regmap_noinc_readwrite(). 2209 */ 2210 if (map->bus->reg_noinc_write) 2211 return regmap_noinc_readwrite(map, reg, (void *)val, val_len, true); 2212 2213 while (val_len) { 2214 if (map->max_raw_write && map->max_raw_write < val_len) 2215 write_len = map->max_raw_write; 2216 else 2217 write_len = val_len; 2218 ret = _regmap_raw_write(map, reg, val, write_len, true); 2219 if (ret) 2220 return ret; 2221 val = ((u8 *)val) + write_len; 2222 val_len -= write_len; 2223 } 2224 2225 return 0; 2226 } 2227 EXPORT_SYMBOL_GPL(regmap_noinc_write); 2228 2229 /** 2230 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a 2231 * register field. 2232 * 2233 * @field: Register field to write to 2234 * @mask: Bitmask to change 2235 * @val: Value to be written 2236 * @change: Boolean indicating if a write was done 2237 * @async: Boolean indicating asynchronously 2238 * @force: Boolean indicating use force update 2239 * 2240 * Perform a read/modify/write cycle on the register field with change, 2241 * async, force option. 2242 * 2243 * A value of zero will be returned on success, a negative errno will 2244 * be returned in error cases. 2245 */ 2246 int regmap_field_update_bits_base(struct regmap_field *field, 2247 unsigned int mask, unsigned int val, 2248 bool *change, bool async, bool force) 2249 { 2250 mask = (mask << field->shift) & field->mask; 2251 2252 return regmap_update_bits_base(field->regmap, field->reg, 2253 mask, val << field->shift, 2254 change, async, force); 2255 } 2256 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base); 2257 2258 /** 2259 * regmap_field_test_bits() - Check if all specified bits are set in a 2260 * register field. 2261 * 2262 * @field: Register field to operate on 2263 * @bits: Bits to test 2264 * 2265 * Returns negative errno if the underlying regmap_field_read() fails, 2266 * 0 if at least one of the tested bits is not set and 1 if all tested 2267 * bits are set. 2268 */ 2269 int regmap_field_test_bits(struct regmap_field *field, unsigned int bits) 2270 { 2271 unsigned int val; 2272 int ret; 2273 2274 ret = regmap_field_read(field, &val); 2275 if (ret) 2276 return ret; 2277 2278 return (val & bits) == bits; 2279 } 2280 EXPORT_SYMBOL_GPL(regmap_field_test_bits); 2281 2282 /** 2283 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a 2284 * register field with port ID 2285 * 2286 * @field: Register field to write to 2287 * @id: port ID 2288 * @mask: Bitmask to change 2289 * @val: Value to be written 2290 * @change: Boolean indicating if a write was done 2291 * @async: Boolean indicating asynchronously 2292 * @force: Boolean indicating use force update 2293 * 2294 * A value of zero will be returned on success, a negative errno will 2295 * be returned in error cases. 2296 */ 2297 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id, 2298 unsigned int mask, unsigned int val, 2299 bool *change, bool async, bool force) 2300 { 2301 if (id >= field->id_size) 2302 return -EINVAL; 2303 2304 mask = (mask << field->shift) & field->mask; 2305 2306 return regmap_update_bits_base(field->regmap, 2307 field->reg + (field->id_offset * id), 2308 mask, val << field->shift, 2309 change, async, force); 2310 } 2311 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base); 2312 2313 /** 2314 * regmap_bulk_write() - Write multiple registers to the device 2315 * 2316 * @map: Register map to write to 2317 * @reg: First register to be write from 2318 * @val: Block of data to be written, in native register size for device 2319 * @val_count: Number of registers to write 2320 * 2321 * This function is intended to be used for writing a large block of 2322 * data to the device either in single transfer or multiple transfer. 2323 * 2324 * A value of zero will be returned on success, a negative errno will 2325 * be returned in error cases. 2326 */ 2327 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, 2328 size_t val_count) 2329 { 2330 int ret = 0, i; 2331 size_t val_bytes = map->format.val_bytes; 2332 2333 if (!IS_ALIGNED(reg, map->reg_stride)) 2334 return -EINVAL; 2335 2336 /* 2337 * Some devices don't support bulk write, for them we have a series of 2338 * single write operations. 2339 */ 2340 if (!map->write || !map->format.parse_inplace) { 2341 guard(regmap)(map); 2342 2343 for (i = 0; i < val_count; i++) { 2344 unsigned int ival; 2345 2346 switch (val_bytes) { 2347 case 1: 2348 ival = *(u8 *)(val + (i * val_bytes)); 2349 break; 2350 case 2: 2351 ival = *(u16 *)(val + (i * val_bytes)); 2352 break; 2353 case 4: 2354 ival = *(u32 *)(val + (i * val_bytes)); 2355 break; 2356 default: 2357 return -EINVAL; 2358 } 2359 2360 ret = _regmap_write(map, 2361 reg + regmap_get_offset(map, i), 2362 ival); 2363 if (ret != 0) 2364 return ret; 2365 } 2366 } else { 2367 void *wval; 2368 2369 wval = kmemdup_array(val, val_count, val_bytes, map->alloc_flags); 2370 if (!wval) 2371 return -ENOMEM; 2372 2373 for (i = 0; i < val_count * val_bytes; i += val_bytes) 2374 map->format.parse_inplace(wval + i); 2375 2376 ret = regmap_raw_write(map, reg, wval, val_bytes * val_count); 2377 2378 kfree(wval); 2379 } 2380 2381 if (!ret) 2382 trace_regmap_bulk_write(map, reg, val, val_bytes * val_count); 2383 2384 return ret; 2385 } 2386 EXPORT_SYMBOL_GPL(regmap_bulk_write); 2387 2388 /* 2389 * _regmap_raw_multi_reg_write() 2390 * 2391 * the (register,newvalue) pairs in regs have not been formatted, but 2392 * they are all in the same page and have been changed to being page 2393 * relative. The page register has been written if that was necessary. 2394 */ 2395 static int _regmap_raw_multi_reg_write(struct regmap *map, 2396 const struct reg_sequence *regs, 2397 size_t num_regs) 2398 { 2399 int ret; 2400 void *buf; 2401 int i; 2402 u8 *u8; 2403 size_t val_bytes = map->format.val_bytes; 2404 size_t reg_bytes = map->format.reg_bytes; 2405 size_t pad_bytes = map->format.pad_bytes; 2406 size_t pair_size = reg_bytes + pad_bytes + val_bytes; 2407 size_t len = pair_size * num_regs; 2408 2409 if (!len) 2410 return -EINVAL; 2411 2412 buf = kzalloc(len, GFP_KERNEL); 2413 if (!buf) 2414 return -ENOMEM; 2415 2416 /* We have to linearise by hand. */ 2417 2418 u8 = buf; 2419 2420 for (i = 0; i < num_regs; i++) { 2421 unsigned int reg = regs[i].reg; 2422 unsigned int val = regs[i].def; 2423 trace_regmap_hw_write_start(map, reg, 1); 2424 reg = regmap_reg_addr(map, reg); 2425 map->format.format_reg(u8, reg, map->reg_shift); 2426 u8 += reg_bytes + pad_bytes; 2427 map->format.format_val(u8, val, 0); 2428 u8 += val_bytes; 2429 } 2430 u8 = buf; 2431 *u8 |= map->write_flag_mask; 2432 2433 ret = map->write(map->bus_context, buf, len); 2434 2435 kfree(buf); 2436 2437 for (i = 0; i < num_regs; i++) { 2438 int reg = regs[i].reg; 2439 trace_regmap_hw_write_done(map, reg, 1); 2440 } 2441 return ret; 2442 } 2443 2444 static unsigned int _regmap_register_page(struct regmap *map, 2445 unsigned int reg, 2446 struct regmap_range_node *range) 2447 { 2448 unsigned int win_page = (reg - range->range_min) / range->window_len; 2449 2450 return win_page; 2451 } 2452 2453 static int _regmap_range_multi_paged_reg_write(struct regmap *map, 2454 struct reg_sequence *regs, 2455 size_t num_regs) 2456 { 2457 int ret; 2458 int i, n; 2459 struct reg_sequence *base; 2460 unsigned int this_page = 0; 2461 unsigned int page_change = 0; 2462 /* 2463 * the set of registers are not neccessarily in order, but 2464 * since the order of write must be preserved this algorithm 2465 * chops the set each time the page changes. This also applies 2466 * if there is a delay required at any point in the sequence. 2467 */ 2468 base = regs; 2469 for (i = 0, n = 0; i < num_regs; i++, n++) { 2470 unsigned int reg = regs[i].reg; 2471 struct regmap_range_node *range; 2472 2473 range = _regmap_range_lookup(map, reg); 2474 if (range) { 2475 unsigned int win_page = _regmap_register_page(map, reg, 2476 range); 2477 2478 if (i == 0) 2479 this_page = win_page; 2480 if (win_page != this_page) { 2481 this_page = win_page; 2482 page_change = 1; 2483 } 2484 } 2485 2486 /* If we have both a page change and a delay make sure to 2487 * write the regs and apply the delay before we change the 2488 * page. 2489 */ 2490 2491 if (page_change || regs[i].delay_us) { 2492 2493 /* For situations where the first write requires 2494 * a delay we need to make sure we don't call 2495 * raw_multi_reg_write with n=0 2496 * This can't occur with page breaks as we 2497 * never write on the first iteration 2498 */ 2499 if (regs[i].delay_us && i == 0) 2500 n = 1; 2501 2502 ret = _regmap_raw_multi_reg_write(map, base, n); 2503 if (ret != 0) 2504 return ret; 2505 2506 if (regs[i].delay_us) { 2507 if (map->can_sleep) 2508 fsleep(regs[i].delay_us); 2509 else 2510 udelay(regs[i].delay_us); 2511 } 2512 2513 base += n; 2514 n = 0; 2515 2516 if (page_change) { 2517 ret = _regmap_select_page(map, 2518 &base[n].reg, 2519 range, 1); 2520 if (ret != 0) 2521 return ret; 2522 2523 page_change = 0; 2524 } 2525 2526 } 2527 2528 } 2529 if (n > 0) 2530 return _regmap_raw_multi_reg_write(map, base, n); 2531 return 0; 2532 } 2533 2534 static int _regmap_multi_reg_write(struct regmap *map, 2535 const struct reg_sequence *regs, 2536 size_t num_regs) 2537 { 2538 int i; 2539 int ret; 2540 2541 if (!map->can_multi_write) { 2542 for (i = 0; i < num_regs; i++) { 2543 ret = _regmap_write(map, regs[i].reg, regs[i].def); 2544 if (ret != 0) 2545 return ret; 2546 2547 if (regs[i].delay_us) { 2548 if (map->can_sleep) 2549 fsleep(regs[i].delay_us); 2550 else 2551 udelay(regs[i].delay_us); 2552 } 2553 } 2554 return 0; 2555 } 2556 2557 if (!map->format.parse_inplace) 2558 return -EINVAL; 2559 2560 if (map->writeable_reg) 2561 for (i = 0; i < num_regs; i++) { 2562 int reg = regs[i].reg; 2563 if (!map->writeable_reg(map->dev, reg)) 2564 return -EINVAL; 2565 if (!IS_ALIGNED(reg, map->reg_stride)) 2566 return -EINVAL; 2567 } 2568 2569 if (!map->cache_bypass) { 2570 for (i = 0; i < num_regs; i++) { 2571 unsigned int val = regs[i].def; 2572 unsigned int reg = regs[i].reg; 2573 ret = regcache_write(map, reg, val); 2574 if (ret) { 2575 dev_err(map->dev, 2576 "Error in caching of register: %x ret: %d\n", 2577 reg, ret); 2578 return ret; 2579 } 2580 } 2581 if (map->cache_only) { 2582 map->cache_dirty = true; 2583 return 0; 2584 } 2585 } 2586 2587 WARN_ON(!map->bus); 2588 2589 for (i = 0; i < num_regs; i++) { 2590 unsigned int reg = regs[i].reg; 2591 struct regmap_range_node *range; 2592 2593 /* Coalesce all the writes between a page break or a delay 2594 * in a sequence 2595 */ 2596 range = _regmap_range_lookup(map, reg); 2597 if (range || regs[i].delay_us) { 2598 size_t len = sizeof(struct reg_sequence)*num_regs; 2599 struct reg_sequence *base = kmemdup(regs, len, 2600 GFP_KERNEL); 2601 if (!base) 2602 return -ENOMEM; 2603 ret = _regmap_range_multi_paged_reg_write(map, base, 2604 num_regs); 2605 kfree(base); 2606 2607 return ret; 2608 } 2609 } 2610 return _regmap_raw_multi_reg_write(map, regs, num_regs); 2611 } 2612 2613 /** 2614 * regmap_multi_reg_write() - Write multiple registers to the device 2615 * 2616 * @map: Register map to write to 2617 * @regs: Array of structures containing register,value to be written 2618 * @num_regs: Number of registers to write 2619 * 2620 * Write multiple registers to the device where the set of register, value 2621 * pairs are supplied in any order, possibly not all in a single range. 2622 * 2623 * The 'normal' block write mode will send ultimately send data on the 2624 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are 2625 * addressed. However, this alternative block multi write mode will send 2626 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device 2627 * must of course support the mode. 2628 * 2629 * A value of zero will be returned on success, a negative errno will be 2630 * returned in error cases. 2631 */ 2632 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs, 2633 int num_regs) 2634 { 2635 guard(regmap)(map); 2636 2637 return _regmap_multi_reg_write(map, regs, num_regs); 2638 } 2639 EXPORT_SYMBOL_GPL(regmap_multi_reg_write); 2640 2641 /** 2642 * regmap_multi_reg_write_bypassed() - Write multiple registers to the 2643 * device but not the cache 2644 * 2645 * @map: Register map to write to 2646 * @regs: Array of structures containing register,value to be written 2647 * @num_regs: Number of registers to write 2648 * 2649 * Write multiple registers to the device but not the cache where the set 2650 * of register are supplied in any order. 2651 * 2652 * This function is intended to be used for writing a large block of data 2653 * atomically to the device in single transfer for those I2C client devices 2654 * that implement this alternative block write mode. 2655 * 2656 * A value of zero will be returned on success, a negative errno will 2657 * be returned in error cases. 2658 */ 2659 int regmap_multi_reg_write_bypassed(struct regmap *map, 2660 const struct reg_sequence *regs, 2661 int num_regs) 2662 { 2663 int ret; 2664 bool bypass; 2665 2666 guard(regmap)(map); 2667 2668 bypass = map->cache_bypass; 2669 map->cache_bypass = true; 2670 2671 ret = _regmap_multi_reg_write(map, regs, num_regs); 2672 2673 map->cache_bypass = bypass; 2674 2675 return ret; 2676 } 2677 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed); 2678 2679 /** 2680 * regmap_raw_write_async() - Write raw values to one or more registers 2681 * asynchronously 2682 * 2683 * @map: Register map to write to 2684 * @reg: Initial register to write to 2685 * @val: Block of data to be written, laid out for direct transmission to the 2686 * device. Must be valid until regmap_async_complete() is called. 2687 * @val_len: Length of data pointed to by val. 2688 * 2689 * This function is intended to be used for things like firmware 2690 * download where a large block of data needs to be transferred to the 2691 * device. No formatting will be done on the data provided. 2692 * 2693 * If supported by the underlying bus the write will be scheduled 2694 * asynchronously, helping maximise I/O speed on higher speed buses 2695 * like SPI. regmap_async_complete() can be called to ensure that all 2696 * asynchronous writes have been completed. 2697 * 2698 * A value of zero will be returned on success, a negative errno will 2699 * be returned in error cases. 2700 */ 2701 int regmap_raw_write_async(struct regmap *map, unsigned int reg, 2702 const void *val, size_t val_len) 2703 { 2704 int ret; 2705 2706 if (val_len % map->format.val_bytes) 2707 return -EINVAL; 2708 if (!IS_ALIGNED(reg, map->reg_stride)) 2709 return -EINVAL; 2710 2711 guard(regmap)(map); 2712 2713 map->async = true; 2714 2715 ret = _regmap_raw_write(map, reg, val, val_len, false); 2716 2717 map->async = false; 2718 2719 return ret; 2720 } 2721 EXPORT_SYMBOL_GPL(regmap_raw_write_async); 2722 2723 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 2724 unsigned int val_len, bool noinc) 2725 { 2726 struct regmap_range_node *range; 2727 int ret; 2728 2729 if (!map->read) 2730 return -EINVAL; 2731 2732 range = _regmap_range_lookup(map, reg); 2733 if (range) { 2734 ret = _regmap_select_page(map, ®, range, 2735 noinc ? 1 : val_len / map->format.val_bytes); 2736 if (ret != 0) 2737 return ret; 2738 } 2739 2740 reg = regmap_reg_addr(map, reg); 2741 map->format.format_reg(map->work_buf, reg, map->reg_shift); 2742 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes, 2743 map->read_flag_mask); 2744 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes); 2745 2746 ret = map->read(map->bus_context, map->work_buf, 2747 map->format.reg_bytes + map->format.pad_bytes, 2748 val, val_len); 2749 2750 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes); 2751 2752 return ret; 2753 } 2754 2755 static int _regmap_bus_reg_read(void *context, unsigned int reg, 2756 unsigned int *val) 2757 { 2758 struct regmap *map = context; 2759 struct regmap_range_node *range; 2760 int ret; 2761 2762 range = _regmap_range_lookup(map, reg); 2763 if (range) { 2764 ret = _regmap_select_page(map, ®, range, 1); 2765 if (ret != 0) 2766 return ret; 2767 } 2768 2769 reg = regmap_reg_addr(map, reg); 2770 return map->bus->reg_read(map->bus_context, reg, val); 2771 } 2772 2773 static int _regmap_bus_read(void *context, unsigned int reg, 2774 unsigned int *val) 2775 { 2776 int ret; 2777 struct regmap *map = context; 2778 void *work_val = map->work_buf + map->format.reg_bytes + 2779 map->format.pad_bytes; 2780 2781 if (!map->format.parse_val) 2782 return -EINVAL; 2783 2784 ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false); 2785 if (ret == 0) 2786 *val = map->format.parse_val(work_val); 2787 2788 return ret; 2789 } 2790 2791 static int _regmap_read(struct regmap *map, unsigned int reg, 2792 unsigned int *val) 2793 { 2794 int ret; 2795 void *context = _regmap_map_get_context(map); 2796 2797 if (!map->cache_bypass) { 2798 ret = regcache_read(map, reg, val); 2799 if (ret == 0) 2800 return 0; 2801 } 2802 2803 if (map->cache_only) 2804 return -EBUSY; 2805 2806 if (!regmap_readable(map, reg)) 2807 return -EIO; 2808 2809 ret = map->reg_read(context, reg, val); 2810 if (ret == 0) { 2811 if (regmap_should_log(map)) 2812 dev_info(map->dev, "%x => %x\n", reg, *val); 2813 2814 trace_regmap_reg_read(map, reg, *val); 2815 2816 if (!map->cache_bypass) 2817 regcache_write(map, reg, *val); 2818 } 2819 2820 return ret; 2821 } 2822 2823 /** 2824 * regmap_read() - Read a value from a single register 2825 * 2826 * @map: Register map to read from 2827 * @reg: Register to be read from 2828 * @val: Pointer to store read value 2829 * 2830 * A value of zero will be returned on success, a negative errno will 2831 * be returned in error cases. 2832 */ 2833 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) 2834 { 2835 if (!IS_ALIGNED(reg, map->reg_stride)) 2836 return -EINVAL; 2837 2838 guard(regmap)(map); 2839 2840 return _regmap_read(map, reg, val); 2841 } 2842 EXPORT_SYMBOL_GPL(regmap_read); 2843 2844 /** 2845 * regmap_read_bypassed() - Read a value from a single register direct 2846 * from the device, bypassing the cache 2847 * 2848 * @map: Register map to read from 2849 * @reg: Register to be read from 2850 * @val: Pointer to store read value 2851 * 2852 * A value of zero will be returned on success, a negative errno will 2853 * be returned in error cases. 2854 */ 2855 int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val) 2856 { 2857 int ret; 2858 bool bypass, cache_only; 2859 2860 if (!IS_ALIGNED(reg, map->reg_stride)) 2861 return -EINVAL; 2862 2863 guard(regmap)(map); 2864 2865 bypass = map->cache_bypass; 2866 cache_only = map->cache_only; 2867 map->cache_bypass = true; 2868 map->cache_only = false; 2869 2870 ret = _regmap_read(map, reg, val); 2871 2872 map->cache_bypass = bypass; 2873 map->cache_only = cache_only; 2874 2875 return ret; 2876 } 2877 EXPORT_SYMBOL_GPL(regmap_read_bypassed); 2878 2879 /** 2880 * regmap_raw_read() - Read raw data from the device 2881 * 2882 * @map: Register map to read from 2883 * @reg: First register to be read from 2884 * @val: Pointer to store read value 2885 * @val_len: Size of data to read 2886 * 2887 * A value of zero will be returned on success, a negative errno will 2888 * be returned in error cases. 2889 */ 2890 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 2891 size_t val_len) 2892 { 2893 size_t val_bytes = map->format.val_bytes; 2894 size_t val_count = val_len / val_bytes; 2895 unsigned int v; 2896 int ret, i; 2897 2898 if (val_len % map->format.val_bytes) 2899 return -EINVAL; 2900 if (!IS_ALIGNED(reg, map->reg_stride)) 2901 return -EINVAL; 2902 if (val_count == 0) 2903 return -EINVAL; 2904 2905 guard(regmap)(map); 2906 2907 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || 2908 map->cache_type == REGCACHE_NONE) { 2909 size_t chunk_count, chunk_bytes; 2910 size_t chunk_regs = val_count; 2911 2912 if (!map->cache_bypass && map->cache_only) 2913 return -EBUSY; 2914 2915 if (!map->read) 2916 return -ENOTSUPP; 2917 2918 if (map->use_single_read) 2919 chunk_regs = 1; 2920 else if (map->max_raw_read && val_len > map->max_raw_read) 2921 chunk_regs = map->max_raw_read / val_bytes; 2922 2923 chunk_count = val_count / chunk_regs; 2924 chunk_bytes = chunk_regs * val_bytes; 2925 2926 /* Read bytes that fit into whole chunks */ 2927 for (i = 0; i < chunk_count; i++) { 2928 ret = _regmap_raw_read(map, reg, val, chunk_bytes, false); 2929 if (ret != 0) 2930 return ret; 2931 2932 reg += regmap_get_offset(map, chunk_regs); 2933 val += chunk_bytes; 2934 val_len -= chunk_bytes; 2935 } 2936 2937 /* Read remaining bytes */ 2938 if (val_len) { 2939 ret = _regmap_raw_read(map, reg, val, val_len, false); 2940 if (ret != 0) 2941 return ret; 2942 } 2943 } else { 2944 /* Otherwise go word by word for the cache; should be low 2945 * cost as we expect to hit the cache. 2946 */ 2947 for (i = 0; i < val_count; i++) { 2948 ret = _regmap_read(map, reg + regmap_get_offset(map, i), 2949 &v); 2950 if (ret != 0) 2951 return ret; 2952 2953 map->format.format_val(val + (i * val_bytes), v, 0); 2954 } 2955 } 2956 2957 return 0; 2958 } 2959 EXPORT_SYMBOL_GPL(regmap_raw_read); 2960 2961 /** 2962 * regmap_noinc_read(): Read data from a register without incrementing the 2963 * register number 2964 * 2965 * @map: Register map to read from 2966 * @reg: Register to read from 2967 * @val: Pointer to data buffer 2968 * @val_len: Length of output buffer in bytes. 2969 * 2970 * The regmap API usually assumes that bulk read operations will read a 2971 * range of registers. Some devices have certain registers for which a read 2972 * operation read will read from an internal FIFO. 2973 * 2974 * The target register must be volatile but registers after it can be 2975 * completely unrelated cacheable registers. 2976 * 2977 * This will attempt multiple reads as required to read val_len bytes. 2978 * 2979 * A value of zero will be returned on success, a negative errno will be 2980 * returned in error cases. 2981 */ 2982 int regmap_noinc_read(struct regmap *map, unsigned int reg, 2983 void *val, size_t val_len) 2984 { 2985 size_t read_len; 2986 int ret; 2987 2988 if (!map->read) 2989 return -ENOTSUPP; 2990 2991 if (val_len % map->format.val_bytes) 2992 return -EINVAL; 2993 if (!IS_ALIGNED(reg, map->reg_stride)) 2994 return -EINVAL; 2995 if (val_len == 0) 2996 return -EINVAL; 2997 2998 guard(regmap)(map); 2999 3000 if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) 3001 return -EINVAL; 3002 3003 /* 3004 * We have not defined the FIFO semantics for cache, as the 3005 * cache is just one value deep. Should we return the last 3006 * written value? Just avoid this by always reading the FIFO 3007 * even when using cache. Cache only will not work. 3008 */ 3009 if (!map->cache_bypass && map->cache_only) 3010 return -EBUSY; 3011 3012 /* Use the accelerated operation if we can */ 3013 if (map->bus->reg_noinc_read) 3014 return regmap_noinc_readwrite(map, reg, val, val_len, false); 3015 3016 while (val_len) { 3017 if (map->max_raw_read && map->max_raw_read < val_len) 3018 read_len = map->max_raw_read; 3019 else 3020 read_len = val_len; 3021 ret = _regmap_raw_read(map, reg, val, read_len, true); 3022 if (ret) 3023 return ret; 3024 val = ((u8 *)val) + read_len; 3025 val_len -= read_len; 3026 } 3027 3028 return 0; 3029 } 3030 EXPORT_SYMBOL_GPL(regmap_noinc_read); 3031 3032 /** 3033 * regmap_field_read(): Read a value to a single register field 3034 * 3035 * @field: Register field to read from 3036 * @val: Pointer to store read value 3037 * 3038 * A value of zero will be returned on success, a negative errno will 3039 * be returned in error cases. 3040 */ 3041 int regmap_field_read(struct regmap_field *field, unsigned int *val) 3042 { 3043 int ret; 3044 unsigned int reg_val; 3045 ret = regmap_read(field->regmap, field->reg, ®_val); 3046 if (ret != 0) 3047 return ret; 3048 3049 reg_val &= field->mask; 3050 reg_val >>= field->shift; 3051 *val = reg_val; 3052 3053 return ret; 3054 } 3055 EXPORT_SYMBOL_GPL(regmap_field_read); 3056 3057 /** 3058 * regmap_fields_read() - Read a value to a single register field with port ID 3059 * 3060 * @field: Register field to read from 3061 * @id: port ID 3062 * @val: Pointer to store read value 3063 * 3064 * A value of zero will be returned on success, a negative errno will 3065 * be returned in error cases. 3066 */ 3067 int regmap_fields_read(struct regmap_field *field, unsigned int id, 3068 unsigned int *val) 3069 { 3070 int ret; 3071 unsigned int reg_val; 3072 3073 if (id >= field->id_size) 3074 return -EINVAL; 3075 3076 ret = regmap_read(field->regmap, 3077 field->reg + (field->id_offset * id), 3078 ®_val); 3079 if (ret != 0) 3080 return ret; 3081 3082 reg_val &= field->mask; 3083 reg_val >>= field->shift; 3084 *val = reg_val; 3085 3086 return ret; 3087 } 3088 EXPORT_SYMBOL_GPL(regmap_fields_read); 3089 3090 static int _regmap_bulk_read(struct regmap *map, unsigned int reg, 3091 const unsigned int *regs, void *val, size_t val_count) 3092 { 3093 u32 *u32 = val; 3094 u16 *u16 = val; 3095 u8 *u8 = val; 3096 int ret, i; 3097 3098 guard(regmap)(map); 3099 3100 for (i = 0; i < val_count; i++) { 3101 unsigned int ival; 3102 3103 if (regs) { 3104 if (!IS_ALIGNED(regs[i], map->reg_stride)) 3105 return -EINVAL; 3106 ret = _regmap_read(map, regs[i], &ival); 3107 } else { 3108 ret = _regmap_read(map, reg + regmap_get_offset(map, i), &ival); 3109 } 3110 if (ret != 0) 3111 return ret; 3112 3113 switch (map->format.val_bytes) { 3114 case 4: 3115 u32[i] = ival; 3116 break; 3117 case 2: 3118 u16[i] = ival; 3119 break; 3120 case 1: 3121 u8[i] = ival; 3122 break; 3123 default: 3124 return -EINVAL; 3125 } 3126 } 3127 3128 return 0; 3129 } 3130 3131 /** 3132 * regmap_bulk_read() - Read multiple sequential registers from the device 3133 * 3134 * @map: Register map to read from 3135 * @reg: First register to be read from 3136 * @val: Pointer to store read value, in native register size for device 3137 * @val_count: Number of registers to read 3138 * 3139 * A value of zero will be returned on success, a negative errno will 3140 * be returned in error cases. 3141 */ 3142 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, 3143 size_t val_count) 3144 { 3145 int ret, i; 3146 size_t val_bytes = map->format.val_bytes; 3147 bool vol = regmap_volatile_range(map, reg, val_count); 3148 3149 if (!IS_ALIGNED(reg, map->reg_stride)) 3150 return -EINVAL; 3151 if (val_count == 0) 3152 return -EINVAL; 3153 3154 if (map->read && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) { 3155 ret = regmap_raw_read(map, reg, val, val_bytes * val_count); 3156 if (ret != 0) 3157 return ret; 3158 3159 for (i = 0; i < val_count * val_bytes; i += val_bytes) 3160 map->format.parse_inplace(val + i); 3161 } else { 3162 ret = _regmap_bulk_read(map, reg, NULL, val, val_count); 3163 } 3164 if (!ret) 3165 trace_regmap_bulk_read(map, reg, val, val_bytes * val_count); 3166 return ret; 3167 } 3168 EXPORT_SYMBOL_GPL(regmap_bulk_read); 3169 3170 /** 3171 * regmap_multi_reg_read() - Read multiple non-sequential registers from the device 3172 * 3173 * @map: Register map to read from 3174 * @regs: Array of registers to read from 3175 * @val: Pointer to store read value, in native register size for device 3176 * @val_count: Number of registers to read 3177 * 3178 * A value of zero will be returned on success, a negative errno will 3179 * be returned in error cases. 3180 */ 3181 int regmap_multi_reg_read(struct regmap *map, const unsigned int *regs, void *val, 3182 size_t val_count) 3183 { 3184 if (val_count == 0) 3185 return -EINVAL; 3186 3187 return _regmap_bulk_read(map, 0, regs, val, val_count); 3188 } 3189 EXPORT_SYMBOL_GPL(regmap_multi_reg_read); 3190 3191 static int _regmap_update_bits(struct regmap *map, unsigned int reg, 3192 unsigned int mask, unsigned int val, 3193 bool *change, bool force_write) 3194 { 3195 int ret; 3196 unsigned int tmp, orig; 3197 3198 if (change) 3199 *change = false; 3200 3201 if (regmap_volatile(map, reg) && map->reg_update_bits) { 3202 if (map->cache_only) 3203 return -EBUSY; 3204 3205 reg = regmap_reg_addr(map, reg); 3206 ret = map->reg_update_bits(map->bus_context, reg, mask, val); 3207 if (ret == 0 && change) 3208 *change = true; 3209 } else { 3210 ret = _regmap_read(map, reg, &orig); 3211 if (ret != 0) 3212 return ret; 3213 3214 tmp = orig & ~mask; 3215 tmp |= val & mask; 3216 3217 if (force_write || (tmp != orig) || map->force_write_field) { 3218 ret = _regmap_write(map, reg, tmp); 3219 if (ret == 0 && change) 3220 *change = true; 3221 } 3222 } 3223 3224 return ret; 3225 } 3226 3227 /** 3228 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register 3229 * 3230 * @map: Register map to update 3231 * @reg: Register to update 3232 * @mask: Bitmask to change 3233 * @val: New value for bitmask 3234 * @change: Boolean indicating if a write was done 3235 * @async: Boolean indicating asynchronously 3236 * @force: Boolean indicating use force update 3237 * 3238 * Perform a read/modify/write cycle on a register map with change, async, force 3239 * options. 3240 * 3241 * If async is true: 3242 * 3243 * With most buses the read must be done synchronously so this is most useful 3244 * for devices with a cache which do not need to interact with the hardware to 3245 * determine the current register value. 3246 * 3247 * Returns zero for success, a negative number on error. 3248 */ 3249 int regmap_update_bits_base(struct regmap *map, unsigned int reg, 3250 unsigned int mask, unsigned int val, 3251 bool *change, bool async, bool force) 3252 { 3253 int ret; 3254 3255 guard(regmap)(map); 3256 3257 map->async = async; 3258 3259 ret = _regmap_update_bits(map, reg, mask, val, change, force); 3260 3261 map->async = false; 3262 3263 return ret; 3264 } 3265 EXPORT_SYMBOL_GPL(regmap_update_bits_base); 3266 3267 /** 3268 * regmap_test_bits() - Check if all specified bits are set in a register. 3269 * 3270 * @map: Register map to operate on 3271 * @reg: Register to read from 3272 * @bits: Bits to test 3273 * 3274 * Returns 0 if at least one of the tested bits is not set, 1 if all tested 3275 * bits are set and a negative error number if the underlying regmap_read() 3276 * fails. 3277 */ 3278 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits) 3279 { 3280 unsigned int val; 3281 int ret; 3282 3283 ret = regmap_read(map, reg, &val); 3284 if (ret) 3285 return ret; 3286 3287 return (val & bits) == bits; 3288 } 3289 EXPORT_SYMBOL_GPL(regmap_test_bits); 3290 3291 void regmap_async_complete_cb(struct regmap_async *async, int ret) 3292 { 3293 struct regmap *map = async->map; 3294 bool wake; 3295 3296 trace_regmap_async_io_complete(map); 3297 3298 spin_lock(&map->async_lock); 3299 list_move(&async->list, &map->async_free); 3300 wake = list_empty(&map->async_list); 3301 3302 if (ret != 0) 3303 map->async_ret = ret; 3304 3305 spin_unlock(&map->async_lock); 3306 3307 if (wake) 3308 wake_up(&map->async_waitq); 3309 } 3310 EXPORT_SYMBOL_GPL(regmap_async_complete_cb); 3311 3312 static int regmap_async_is_done(struct regmap *map) 3313 { 3314 unsigned long flags; 3315 int ret; 3316 3317 spin_lock_irqsave(&map->async_lock, flags); 3318 ret = list_empty(&map->async_list); 3319 spin_unlock_irqrestore(&map->async_lock, flags); 3320 3321 return ret; 3322 } 3323 3324 /** 3325 * regmap_async_complete - Ensure all asynchronous I/O has completed. 3326 * 3327 * @map: Map to operate on. 3328 * 3329 * Blocks until any pending asynchronous I/O has completed. Returns 3330 * an error code for any failed I/O operations. 3331 */ 3332 int regmap_async_complete(struct regmap *map) 3333 { 3334 unsigned long flags; 3335 int ret; 3336 3337 /* Nothing to do with no async support */ 3338 if (!map->bus || !map->bus->async_write) 3339 return 0; 3340 3341 trace_regmap_async_complete_start(map); 3342 3343 wait_event(map->async_waitq, regmap_async_is_done(map)); 3344 3345 spin_lock_irqsave(&map->async_lock, flags); 3346 ret = map->async_ret; 3347 map->async_ret = 0; 3348 spin_unlock_irqrestore(&map->async_lock, flags); 3349 3350 trace_regmap_async_complete_done(map); 3351 3352 return ret; 3353 } 3354 EXPORT_SYMBOL_GPL(regmap_async_complete); 3355 3356 /** 3357 * regmap_register_patch - Register and apply register updates to be applied 3358 * on device initialistion 3359 * 3360 * @map: Register map to apply updates to. 3361 * @regs: Values to update. 3362 * @num_regs: Number of entries in regs. 3363 * 3364 * Register a set of register updates to be applied to the device 3365 * whenever the device registers are synchronised with the cache and 3366 * apply them immediately. Typically this is used to apply 3367 * corrections to be applied to the device defaults on startup, such 3368 * as the updates some vendors provide to undocumented registers. 3369 * 3370 * The caller must ensure that this function cannot be called 3371 * concurrently with either itself or regcache_sync(). 3372 */ 3373 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs, 3374 int num_regs) 3375 { 3376 struct reg_sequence *p; 3377 int ret; 3378 bool bypass; 3379 3380 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n", 3381 num_regs)) 3382 return 0; 3383 3384 p = krealloc_array(map->patch, map->patch_regs + num_regs, sizeof(*p), GFP_KERNEL); 3385 if (!p) 3386 return -ENOMEM; 3387 3388 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs)); 3389 map->patch = p; 3390 map->patch_regs += num_regs; 3391 3392 scoped_guard(regmap, map) { 3393 bypass = map->cache_bypass; 3394 3395 map->cache_bypass = true; 3396 map->async = true; 3397 3398 ret = _regmap_multi_reg_write(map, regs, num_regs); 3399 3400 map->async = false; 3401 map->cache_bypass = bypass; 3402 } 3403 3404 regmap_async_complete(map); 3405 3406 return ret; 3407 } 3408 EXPORT_SYMBOL_GPL(regmap_register_patch); 3409 3410 /** 3411 * regmap_get_val_bytes() - Report the size of a register value 3412 * 3413 * @map: Register map to operate on. 3414 * 3415 * Report the size of a register value, mainly intended to for use by 3416 * generic infrastructure built on top of regmap. 3417 */ 3418 int regmap_get_val_bytes(struct regmap *map) 3419 { 3420 if (map->format.format_write) 3421 return -EINVAL; 3422 3423 return map->format.val_bytes; 3424 } 3425 EXPORT_SYMBOL_GPL(regmap_get_val_bytes); 3426 3427 /** 3428 * regmap_get_max_register() - Report the max register value 3429 * 3430 * @map: Register map to operate on. 3431 * 3432 * Report the max register value, mainly intended to for use by 3433 * generic infrastructure built on top of regmap. 3434 */ 3435 int regmap_get_max_register(struct regmap *map) 3436 { 3437 return map->max_register_is_set ? map->max_register : -EINVAL; 3438 } 3439 EXPORT_SYMBOL_GPL(regmap_get_max_register); 3440 3441 /** 3442 * regmap_get_reg_stride() - Report the register address stride 3443 * 3444 * @map: Register map to operate on. 3445 * 3446 * Report the register address stride, mainly intended to for use by 3447 * generic infrastructure built on top of regmap. 3448 */ 3449 int regmap_get_reg_stride(struct regmap *map) 3450 { 3451 return map->reg_stride; 3452 } 3453 EXPORT_SYMBOL_GPL(regmap_get_reg_stride); 3454 3455 /** 3456 * regmap_might_sleep() - Returns whether a regmap access might sleep. 3457 * 3458 * @map: Register map to operate on. 3459 * 3460 * Returns true if an access to the register might sleep, else false. 3461 */ 3462 bool regmap_might_sleep(struct regmap *map) 3463 { 3464 return map->can_sleep; 3465 } 3466 EXPORT_SYMBOL_GPL(regmap_might_sleep); 3467 3468 int regmap_parse_val(struct regmap *map, const void *buf, 3469 unsigned int *val) 3470 { 3471 if (!map->format.parse_val) 3472 return -EINVAL; 3473 3474 *val = map->format.parse_val(buf); 3475 3476 return 0; 3477 } 3478 EXPORT_SYMBOL_GPL(regmap_parse_val); 3479 3480 static int __init regmap_initcall(void) 3481 { 3482 regmap_debugfs_initcall(); 3483 3484 return 0; 3485 } 3486 postcore_initcall(regmap_initcall); 3487