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