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(dev, 1241 sizeof(*rm_field), GFP_KERNEL); 1242 if (!rm_field) 1243 return ERR_PTR(-ENOMEM); 1244 1245 regmap_field_init(rm_field, regmap, reg_field); 1246 1247 return rm_field; 1248 1249 } 1250 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc); 1251 1252 1253 /** 1254 * regmap_field_bulk_alloc() - Allocate and initialise a bulk register field. 1255 * 1256 * @regmap: regmap bank in which this register field is located. 1257 * @rm_field: regmap register fields within the bank. 1258 * @reg_field: Register fields within the bank. 1259 * @num_fields: Number of register fields. 1260 * 1261 * The return value will be an -ENOMEM on error or zero for success. 1262 * Newly allocated regmap_fields should be freed by calling 1263 * regmap_field_bulk_free() 1264 */ 1265 int regmap_field_bulk_alloc(struct regmap *regmap, 1266 struct regmap_field **rm_field, 1267 const struct reg_field *reg_field, 1268 int num_fields) 1269 { 1270 struct regmap_field *rf; 1271 int i; 1272 1273 rf = kzalloc_objs(*rf, num_fields); 1274 if (!rf) 1275 return -ENOMEM; 1276 1277 for (i = 0; i < num_fields; i++) { 1278 regmap_field_init(&rf[i], regmap, reg_field[i]); 1279 rm_field[i] = &rf[i]; 1280 } 1281 1282 return 0; 1283 } 1284 EXPORT_SYMBOL_GPL(regmap_field_bulk_alloc); 1285 1286 /** 1287 * devm_regmap_field_bulk_alloc() - Allocate and initialise a bulk register 1288 * fields. 1289 * 1290 * @dev: Device that will be interacted with 1291 * @regmap: regmap bank in which this register field is located. 1292 * @rm_field: regmap register fields within the bank. 1293 * @reg_field: Register fields within the bank. 1294 * @num_fields: Number of register fields. 1295 * 1296 * The return value will be an -ENOMEM on error or zero for success. 1297 * Newly allocated regmap_fields will be automatically freed by the 1298 * device management code. 1299 */ 1300 int devm_regmap_field_bulk_alloc(struct device *dev, 1301 struct regmap *regmap, 1302 struct regmap_field **rm_field, 1303 const struct reg_field *reg_field, 1304 int num_fields) 1305 { 1306 struct regmap_field *rf; 1307 int i; 1308 1309 rf = devm_kcalloc(dev, num_fields, sizeof(*rf), GFP_KERNEL); 1310 if (!rf) 1311 return -ENOMEM; 1312 1313 for (i = 0; i < num_fields; i++) { 1314 regmap_field_init(&rf[i], regmap, reg_field[i]); 1315 rm_field[i] = &rf[i]; 1316 } 1317 1318 return 0; 1319 } 1320 EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_alloc); 1321 1322 /** 1323 * regmap_field_bulk_free() - Free register field allocated using 1324 * regmap_field_bulk_alloc. 1325 * 1326 * @field: regmap fields which should be freed. 1327 */ 1328 void regmap_field_bulk_free(struct regmap_field *field) 1329 { 1330 kfree(field); 1331 } 1332 EXPORT_SYMBOL_GPL(regmap_field_bulk_free); 1333 1334 /** 1335 * devm_regmap_field_bulk_free() - Free a bulk register field allocated using 1336 * devm_regmap_field_bulk_alloc. 1337 * 1338 * @dev: Device that will be interacted with 1339 * @field: regmap field which should be freed. 1340 * 1341 * Free register field allocated using devm_regmap_field_bulk_alloc(). Usually 1342 * drivers need not call this function, as the memory allocated via devm 1343 * will be freed as per device-driver life-cycle. 1344 */ 1345 void devm_regmap_field_bulk_free(struct device *dev, 1346 struct regmap_field *field) 1347 { 1348 devm_kfree(dev, field); 1349 } 1350 EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_free); 1351 1352 /** 1353 * devm_regmap_field_free() - Free a register field allocated using 1354 * devm_regmap_field_alloc. 1355 * 1356 * @dev: Device that will be interacted with 1357 * @field: regmap field which should be freed. 1358 * 1359 * Free register field allocated using devm_regmap_field_alloc(). Usually 1360 * drivers need not call this function, as the memory allocated via devm 1361 * will be freed as per device-driver life-cyle. 1362 */ 1363 void devm_regmap_field_free(struct device *dev, 1364 struct regmap_field *field) 1365 { 1366 devm_kfree(dev, field); 1367 } 1368 EXPORT_SYMBOL_GPL(devm_regmap_field_free); 1369 1370 /** 1371 * regmap_field_alloc() - Allocate and initialise a register field. 1372 * 1373 * @regmap: regmap bank in which this register field is located. 1374 * @reg_field: Register field with in the bank. 1375 * 1376 * The return value will be an ERR_PTR() on error or a valid pointer 1377 * to a struct regmap_field. The regmap_field should be freed by the 1378 * user once its finished working with it using regmap_field_free(). 1379 */ 1380 struct regmap_field *regmap_field_alloc(struct regmap *regmap, 1381 struct reg_field reg_field) 1382 { 1383 struct regmap_field *rm_field = kzalloc_obj(*rm_field); 1384 1385 if (!rm_field) 1386 return ERR_PTR(-ENOMEM); 1387 1388 regmap_field_init(rm_field, regmap, reg_field); 1389 1390 return rm_field; 1391 } 1392 EXPORT_SYMBOL_GPL(regmap_field_alloc); 1393 1394 /** 1395 * regmap_field_free() - Free register field allocated using 1396 * regmap_field_alloc. 1397 * 1398 * @field: regmap field which should be freed. 1399 */ 1400 void regmap_field_free(struct regmap_field *field) 1401 { 1402 kfree(field); 1403 } 1404 EXPORT_SYMBOL_GPL(regmap_field_free); 1405 1406 /** 1407 * regmap_reinit_cache() - Reinitialise the current register cache 1408 * 1409 * @map: Register map to operate on. 1410 * @config: New configuration. Only the cache data will be used. 1411 * 1412 * Discard any existing register cache for the map and initialize a 1413 * new cache. This can be used to restore the cache to defaults or to 1414 * update the cache configuration to reflect runtime discovery of the 1415 * hardware. 1416 * 1417 * No explicit locking is done here, the user needs to ensure that 1418 * this function will not race with other calls to regmap. 1419 */ 1420 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) 1421 { 1422 int ret; 1423 1424 regmap_debugfs_exit(map); 1425 regcache_exit(map); 1426 1427 map->max_register = config->max_register; 1428 map->max_register_is_set = map->max_register ?: config->max_register_is_0; 1429 map->writeable_reg = config->writeable_reg; 1430 map->readable_reg = config->readable_reg; 1431 map->volatile_reg = config->volatile_reg; 1432 map->precious_reg = config->precious_reg; 1433 map->writeable_noinc_reg = config->writeable_noinc_reg; 1434 map->readable_noinc_reg = config->readable_noinc_reg; 1435 map->reg_default_cb = config->reg_default_cb; 1436 map->cache_type = config->cache_type; 1437 1438 ret = regmap_set_name(map, config); 1439 if (ret) 1440 return ret; 1441 1442 map->cache_bypass = false; 1443 map->cache_only = false; 1444 1445 ret = regcache_init(map, config); 1446 if (ret) 1447 return ret; 1448 1449 regmap_debugfs_init(map); 1450 1451 return 0; 1452 } 1453 EXPORT_SYMBOL_GPL(regmap_reinit_cache); 1454 1455 /** 1456 * regmap_exit() - Free a previously allocated register map 1457 * 1458 * @map: Register map to operate on. 1459 */ 1460 void regmap_exit(struct regmap *map) 1461 { 1462 struct regmap_async *async; 1463 1464 regmap_detach_dev(map->dev, map); 1465 regmap_debugfs_exit(map); 1466 regcache_exit(map); 1467 regmap_range_exit(map); 1468 if (map->bus && map->bus->free_context) 1469 map->bus->free_context(map->bus_context); 1470 kfree(map->work_buf); 1471 while (!list_empty(&map->async_free)) { 1472 async = list_first_entry_or_null(&map->async_free, 1473 struct regmap_async, 1474 list); 1475 list_del(&async->list); 1476 kfree(async->work_buf); 1477 kfree(async); 1478 } 1479 if (map->hwlock) 1480 hwspin_lock_free(map->hwlock); 1481 if (map->lock == regmap_lock_mutex) 1482 mutex_destroy(&map->mutex); 1483 kfree_const(map->name); 1484 kfree(map->patch); 1485 if (map->bus && map->bus->free_on_exit) 1486 kfree(map->bus); 1487 kfree(map); 1488 } 1489 EXPORT_SYMBOL_GPL(regmap_exit); 1490 1491 static int dev_get_regmap_match(struct device *dev, void *res, void *data) 1492 { 1493 struct regmap **r = res; 1494 if (!r || !*r) { 1495 WARN_ON(!r || !*r); 1496 return 0; 1497 } 1498 1499 /* If the user didn't specify a name match any */ 1500 if (data) 1501 return (*r)->name && !strcmp((*r)->name, data); 1502 else 1503 return 1; 1504 } 1505 1506 /** 1507 * dev_get_regmap() - Obtain the regmap (if any) for a device 1508 * 1509 * @dev: Device to retrieve the map for 1510 * @name: Optional name for the register map, usually NULL. 1511 * 1512 * Returns the regmap for the device if one is present, or NULL. If 1513 * name is specified then it must match the name specified when 1514 * registering the device, if it is NULL then the first regmap found 1515 * will be used. Devices with multiple register maps are very rare, 1516 * generic code should normally not need to specify a name. 1517 */ 1518 struct regmap *dev_get_regmap(struct device *dev, const char *name) 1519 { 1520 struct regmap **r = devres_find(dev, dev_get_regmap_release, 1521 dev_get_regmap_match, (void *)name); 1522 1523 if (!r) 1524 return NULL; 1525 return *r; 1526 } 1527 EXPORT_SYMBOL_GPL(dev_get_regmap); 1528 1529 /** 1530 * regmap_get_device() - Obtain the device from a regmap 1531 * 1532 * @map: Register map to operate on. 1533 * 1534 * Returns the underlying device that the regmap has been created for. 1535 */ 1536 struct device *regmap_get_device(struct regmap *map) 1537 { 1538 return map->dev; 1539 } 1540 EXPORT_SYMBOL_GPL(regmap_get_device); 1541 1542 static int _regmap_select_page(struct regmap *map, unsigned int *reg, 1543 struct regmap_range_node *range, 1544 unsigned int val_num) 1545 { 1546 void *orig_work_buf; 1547 unsigned int selector_reg; 1548 unsigned int win_offset; 1549 unsigned int win_page; 1550 bool page_chg; 1551 int ret; 1552 1553 win_offset = (*reg - range->range_min) % range->window_len; 1554 win_page = (*reg - range->range_min) / range->window_len; 1555 1556 if (val_num > 1) { 1557 /* Bulk write shouldn't cross range boundary */ 1558 if (*reg + val_num - 1 > range->range_max) 1559 return -EINVAL; 1560 1561 /* ... or single page boundary */ 1562 if (val_num > range->window_len - win_offset) 1563 return -EINVAL; 1564 } 1565 1566 /* 1567 * Calculate the address of the selector register in the corresponding 1568 * data window if it is located on every page. 1569 */ 1570 page_chg = in_range(range->selector_reg, range->window_start, range->window_len); 1571 if (page_chg) 1572 selector_reg = range->range_min + win_page * range->window_len + 1573 range->selector_reg - range->window_start; 1574 1575 /* 1576 * It is possible to have selector register inside data window. 1577 * In that case, selector register is located on every page and it 1578 * needs no page switching, when accessed alone. 1579 * 1580 * Nevertheless we should synchronize the cache values for it. 1581 * This can't be properly achieved if the selector register is 1582 * the first and the only one to be read inside the data window. 1583 * That's why we update it in that case as well. 1584 * 1585 * However, we specifically avoid updating it for the default page, 1586 * when it's overlapped with the real data window, to prevent from 1587 * infinite looping. 1588 */ 1589 if (val_num > 1 || 1590 (page_chg && selector_reg != range->selector_reg) || 1591 range->window_start + win_offset != range->selector_reg) { 1592 /* Use separate work_buf during page switching */ 1593 orig_work_buf = map->work_buf; 1594 map->work_buf = map->selector_work_buf; 1595 1596 ret = _regmap_update_bits(map, range->selector_reg, 1597 range->selector_mask, 1598 win_page << range->selector_shift, 1599 NULL, false); 1600 1601 map->work_buf = orig_work_buf; 1602 1603 if (ret != 0) 1604 return ret; 1605 } 1606 1607 *reg = range->window_start + win_offset; 1608 1609 return 0; 1610 } 1611 1612 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes, 1613 unsigned long mask) 1614 { 1615 u8 *buf; 1616 int i; 1617 1618 if (!mask || !map->work_buf) 1619 return; 1620 1621 buf = map->work_buf; 1622 1623 for (i = 0; i < max_bytes; i++) 1624 buf[i] |= (mask >> (8 * i)) & 0xff; 1625 } 1626 1627 static unsigned int regmap_reg_addr(struct regmap *map, unsigned int reg) 1628 { 1629 reg += map->reg_base; 1630 1631 if (map->format.reg_shift > 0) 1632 reg >>= map->format.reg_shift; 1633 else if (map->format.reg_shift < 0) 1634 reg <<= -(map->format.reg_shift); 1635 1636 return reg; 1637 } 1638 1639 static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg, 1640 const void *val, size_t val_len, bool noinc) 1641 { 1642 struct regmap_range_node *range; 1643 unsigned long flags; 1644 void *work_val = map->work_buf + map->format.reg_bytes + 1645 map->format.pad_bytes; 1646 void *buf; 1647 int ret = -ENOTSUPP; 1648 size_t len; 1649 int i; 1650 1651 /* Check for unwritable or noinc registers in range 1652 * before we start 1653 */ 1654 if (!regmap_writeable_noinc(map, reg)) { 1655 for (i = 0; i < val_len / map->format.val_bytes; i++) { 1656 unsigned int element = 1657 reg + regmap_get_offset(map, i); 1658 if (!regmap_writeable(map, element) || 1659 regmap_writeable_noinc(map, element)) 1660 return -EINVAL; 1661 } 1662 } 1663 1664 if (!map->cache_bypass && map->format.parse_val) { 1665 unsigned int ival, offset; 1666 int val_bytes = map->format.val_bytes; 1667 1668 /* Cache the last written value for noinc writes */ 1669 i = noinc ? val_len - val_bytes : 0; 1670 for (; i < val_len; i += val_bytes) { 1671 ival = map->format.parse_val(val + i); 1672 offset = noinc ? 0 : regmap_get_offset(map, i / val_bytes); 1673 ret = regcache_write(map, reg + offset, ival); 1674 if (ret) { 1675 dev_err(map->dev, 1676 "Error in caching of register: %x ret: %d\n", 1677 reg + offset, ret); 1678 return ret; 1679 } 1680 } 1681 if (map->cache_only) { 1682 map->cache_dirty = true; 1683 return 0; 1684 } 1685 } 1686 1687 range = _regmap_range_lookup(map, reg); 1688 if (range) { 1689 int val_num = val_len / map->format.val_bytes; 1690 int win_offset = (reg - range->range_min) % range->window_len; 1691 int win_residue = range->window_len - win_offset; 1692 1693 /* If the write goes beyond the end of the window split it */ 1694 while (val_num > win_residue) { 1695 dev_dbg(map->dev, "Writing window %d/%zu\n", 1696 win_residue, val_len / map->format.val_bytes); 1697 ret = _regmap_raw_write_impl(map, reg, val, 1698 win_residue * 1699 map->format.val_bytes, noinc); 1700 if (ret != 0) 1701 return ret; 1702 1703 reg += win_residue; 1704 val_num -= win_residue; 1705 val += win_residue * map->format.val_bytes; 1706 val_len -= win_residue * map->format.val_bytes; 1707 1708 win_offset = (reg - range->range_min) % 1709 range->window_len; 1710 win_residue = range->window_len - win_offset; 1711 } 1712 1713 ret = _regmap_select_page(map, ®, range, noinc ? 1 : val_num); 1714 if (ret != 0) 1715 return ret; 1716 } 1717 1718 reg = regmap_reg_addr(map, reg); 1719 map->format.format_reg(map->work_buf, reg, map->reg_shift); 1720 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes, 1721 map->write_flag_mask); 1722 1723 /* 1724 * Essentially all I/O mechanisms will be faster with a single 1725 * buffer to write. Since register syncs often generate raw 1726 * writes of single registers optimise that case. 1727 */ 1728 if (val != work_val && val_len == map->format.val_bytes) { 1729 memcpy(work_val, val, map->format.val_bytes); 1730 val = work_val; 1731 } 1732 1733 if (map->async && map->bus && map->bus->async_write) { 1734 struct regmap_async *async; 1735 1736 trace_regmap_async_write_start(map, reg, val_len); 1737 1738 spin_lock_irqsave(&map->async_lock, flags); 1739 async = list_first_entry_or_null(&map->async_free, 1740 struct regmap_async, 1741 list); 1742 if (async) 1743 list_del(&async->list); 1744 spin_unlock_irqrestore(&map->async_lock, flags); 1745 1746 if (!async) { 1747 async = map->bus->async_alloc(); 1748 if (!async) 1749 return -ENOMEM; 1750 1751 async->work_buf = kzalloc(map->format.buf_size, 1752 GFP_KERNEL | GFP_DMA); 1753 if (!async->work_buf) { 1754 kfree(async); 1755 return -ENOMEM; 1756 } 1757 } 1758 1759 async->map = map; 1760 1761 /* If the caller supplied the value we can use it safely. */ 1762 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes + 1763 map->format.reg_bytes + map->format.val_bytes); 1764 1765 spin_lock_irqsave(&map->async_lock, flags); 1766 list_add_tail(&async->list, &map->async_list); 1767 spin_unlock_irqrestore(&map->async_lock, flags); 1768 1769 if (val != work_val) 1770 ret = map->bus->async_write(map->bus_context, 1771 async->work_buf, 1772 map->format.reg_bytes + 1773 map->format.pad_bytes, 1774 val, val_len, async); 1775 else 1776 ret = map->bus->async_write(map->bus_context, 1777 async->work_buf, 1778 map->format.reg_bytes + 1779 map->format.pad_bytes + 1780 val_len, NULL, 0, async); 1781 1782 if (ret != 0) { 1783 dev_err(map->dev, "Failed to schedule write: %d\n", 1784 ret); 1785 1786 spin_lock_irqsave(&map->async_lock, flags); 1787 list_move(&async->list, &map->async_free); 1788 spin_unlock_irqrestore(&map->async_lock, flags); 1789 } 1790 1791 return ret; 1792 } 1793 1794 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes); 1795 1796 /* If we're doing a single register write we can probably just 1797 * send the work_buf directly, otherwise try to do a gather 1798 * write. 1799 */ 1800 if (val == work_val) 1801 ret = map->write(map->bus_context, map->work_buf, 1802 map->format.reg_bytes + 1803 map->format.pad_bytes + 1804 val_len); 1805 else if (map->bus && map->bus->gather_write) 1806 ret = map->bus->gather_write(map->bus_context, map->work_buf, 1807 map->format.reg_bytes + 1808 map->format.pad_bytes, 1809 val, val_len); 1810 else 1811 ret = -ENOTSUPP; 1812 1813 /* If that didn't work fall back on linearising by hand. */ 1814 if (ret == -ENOTSUPP) { 1815 len = map->format.reg_bytes + map->format.pad_bytes + val_len; 1816 buf = kzalloc(len, GFP_KERNEL); 1817 if (!buf) 1818 return -ENOMEM; 1819 1820 memcpy(buf, map->work_buf, map->format.reg_bytes); 1821 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, 1822 val, val_len); 1823 ret = map->write(map->bus_context, buf, len); 1824 1825 kfree(buf); 1826 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) { 1827 /* regcache_drop_region() takes lock that we already have, 1828 * thus call map->cache_ops->drop() directly 1829 */ 1830 if (map->cache_ops && map->cache_ops->drop) 1831 map->cache_ops->drop(map, reg, reg + 1); 1832 } 1833 1834 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes); 1835 1836 return ret; 1837 } 1838 1839 /** 1840 * regmap_can_raw_write - Test if regmap_raw_write() is supported 1841 * 1842 * @map: Map to check. 1843 */ 1844 bool regmap_can_raw_write(struct regmap *map) 1845 { 1846 return map->write && map->format.format_val && map->format.format_reg; 1847 } 1848 EXPORT_SYMBOL_GPL(regmap_can_raw_write); 1849 1850 /** 1851 * regmap_get_raw_read_max - Get the maximum size we can read 1852 * 1853 * @map: Map to check. 1854 */ 1855 size_t regmap_get_raw_read_max(struct regmap *map) 1856 { 1857 return map->max_raw_read; 1858 } 1859 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max); 1860 1861 /** 1862 * regmap_get_raw_write_max - Get the maximum size we can read 1863 * 1864 * @map: Map to check. 1865 */ 1866 size_t regmap_get_raw_write_max(struct regmap *map) 1867 { 1868 return map->max_raw_write; 1869 } 1870 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max); 1871 1872 static int _regmap_bus_formatted_write(void *context, unsigned int reg, 1873 unsigned int val) 1874 { 1875 int ret; 1876 struct regmap_range_node *range; 1877 struct regmap *map = context; 1878 1879 WARN_ON(!map->format.format_write); 1880 1881 range = _regmap_range_lookup(map, reg); 1882 if (range) { 1883 ret = _regmap_select_page(map, ®, range, 1); 1884 if (ret != 0) 1885 return ret; 1886 } 1887 1888 reg = regmap_reg_addr(map, reg); 1889 map->format.format_write(map, reg, val); 1890 1891 trace_regmap_hw_write_start(map, reg, 1); 1892 1893 ret = map->write(map->bus_context, map->work_buf, map->format.buf_size); 1894 1895 trace_regmap_hw_write_done(map, reg, 1); 1896 1897 return ret; 1898 } 1899 1900 static int _regmap_bus_reg_write(void *context, unsigned int reg, 1901 unsigned int val) 1902 { 1903 struct regmap *map = context; 1904 struct regmap_range_node *range; 1905 int ret; 1906 1907 range = _regmap_range_lookup(map, reg); 1908 if (range) { 1909 ret = _regmap_select_page(map, ®, range, 1); 1910 if (ret != 0) 1911 return ret; 1912 } 1913 1914 reg = regmap_reg_addr(map, reg); 1915 return map->bus->reg_write(map->bus_context, reg, val); 1916 } 1917 1918 static int _regmap_bus_raw_write(void *context, unsigned int reg, 1919 unsigned int val) 1920 { 1921 struct regmap *map = context; 1922 1923 WARN_ON(!map->format.format_val); 1924 1925 map->format.format_val(map->work_buf + map->format.reg_bytes 1926 + map->format.pad_bytes, val, 0); 1927 return _regmap_raw_write_impl(map, reg, 1928 map->work_buf + 1929 map->format.reg_bytes + 1930 map->format.pad_bytes, 1931 map->format.val_bytes, 1932 false); 1933 } 1934 1935 static inline void *_regmap_map_get_context(struct regmap *map) 1936 { 1937 return (map->bus || (!map->bus && map->read)) ? map : map->bus_context; 1938 } 1939 1940 int _regmap_write(struct regmap *map, unsigned int reg, 1941 unsigned int val) 1942 { 1943 int ret; 1944 void *context = _regmap_map_get_context(map); 1945 1946 if (!regmap_writeable(map, reg)) 1947 return -EIO; 1948 1949 if (!map->cache_bypass && !map->defer_caching) { 1950 ret = regcache_write(map, reg, val); 1951 if (ret != 0) 1952 return ret; 1953 if (map->cache_only) { 1954 map->cache_dirty = true; 1955 return 0; 1956 } 1957 } 1958 1959 ret = map->reg_write(context, reg, val); 1960 if (ret == 0) { 1961 if (regmap_should_log(map)) 1962 dev_info(map->dev, "%x <= %x\n", reg, val); 1963 1964 trace_regmap_reg_write(map, reg, val); 1965 } 1966 1967 return ret; 1968 } 1969 1970 /** 1971 * regmap_write() - Write a value to a single register 1972 * 1973 * @map: Register map to write to 1974 * @reg: Register to write to 1975 * @val: Value to be written 1976 * 1977 * A value of zero will be returned on success, a negative errno will 1978 * be returned in error cases. 1979 */ 1980 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) 1981 { 1982 if (!IS_ALIGNED(reg, map->reg_stride)) 1983 return -EINVAL; 1984 1985 guard(regmap)(map); 1986 1987 return _regmap_write(map, reg, val); 1988 } 1989 EXPORT_SYMBOL_GPL(regmap_write); 1990 1991 /** 1992 * regmap_write_async() - Write a value to a single register asynchronously 1993 * 1994 * @map: Register map to write to 1995 * @reg: Register to write to 1996 * @val: Value to be written 1997 * 1998 * A value of zero will be returned on success, a negative errno will 1999 * be returned in error cases. 2000 */ 2001 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val) 2002 { 2003 int ret; 2004 2005 if (!IS_ALIGNED(reg, map->reg_stride)) 2006 return -EINVAL; 2007 2008 guard(regmap)(map); 2009 2010 map->async = true; 2011 2012 ret = _regmap_write(map, reg, val); 2013 2014 map->async = false; 2015 2016 return ret; 2017 } 2018 EXPORT_SYMBOL_GPL(regmap_write_async); 2019 2020 int _regmap_raw_write(struct regmap *map, unsigned int reg, 2021 const void *val, size_t val_len, bool noinc) 2022 { 2023 size_t val_bytes = map->format.val_bytes; 2024 size_t val_count = val_len / val_bytes; 2025 size_t chunk_count, chunk_bytes; 2026 size_t chunk_regs = val_count; 2027 int ret, i; 2028 2029 if (!val_count) 2030 return -EINVAL; 2031 2032 if (map->use_single_write) 2033 chunk_regs = 1; 2034 else if (map->max_raw_write && val_len > map->max_raw_write) 2035 chunk_regs = map->max_raw_write / val_bytes; 2036 2037 chunk_count = val_count / chunk_regs; 2038 chunk_bytes = chunk_regs * val_bytes; 2039 2040 /* Write as many bytes as possible with chunk_size */ 2041 for (i = 0; i < chunk_count; i++) { 2042 ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc); 2043 if (ret) 2044 return ret; 2045 2046 reg += regmap_get_offset(map, chunk_regs); 2047 val += chunk_bytes; 2048 val_len -= chunk_bytes; 2049 } 2050 2051 /* Write remaining bytes */ 2052 if (val_len) 2053 ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc); 2054 2055 return ret; 2056 } 2057 2058 /** 2059 * regmap_raw_write() - Write raw values to one or more registers 2060 * 2061 * @map: Register map to write to 2062 * @reg: Initial register to write to 2063 * @val: Block of data to be written, laid out for direct transmission to the 2064 * device 2065 * @val_len: Length of data pointed to by val. 2066 * 2067 * This function is intended to be used for things like firmware 2068 * download where a large block of data needs to be transferred to the 2069 * device. No formatting will be done on the data provided. 2070 * 2071 * A value of zero will be returned on success, a negative errno will 2072 * be returned in error cases. 2073 */ 2074 int regmap_raw_write(struct regmap *map, unsigned int reg, 2075 const void *val, size_t val_len) 2076 { 2077 if (!regmap_can_raw_write(map)) 2078 return -EINVAL; 2079 if (val_len % map->format.val_bytes) 2080 return -EINVAL; 2081 2082 guard(regmap)(map); 2083 2084 return _regmap_raw_write(map, reg, val, val_len, false); 2085 } 2086 EXPORT_SYMBOL_GPL(regmap_raw_write); 2087 2088 static int regmap_noinc_readwrite(struct regmap *map, unsigned int reg, 2089 void *val, unsigned int val_len, bool write) 2090 { 2091 size_t val_bytes = map->format.val_bytes; 2092 size_t val_count = val_len / val_bytes; 2093 unsigned int lastval; 2094 u8 *u8p; 2095 u16 *u16p; 2096 u32 *u32p; 2097 int ret; 2098 int i; 2099 2100 switch (val_bytes) { 2101 case 1: 2102 u8p = val; 2103 if (write) 2104 lastval = (unsigned int)u8p[val_count - 1]; 2105 break; 2106 case 2: 2107 u16p = val; 2108 if (write) 2109 lastval = (unsigned int)u16p[val_count - 1]; 2110 break; 2111 case 4: 2112 u32p = val; 2113 if (write) 2114 lastval = (unsigned int)u32p[val_count - 1]; 2115 break; 2116 default: 2117 return -EINVAL; 2118 } 2119 2120 /* 2121 * Update the cache with the last value we write, the rest is just 2122 * gone down in the hardware FIFO. We can't cache FIFOs. This makes 2123 * sure a single read from the cache will work. 2124 */ 2125 if (write) { 2126 if (!map->cache_bypass && !map->defer_caching) { 2127 ret = regcache_write(map, reg, lastval); 2128 if (ret != 0) 2129 return ret; 2130 if (map->cache_only) { 2131 map->cache_dirty = true; 2132 return 0; 2133 } 2134 } 2135 ret = map->bus->reg_noinc_write(map->bus_context, reg, val, val_count); 2136 } else { 2137 ret = map->bus->reg_noinc_read(map->bus_context, reg, val, val_count); 2138 } 2139 2140 if (!ret && regmap_should_log(map)) { 2141 dev_info(map->dev, "%x %s [", reg, write ? "<=" : "=>"); 2142 for (i = 0; i < val_count; i++) { 2143 switch (val_bytes) { 2144 case 1: 2145 pr_cont("%x", u8p[i]); 2146 break; 2147 case 2: 2148 pr_cont("%x", u16p[i]); 2149 break; 2150 case 4: 2151 pr_cont("%x", u32p[i]); 2152 break; 2153 default: 2154 break; 2155 } 2156 if (i == (val_count - 1)) 2157 pr_cont("]\n"); 2158 else 2159 pr_cont(","); 2160 } 2161 } 2162 2163 return 0; 2164 } 2165 2166 /** 2167 * regmap_noinc_write(): Write data to a register without incrementing the 2168 * register number 2169 * 2170 * @map: Register map to write to 2171 * @reg: Register to write to 2172 * @val: Pointer to data buffer 2173 * @val_len: Length of output buffer in bytes. 2174 * 2175 * The regmap API usually assumes that bulk bus write operations will write a 2176 * range of registers. Some devices have certain registers for which a write 2177 * operation can write to an internal FIFO. 2178 * 2179 * The target register must be volatile but registers after it can be 2180 * completely unrelated cacheable registers. 2181 * 2182 * This will attempt multiple writes as required to write val_len bytes. 2183 * 2184 * A value of zero will be returned on success, a negative errno will be 2185 * returned in error cases. 2186 */ 2187 int regmap_noinc_write(struct regmap *map, unsigned int reg, 2188 const void *val, size_t val_len) 2189 { 2190 size_t write_len; 2191 int ret; 2192 2193 if (!map->write && !(map->bus && map->bus->reg_noinc_write)) 2194 return -EINVAL; 2195 if (val_len % map->format.val_bytes) 2196 return -EINVAL; 2197 if (!IS_ALIGNED(reg, map->reg_stride)) 2198 return -EINVAL; 2199 if (val_len == 0) 2200 return -EINVAL; 2201 2202 guard(regmap)(map); 2203 2204 if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) 2205 return -EINVAL; 2206 2207 /* 2208 * Use the accelerated operation if we can. The val drops the const 2209 * typing in order to facilitate code reuse in regmap_noinc_readwrite(). 2210 */ 2211 if (map->bus->reg_noinc_write) 2212 return regmap_noinc_readwrite(map, reg, (void *)val, val_len, true); 2213 2214 while (val_len) { 2215 if (map->max_raw_write && map->max_raw_write < val_len) 2216 write_len = map->max_raw_write; 2217 else 2218 write_len = val_len; 2219 ret = _regmap_raw_write(map, reg, val, write_len, true); 2220 if (ret) 2221 return ret; 2222 val = ((u8 *)val) + write_len; 2223 val_len -= write_len; 2224 } 2225 2226 return 0; 2227 } 2228 EXPORT_SYMBOL_GPL(regmap_noinc_write); 2229 2230 /** 2231 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a 2232 * register field. 2233 * 2234 * @field: Register field to write to 2235 * @mask: Bitmask to change 2236 * @val: Value to be written 2237 * @change: Boolean indicating if a write was done 2238 * @async: Boolean indicating asynchronously 2239 * @force: Boolean indicating use force update 2240 * 2241 * Perform a read/modify/write cycle on the register field with change, 2242 * async, force option. 2243 * 2244 * A value of zero will be returned on success, a negative errno will 2245 * be returned in error cases. 2246 */ 2247 int regmap_field_update_bits_base(struct regmap_field *field, 2248 unsigned int mask, unsigned int val, 2249 bool *change, bool async, bool force) 2250 { 2251 mask = (mask << field->shift) & field->mask; 2252 2253 return regmap_update_bits_base(field->regmap, field->reg, 2254 mask, val << field->shift, 2255 change, async, force); 2256 } 2257 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base); 2258 2259 /** 2260 * regmap_field_test_bits() - Check if all specified bits are set in a 2261 * register field. 2262 * 2263 * @field: Register field to operate on 2264 * @bits: Bits to test 2265 * 2266 * Returns negative errno if the underlying regmap_field_read() fails, 2267 * 0 if at least one of the tested bits is not set and 1 if all tested 2268 * bits are set. 2269 */ 2270 int regmap_field_test_bits(struct regmap_field *field, unsigned int bits) 2271 { 2272 unsigned int val; 2273 int ret; 2274 2275 ret = regmap_field_read(field, &val); 2276 if (ret) 2277 return ret; 2278 2279 return (val & bits) == bits; 2280 } 2281 EXPORT_SYMBOL_GPL(regmap_field_test_bits); 2282 2283 /** 2284 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a 2285 * register field with port ID 2286 * 2287 * @field: Register field to write to 2288 * @id: port ID 2289 * @mask: Bitmask to change 2290 * @val: Value to be written 2291 * @change: Boolean indicating if a write was done 2292 * @async: Boolean indicating asynchronously 2293 * @force: Boolean indicating use force update 2294 * 2295 * A value of zero will be returned on success, a negative errno will 2296 * be returned in error cases. 2297 */ 2298 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id, 2299 unsigned int mask, unsigned int val, 2300 bool *change, bool async, bool force) 2301 { 2302 if (id >= field->id_size) 2303 return -EINVAL; 2304 2305 mask = (mask << field->shift) & field->mask; 2306 2307 return regmap_update_bits_base(field->regmap, 2308 field->reg + (field->id_offset * id), 2309 mask, val << field->shift, 2310 change, async, force); 2311 } 2312 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base); 2313 2314 /** 2315 * regmap_bulk_write() - Write multiple registers to the device 2316 * 2317 * @map: Register map to write to 2318 * @reg: First register to be write from 2319 * @val: Block of data to be written, in native register size for device 2320 * @val_count: Number of registers to write 2321 * 2322 * This function is intended to be used for writing a large block of 2323 * data to the device either in single transfer or multiple transfer. 2324 * 2325 * A value of zero will be returned on success, a negative errno will 2326 * be returned in error cases. 2327 */ 2328 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, 2329 size_t val_count) 2330 { 2331 int ret = 0, i; 2332 size_t val_bytes = map->format.val_bytes; 2333 2334 if (!IS_ALIGNED(reg, map->reg_stride)) 2335 return -EINVAL; 2336 2337 /* 2338 * Some devices don't support bulk write, for them we have a series of 2339 * single write operations. 2340 */ 2341 if (!map->write || !map->format.parse_inplace) { 2342 guard(regmap)(map); 2343 2344 for (i = 0; i < val_count; i++) { 2345 unsigned int ival; 2346 2347 switch (val_bytes) { 2348 case 1: 2349 ival = *(u8 *)(val + (i * val_bytes)); 2350 break; 2351 case 2: 2352 ival = *(u16 *)(val + (i * val_bytes)); 2353 break; 2354 case 4: 2355 ival = *(u32 *)(val + (i * val_bytes)); 2356 break; 2357 default: 2358 return -EINVAL; 2359 } 2360 2361 ret = _regmap_write(map, 2362 reg + regmap_get_offset(map, i), 2363 ival); 2364 if (ret != 0) 2365 return ret; 2366 } 2367 } else { 2368 void *wval; 2369 2370 wval = kmemdup_array(val, val_count, val_bytes, map->alloc_flags); 2371 if (!wval) 2372 return -ENOMEM; 2373 2374 for (i = 0; i < val_count * val_bytes; i += val_bytes) 2375 map->format.parse_inplace(wval + i); 2376 2377 ret = regmap_raw_write(map, reg, wval, val_bytes * val_count); 2378 2379 kfree(wval); 2380 } 2381 2382 if (!ret) 2383 trace_regmap_bulk_write(map, reg, val, val_bytes * val_count); 2384 2385 return ret; 2386 } 2387 EXPORT_SYMBOL_GPL(regmap_bulk_write); 2388 2389 /* 2390 * _regmap_raw_multi_reg_write() 2391 * 2392 * the (register,newvalue) pairs in regs have not been formatted, but 2393 * they are all in the same page and have been changed to being page 2394 * relative. The page register has been written if that was necessary. 2395 */ 2396 static int _regmap_raw_multi_reg_write(struct regmap *map, 2397 const struct reg_sequence *regs, 2398 size_t num_regs) 2399 { 2400 int ret; 2401 void *buf; 2402 int i; 2403 u8 *u8; 2404 size_t val_bytes = map->format.val_bytes; 2405 size_t reg_bytes = map->format.reg_bytes; 2406 size_t pad_bytes = map->format.pad_bytes; 2407 size_t pair_size = reg_bytes + pad_bytes + val_bytes; 2408 size_t len = pair_size * num_regs; 2409 2410 if (!len) 2411 return -EINVAL; 2412 2413 buf = kzalloc(len, GFP_KERNEL); 2414 if (!buf) 2415 return -ENOMEM; 2416 2417 /* We have to linearise by hand. */ 2418 2419 u8 = buf; 2420 2421 for (i = 0; i < num_regs; i++) { 2422 unsigned int reg = regs[i].reg; 2423 unsigned int val = regs[i].def; 2424 trace_regmap_hw_write_start(map, reg, 1); 2425 reg = regmap_reg_addr(map, reg); 2426 map->format.format_reg(u8, reg, map->reg_shift); 2427 u8 += reg_bytes + pad_bytes; 2428 map->format.format_val(u8, val, 0); 2429 u8 += val_bytes; 2430 } 2431 u8 = buf; 2432 *u8 |= map->write_flag_mask; 2433 2434 ret = map->write(map->bus_context, buf, len); 2435 2436 kfree(buf); 2437 2438 for (i = 0; i < num_regs; i++) { 2439 int reg = regs[i].reg; 2440 trace_regmap_hw_write_done(map, reg, 1); 2441 } 2442 return ret; 2443 } 2444 2445 static unsigned int _regmap_register_page(struct regmap *map, 2446 unsigned int reg, 2447 struct regmap_range_node *range) 2448 { 2449 unsigned int win_page = (reg - range->range_min) / range->window_len; 2450 2451 return win_page; 2452 } 2453 2454 static int _regmap_range_multi_paged_reg_write(struct regmap *map, 2455 struct reg_sequence *regs, 2456 size_t num_regs) 2457 { 2458 int ret; 2459 int i, n; 2460 struct reg_sequence *base; 2461 unsigned int this_page = 0; 2462 unsigned int page_change = 0; 2463 /* 2464 * the set of registers are not neccessarily in order, but 2465 * since the order of write must be preserved this algorithm 2466 * chops the set each time the page changes. This also applies 2467 * if there is a delay required at any point in the sequence. 2468 */ 2469 base = regs; 2470 for (i = 0, n = 0; i < num_regs; i++, n++) { 2471 unsigned int reg = regs[i].reg; 2472 struct regmap_range_node *range; 2473 2474 range = _regmap_range_lookup(map, reg); 2475 if (range) { 2476 unsigned int win_page = _regmap_register_page(map, reg, 2477 range); 2478 2479 if (i == 0) 2480 this_page = win_page; 2481 if (win_page != this_page) { 2482 this_page = win_page; 2483 page_change = 1; 2484 } 2485 } 2486 2487 /* If we have both a page change and a delay make sure to 2488 * write the regs and apply the delay before we change the 2489 * page. 2490 */ 2491 2492 if (page_change || regs[i].delay_us) { 2493 2494 /* For situations where the first write requires 2495 * a delay we need to make sure we don't call 2496 * raw_multi_reg_write with n=0 2497 * This can't occur with page breaks as we 2498 * never write on the first iteration 2499 */ 2500 if (regs[i].delay_us && i == 0) 2501 n = 1; 2502 2503 ret = _regmap_raw_multi_reg_write(map, base, n); 2504 if (ret != 0) 2505 return ret; 2506 2507 if (regs[i].delay_us) { 2508 if (map->can_sleep) 2509 fsleep(regs[i].delay_us); 2510 else 2511 udelay(regs[i].delay_us); 2512 } 2513 2514 base += n; 2515 n = 0; 2516 2517 if (page_change) { 2518 ret = _regmap_select_page(map, 2519 &base[n].reg, 2520 range, 1); 2521 if (ret != 0) 2522 return ret; 2523 2524 page_change = 0; 2525 } 2526 2527 } 2528 2529 } 2530 if (n > 0) 2531 return _regmap_raw_multi_reg_write(map, base, n); 2532 return 0; 2533 } 2534 2535 static int _regmap_multi_reg_write(struct regmap *map, 2536 const struct reg_sequence *regs, 2537 size_t num_regs) 2538 { 2539 int i; 2540 int ret; 2541 2542 if (!map->can_multi_write) { 2543 for (i = 0; i < num_regs; i++) { 2544 ret = _regmap_write(map, regs[i].reg, regs[i].def); 2545 if (ret != 0) 2546 return ret; 2547 2548 if (regs[i].delay_us) { 2549 if (map->can_sleep) 2550 fsleep(regs[i].delay_us); 2551 else 2552 udelay(regs[i].delay_us); 2553 } 2554 } 2555 return 0; 2556 } 2557 2558 if (!map->format.parse_inplace) 2559 return -EINVAL; 2560 2561 if (map->writeable_reg) 2562 for (i = 0; i < num_regs; i++) { 2563 int reg = regs[i].reg; 2564 if (!map->writeable_reg(map->dev, reg)) 2565 return -EINVAL; 2566 if (!IS_ALIGNED(reg, map->reg_stride)) 2567 return -EINVAL; 2568 } 2569 2570 if (!map->cache_bypass) { 2571 for (i = 0; i < num_regs; i++) { 2572 unsigned int val = regs[i].def; 2573 unsigned int reg = regs[i].reg; 2574 ret = regcache_write(map, reg, val); 2575 if (ret) { 2576 dev_err(map->dev, 2577 "Error in caching of register: %x ret: %d\n", 2578 reg, ret); 2579 return ret; 2580 } 2581 } 2582 if (map->cache_only) { 2583 map->cache_dirty = true; 2584 return 0; 2585 } 2586 } 2587 2588 WARN_ON(!map->bus); 2589 2590 for (i = 0; i < num_regs; i++) { 2591 unsigned int reg = regs[i].reg; 2592 struct regmap_range_node *range; 2593 2594 /* Coalesce all the writes between a page break or a delay 2595 * in a sequence 2596 */ 2597 range = _regmap_range_lookup(map, reg); 2598 if (range || regs[i].delay_us) { 2599 size_t len = sizeof(struct reg_sequence)*num_regs; 2600 struct reg_sequence *base = kmemdup(regs, len, 2601 GFP_KERNEL); 2602 if (!base) 2603 return -ENOMEM; 2604 ret = _regmap_range_multi_paged_reg_write(map, base, 2605 num_regs); 2606 kfree(base); 2607 2608 return ret; 2609 } 2610 } 2611 return _regmap_raw_multi_reg_write(map, regs, num_regs); 2612 } 2613 2614 /** 2615 * regmap_multi_reg_write() - Write multiple registers to the device 2616 * 2617 * @map: Register map to write to 2618 * @regs: Array of structures containing register,value to be written 2619 * @num_regs: Number of registers to write 2620 * 2621 * Write multiple registers to the device where the set of register, value 2622 * pairs are supplied in any order, possibly not all in a single range. 2623 * 2624 * The 'normal' block write mode will send ultimately send data on the 2625 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are 2626 * addressed. However, this alternative block multi write mode will send 2627 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device 2628 * must of course support the mode. 2629 * 2630 * A value of zero will be returned on success, a negative errno will be 2631 * returned in error cases. 2632 */ 2633 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs, 2634 int num_regs) 2635 { 2636 guard(regmap)(map); 2637 2638 return _regmap_multi_reg_write(map, regs, num_regs); 2639 } 2640 EXPORT_SYMBOL_GPL(regmap_multi_reg_write); 2641 2642 /** 2643 * regmap_multi_reg_write_bypassed() - Write multiple registers to the 2644 * device but not the cache 2645 * 2646 * @map: Register map to write to 2647 * @regs: Array of structures containing register,value to be written 2648 * @num_regs: Number of registers to write 2649 * 2650 * Write multiple registers to the device but not the cache where the set 2651 * of register are supplied in any order. 2652 * 2653 * This function is intended to be used for writing a large block of data 2654 * atomically to the device in single transfer for those I2C client devices 2655 * that implement this alternative block write mode. 2656 * 2657 * A value of zero will be returned on success, a negative errno will 2658 * be returned in error cases. 2659 */ 2660 int regmap_multi_reg_write_bypassed(struct regmap *map, 2661 const struct reg_sequence *regs, 2662 int num_regs) 2663 { 2664 int ret; 2665 bool bypass; 2666 2667 guard(regmap)(map); 2668 2669 bypass = map->cache_bypass; 2670 map->cache_bypass = true; 2671 2672 ret = _regmap_multi_reg_write(map, regs, num_regs); 2673 2674 map->cache_bypass = bypass; 2675 2676 return ret; 2677 } 2678 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed); 2679 2680 /** 2681 * regmap_raw_write_async() - Write raw values to one or more registers 2682 * asynchronously 2683 * 2684 * @map: Register map to write to 2685 * @reg: Initial register to write to 2686 * @val: Block of data to be written, laid out for direct transmission to the 2687 * device. Must be valid until regmap_async_complete() is called. 2688 * @val_len: Length of data pointed to by val. 2689 * 2690 * This function is intended to be used for things like firmware 2691 * download where a large block of data needs to be transferred to the 2692 * device. No formatting will be done on the data provided. 2693 * 2694 * If supported by the underlying bus the write will be scheduled 2695 * asynchronously, helping maximise I/O speed on higher speed buses 2696 * like SPI. regmap_async_complete() can be called to ensure that all 2697 * asynchronous writes have been completed. 2698 * 2699 * A value of zero will be returned on success, a negative errno will 2700 * be returned in error cases. 2701 */ 2702 int regmap_raw_write_async(struct regmap *map, unsigned int reg, 2703 const void *val, size_t val_len) 2704 { 2705 int ret; 2706 2707 if (val_len % map->format.val_bytes) 2708 return -EINVAL; 2709 if (!IS_ALIGNED(reg, map->reg_stride)) 2710 return -EINVAL; 2711 2712 guard(regmap)(map); 2713 2714 map->async = true; 2715 2716 ret = _regmap_raw_write(map, reg, val, val_len, false); 2717 2718 map->async = false; 2719 2720 return ret; 2721 } 2722 EXPORT_SYMBOL_GPL(regmap_raw_write_async); 2723 2724 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 2725 unsigned int val_len, bool noinc) 2726 { 2727 struct regmap_range_node *range; 2728 int ret; 2729 2730 if (!map->read) 2731 return -EINVAL; 2732 2733 range = _regmap_range_lookup(map, reg); 2734 if (range) { 2735 ret = _regmap_select_page(map, ®, range, 2736 noinc ? 1 : val_len / map->format.val_bytes); 2737 if (ret != 0) 2738 return ret; 2739 } 2740 2741 reg = regmap_reg_addr(map, reg); 2742 map->format.format_reg(map->work_buf, reg, map->reg_shift); 2743 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes, 2744 map->read_flag_mask); 2745 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes); 2746 2747 ret = map->read(map->bus_context, map->work_buf, 2748 map->format.reg_bytes + map->format.pad_bytes, 2749 val, val_len); 2750 2751 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes); 2752 2753 return ret; 2754 } 2755 2756 static int _regmap_bus_reg_read(void *context, unsigned int reg, 2757 unsigned int *val) 2758 { 2759 struct regmap *map = context; 2760 struct regmap_range_node *range; 2761 int ret; 2762 2763 range = _regmap_range_lookup(map, reg); 2764 if (range) { 2765 ret = _regmap_select_page(map, ®, range, 1); 2766 if (ret != 0) 2767 return ret; 2768 } 2769 2770 reg = regmap_reg_addr(map, reg); 2771 return map->bus->reg_read(map->bus_context, reg, val); 2772 } 2773 2774 static int _regmap_bus_read(void *context, unsigned int reg, 2775 unsigned int *val) 2776 { 2777 int ret; 2778 struct regmap *map = context; 2779 void *work_val = map->work_buf + map->format.reg_bytes + 2780 map->format.pad_bytes; 2781 2782 if (!map->format.parse_val) 2783 return -EINVAL; 2784 2785 ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false); 2786 if (ret == 0) 2787 *val = map->format.parse_val(work_val); 2788 2789 return ret; 2790 } 2791 2792 static int _regmap_read(struct regmap *map, unsigned int reg, 2793 unsigned int *val) 2794 { 2795 int ret; 2796 void *context = _regmap_map_get_context(map); 2797 2798 if (!map->cache_bypass) { 2799 ret = regcache_read(map, reg, val); 2800 if (ret == 0) 2801 return 0; 2802 } 2803 2804 if (map->cache_only) 2805 return -EBUSY; 2806 2807 if (!regmap_readable(map, reg)) 2808 return -EIO; 2809 2810 ret = map->reg_read(context, reg, val); 2811 if (ret == 0) { 2812 if (regmap_should_log(map)) 2813 dev_info(map->dev, "%x => %x\n", reg, *val); 2814 2815 trace_regmap_reg_read(map, reg, *val); 2816 2817 if (!map->cache_bypass) 2818 regcache_write(map, reg, *val); 2819 } 2820 2821 return ret; 2822 } 2823 2824 /** 2825 * regmap_read() - Read a value from a single register 2826 * 2827 * @map: Register map to read from 2828 * @reg: Register to be read from 2829 * @val: Pointer to store read value 2830 * 2831 * A value of zero will be returned on success, a negative errno will 2832 * be returned in error cases. 2833 */ 2834 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) 2835 { 2836 if (!IS_ALIGNED(reg, map->reg_stride)) 2837 return -EINVAL; 2838 2839 guard(regmap)(map); 2840 2841 return _regmap_read(map, reg, val); 2842 } 2843 EXPORT_SYMBOL_GPL(regmap_read); 2844 2845 /** 2846 * regmap_read_bypassed() - Read a value from a single register direct 2847 * from the device, bypassing the cache 2848 * 2849 * @map: Register map to read from 2850 * @reg: Register to be read from 2851 * @val: Pointer to store read value 2852 * 2853 * A value of zero will be returned on success, a negative errno will 2854 * be returned in error cases. 2855 */ 2856 int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val) 2857 { 2858 int ret; 2859 bool bypass, cache_only; 2860 2861 if (!IS_ALIGNED(reg, map->reg_stride)) 2862 return -EINVAL; 2863 2864 guard(regmap)(map); 2865 2866 bypass = map->cache_bypass; 2867 cache_only = map->cache_only; 2868 map->cache_bypass = true; 2869 map->cache_only = false; 2870 2871 ret = _regmap_read(map, reg, val); 2872 2873 map->cache_bypass = bypass; 2874 map->cache_only = cache_only; 2875 2876 return ret; 2877 } 2878 EXPORT_SYMBOL_GPL(regmap_read_bypassed); 2879 2880 /** 2881 * regmap_raw_read() - Read raw data from the device 2882 * 2883 * @map: Register map to read from 2884 * @reg: First register to be read from 2885 * @val: Pointer to store read value 2886 * @val_len: Size of data to read 2887 * 2888 * A value of zero will be returned on success, a negative errno will 2889 * be returned in error cases. 2890 */ 2891 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 2892 size_t val_len) 2893 { 2894 size_t val_bytes = map->format.val_bytes; 2895 size_t val_count = val_len / val_bytes; 2896 unsigned int v; 2897 int ret, i; 2898 2899 if (val_len % map->format.val_bytes) 2900 return -EINVAL; 2901 if (!IS_ALIGNED(reg, map->reg_stride)) 2902 return -EINVAL; 2903 if (val_count == 0) 2904 return -EINVAL; 2905 2906 guard(regmap)(map); 2907 2908 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || 2909 map->cache_type == REGCACHE_NONE) { 2910 size_t chunk_count, chunk_bytes; 2911 size_t chunk_regs = val_count; 2912 2913 if (!map->cache_bypass && map->cache_only) 2914 return -EBUSY; 2915 2916 if (!map->read) 2917 return -ENOTSUPP; 2918 2919 if (map->use_single_read) 2920 chunk_regs = 1; 2921 else if (map->max_raw_read && val_len > map->max_raw_read) 2922 chunk_regs = map->max_raw_read / val_bytes; 2923 2924 chunk_count = val_count / chunk_regs; 2925 chunk_bytes = chunk_regs * val_bytes; 2926 2927 /* Read bytes that fit into whole chunks */ 2928 for (i = 0; i < chunk_count; i++) { 2929 ret = _regmap_raw_read(map, reg, val, chunk_bytes, false); 2930 if (ret != 0) 2931 return ret; 2932 2933 reg += regmap_get_offset(map, chunk_regs); 2934 val += chunk_bytes; 2935 val_len -= chunk_bytes; 2936 } 2937 2938 /* Read remaining bytes */ 2939 if (val_len) { 2940 ret = _regmap_raw_read(map, reg, val, val_len, false); 2941 if (ret != 0) 2942 return ret; 2943 } 2944 } else { 2945 /* Otherwise go word by word for the cache; should be low 2946 * cost as we expect to hit the cache. 2947 */ 2948 for (i = 0; i < val_count; i++) { 2949 ret = _regmap_read(map, reg + regmap_get_offset(map, i), 2950 &v); 2951 if (ret != 0) 2952 return ret; 2953 2954 map->format.format_val(val + (i * val_bytes), v, 0); 2955 } 2956 } 2957 2958 return 0; 2959 } 2960 EXPORT_SYMBOL_GPL(regmap_raw_read); 2961 2962 /** 2963 * regmap_noinc_read(): Read data from a register without incrementing the 2964 * register number 2965 * 2966 * @map: Register map to read from 2967 * @reg: Register to read from 2968 * @val: Pointer to data buffer 2969 * @val_len: Length of output buffer in bytes. 2970 * 2971 * The regmap API usually assumes that bulk read operations will read a 2972 * range of registers. Some devices have certain registers for which a read 2973 * operation read will read from an internal FIFO. 2974 * 2975 * The target register must be volatile but registers after it can be 2976 * completely unrelated cacheable registers. 2977 * 2978 * This will attempt multiple reads as required to read val_len bytes. 2979 * 2980 * A value of zero will be returned on success, a negative errno will be 2981 * returned in error cases. 2982 */ 2983 int regmap_noinc_read(struct regmap *map, unsigned int reg, 2984 void *val, size_t val_len) 2985 { 2986 size_t read_len; 2987 int ret; 2988 2989 if (!map->read) 2990 return -ENOTSUPP; 2991 2992 if (val_len % map->format.val_bytes) 2993 return -EINVAL; 2994 if (!IS_ALIGNED(reg, map->reg_stride)) 2995 return -EINVAL; 2996 if (val_len == 0) 2997 return -EINVAL; 2998 2999 guard(regmap)(map); 3000 3001 if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) 3002 return -EINVAL; 3003 3004 /* 3005 * We have not defined the FIFO semantics for cache, as the 3006 * cache is just one value deep. Should we return the last 3007 * written value? Just avoid this by always reading the FIFO 3008 * even when using cache. Cache only will not work. 3009 */ 3010 if (!map->cache_bypass && map->cache_only) 3011 return -EBUSY; 3012 3013 /* Use the accelerated operation if we can */ 3014 if (map->bus->reg_noinc_read) 3015 return regmap_noinc_readwrite(map, reg, val, val_len, false); 3016 3017 while (val_len) { 3018 if (map->max_raw_read && map->max_raw_read < val_len) 3019 read_len = map->max_raw_read; 3020 else 3021 read_len = val_len; 3022 ret = _regmap_raw_read(map, reg, val, read_len, true); 3023 if (ret) 3024 return ret; 3025 val = ((u8 *)val) + read_len; 3026 val_len -= read_len; 3027 } 3028 3029 return 0; 3030 } 3031 EXPORT_SYMBOL_GPL(regmap_noinc_read); 3032 3033 /** 3034 * regmap_field_read(): Read a value to a single register field 3035 * 3036 * @field: Register field to read from 3037 * @val: Pointer to store read value 3038 * 3039 * A value of zero will be returned on success, a negative errno will 3040 * be returned in error cases. 3041 */ 3042 int regmap_field_read(struct regmap_field *field, unsigned int *val) 3043 { 3044 int ret; 3045 unsigned int reg_val; 3046 ret = regmap_read(field->regmap, field->reg, ®_val); 3047 if (ret != 0) 3048 return ret; 3049 3050 reg_val &= field->mask; 3051 reg_val >>= field->shift; 3052 *val = reg_val; 3053 3054 return ret; 3055 } 3056 EXPORT_SYMBOL_GPL(regmap_field_read); 3057 3058 /** 3059 * regmap_fields_read() - Read a value to a single register field with port ID 3060 * 3061 * @field: Register field to read from 3062 * @id: port ID 3063 * @val: Pointer to store read value 3064 * 3065 * A value of zero will be returned on success, a negative errno will 3066 * be returned in error cases. 3067 */ 3068 int regmap_fields_read(struct regmap_field *field, unsigned int id, 3069 unsigned int *val) 3070 { 3071 int ret; 3072 unsigned int reg_val; 3073 3074 if (id >= field->id_size) 3075 return -EINVAL; 3076 3077 ret = regmap_read(field->regmap, 3078 field->reg + (field->id_offset * id), 3079 ®_val); 3080 if (ret != 0) 3081 return ret; 3082 3083 reg_val &= field->mask; 3084 reg_val >>= field->shift; 3085 *val = reg_val; 3086 3087 return ret; 3088 } 3089 EXPORT_SYMBOL_GPL(regmap_fields_read); 3090 3091 static int _regmap_bulk_read(struct regmap *map, unsigned int reg, 3092 const unsigned int *regs, void *val, size_t val_count) 3093 { 3094 u32 *u32 = val; 3095 u16 *u16 = val; 3096 u8 *u8 = val; 3097 int ret, i; 3098 3099 guard(regmap)(map); 3100 3101 for (i = 0; i < val_count; i++) { 3102 unsigned int ival; 3103 3104 if (regs) { 3105 if (!IS_ALIGNED(regs[i], map->reg_stride)) 3106 return -EINVAL; 3107 ret = _regmap_read(map, regs[i], &ival); 3108 } else { 3109 ret = _regmap_read(map, reg + regmap_get_offset(map, i), &ival); 3110 } 3111 if (ret != 0) 3112 return ret; 3113 3114 switch (map->format.val_bytes) { 3115 case 4: 3116 u32[i] = ival; 3117 break; 3118 case 2: 3119 u16[i] = ival; 3120 break; 3121 case 1: 3122 u8[i] = ival; 3123 break; 3124 default: 3125 return -EINVAL; 3126 } 3127 } 3128 3129 return 0; 3130 } 3131 3132 /** 3133 * regmap_bulk_read() - Read multiple sequential registers from the device 3134 * 3135 * @map: Register map to read from 3136 * @reg: First register to be read from 3137 * @val: Pointer to store read value, in native register size for device 3138 * @val_count: Number of registers to read 3139 * 3140 * A value of zero will be returned on success, a negative errno will 3141 * be returned in error cases. 3142 */ 3143 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, 3144 size_t val_count) 3145 { 3146 int ret, i; 3147 size_t val_bytes = map->format.val_bytes; 3148 bool vol = regmap_volatile_range(map, reg, val_count); 3149 3150 if (!IS_ALIGNED(reg, map->reg_stride)) 3151 return -EINVAL; 3152 if (val_count == 0) 3153 return -EINVAL; 3154 3155 if (map->read && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) { 3156 ret = regmap_raw_read(map, reg, val, val_bytes * val_count); 3157 if (ret != 0) 3158 return ret; 3159 3160 for (i = 0; i < val_count * val_bytes; i += val_bytes) 3161 map->format.parse_inplace(val + i); 3162 } else { 3163 ret = _regmap_bulk_read(map, reg, NULL, val, val_count); 3164 } 3165 if (!ret) 3166 trace_regmap_bulk_read(map, reg, val, val_bytes * val_count); 3167 return ret; 3168 } 3169 EXPORT_SYMBOL_GPL(regmap_bulk_read); 3170 3171 /** 3172 * regmap_multi_reg_read() - Read multiple non-sequential registers from the device 3173 * 3174 * @map: Register map to read from 3175 * @regs: Array of registers to read from 3176 * @val: Pointer to store read value, in native register size for device 3177 * @val_count: Number of registers to read 3178 * 3179 * A value of zero will be returned on success, a negative errno will 3180 * be returned in error cases. 3181 */ 3182 int regmap_multi_reg_read(struct regmap *map, const unsigned int *regs, void *val, 3183 size_t val_count) 3184 { 3185 if (val_count == 0) 3186 return -EINVAL; 3187 3188 return _regmap_bulk_read(map, 0, regs, val, val_count); 3189 } 3190 EXPORT_SYMBOL_GPL(regmap_multi_reg_read); 3191 3192 static int _regmap_update_bits(struct regmap *map, unsigned int reg, 3193 unsigned int mask, unsigned int val, 3194 bool *change, bool force_write) 3195 { 3196 int ret; 3197 unsigned int tmp, orig; 3198 3199 if (change) 3200 *change = false; 3201 3202 if (regmap_volatile(map, reg) && map->reg_update_bits) { 3203 if (map->cache_only) 3204 return -EBUSY; 3205 3206 reg = regmap_reg_addr(map, reg); 3207 ret = map->reg_update_bits(map->bus_context, reg, mask, val); 3208 if (ret == 0 && change) 3209 *change = true; 3210 } else { 3211 ret = _regmap_read(map, reg, &orig); 3212 if (ret != 0) 3213 return ret; 3214 3215 tmp = orig & ~mask; 3216 tmp |= val & mask; 3217 3218 if (force_write || (tmp != orig) || map->force_write_field) { 3219 ret = _regmap_write(map, reg, tmp); 3220 if (ret == 0 && change) 3221 *change = true; 3222 } 3223 } 3224 3225 return ret; 3226 } 3227 3228 /** 3229 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register 3230 * 3231 * @map: Register map to update 3232 * @reg: Register to update 3233 * @mask: Bitmask to change 3234 * @val: New value for bitmask 3235 * @change: Boolean indicating if a write was done 3236 * @async: Boolean indicating asynchronously 3237 * @force: Boolean indicating use force update 3238 * 3239 * Perform a read/modify/write cycle on a register map with change, async, force 3240 * options. 3241 * 3242 * If async is true: 3243 * 3244 * With most buses the read must be done synchronously so this is most useful 3245 * for devices with a cache which do not need to interact with the hardware to 3246 * determine the current register value. 3247 * 3248 * Returns zero for success, a negative number on error. 3249 */ 3250 int regmap_update_bits_base(struct regmap *map, unsigned int reg, 3251 unsigned int mask, unsigned int val, 3252 bool *change, bool async, bool force) 3253 { 3254 int ret; 3255 3256 guard(regmap)(map); 3257 3258 map->async = async; 3259 3260 ret = _regmap_update_bits(map, reg, mask, val, change, force); 3261 3262 map->async = false; 3263 3264 return ret; 3265 } 3266 EXPORT_SYMBOL_GPL(regmap_update_bits_base); 3267 3268 /** 3269 * regmap_test_bits() - Check if all specified bits are set in a register. 3270 * 3271 * @map: Register map to operate on 3272 * @reg: Register to read from 3273 * @bits: Bits to test 3274 * 3275 * Returns 0 if at least one of the tested bits is not set, 1 if all tested 3276 * bits are set and a negative error number if the underlying regmap_read() 3277 * fails. 3278 */ 3279 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits) 3280 { 3281 unsigned int val; 3282 int ret; 3283 3284 ret = regmap_read(map, reg, &val); 3285 if (ret) 3286 return ret; 3287 3288 return (val & bits) == bits; 3289 } 3290 EXPORT_SYMBOL_GPL(regmap_test_bits); 3291 3292 void regmap_async_complete_cb(struct regmap_async *async, int ret) 3293 { 3294 struct regmap *map = async->map; 3295 bool wake; 3296 3297 trace_regmap_async_io_complete(map); 3298 3299 spin_lock(&map->async_lock); 3300 list_move(&async->list, &map->async_free); 3301 wake = list_empty(&map->async_list); 3302 3303 if (ret != 0) 3304 map->async_ret = ret; 3305 3306 spin_unlock(&map->async_lock); 3307 3308 if (wake) 3309 wake_up(&map->async_waitq); 3310 } 3311 EXPORT_SYMBOL_GPL(regmap_async_complete_cb); 3312 3313 static int regmap_async_is_done(struct regmap *map) 3314 { 3315 unsigned long flags; 3316 int ret; 3317 3318 spin_lock_irqsave(&map->async_lock, flags); 3319 ret = list_empty(&map->async_list); 3320 spin_unlock_irqrestore(&map->async_lock, flags); 3321 3322 return ret; 3323 } 3324 3325 /** 3326 * regmap_async_complete - Ensure all asynchronous I/O has completed. 3327 * 3328 * @map: Map to operate on. 3329 * 3330 * Blocks until any pending asynchronous I/O has completed. Returns 3331 * an error code for any failed I/O operations. 3332 */ 3333 int regmap_async_complete(struct regmap *map) 3334 { 3335 unsigned long flags; 3336 int ret; 3337 3338 /* Nothing to do with no async support */ 3339 if (!map->bus || !map->bus->async_write) 3340 return 0; 3341 3342 trace_regmap_async_complete_start(map); 3343 3344 wait_event(map->async_waitq, regmap_async_is_done(map)); 3345 3346 spin_lock_irqsave(&map->async_lock, flags); 3347 ret = map->async_ret; 3348 map->async_ret = 0; 3349 spin_unlock_irqrestore(&map->async_lock, flags); 3350 3351 trace_regmap_async_complete_done(map); 3352 3353 return ret; 3354 } 3355 EXPORT_SYMBOL_GPL(regmap_async_complete); 3356 3357 /** 3358 * regmap_register_patch - Register and apply register updates to be applied 3359 * on device initialistion 3360 * 3361 * @map: Register map to apply updates to. 3362 * @regs: Values to update. 3363 * @num_regs: Number of entries in regs. 3364 * 3365 * Register a set of register updates to be applied to the device 3366 * whenever the device registers are synchronised with the cache and 3367 * apply them immediately. Typically this is used to apply 3368 * corrections to be applied to the device defaults on startup, such 3369 * as the updates some vendors provide to undocumented registers. 3370 * 3371 * The caller must ensure that this function cannot be called 3372 * concurrently with either itself or regcache_sync(). 3373 */ 3374 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs, 3375 int num_regs) 3376 { 3377 struct reg_sequence *p; 3378 int ret; 3379 bool bypass; 3380 3381 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n", 3382 num_regs)) 3383 return 0; 3384 3385 p = krealloc_array(map->patch, map->patch_regs + num_regs, sizeof(*p), GFP_KERNEL); 3386 if (!p) 3387 return -ENOMEM; 3388 3389 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs)); 3390 map->patch = p; 3391 map->patch_regs += num_regs; 3392 3393 scoped_guard(regmap, map) { 3394 bypass = map->cache_bypass; 3395 3396 map->cache_bypass = true; 3397 map->async = true; 3398 3399 ret = _regmap_multi_reg_write(map, regs, num_regs); 3400 3401 map->async = false; 3402 map->cache_bypass = bypass; 3403 } 3404 3405 regmap_async_complete(map); 3406 3407 return ret; 3408 } 3409 EXPORT_SYMBOL_GPL(regmap_register_patch); 3410 3411 /** 3412 * regmap_get_val_bytes() - Report the size of a register value 3413 * 3414 * @map: Register map to operate on. 3415 * 3416 * Report the size of a register value, mainly intended to for use by 3417 * generic infrastructure built on top of regmap. 3418 */ 3419 int regmap_get_val_bytes(struct regmap *map) 3420 { 3421 if (map->format.format_write) 3422 return -EINVAL; 3423 3424 return map->format.val_bytes; 3425 } 3426 EXPORT_SYMBOL_GPL(regmap_get_val_bytes); 3427 3428 /** 3429 * regmap_get_max_register() - Report the max register value 3430 * 3431 * @map: Register map to operate on. 3432 * 3433 * Report the max register value, mainly intended to for use by 3434 * generic infrastructure built on top of regmap. 3435 */ 3436 int regmap_get_max_register(struct regmap *map) 3437 { 3438 return map->max_register_is_set ? map->max_register : -EINVAL; 3439 } 3440 EXPORT_SYMBOL_GPL(regmap_get_max_register); 3441 3442 /** 3443 * regmap_get_reg_stride() - Report the register address stride 3444 * 3445 * @map: Register map to operate on. 3446 * 3447 * Report the register address stride, mainly intended to for use by 3448 * generic infrastructure built on top of regmap. 3449 */ 3450 int regmap_get_reg_stride(struct regmap *map) 3451 { 3452 return map->reg_stride; 3453 } 3454 EXPORT_SYMBOL_GPL(regmap_get_reg_stride); 3455 3456 /** 3457 * regmap_might_sleep() - Returns whether a regmap access might sleep. 3458 * 3459 * @map: Register map to operate on. 3460 * 3461 * Returns true if an access to the register might sleep, else false. 3462 */ 3463 bool regmap_might_sleep(struct regmap *map) 3464 { 3465 return map->can_sleep; 3466 } 3467 EXPORT_SYMBOL_GPL(regmap_might_sleep); 3468 3469 int regmap_parse_val(struct regmap *map, const void *buf, 3470 unsigned int *val) 3471 { 3472 if (!map->format.parse_val) 3473 return -EINVAL; 3474 3475 *val = map->format.parse_val(buf); 3476 3477 return 0; 3478 } 3479 EXPORT_SYMBOL_GPL(regmap_parse_val); 3480 3481 static int __init regmap_initcall(void) 3482 { 3483 regmap_debugfs_initcall(); 3484 3485 return 0; 3486 } 3487 postcore_initcall(regmap_initcall); 3488