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, unsigned int shift) 116 { 117 u8 *b = buf; 118 119 b[0] = val << shift; 120 } 121 122 static void regmap_format_16(void *buf, unsigned int val, unsigned int shift) 123 { 124 __be16 *b = buf; 125 126 b[0] = cpu_to_be16(val << shift); 127 } 128 129 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift) 130 { 131 u8 *b = buf; 132 133 val <<= shift; 134 135 b[0] = val >> 16; 136 b[1] = val >> 8; 137 b[2] = val; 138 } 139 140 static void regmap_format_32(void *buf, unsigned int val, unsigned int shift) 141 { 142 __be32 *b = buf; 143 144 b[0] = cpu_to_be32(val << shift); 145 } 146 147 static unsigned int regmap_parse_8(void *buf) 148 { 149 u8 *b = buf; 150 151 return b[0]; 152 } 153 154 static unsigned int regmap_parse_16(void *buf) 155 { 156 __be16 *b = buf; 157 158 b[0] = be16_to_cpu(b[0]); 159 160 return b[0]; 161 } 162 163 static unsigned int regmap_parse_24(void *buf) 164 { 165 u8 *b = buf; 166 unsigned int ret = b[2]; 167 ret |= ((unsigned int)b[1]) << 8; 168 ret |= ((unsigned int)b[0]) << 16; 169 170 return ret; 171 } 172 173 static unsigned int regmap_parse_32(void *buf) 174 { 175 __be32 *b = buf; 176 177 b[0] = be32_to_cpu(b[0]); 178 179 return b[0]; 180 } 181 182 static void regmap_lock_mutex(struct regmap *map) 183 { 184 mutex_lock(&map->mutex); 185 } 186 187 static void regmap_unlock_mutex(struct regmap *map) 188 { 189 mutex_unlock(&map->mutex); 190 } 191 192 static void regmap_lock_spinlock(struct regmap *map) 193 { 194 spin_lock(&map->spinlock); 195 } 196 197 static void regmap_unlock_spinlock(struct regmap *map) 198 { 199 spin_unlock(&map->spinlock); 200 } 201 202 static void dev_get_regmap_release(struct device *dev, void *res) 203 { 204 /* 205 * We don't actually have anything to do here; the goal here 206 * is not to manage the regmap but to provide a simple way to 207 * get the regmap back given a struct device. 208 */ 209 } 210 211 /** 212 * regmap_init(): Initialise register map 213 * 214 * @dev: Device that will be interacted with 215 * @bus: Bus-specific callbacks to use with device 216 * @bus_context: Data passed to bus-specific callbacks 217 * @config: Configuration for register map 218 * 219 * The return value will be an ERR_PTR() on error or a valid pointer to 220 * a struct regmap. This function should generally not be called 221 * directly, it should be called by bus-specific init functions. 222 */ 223 struct regmap *regmap_init(struct device *dev, 224 const struct regmap_bus *bus, 225 void *bus_context, 226 const struct regmap_config *config) 227 { 228 struct regmap *map, **m; 229 int ret = -EINVAL; 230 231 if (!bus || !config) 232 goto err; 233 234 map = kzalloc(sizeof(*map), GFP_KERNEL); 235 if (map == NULL) { 236 ret = -ENOMEM; 237 goto err; 238 } 239 240 if (bus->fast_io) { 241 spin_lock_init(&map->spinlock); 242 map->lock = regmap_lock_spinlock; 243 map->unlock = regmap_unlock_spinlock; 244 } else { 245 mutex_init(&map->mutex); 246 map->lock = regmap_lock_mutex; 247 map->unlock = regmap_unlock_mutex; 248 } 249 map->format.buf_size = (config->reg_bits + config->val_bits) / 8; 250 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); 251 map->format.pad_bytes = config->pad_bits / 8; 252 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); 253 map->format.buf_size += map->format.pad_bytes; 254 map->reg_shift = config->pad_bits % 8; 255 if (config->reg_stride) 256 map->reg_stride = config->reg_stride; 257 else 258 map->reg_stride = 1; 259 map->use_single_rw = config->use_single_rw; 260 map->dev = dev; 261 map->bus = bus; 262 map->bus_context = bus_context; 263 map->max_register = config->max_register; 264 map->writeable_reg = config->writeable_reg; 265 map->readable_reg = config->readable_reg; 266 map->volatile_reg = config->volatile_reg; 267 map->precious_reg = config->precious_reg; 268 map->cache_type = config->cache_type; 269 map->name = config->name; 270 271 if (config->read_flag_mask || config->write_flag_mask) { 272 map->read_flag_mask = config->read_flag_mask; 273 map->write_flag_mask = config->write_flag_mask; 274 } else { 275 map->read_flag_mask = bus->read_flag_mask; 276 } 277 278 switch (config->reg_bits + map->reg_shift) { 279 case 2: 280 switch (config->val_bits) { 281 case 6: 282 map->format.format_write = regmap_format_2_6_write; 283 break; 284 default: 285 goto err_map; 286 } 287 break; 288 289 case 4: 290 switch (config->val_bits) { 291 case 12: 292 map->format.format_write = regmap_format_4_12_write; 293 break; 294 default: 295 goto err_map; 296 } 297 break; 298 299 case 7: 300 switch (config->val_bits) { 301 case 9: 302 map->format.format_write = regmap_format_7_9_write; 303 break; 304 default: 305 goto err_map; 306 } 307 break; 308 309 case 10: 310 switch (config->val_bits) { 311 case 14: 312 map->format.format_write = regmap_format_10_14_write; 313 break; 314 default: 315 goto err_map; 316 } 317 break; 318 319 case 8: 320 map->format.format_reg = regmap_format_8; 321 break; 322 323 case 16: 324 map->format.format_reg = regmap_format_16; 325 break; 326 327 case 32: 328 map->format.format_reg = regmap_format_32; 329 break; 330 331 default: 332 goto err_map; 333 } 334 335 switch (config->val_bits) { 336 case 8: 337 map->format.format_val = regmap_format_8; 338 map->format.parse_val = regmap_parse_8; 339 break; 340 case 16: 341 map->format.format_val = regmap_format_16; 342 map->format.parse_val = regmap_parse_16; 343 break; 344 case 24: 345 map->format.format_val = regmap_format_24; 346 map->format.parse_val = regmap_parse_24; 347 break; 348 case 32: 349 map->format.format_val = regmap_format_32; 350 map->format.parse_val = regmap_parse_32; 351 break; 352 } 353 354 if (map->format.format_write) 355 map->use_single_rw = true; 356 357 if (!map->format.format_write && 358 !(map->format.format_reg && map->format.format_val)) 359 goto err_map; 360 361 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); 362 if (map->work_buf == NULL) { 363 ret = -ENOMEM; 364 goto err_map; 365 } 366 367 regmap_debugfs_init(map, config->name); 368 369 ret = regcache_init(map, config); 370 if (ret < 0) 371 goto err_free_workbuf; 372 373 /* Add a devres resource for dev_get_regmap() */ 374 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); 375 if (!m) { 376 ret = -ENOMEM; 377 goto err_cache; 378 } 379 *m = map; 380 devres_add(dev, m); 381 382 return map; 383 384 err_cache: 385 regcache_exit(map); 386 err_free_workbuf: 387 kfree(map->work_buf); 388 err_map: 389 kfree(map); 390 err: 391 return ERR_PTR(ret); 392 } 393 EXPORT_SYMBOL_GPL(regmap_init); 394 395 static void devm_regmap_release(struct device *dev, void *res) 396 { 397 regmap_exit(*(struct regmap **)res); 398 } 399 400 /** 401 * devm_regmap_init(): Initialise managed register map 402 * 403 * @dev: Device that will be interacted with 404 * @bus: Bus-specific callbacks to use with device 405 * @bus_context: Data passed to bus-specific callbacks 406 * @config: Configuration for register map 407 * 408 * The return value will be an ERR_PTR() on error or a valid pointer 409 * to a struct regmap. This function should generally not be called 410 * directly, it should be called by bus-specific init functions. The 411 * map will be automatically freed by the device management code. 412 */ 413 struct regmap *devm_regmap_init(struct device *dev, 414 const struct regmap_bus *bus, 415 void *bus_context, 416 const struct regmap_config *config) 417 { 418 struct regmap **ptr, *regmap; 419 420 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); 421 if (!ptr) 422 return ERR_PTR(-ENOMEM); 423 424 regmap = regmap_init(dev, bus, bus_context, config); 425 if (!IS_ERR(regmap)) { 426 *ptr = regmap; 427 devres_add(dev, ptr); 428 } else { 429 devres_free(ptr); 430 } 431 432 return regmap; 433 } 434 EXPORT_SYMBOL_GPL(devm_regmap_init); 435 436 /** 437 * regmap_reinit_cache(): Reinitialise the current register cache 438 * 439 * @map: Register map to operate on. 440 * @config: New configuration. Only the cache data will be used. 441 * 442 * Discard any existing register cache for the map and initialize a 443 * new cache. This can be used to restore the cache to defaults or to 444 * update the cache configuration to reflect runtime discovery of the 445 * hardware. 446 */ 447 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) 448 { 449 int ret; 450 451 map->lock(map); 452 453 regcache_exit(map); 454 regmap_debugfs_exit(map); 455 456 map->max_register = config->max_register; 457 map->writeable_reg = config->writeable_reg; 458 map->readable_reg = config->readable_reg; 459 map->volatile_reg = config->volatile_reg; 460 map->precious_reg = config->precious_reg; 461 map->cache_type = config->cache_type; 462 463 regmap_debugfs_init(map, config->name); 464 465 map->cache_bypass = false; 466 map->cache_only = false; 467 468 ret = regcache_init(map, config); 469 470 map->unlock(map); 471 472 return ret; 473 } 474 475 /** 476 * regmap_exit(): Free a previously allocated register map 477 */ 478 void regmap_exit(struct regmap *map) 479 { 480 regcache_exit(map); 481 regmap_debugfs_exit(map); 482 if (map->bus->free_context) 483 map->bus->free_context(map->bus_context); 484 kfree(map->work_buf); 485 kfree(map); 486 } 487 EXPORT_SYMBOL_GPL(regmap_exit); 488 489 static int dev_get_regmap_match(struct device *dev, void *res, void *data) 490 { 491 struct regmap **r = res; 492 if (!r || !*r) { 493 WARN_ON(!r || !*r); 494 return 0; 495 } 496 497 /* If the user didn't specify a name match any */ 498 if (data) 499 return (*r)->name == data; 500 else 501 return 1; 502 } 503 504 /** 505 * dev_get_regmap(): Obtain the regmap (if any) for a device 506 * 507 * @dev: Device to retrieve the map for 508 * @name: Optional name for the register map, usually NULL. 509 * 510 * Returns the regmap for the device if one is present, or NULL. If 511 * name is specified then it must match the name specified when 512 * registering the device, if it is NULL then the first regmap found 513 * will be used. Devices with multiple register maps are very rare, 514 * generic code should normally not need to specify a name. 515 */ 516 struct regmap *dev_get_regmap(struct device *dev, const char *name) 517 { 518 struct regmap **r = devres_find(dev, dev_get_regmap_release, 519 dev_get_regmap_match, (void *)name); 520 521 if (!r) 522 return NULL; 523 return *r; 524 } 525 EXPORT_SYMBOL_GPL(dev_get_regmap); 526 527 static int _regmap_raw_write(struct regmap *map, unsigned int reg, 528 const void *val, size_t val_len) 529 { 530 u8 *u8 = map->work_buf; 531 void *buf; 532 int ret = -ENOTSUPP; 533 size_t len; 534 int i; 535 536 /* Check for unwritable registers before we start */ 537 if (map->writeable_reg) 538 for (i = 0; i < val_len / map->format.val_bytes; i++) 539 if (!map->writeable_reg(map->dev, 540 reg + (i * map->reg_stride))) 541 return -EINVAL; 542 543 if (!map->cache_bypass && map->format.parse_val) { 544 unsigned int ival; 545 int val_bytes = map->format.val_bytes; 546 for (i = 0; i < val_len / val_bytes; i++) { 547 memcpy(map->work_buf, val + (i * val_bytes), val_bytes); 548 ival = map->format.parse_val(map->work_buf); 549 ret = regcache_write(map, reg + (i * map->reg_stride), 550 ival); 551 if (ret) { 552 dev_err(map->dev, 553 "Error in caching of register: %u ret: %d\n", 554 reg + i, ret); 555 return ret; 556 } 557 } 558 if (map->cache_only) { 559 map->cache_dirty = true; 560 return 0; 561 } 562 } 563 564 map->format.format_reg(map->work_buf, reg, map->reg_shift); 565 566 u8[0] |= map->write_flag_mask; 567 568 trace_regmap_hw_write_start(map->dev, reg, 569 val_len / map->format.val_bytes); 570 571 /* If we're doing a single register write we can probably just 572 * send the work_buf directly, otherwise try to do a gather 573 * write. 574 */ 575 if (val == (map->work_buf + map->format.pad_bytes + 576 map->format.reg_bytes)) 577 ret = map->bus->write(map->bus_context, map->work_buf, 578 map->format.reg_bytes + 579 map->format.pad_bytes + 580 val_len); 581 else if (map->bus->gather_write) 582 ret = map->bus->gather_write(map->bus_context, map->work_buf, 583 map->format.reg_bytes + 584 map->format.pad_bytes, 585 val, val_len); 586 587 /* If that didn't work fall back on linearising by hand. */ 588 if (ret == -ENOTSUPP) { 589 len = map->format.reg_bytes + map->format.pad_bytes + val_len; 590 buf = kzalloc(len, GFP_KERNEL); 591 if (!buf) 592 return -ENOMEM; 593 594 memcpy(buf, map->work_buf, map->format.reg_bytes); 595 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, 596 val, val_len); 597 ret = map->bus->write(map->bus_context, buf, len); 598 599 kfree(buf); 600 } 601 602 trace_regmap_hw_write_done(map->dev, reg, 603 val_len / map->format.val_bytes); 604 605 return ret; 606 } 607 608 int _regmap_write(struct regmap *map, unsigned int reg, 609 unsigned int val) 610 { 611 int ret; 612 BUG_ON(!map->format.format_write && !map->format.format_val); 613 614 if (!map->cache_bypass && map->format.format_write) { 615 ret = regcache_write(map, reg, val); 616 if (ret != 0) 617 return ret; 618 if (map->cache_only) { 619 map->cache_dirty = true; 620 return 0; 621 } 622 } 623 624 trace_regmap_reg_write(map->dev, reg, val); 625 626 if (map->format.format_write) { 627 map->format.format_write(map, reg, val); 628 629 trace_regmap_hw_write_start(map->dev, reg, 1); 630 631 ret = map->bus->write(map->bus_context, map->work_buf, 632 map->format.buf_size); 633 634 trace_regmap_hw_write_done(map->dev, reg, 1); 635 636 return ret; 637 } else { 638 map->format.format_val(map->work_buf + map->format.reg_bytes 639 + map->format.pad_bytes, val, 0); 640 return _regmap_raw_write(map, reg, 641 map->work_buf + 642 map->format.reg_bytes + 643 map->format.pad_bytes, 644 map->format.val_bytes); 645 } 646 } 647 648 /** 649 * regmap_write(): Write a value to a single register 650 * 651 * @map: Register map to write to 652 * @reg: Register to write to 653 * @val: Value to be written 654 * 655 * A value of zero will be returned on success, a negative errno will 656 * be returned in error cases. 657 */ 658 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) 659 { 660 int ret; 661 662 if (reg % map->reg_stride) 663 return -EINVAL; 664 665 map->lock(map); 666 667 ret = _regmap_write(map, reg, val); 668 669 map->unlock(map); 670 671 return ret; 672 } 673 EXPORT_SYMBOL_GPL(regmap_write); 674 675 /** 676 * regmap_raw_write(): Write raw values to one or more registers 677 * 678 * @map: Register map to write to 679 * @reg: Initial register to write to 680 * @val: Block of data to be written, laid out for direct transmission to the 681 * device 682 * @val_len: Length of data pointed to by val. 683 * 684 * This function is intended to be used for things like firmware 685 * download where a large block of data needs to be transferred to the 686 * device. No formatting will be done on the data provided. 687 * 688 * A value of zero will be returned on success, a negative errno will 689 * be returned in error cases. 690 */ 691 int regmap_raw_write(struct regmap *map, unsigned int reg, 692 const void *val, size_t val_len) 693 { 694 int ret; 695 696 if (val_len % map->format.val_bytes) 697 return -EINVAL; 698 if (reg % map->reg_stride) 699 return -EINVAL; 700 701 map->lock(map); 702 703 ret = _regmap_raw_write(map, reg, val, val_len); 704 705 map->unlock(map); 706 707 return ret; 708 } 709 EXPORT_SYMBOL_GPL(regmap_raw_write); 710 711 /* 712 * regmap_bulk_write(): Write multiple registers to the device 713 * 714 * @map: Register map to write to 715 * @reg: First register to be write from 716 * @val: Block of data to be written, in native register size for device 717 * @val_count: Number of registers to write 718 * 719 * This function is intended to be used for writing a large block of 720 * data to be device either in single transfer or multiple transfer. 721 * 722 * A value of zero will be returned on success, a negative errno will 723 * be returned in error cases. 724 */ 725 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, 726 size_t val_count) 727 { 728 int ret = 0, i; 729 size_t val_bytes = map->format.val_bytes; 730 void *wval; 731 732 if (!map->format.parse_val) 733 return -EINVAL; 734 if (reg % map->reg_stride) 735 return -EINVAL; 736 737 map->lock(map); 738 739 /* No formatting is require if val_byte is 1 */ 740 if (val_bytes == 1) { 741 wval = (void *)val; 742 } else { 743 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); 744 if (!wval) { 745 ret = -ENOMEM; 746 dev_err(map->dev, "Error in memory allocation\n"); 747 goto out; 748 } 749 for (i = 0; i < val_count * val_bytes; i += val_bytes) 750 map->format.parse_val(wval + i); 751 } 752 /* 753 * Some devices does not support bulk write, for 754 * them we have a series of single write operations. 755 */ 756 if (map->use_single_rw) { 757 for (i = 0; i < val_count; i++) { 758 ret = regmap_raw_write(map, 759 reg + (i * map->reg_stride), 760 val + (i * val_bytes), 761 val_bytes); 762 if (ret != 0) 763 return ret; 764 } 765 } else { 766 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); 767 } 768 769 if (val_bytes != 1) 770 kfree(wval); 771 772 out: 773 map->unlock(map); 774 return ret; 775 } 776 EXPORT_SYMBOL_GPL(regmap_bulk_write); 777 778 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 779 unsigned int val_len) 780 { 781 u8 *u8 = map->work_buf; 782 int ret; 783 784 map->format.format_reg(map->work_buf, reg, map->reg_shift); 785 786 /* 787 * Some buses or devices flag reads by setting the high bits in the 788 * register addresss; since it's always the high bits for all 789 * current formats we can do this here rather than in 790 * formatting. This may break if we get interesting formats. 791 */ 792 u8[0] |= map->read_flag_mask; 793 794 trace_regmap_hw_read_start(map->dev, reg, 795 val_len / map->format.val_bytes); 796 797 ret = map->bus->read(map->bus_context, map->work_buf, 798 map->format.reg_bytes + map->format.pad_bytes, 799 val, val_len); 800 801 trace_regmap_hw_read_done(map->dev, reg, 802 val_len / map->format.val_bytes); 803 804 return ret; 805 } 806 807 static int _regmap_read(struct regmap *map, unsigned int reg, 808 unsigned int *val) 809 { 810 int ret; 811 812 if (!map->cache_bypass) { 813 ret = regcache_read(map, reg, val); 814 if (ret == 0) 815 return 0; 816 } 817 818 if (!map->format.parse_val) 819 return -EINVAL; 820 821 if (map->cache_only) 822 return -EBUSY; 823 824 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); 825 if (ret == 0) { 826 *val = map->format.parse_val(map->work_buf); 827 trace_regmap_reg_read(map->dev, reg, *val); 828 } 829 830 if (ret == 0 && !map->cache_bypass) 831 regcache_write(map, reg, *val); 832 833 return ret; 834 } 835 836 /** 837 * regmap_read(): Read a value from a single register 838 * 839 * @map: Register map to write to 840 * @reg: Register to be read from 841 * @val: Pointer to store read value 842 * 843 * A value of zero will be returned on success, a negative errno will 844 * be returned in error cases. 845 */ 846 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) 847 { 848 int ret; 849 850 if (reg % map->reg_stride) 851 return -EINVAL; 852 853 map->lock(map); 854 855 ret = _regmap_read(map, reg, val); 856 857 map->unlock(map); 858 859 return ret; 860 } 861 EXPORT_SYMBOL_GPL(regmap_read); 862 863 /** 864 * regmap_raw_read(): Read raw data from the device 865 * 866 * @map: Register map to write to 867 * @reg: First register to be read from 868 * @val: Pointer to store read value 869 * @val_len: Size of data to read 870 * 871 * A value of zero will be returned on success, a negative errno will 872 * be returned in error cases. 873 */ 874 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 875 size_t val_len) 876 { 877 size_t val_bytes = map->format.val_bytes; 878 size_t val_count = val_len / val_bytes; 879 unsigned int v; 880 int ret, i; 881 882 if (val_len % map->format.val_bytes) 883 return -EINVAL; 884 if (reg % map->reg_stride) 885 return -EINVAL; 886 887 map->lock(map); 888 889 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || 890 map->cache_type == REGCACHE_NONE) { 891 /* Physical block read if there's no cache involved */ 892 ret = _regmap_raw_read(map, reg, val, val_len); 893 894 } else { 895 /* Otherwise go word by word for the cache; should be low 896 * cost as we expect to hit the cache. 897 */ 898 for (i = 0; i < val_count; i++) { 899 ret = _regmap_read(map, reg + (i * map->reg_stride), 900 &v); 901 if (ret != 0) 902 goto out; 903 904 map->format.format_val(val + (i * val_bytes), v, 0); 905 } 906 } 907 908 out: 909 map->unlock(map); 910 911 return ret; 912 } 913 EXPORT_SYMBOL_GPL(regmap_raw_read); 914 915 /** 916 * regmap_bulk_read(): Read multiple registers from the device 917 * 918 * @map: Register map to write to 919 * @reg: First register to be read from 920 * @val: Pointer to store read value, in native register size for device 921 * @val_count: Number of registers to read 922 * 923 * A value of zero will be returned on success, a negative errno will 924 * be returned in error cases. 925 */ 926 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, 927 size_t val_count) 928 { 929 int ret, i; 930 size_t val_bytes = map->format.val_bytes; 931 bool vol = regmap_volatile_range(map, reg, val_count); 932 933 if (!map->format.parse_val) 934 return -EINVAL; 935 if (reg % map->reg_stride) 936 return -EINVAL; 937 938 if (vol || map->cache_type == REGCACHE_NONE) { 939 /* 940 * Some devices does not support bulk read, for 941 * them we have a series of single read operations. 942 */ 943 if (map->use_single_rw) { 944 for (i = 0; i < val_count; i++) { 945 ret = regmap_raw_read(map, 946 reg + (i * map->reg_stride), 947 val + (i * val_bytes), 948 val_bytes); 949 if (ret != 0) 950 return ret; 951 } 952 } else { 953 ret = regmap_raw_read(map, reg, val, 954 val_bytes * val_count); 955 if (ret != 0) 956 return ret; 957 } 958 959 for (i = 0; i < val_count * val_bytes; i += val_bytes) 960 map->format.parse_val(val + i); 961 } else { 962 for (i = 0; i < val_count; i++) { 963 unsigned int ival; 964 ret = regmap_read(map, reg + (i * map->reg_stride), 965 &ival); 966 if (ret != 0) 967 return ret; 968 memcpy(val + (i * val_bytes), &ival, val_bytes); 969 } 970 } 971 972 return 0; 973 } 974 EXPORT_SYMBOL_GPL(regmap_bulk_read); 975 976 static int _regmap_update_bits(struct regmap *map, unsigned int reg, 977 unsigned int mask, unsigned int val, 978 bool *change) 979 { 980 int ret; 981 unsigned int tmp, orig; 982 983 map->lock(map); 984 985 ret = _regmap_read(map, reg, &orig); 986 if (ret != 0) 987 goto out; 988 989 tmp = orig & ~mask; 990 tmp |= val & mask; 991 992 if (tmp != orig) { 993 ret = _regmap_write(map, reg, tmp); 994 *change = true; 995 } else { 996 *change = false; 997 } 998 999 out: 1000 map->unlock(map); 1001 1002 return ret; 1003 } 1004 1005 /** 1006 * regmap_update_bits: Perform a read/modify/write cycle on the register map 1007 * 1008 * @map: Register map to update 1009 * @reg: Register to update 1010 * @mask: Bitmask to change 1011 * @val: New value for bitmask 1012 * 1013 * Returns zero for success, a negative number on error. 1014 */ 1015 int regmap_update_bits(struct regmap *map, unsigned int reg, 1016 unsigned int mask, unsigned int val) 1017 { 1018 bool change; 1019 return _regmap_update_bits(map, reg, mask, val, &change); 1020 } 1021 EXPORT_SYMBOL_GPL(regmap_update_bits); 1022 1023 /** 1024 * regmap_update_bits_check: Perform a read/modify/write cycle on the 1025 * register map and report if updated 1026 * 1027 * @map: Register map to update 1028 * @reg: Register to update 1029 * @mask: Bitmask to change 1030 * @val: New value for bitmask 1031 * @change: Boolean indicating if a write was done 1032 * 1033 * Returns zero for success, a negative number on error. 1034 */ 1035 int regmap_update_bits_check(struct regmap *map, unsigned int reg, 1036 unsigned int mask, unsigned int val, 1037 bool *change) 1038 { 1039 return _regmap_update_bits(map, reg, mask, val, change); 1040 } 1041 EXPORT_SYMBOL_GPL(regmap_update_bits_check); 1042 1043 /** 1044 * regmap_register_patch: Register and apply register updates to be applied 1045 * on device initialistion 1046 * 1047 * @map: Register map to apply updates to. 1048 * @regs: Values to update. 1049 * @num_regs: Number of entries in regs. 1050 * 1051 * Register a set of register updates to be applied to the device 1052 * whenever the device registers are synchronised with the cache and 1053 * apply them immediately. Typically this is used to apply 1054 * corrections to be applied to the device defaults on startup, such 1055 * as the updates some vendors provide to undocumented registers. 1056 */ 1057 int regmap_register_patch(struct regmap *map, const struct reg_default *regs, 1058 int num_regs) 1059 { 1060 int i, ret; 1061 bool bypass; 1062 1063 /* If needed the implementation can be extended to support this */ 1064 if (map->patch) 1065 return -EBUSY; 1066 1067 map->lock(map); 1068 1069 bypass = map->cache_bypass; 1070 1071 map->cache_bypass = true; 1072 1073 /* Write out first; it's useful to apply even if we fail later. */ 1074 for (i = 0; i < num_regs; i++) { 1075 ret = _regmap_write(map, regs[i].reg, regs[i].def); 1076 if (ret != 0) { 1077 dev_err(map->dev, "Failed to write %x = %x: %d\n", 1078 regs[i].reg, regs[i].def, ret); 1079 goto out; 1080 } 1081 } 1082 1083 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); 1084 if (map->patch != NULL) { 1085 memcpy(map->patch, regs, 1086 num_regs * sizeof(struct reg_default)); 1087 map->patch_regs = num_regs; 1088 } else { 1089 ret = -ENOMEM; 1090 } 1091 1092 out: 1093 map->cache_bypass = bypass; 1094 1095 map->unlock(map); 1096 1097 return ret; 1098 } 1099 EXPORT_SYMBOL_GPL(regmap_register_patch); 1100 1101 /* 1102 * regmap_get_val_bytes(): Report the size of a register value 1103 * 1104 * Report the size of a register value, mainly intended to for use by 1105 * generic infrastructure built on top of regmap. 1106 */ 1107 int regmap_get_val_bytes(struct regmap *map) 1108 { 1109 if (map->format.format_write) 1110 return -EINVAL; 1111 1112 return map->format.val_bytes; 1113 } 1114 EXPORT_SYMBOL_GPL(regmap_get_val_bytes); 1115 1116 static int __init regmap_initcall(void) 1117 { 1118 regmap_debugfs_initcall(); 1119 1120 return 0; 1121 } 1122 postcore_initcall(regmap_initcall); 1123