1 /* 2 * Register map access API 3 * 4 * Copyright 2011 Wolfson Microelectronics plc 5 * 6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/device.h> 14 #include <linux/slab.h> 15 #include <linux/export.h> 16 #include <linux/mutex.h> 17 #include <linux/err.h> 18 19 #define CREATE_TRACE_POINTS 20 #include <trace/events/regmap.h> 21 22 #include "internal.h" 23 24 bool regmap_writeable(struct regmap *map, unsigned int reg) 25 { 26 if (map->max_register && reg > map->max_register) 27 return false; 28 29 if (map->writeable_reg) 30 return map->writeable_reg(map->dev, reg); 31 32 return true; 33 } 34 35 bool regmap_readable(struct regmap *map, unsigned int reg) 36 { 37 if (map->max_register && reg > map->max_register) 38 return false; 39 40 if (map->format.format_write) 41 return false; 42 43 if (map->readable_reg) 44 return map->readable_reg(map->dev, reg); 45 46 return true; 47 } 48 49 bool regmap_volatile(struct regmap *map, unsigned int reg) 50 { 51 if (!regmap_readable(map, reg)) 52 return false; 53 54 if (map->volatile_reg) 55 return map->volatile_reg(map->dev, reg); 56 57 return true; 58 } 59 60 bool regmap_precious(struct regmap *map, unsigned int reg) 61 { 62 if (!regmap_readable(map, reg)) 63 return false; 64 65 if (map->precious_reg) 66 return map->precious_reg(map->dev, reg); 67 68 return false; 69 } 70 71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg, 72 unsigned int num) 73 { 74 unsigned int i; 75 76 for (i = 0; i < num; i++) 77 if (!regmap_volatile(map, reg + i)) 78 return false; 79 80 return true; 81 } 82 83 static void regmap_format_2_6_write(struct regmap *map, 84 unsigned int reg, unsigned int val) 85 { 86 u8 *out = map->work_buf; 87 88 *out = (reg << 6) | val; 89 } 90 91 static void regmap_format_4_12_write(struct regmap *map, 92 unsigned int reg, unsigned int val) 93 { 94 __be16 *out = map->work_buf; 95 *out = cpu_to_be16((reg << 12) | val); 96 } 97 98 static void regmap_format_7_9_write(struct regmap *map, 99 unsigned int reg, unsigned int val) 100 { 101 __be16 *out = map->work_buf; 102 *out = cpu_to_be16((reg << 9) | val); 103 } 104 105 static void regmap_format_10_14_write(struct regmap *map, 106 unsigned int reg, unsigned int val) 107 { 108 u8 *out = map->work_buf; 109 110 out[2] = val; 111 out[1] = (val >> 8) | (reg << 6); 112 out[0] = reg >> 2; 113 } 114 115 static void regmap_format_8(void *buf, unsigned int val) 116 { 117 u8 *b = buf; 118 119 b[0] = val; 120 } 121 122 static void regmap_format_16(void *buf, unsigned int val) 123 { 124 __be16 *b = buf; 125 126 b[0] = cpu_to_be16(val); 127 } 128 129 static void regmap_format_32(void *buf, unsigned int val) 130 { 131 __be32 *b = buf; 132 133 b[0] = cpu_to_be32(val); 134 } 135 136 static unsigned int regmap_parse_8(void *buf) 137 { 138 u8 *b = buf; 139 140 return b[0]; 141 } 142 143 static unsigned int regmap_parse_16(void *buf) 144 { 145 __be16 *b = buf; 146 147 b[0] = be16_to_cpu(b[0]); 148 149 return b[0]; 150 } 151 152 static unsigned int regmap_parse_32(void *buf) 153 { 154 __be32 *b = buf; 155 156 b[0] = be32_to_cpu(b[0]); 157 158 return b[0]; 159 } 160 161 /** 162 * regmap_init(): Initialise register map 163 * 164 * @dev: Device that will be interacted with 165 * @bus: Bus-specific callbacks to use with device 166 * @bus_context: Data passed to bus-specific callbacks 167 * @config: Configuration for register map 168 * 169 * The return value will be an ERR_PTR() on error or a valid pointer to 170 * a struct regmap. This function should generally not be called 171 * directly, it should be called by bus-specific init functions. 172 */ 173 struct regmap *regmap_init(struct device *dev, 174 const struct regmap_bus *bus, 175 void *bus_context, 176 const struct regmap_config *config) 177 { 178 struct regmap *map; 179 int ret = -EINVAL; 180 181 if (!bus || !config) 182 goto err; 183 184 map = kzalloc(sizeof(*map), GFP_KERNEL); 185 if (map == NULL) { 186 ret = -ENOMEM; 187 goto err; 188 } 189 190 mutex_init(&map->lock); 191 map->format.buf_size = (config->reg_bits + config->val_bits) / 8; 192 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); 193 map->format.pad_bytes = config->pad_bits / 8; 194 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); 195 map->format.buf_size += map->format.pad_bytes; 196 map->dev = dev; 197 map->bus = bus; 198 map->bus_context = bus_context; 199 map->max_register = config->max_register; 200 map->writeable_reg = config->writeable_reg; 201 map->readable_reg = config->readable_reg; 202 map->volatile_reg = config->volatile_reg; 203 map->precious_reg = config->precious_reg; 204 map->cache_type = config->cache_type; 205 206 if (config->read_flag_mask || config->write_flag_mask) { 207 map->read_flag_mask = config->read_flag_mask; 208 map->write_flag_mask = config->write_flag_mask; 209 } else { 210 map->read_flag_mask = bus->read_flag_mask; 211 } 212 213 switch (config->reg_bits) { 214 case 2: 215 switch (config->val_bits) { 216 case 6: 217 map->format.format_write = regmap_format_2_6_write; 218 break; 219 default: 220 goto err_map; 221 } 222 break; 223 224 case 4: 225 switch (config->val_bits) { 226 case 12: 227 map->format.format_write = regmap_format_4_12_write; 228 break; 229 default: 230 goto err_map; 231 } 232 break; 233 234 case 7: 235 switch (config->val_bits) { 236 case 9: 237 map->format.format_write = regmap_format_7_9_write; 238 break; 239 default: 240 goto err_map; 241 } 242 break; 243 244 case 10: 245 switch (config->val_bits) { 246 case 14: 247 map->format.format_write = regmap_format_10_14_write; 248 break; 249 default: 250 goto err_map; 251 } 252 break; 253 254 case 8: 255 map->format.format_reg = regmap_format_8; 256 break; 257 258 case 16: 259 map->format.format_reg = regmap_format_16; 260 break; 261 262 case 32: 263 map->format.format_reg = regmap_format_32; 264 break; 265 266 default: 267 goto err_map; 268 } 269 270 switch (config->val_bits) { 271 case 8: 272 map->format.format_val = regmap_format_8; 273 map->format.parse_val = regmap_parse_8; 274 break; 275 case 16: 276 map->format.format_val = regmap_format_16; 277 map->format.parse_val = regmap_parse_16; 278 break; 279 case 32: 280 map->format.format_val = regmap_format_32; 281 map->format.parse_val = regmap_parse_32; 282 break; 283 } 284 285 if (!map->format.format_write && 286 !(map->format.format_reg && map->format.format_val)) 287 goto err_map; 288 289 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); 290 if (map->work_buf == NULL) { 291 ret = -ENOMEM; 292 goto err_map; 293 } 294 295 regmap_debugfs_init(map); 296 297 ret = regcache_init(map, config); 298 if (ret < 0) 299 goto err_free_workbuf; 300 301 return map; 302 303 err_free_workbuf: 304 kfree(map->work_buf); 305 err_map: 306 kfree(map); 307 err: 308 return ERR_PTR(ret); 309 } 310 EXPORT_SYMBOL_GPL(regmap_init); 311 312 static void devm_regmap_release(struct device *dev, void *res) 313 { 314 regmap_exit(*(struct regmap **)res); 315 } 316 317 /** 318 * devm_regmap_init(): Initialise managed register map 319 * 320 * @dev: Device that will be interacted with 321 * @bus: Bus-specific callbacks to use with device 322 * @bus_context: Data passed to bus-specific callbacks 323 * @config: Configuration for register map 324 * 325 * The return value will be an ERR_PTR() on error or a valid pointer 326 * to a struct regmap. This function should generally not be called 327 * directly, it should be called by bus-specific init functions. The 328 * map will be automatically freed by the device management code. 329 */ 330 struct regmap *devm_regmap_init(struct device *dev, 331 const struct regmap_bus *bus, 332 void *bus_context, 333 const struct regmap_config *config) 334 { 335 struct regmap **ptr, *regmap; 336 337 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); 338 if (!ptr) 339 return ERR_PTR(-ENOMEM); 340 341 regmap = regmap_init(dev, bus, bus_context, config); 342 if (!IS_ERR(regmap)) { 343 *ptr = regmap; 344 devres_add(dev, ptr); 345 } else { 346 devres_free(ptr); 347 } 348 349 return regmap; 350 } 351 EXPORT_SYMBOL_GPL(devm_regmap_init); 352 353 /** 354 * regmap_reinit_cache(): Reinitialise the current register cache 355 * 356 * @map: Register map to operate on. 357 * @config: New configuration. Only the cache data will be used. 358 * 359 * Discard any existing register cache for the map and initialize a 360 * new cache. This can be used to restore the cache to defaults or to 361 * update the cache configuration to reflect runtime discovery of the 362 * hardware. 363 */ 364 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) 365 { 366 int ret; 367 368 mutex_lock(&map->lock); 369 370 regcache_exit(map); 371 regmap_debugfs_exit(map); 372 373 map->max_register = config->max_register; 374 map->writeable_reg = config->writeable_reg; 375 map->readable_reg = config->readable_reg; 376 map->volatile_reg = config->volatile_reg; 377 map->precious_reg = config->precious_reg; 378 map->cache_type = config->cache_type; 379 380 regmap_debugfs_init(map); 381 382 map->cache_bypass = false; 383 map->cache_only = false; 384 385 ret = regcache_init(map, config); 386 387 mutex_unlock(&map->lock); 388 389 return ret; 390 } 391 392 /** 393 * regmap_exit(): Free a previously allocated register map 394 */ 395 void regmap_exit(struct regmap *map) 396 { 397 regcache_exit(map); 398 regmap_debugfs_exit(map); 399 if (map->bus->free_context) 400 map->bus->free_context(map->bus_context); 401 kfree(map->work_buf); 402 kfree(map); 403 } 404 EXPORT_SYMBOL_GPL(regmap_exit); 405 406 static int _regmap_raw_write(struct regmap *map, unsigned int reg, 407 const void *val, size_t val_len) 408 { 409 u8 *u8 = map->work_buf; 410 void *buf; 411 int ret = -ENOTSUPP; 412 size_t len; 413 int i; 414 415 /* Check for unwritable registers before we start */ 416 if (map->writeable_reg) 417 for (i = 0; i < val_len / map->format.val_bytes; i++) 418 if (!map->writeable_reg(map->dev, reg + i)) 419 return -EINVAL; 420 421 if (!map->cache_bypass && map->format.parse_val) { 422 unsigned int ival; 423 int val_bytes = map->format.val_bytes; 424 for (i = 0; i < val_len / val_bytes; i++) { 425 memcpy(map->work_buf, val + (i * val_bytes), val_bytes); 426 ival = map->format.parse_val(map->work_buf); 427 ret = regcache_write(map, reg + i, ival); 428 if (ret) { 429 dev_err(map->dev, 430 "Error in caching of register: %u ret: %d\n", 431 reg + i, ret); 432 return ret; 433 } 434 } 435 if (map->cache_only) { 436 map->cache_dirty = true; 437 return 0; 438 } 439 } 440 441 map->format.format_reg(map->work_buf, reg); 442 443 u8[0] |= map->write_flag_mask; 444 445 trace_regmap_hw_write_start(map->dev, reg, 446 val_len / map->format.val_bytes); 447 448 /* If we're doing a single register write we can probably just 449 * send the work_buf directly, otherwise try to do a gather 450 * write. 451 */ 452 if (val == (map->work_buf + map->format.pad_bytes + 453 map->format.reg_bytes)) 454 ret = map->bus->write(map->bus_context, map->work_buf, 455 map->format.reg_bytes + 456 map->format.pad_bytes + 457 val_len); 458 else if (map->bus->gather_write) 459 ret = map->bus->gather_write(map->bus_context, map->work_buf, 460 map->format.reg_bytes + 461 map->format.pad_bytes, 462 val, val_len); 463 464 /* If that didn't work fall back on linearising by hand. */ 465 if (ret == -ENOTSUPP) { 466 len = map->format.reg_bytes + map->format.pad_bytes + val_len; 467 buf = kzalloc(len, GFP_KERNEL); 468 if (!buf) 469 return -ENOMEM; 470 471 memcpy(buf, map->work_buf, map->format.reg_bytes); 472 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, 473 val, val_len); 474 ret = map->bus->write(map->bus_context, buf, len); 475 476 kfree(buf); 477 } 478 479 trace_regmap_hw_write_done(map->dev, reg, 480 val_len / map->format.val_bytes); 481 482 return ret; 483 } 484 485 int _regmap_write(struct regmap *map, unsigned int reg, 486 unsigned int val) 487 { 488 int ret; 489 BUG_ON(!map->format.format_write && !map->format.format_val); 490 491 if (!map->cache_bypass && map->format.format_write) { 492 ret = regcache_write(map, reg, val); 493 if (ret != 0) 494 return ret; 495 if (map->cache_only) { 496 map->cache_dirty = true; 497 return 0; 498 } 499 } 500 501 trace_regmap_reg_write(map->dev, reg, val); 502 503 if (map->format.format_write) { 504 map->format.format_write(map, reg, val); 505 506 trace_regmap_hw_write_start(map->dev, reg, 1); 507 508 ret = map->bus->write(map->bus_context, map->work_buf, 509 map->format.buf_size); 510 511 trace_regmap_hw_write_done(map->dev, reg, 1); 512 513 return ret; 514 } else { 515 map->format.format_val(map->work_buf + map->format.reg_bytes 516 + map->format.pad_bytes, val); 517 return _regmap_raw_write(map, reg, 518 map->work_buf + 519 map->format.reg_bytes + 520 map->format.pad_bytes, 521 map->format.val_bytes); 522 } 523 } 524 525 /** 526 * regmap_write(): Write a value to a single register 527 * 528 * @map: Register map to write to 529 * @reg: Register to write to 530 * @val: Value to be written 531 * 532 * A value of zero will be returned on success, a negative errno will 533 * be returned in error cases. 534 */ 535 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) 536 { 537 int ret; 538 539 mutex_lock(&map->lock); 540 541 ret = _regmap_write(map, reg, val); 542 543 mutex_unlock(&map->lock); 544 545 return ret; 546 } 547 EXPORT_SYMBOL_GPL(regmap_write); 548 549 /** 550 * regmap_raw_write(): Write raw values to one or more registers 551 * 552 * @map: Register map to write to 553 * @reg: Initial register to write to 554 * @val: Block of data to be written, laid out for direct transmission to the 555 * device 556 * @val_len: Length of data pointed to by val. 557 * 558 * This function is intended to be used for things like firmware 559 * download where a large block of data needs to be transferred to the 560 * device. No formatting will be done on the data provided. 561 * 562 * A value of zero will be returned on success, a negative errno will 563 * be returned in error cases. 564 */ 565 int regmap_raw_write(struct regmap *map, unsigned int reg, 566 const void *val, size_t val_len) 567 { 568 int ret; 569 570 mutex_lock(&map->lock); 571 572 ret = _regmap_raw_write(map, reg, val, val_len); 573 574 mutex_unlock(&map->lock); 575 576 return ret; 577 } 578 EXPORT_SYMBOL_GPL(regmap_raw_write); 579 580 /* 581 * regmap_bulk_write(): Write multiple registers to the device 582 * 583 * @map: Register map to write to 584 * @reg: First register to be write from 585 * @val: Block of data to be written, in native register size for device 586 * @val_count: Number of registers to write 587 * 588 * This function is intended to be used for writing a large block of 589 * data to be device either in single transfer or multiple transfer. 590 * 591 * A value of zero will be returned on success, a negative errno will 592 * be returned in error cases. 593 */ 594 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, 595 size_t val_count) 596 { 597 int ret = 0, i; 598 size_t val_bytes = map->format.val_bytes; 599 void *wval; 600 601 if (!map->format.parse_val) 602 return -EINVAL; 603 604 mutex_lock(&map->lock); 605 606 /* No formatting is require if val_byte is 1 */ 607 if (val_bytes == 1) { 608 wval = (void *)val; 609 } else { 610 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); 611 if (!wval) { 612 ret = -ENOMEM; 613 dev_err(map->dev, "Error in memory allocation\n"); 614 goto out; 615 } 616 for (i = 0; i < val_count * val_bytes; i += val_bytes) 617 map->format.parse_val(wval + i); 618 } 619 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); 620 621 if (val_bytes != 1) 622 kfree(wval); 623 624 out: 625 mutex_unlock(&map->lock); 626 return ret; 627 } 628 EXPORT_SYMBOL_GPL(regmap_bulk_write); 629 630 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 631 unsigned int val_len) 632 { 633 u8 *u8 = map->work_buf; 634 int ret; 635 636 map->format.format_reg(map->work_buf, reg); 637 638 /* 639 * Some buses or devices flag reads by setting the high bits in the 640 * register addresss; since it's always the high bits for all 641 * current formats we can do this here rather than in 642 * formatting. This may break if we get interesting formats. 643 */ 644 u8[0] |= map->read_flag_mask; 645 646 trace_regmap_hw_read_start(map->dev, reg, 647 val_len / map->format.val_bytes); 648 649 ret = map->bus->read(map->bus_context, map->work_buf, 650 map->format.reg_bytes + map->format.pad_bytes, 651 val, val_len); 652 653 trace_regmap_hw_read_done(map->dev, reg, 654 val_len / map->format.val_bytes); 655 656 return ret; 657 } 658 659 static int _regmap_read(struct regmap *map, unsigned int reg, 660 unsigned int *val) 661 { 662 int ret; 663 664 if (!map->cache_bypass) { 665 ret = regcache_read(map, reg, val); 666 if (ret == 0) 667 return 0; 668 } 669 670 if (!map->format.parse_val) 671 return -EINVAL; 672 673 if (map->cache_only) 674 return -EBUSY; 675 676 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); 677 if (ret == 0) { 678 *val = map->format.parse_val(map->work_buf); 679 trace_regmap_reg_read(map->dev, reg, *val); 680 } 681 682 return ret; 683 } 684 685 /** 686 * regmap_read(): Read a value from a single register 687 * 688 * @map: Register map to write to 689 * @reg: Register to be read from 690 * @val: Pointer to store read value 691 * 692 * A value of zero will be returned on success, a negative errno will 693 * be returned in error cases. 694 */ 695 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) 696 { 697 int ret; 698 699 mutex_lock(&map->lock); 700 701 ret = _regmap_read(map, reg, val); 702 703 mutex_unlock(&map->lock); 704 705 return ret; 706 } 707 EXPORT_SYMBOL_GPL(regmap_read); 708 709 /** 710 * regmap_raw_read(): Read raw data from the device 711 * 712 * @map: Register map to write to 713 * @reg: First register to be read from 714 * @val: Pointer to store read value 715 * @val_len: Size of data to read 716 * 717 * A value of zero will be returned on success, a negative errno will 718 * be returned in error cases. 719 */ 720 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 721 size_t val_len) 722 { 723 size_t val_bytes = map->format.val_bytes; 724 size_t val_count = val_len / val_bytes; 725 unsigned int v; 726 int ret, i; 727 728 mutex_lock(&map->lock); 729 730 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || 731 map->cache_type == REGCACHE_NONE) { 732 /* Physical block read if there's no cache involved */ 733 ret = _regmap_raw_read(map, reg, val, val_len); 734 735 } else { 736 /* Otherwise go word by word for the cache; should be low 737 * cost as we expect to hit the cache. 738 */ 739 for (i = 0; i < val_count; i++) { 740 ret = _regmap_read(map, reg + i, &v); 741 if (ret != 0) 742 goto out; 743 744 map->format.format_val(val + (i * val_bytes), v); 745 } 746 } 747 748 out: 749 mutex_unlock(&map->lock); 750 751 return ret; 752 } 753 EXPORT_SYMBOL_GPL(regmap_raw_read); 754 755 /** 756 * regmap_bulk_read(): Read multiple registers from the device 757 * 758 * @map: Register map to write to 759 * @reg: First register to be read from 760 * @val: Pointer to store read value, in native register size for device 761 * @val_count: Number of registers to read 762 * 763 * A value of zero will be returned on success, a negative errno will 764 * be returned in error cases. 765 */ 766 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, 767 size_t val_count) 768 { 769 int ret, i; 770 size_t val_bytes = map->format.val_bytes; 771 bool vol = regmap_volatile_range(map, reg, val_count); 772 773 if (!map->format.parse_val) 774 return -EINVAL; 775 776 if (vol || map->cache_type == REGCACHE_NONE) { 777 ret = regmap_raw_read(map, reg, val, val_bytes * val_count); 778 if (ret != 0) 779 return ret; 780 781 for (i = 0; i < val_count * val_bytes; i += val_bytes) 782 map->format.parse_val(val + i); 783 } else { 784 for (i = 0; i < val_count; i++) { 785 ret = regmap_read(map, reg + i, val + (i * val_bytes)); 786 if (ret != 0) 787 return ret; 788 } 789 } 790 791 return 0; 792 } 793 EXPORT_SYMBOL_GPL(regmap_bulk_read); 794 795 static int _regmap_update_bits(struct regmap *map, unsigned int reg, 796 unsigned int mask, unsigned int val, 797 bool *change) 798 { 799 int ret; 800 unsigned int tmp, orig; 801 802 mutex_lock(&map->lock); 803 804 ret = _regmap_read(map, reg, &orig); 805 if (ret != 0) 806 goto out; 807 808 tmp = orig & ~mask; 809 tmp |= val & mask; 810 811 if (tmp != orig) { 812 ret = _regmap_write(map, reg, tmp); 813 *change = true; 814 } else { 815 *change = false; 816 } 817 818 out: 819 mutex_unlock(&map->lock); 820 821 return ret; 822 } 823 824 /** 825 * regmap_update_bits: Perform a read/modify/write cycle on the register map 826 * 827 * @map: Register map to update 828 * @reg: Register to update 829 * @mask: Bitmask to change 830 * @val: New value for bitmask 831 * 832 * Returns zero for success, a negative number on error. 833 */ 834 int regmap_update_bits(struct regmap *map, unsigned int reg, 835 unsigned int mask, unsigned int val) 836 { 837 bool change; 838 return _regmap_update_bits(map, reg, mask, val, &change); 839 } 840 EXPORT_SYMBOL_GPL(regmap_update_bits); 841 842 /** 843 * regmap_update_bits_check: Perform a read/modify/write cycle on the 844 * register map and report if updated 845 * 846 * @map: Register map to update 847 * @reg: Register to update 848 * @mask: Bitmask to change 849 * @val: New value for bitmask 850 * @change: Boolean indicating if a write was done 851 * 852 * Returns zero for success, a negative number on error. 853 */ 854 int regmap_update_bits_check(struct regmap *map, unsigned int reg, 855 unsigned int mask, unsigned int val, 856 bool *change) 857 { 858 return _regmap_update_bits(map, reg, mask, val, change); 859 } 860 EXPORT_SYMBOL_GPL(regmap_update_bits_check); 861 862 /** 863 * regmap_register_patch: Register and apply register updates to be applied 864 * on device initialistion 865 * 866 * @map: Register map to apply updates to. 867 * @regs: Values to update. 868 * @num_regs: Number of entries in regs. 869 * 870 * Register a set of register updates to be applied to the device 871 * whenever the device registers are synchronised with the cache and 872 * apply them immediately. Typically this is used to apply 873 * corrections to be applied to the device defaults on startup, such 874 * as the updates some vendors provide to undocumented registers. 875 */ 876 int regmap_register_patch(struct regmap *map, const struct reg_default *regs, 877 int num_regs) 878 { 879 int i, ret; 880 bool bypass; 881 882 /* If needed the implementation can be extended to support this */ 883 if (map->patch) 884 return -EBUSY; 885 886 mutex_lock(&map->lock); 887 888 bypass = map->cache_bypass; 889 890 map->cache_bypass = true; 891 892 /* Write out first; it's useful to apply even if we fail later. */ 893 for (i = 0; i < num_regs; i++) { 894 ret = _regmap_write(map, regs[i].reg, regs[i].def); 895 if (ret != 0) { 896 dev_err(map->dev, "Failed to write %x = %x: %d\n", 897 regs[i].reg, regs[i].def, ret); 898 goto out; 899 } 900 } 901 902 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); 903 if (map->patch != NULL) { 904 memcpy(map->patch, regs, 905 num_regs * sizeof(struct reg_default)); 906 map->patch_regs = num_regs; 907 } else { 908 ret = -ENOMEM; 909 } 910 911 out: 912 map->cache_bypass = bypass; 913 914 mutex_unlock(&map->lock); 915 916 return ret; 917 } 918 EXPORT_SYMBOL_GPL(regmap_register_patch); 919 920 /* 921 * regmap_get_val_bytes(): Report the size of a register value 922 * 923 * Report the size of a register value, mainly intended to for use by 924 * generic infrastructure built on top of regmap. 925 */ 926 int regmap_get_val_bytes(struct regmap *map) 927 { 928 if (map->format.format_write) 929 return -EINVAL; 930 931 return map->format.val_bytes; 932 } 933 EXPORT_SYMBOL_GPL(regmap_get_val_bytes); 934 935 static int __init regmap_initcall(void) 936 { 937 regmap_debugfs_initcall(); 938 939 return 0; 940 } 941 postcore_initcall(regmap_initcall); 942