arm: Add devicetree fixup machine function
[linux/fpc-iii.git] / drivers / base / regmap / regmap.c
blob74d8c0672cf6162e9d3d28afbd68f742a5ecb732
1 /*
2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18 #include <linux/rbtree.h>
19 #include <linux/sched.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/regmap.h>
24 #include "internal.h"
27 * Sometimes for failures during very early init the trace
28 * infrastructure isn't available early enough to be used. For this
29 * sort of problem defining LOG_DEVICE will add printks for basic
30 * register I/O on a specific device.
32 #undef LOG_DEVICE
34 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
35 unsigned int mask, unsigned int val,
36 bool *change);
38 static int _regmap_bus_reg_read(void *context, unsigned int reg,
39 unsigned int *val);
40 static int _regmap_bus_read(void *context, unsigned int reg,
41 unsigned int *val);
42 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
43 unsigned int val);
44 static int _regmap_bus_reg_write(void *context, unsigned int reg,
45 unsigned int val);
46 static int _regmap_bus_raw_write(void *context, unsigned int reg,
47 unsigned int val);
49 bool regmap_reg_in_ranges(unsigned int reg,
50 const struct regmap_range *ranges,
51 unsigned int nranges)
53 const struct regmap_range *r;
54 int i;
56 for (i = 0, r = ranges; i < nranges; i++, r++)
57 if (regmap_reg_in_range(reg, r))
58 return true;
59 return false;
61 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
63 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
64 const struct regmap_access_table *table)
66 /* Check "no ranges" first */
67 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
68 return false;
70 /* In case zero "yes ranges" are supplied, any reg is OK */
71 if (!table->n_yes_ranges)
72 return true;
74 return regmap_reg_in_ranges(reg, table->yes_ranges,
75 table->n_yes_ranges);
77 EXPORT_SYMBOL_GPL(regmap_check_range_table);
79 bool regmap_writeable(struct regmap *map, unsigned int reg)
81 if (map->max_register && reg > map->max_register)
82 return false;
84 if (map->writeable_reg)
85 return map->writeable_reg(map->dev, reg);
87 if (map->wr_table)
88 return regmap_check_range_table(map, reg, map->wr_table);
90 return true;
93 bool regmap_readable(struct regmap *map, unsigned int reg)
95 if (map->max_register && reg > map->max_register)
96 return false;
98 if (map->format.format_write)
99 return false;
101 if (map->readable_reg)
102 return map->readable_reg(map->dev, reg);
104 if (map->rd_table)
105 return regmap_check_range_table(map, reg, map->rd_table);
107 return true;
110 bool regmap_volatile(struct regmap *map, unsigned int reg)
112 if (!regmap_readable(map, reg))
113 return false;
115 if (map->volatile_reg)
116 return map->volatile_reg(map->dev, reg);
118 if (map->volatile_table)
119 return regmap_check_range_table(map, reg, map->volatile_table);
121 if (map->cache_ops)
122 return false;
123 else
124 return true;
127 bool regmap_precious(struct regmap *map, unsigned int reg)
129 if (!regmap_readable(map, reg))
130 return false;
132 if (map->precious_reg)
133 return map->precious_reg(map->dev, reg);
135 if (map->precious_table)
136 return regmap_check_range_table(map, reg, map->precious_table);
138 return false;
141 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
142 size_t num)
144 unsigned int i;
146 for (i = 0; i < num; i++)
147 if (!regmap_volatile(map, reg + i))
148 return false;
150 return true;
153 static void regmap_format_2_6_write(struct regmap *map,
154 unsigned int reg, unsigned int val)
156 u8 *out = map->work_buf;
158 *out = (reg << 6) | val;
161 static void regmap_format_4_12_write(struct regmap *map,
162 unsigned int reg, unsigned int val)
164 __be16 *out = map->work_buf;
165 *out = cpu_to_be16((reg << 12) | val);
168 static void regmap_format_7_9_write(struct regmap *map,
169 unsigned int reg, unsigned int val)
171 __be16 *out = map->work_buf;
172 *out = cpu_to_be16((reg << 9) | val);
175 static void regmap_format_10_14_write(struct regmap *map,
176 unsigned int reg, unsigned int val)
178 u8 *out = map->work_buf;
180 out[2] = val;
181 out[1] = (val >> 8) | (reg << 6);
182 out[0] = reg >> 2;
185 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
187 u8 *b = buf;
189 b[0] = val << shift;
192 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
194 __be16 *b = buf;
196 b[0] = cpu_to_be16(val << shift);
199 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
201 __le16 *b = buf;
203 b[0] = cpu_to_le16(val << shift);
206 static void regmap_format_16_native(void *buf, unsigned int val,
207 unsigned int shift)
209 *(u16 *)buf = val << shift;
212 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
214 u8 *b = buf;
216 val <<= shift;
218 b[0] = val >> 16;
219 b[1] = val >> 8;
220 b[2] = val;
223 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
225 __be32 *b = buf;
227 b[0] = cpu_to_be32(val << shift);
230 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
232 __le32 *b = buf;
234 b[0] = cpu_to_le32(val << shift);
237 static void regmap_format_32_native(void *buf, unsigned int val,
238 unsigned int shift)
240 *(u32 *)buf = val << shift;
243 static void regmap_parse_inplace_noop(void *buf)
247 static unsigned int regmap_parse_8(const void *buf)
249 const u8 *b = buf;
251 return b[0];
254 static unsigned int regmap_parse_16_be(const void *buf)
256 const __be16 *b = buf;
258 return be16_to_cpu(b[0]);
261 static unsigned int regmap_parse_16_le(const void *buf)
263 const __le16 *b = buf;
265 return le16_to_cpu(b[0]);
268 static void regmap_parse_16_be_inplace(void *buf)
270 __be16 *b = buf;
272 b[0] = be16_to_cpu(b[0]);
275 static void regmap_parse_16_le_inplace(void *buf)
277 __le16 *b = buf;
279 b[0] = le16_to_cpu(b[0]);
282 static unsigned int regmap_parse_16_native(const void *buf)
284 return *(u16 *)buf;
287 static unsigned int regmap_parse_24(const void *buf)
289 const u8 *b = buf;
290 unsigned int ret = b[2];
291 ret |= ((unsigned int)b[1]) << 8;
292 ret |= ((unsigned int)b[0]) << 16;
294 return ret;
297 static unsigned int regmap_parse_32_be(const void *buf)
299 const __be32 *b = buf;
301 return be32_to_cpu(b[0]);
304 static unsigned int regmap_parse_32_le(const void *buf)
306 const __le32 *b = buf;
308 return le32_to_cpu(b[0]);
311 static void regmap_parse_32_be_inplace(void *buf)
313 __be32 *b = buf;
315 b[0] = be32_to_cpu(b[0]);
318 static void regmap_parse_32_le_inplace(void *buf)
320 __le32 *b = buf;
322 b[0] = le32_to_cpu(b[0]);
325 static unsigned int regmap_parse_32_native(const void *buf)
327 return *(u32 *)buf;
330 static void regmap_lock_mutex(void *__map)
332 struct regmap *map = __map;
333 mutex_lock(&map->mutex);
336 static void regmap_unlock_mutex(void *__map)
338 struct regmap *map = __map;
339 mutex_unlock(&map->mutex);
342 static void regmap_lock_spinlock(void *__map)
343 __acquires(&map->spinlock)
345 struct regmap *map = __map;
346 unsigned long flags;
348 spin_lock_irqsave(&map->spinlock, flags);
349 map->spinlock_flags = flags;
352 static void regmap_unlock_spinlock(void *__map)
353 __releases(&map->spinlock)
355 struct regmap *map = __map;
356 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
359 static void dev_get_regmap_release(struct device *dev, void *res)
362 * We don't actually have anything to do here; the goal here
363 * is not to manage the regmap but to provide a simple way to
364 * get the regmap back given a struct device.
368 static bool _regmap_range_add(struct regmap *map,
369 struct regmap_range_node *data)
371 struct rb_root *root = &map->range_tree;
372 struct rb_node **new = &(root->rb_node), *parent = NULL;
374 while (*new) {
375 struct regmap_range_node *this =
376 container_of(*new, struct regmap_range_node, node);
378 parent = *new;
379 if (data->range_max < this->range_min)
380 new = &((*new)->rb_left);
381 else if (data->range_min > this->range_max)
382 new = &((*new)->rb_right);
383 else
384 return false;
387 rb_link_node(&data->node, parent, new);
388 rb_insert_color(&data->node, root);
390 return true;
393 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
394 unsigned int reg)
396 struct rb_node *node = map->range_tree.rb_node;
398 while (node) {
399 struct regmap_range_node *this =
400 container_of(node, struct regmap_range_node, node);
402 if (reg < this->range_min)
403 node = node->rb_left;
404 else if (reg > this->range_max)
405 node = node->rb_right;
406 else
407 return this;
410 return NULL;
413 static void regmap_range_exit(struct regmap *map)
415 struct rb_node *next;
416 struct regmap_range_node *range_node;
418 next = rb_first(&map->range_tree);
419 while (next) {
420 range_node = rb_entry(next, struct regmap_range_node, node);
421 next = rb_next(&range_node->node);
422 rb_erase(&range_node->node, &map->range_tree);
423 kfree(range_node);
426 kfree(map->selector_work_buf);
429 int regmap_attach_dev(struct device *dev, struct regmap *map,
430 const struct regmap_config *config)
432 struct regmap **m;
434 map->dev = dev;
436 regmap_debugfs_init(map, config->name);
438 /* Add a devres resource for dev_get_regmap() */
439 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
440 if (!m) {
441 regmap_debugfs_exit(map);
442 return -ENOMEM;
444 *m = map;
445 devres_add(dev, m);
447 return 0;
449 EXPORT_SYMBOL_GPL(regmap_attach_dev);
452 * regmap_init(): Initialise register map
454 * @dev: Device that will be interacted with
455 * @bus: Bus-specific callbacks to use with device
456 * @bus_context: Data passed to bus-specific callbacks
457 * @config: Configuration for register map
459 * The return value will be an ERR_PTR() on error or a valid pointer to
460 * a struct regmap. This function should generally not be called
461 * directly, it should be called by bus-specific init functions.
463 struct regmap *regmap_init(struct device *dev,
464 const struct regmap_bus *bus,
465 void *bus_context,
466 const struct regmap_config *config)
468 struct regmap *map;
469 int ret = -EINVAL;
470 enum regmap_endian reg_endian, val_endian;
471 int i, j;
473 if (!config)
474 goto err;
476 map = kzalloc(sizeof(*map), GFP_KERNEL);
477 if (map == NULL) {
478 ret = -ENOMEM;
479 goto err;
482 if (config->lock && config->unlock) {
483 map->lock = config->lock;
484 map->unlock = config->unlock;
485 map->lock_arg = config->lock_arg;
486 } else {
487 if ((bus && bus->fast_io) ||
488 config->fast_io) {
489 spin_lock_init(&map->spinlock);
490 map->lock = regmap_lock_spinlock;
491 map->unlock = regmap_unlock_spinlock;
492 } else {
493 mutex_init(&map->mutex);
494 map->lock = regmap_lock_mutex;
495 map->unlock = regmap_unlock_mutex;
497 map->lock_arg = map;
499 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
500 map->format.pad_bytes = config->pad_bits / 8;
501 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
502 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
503 config->val_bits + config->pad_bits, 8);
504 map->reg_shift = config->pad_bits % 8;
505 if (config->reg_stride)
506 map->reg_stride = config->reg_stride;
507 else
508 map->reg_stride = 1;
509 map->use_single_rw = config->use_single_rw;
510 map->can_multi_write = config->can_multi_write;
511 map->dev = dev;
512 map->bus = bus;
513 map->bus_context = bus_context;
514 map->max_register = config->max_register;
515 map->wr_table = config->wr_table;
516 map->rd_table = config->rd_table;
517 map->volatile_table = config->volatile_table;
518 map->precious_table = config->precious_table;
519 map->writeable_reg = config->writeable_reg;
520 map->readable_reg = config->readable_reg;
521 map->volatile_reg = config->volatile_reg;
522 map->precious_reg = config->precious_reg;
523 map->cache_type = config->cache_type;
524 map->name = config->name;
526 spin_lock_init(&map->async_lock);
527 INIT_LIST_HEAD(&map->async_list);
528 INIT_LIST_HEAD(&map->async_free);
529 init_waitqueue_head(&map->async_waitq);
531 if (config->read_flag_mask || config->write_flag_mask) {
532 map->read_flag_mask = config->read_flag_mask;
533 map->write_flag_mask = config->write_flag_mask;
534 } else if (bus) {
535 map->read_flag_mask = bus->read_flag_mask;
538 if (!bus) {
539 map->reg_read = config->reg_read;
540 map->reg_write = config->reg_write;
542 map->defer_caching = false;
543 goto skip_format_initialization;
544 } else if (!bus->read || !bus->write) {
545 map->reg_read = _regmap_bus_reg_read;
546 map->reg_write = _regmap_bus_reg_write;
548 map->defer_caching = false;
549 goto skip_format_initialization;
550 } else {
551 map->reg_read = _regmap_bus_read;
554 reg_endian = config->reg_format_endian;
555 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
556 reg_endian = bus->reg_format_endian_default;
557 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
558 reg_endian = REGMAP_ENDIAN_BIG;
560 val_endian = config->val_format_endian;
561 if (val_endian == REGMAP_ENDIAN_DEFAULT)
562 val_endian = bus->val_format_endian_default;
563 if (val_endian == REGMAP_ENDIAN_DEFAULT)
564 val_endian = REGMAP_ENDIAN_BIG;
566 switch (config->reg_bits + map->reg_shift) {
567 case 2:
568 switch (config->val_bits) {
569 case 6:
570 map->format.format_write = regmap_format_2_6_write;
571 break;
572 default:
573 goto err_map;
575 break;
577 case 4:
578 switch (config->val_bits) {
579 case 12:
580 map->format.format_write = regmap_format_4_12_write;
581 break;
582 default:
583 goto err_map;
585 break;
587 case 7:
588 switch (config->val_bits) {
589 case 9:
590 map->format.format_write = regmap_format_7_9_write;
591 break;
592 default:
593 goto err_map;
595 break;
597 case 10:
598 switch (config->val_bits) {
599 case 14:
600 map->format.format_write = regmap_format_10_14_write;
601 break;
602 default:
603 goto err_map;
605 break;
607 case 8:
608 map->format.format_reg = regmap_format_8;
609 break;
611 case 16:
612 switch (reg_endian) {
613 case REGMAP_ENDIAN_BIG:
614 map->format.format_reg = regmap_format_16_be;
615 break;
616 case REGMAP_ENDIAN_NATIVE:
617 map->format.format_reg = regmap_format_16_native;
618 break;
619 default:
620 goto err_map;
622 break;
624 case 24:
625 if (reg_endian != REGMAP_ENDIAN_BIG)
626 goto err_map;
627 map->format.format_reg = regmap_format_24;
628 break;
630 case 32:
631 switch (reg_endian) {
632 case REGMAP_ENDIAN_BIG:
633 map->format.format_reg = regmap_format_32_be;
634 break;
635 case REGMAP_ENDIAN_NATIVE:
636 map->format.format_reg = regmap_format_32_native;
637 break;
638 default:
639 goto err_map;
641 break;
643 default:
644 goto err_map;
647 if (val_endian == REGMAP_ENDIAN_NATIVE)
648 map->format.parse_inplace = regmap_parse_inplace_noop;
650 switch (config->val_bits) {
651 case 8:
652 map->format.format_val = regmap_format_8;
653 map->format.parse_val = regmap_parse_8;
654 map->format.parse_inplace = regmap_parse_inplace_noop;
655 break;
656 case 16:
657 switch (val_endian) {
658 case REGMAP_ENDIAN_BIG:
659 map->format.format_val = regmap_format_16_be;
660 map->format.parse_val = regmap_parse_16_be;
661 map->format.parse_inplace = regmap_parse_16_be_inplace;
662 break;
663 case REGMAP_ENDIAN_LITTLE:
664 map->format.format_val = regmap_format_16_le;
665 map->format.parse_val = regmap_parse_16_le;
666 map->format.parse_inplace = regmap_parse_16_le_inplace;
667 break;
668 case REGMAP_ENDIAN_NATIVE:
669 map->format.format_val = regmap_format_16_native;
670 map->format.parse_val = regmap_parse_16_native;
671 break;
672 default:
673 goto err_map;
675 break;
676 case 24:
677 if (val_endian != REGMAP_ENDIAN_BIG)
678 goto err_map;
679 map->format.format_val = regmap_format_24;
680 map->format.parse_val = regmap_parse_24;
681 break;
682 case 32:
683 switch (val_endian) {
684 case REGMAP_ENDIAN_BIG:
685 map->format.format_val = regmap_format_32_be;
686 map->format.parse_val = regmap_parse_32_be;
687 map->format.parse_inplace = regmap_parse_32_be_inplace;
688 break;
689 case REGMAP_ENDIAN_LITTLE:
690 map->format.format_val = regmap_format_32_le;
691 map->format.parse_val = regmap_parse_32_le;
692 map->format.parse_inplace = regmap_parse_32_le_inplace;
693 break;
694 case REGMAP_ENDIAN_NATIVE:
695 map->format.format_val = regmap_format_32_native;
696 map->format.parse_val = regmap_parse_32_native;
697 break;
698 default:
699 goto err_map;
701 break;
704 if (map->format.format_write) {
705 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
706 (val_endian != REGMAP_ENDIAN_BIG))
707 goto err_map;
708 map->use_single_rw = true;
711 if (!map->format.format_write &&
712 !(map->format.format_reg && map->format.format_val))
713 goto err_map;
715 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
716 if (map->work_buf == NULL) {
717 ret = -ENOMEM;
718 goto err_map;
721 if (map->format.format_write) {
722 map->defer_caching = false;
723 map->reg_write = _regmap_bus_formatted_write;
724 } else if (map->format.format_val) {
725 map->defer_caching = true;
726 map->reg_write = _regmap_bus_raw_write;
729 skip_format_initialization:
731 map->range_tree = RB_ROOT;
732 for (i = 0; i < config->num_ranges; i++) {
733 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
734 struct regmap_range_node *new;
736 /* Sanity check */
737 if (range_cfg->range_max < range_cfg->range_min) {
738 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
739 range_cfg->range_max, range_cfg->range_min);
740 goto err_range;
743 if (range_cfg->range_max > map->max_register) {
744 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
745 range_cfg->range_max, map->max_register);
746 goto err_range;
749 if (range_cfg->selector_reg > map->max_register) {
750 dev_err(map->dev,
751 "Invalid range %d: selector out of map\n", i);
752 goto err_range;
755 if (range_cfg->window_len == 0) {
756 dev_err(map->dev, "Invalid range %d: window_len 0\n",
758 goto err_range;
761 /* Make sure, that this register range has no selector
762 or data window within its boundary */
763 for (j = 0; j < config->num_ranges; j++) {
764 unsigned sel_reg = config->ranges[j].selector_reg;
765 unsigned win_min = config->ranges[j].window_start;
766 unsigned win_max = win_min +
767 config->ranges[j].window_len - 1;
769 /* Allow data window inside its own virtual range */
770 if (j == i)
771 continue;
773 if (range_cfg->range_min <= sel_reg &&
774 sel_reg <= range_cfg->range_max) {
775 dev_err(map->dev,
776 "Range %d: selector for %d in window\n",
777 i, j);
778 goto err_range;
781 if (!(win_max < range_cfg->range_min ||
782 win_min > range_cfg->range_max)) {
783 dev_err(map->dev,
784 "Range %d: window for %d in window\n",
785 i, j);
786 goto err_range;
790 new = kzalloc(sizeof(*new), GFP_KERNEL);
791 if (new == NULL) {
792 ret = -ENOMEM;
793 goto err_range;
796 new->map = map;
797 new->name = range_cfg->name;
798 new->range_min = range_cfg->range_min;
799 new->range_max = range_cfg->range_max;
800 new->selector_reg = range_cfg->selector_reg;
801 new->selector_mask = range_cfg->selector_mask;
802 new->selector_shift = range_cfg->selector_shift;
803 new->window_start = range_cfg->window_start;
804 new->window_len = range_cfg->window_len;
806 if (!_regmap_range_add(map, new)) {
807 dev_err(map->dev, "Failed to add range %d\n", i);
808 kfree(new);
809 goto err_range;
812 if (map->selector_work_buf == NULL) {
813 map->selector_work_buf =
814 kzalloc(map->format.buf_size, GFP_KERNEL);
815 if (map->selector_work_buf == NULL) {
816 ret = -ENOMEM;
817 goto err_range;
822 ret = regcache_init(map, config);
823 if (ret != 0)
824 goto err_range;
826 if (dev) {
827 ret = regmap_attach_dev(dev, map, config);
828 if (ret != 0)
829 goto err_regcache;
832 return map;
834 err_regcache:
835 regcache_exit(map);
836 err_range:
837 regmap_range_exit(map);
838 kfree(map->work_buf);
839 err_map:
840 kfree(map);
841 err:
842 return ERR_PTR(ret);
844 EXPORT_SYMBOL_GPL(regmap_init);
846 static void devm_regmap_release(struct device *dev, void *res)
848 regmap_exit(*(struct regmap **)res);
852 * devm_regmap_init(): Initialise managed register map
854 * @dev: Device that will be interacted with
855 * @bus: Bus-specific callbacks to use with device
856 * @bus_context: Data passed to bus-specific callbacks
857 * @config: Configuration for register map
859 * The return value will be an ERR_PTR() on error or a valid pointer
860 * to a struct regmap. This function should generally not be called
861 * directly, it should be called by bus-specific init functions. The
862 * map will be automatically freed by the device management code.
864 struct regmap *devm_regmap_init(struct device *dev,
865 const struct regmap_bus *bus,
866 void *bus_context,
867 const struct regmap_config *config)
869 struct regmap **ptr, *regmap;
871 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
872 if (!ptr)
873 return ERR_PTR(-ENOMEM);
875 regmap = regmap_init(dev, bus, bus_context, config);
876 if (!IS_ERR(regmap)) {
877 *ptr = regmap;
878 devres_add(dev, ptr);
879 } else {
880 devres_free(ptr);
883 return regmap;
885 EXPORT_SYMBOL_GPL(devm_regmap_init);
887 static void regmap_field_init(struct regmap_field *rm_field,
888 struct regmap *regmap, struct reg_field reg_field)
890 int field_bits = reg_field.msb - reg_field.lsb + 1;
891 rm_field->regmap = regmap;
892 rm_field->reg = reg_field.reg;
893 rm_field->shift = reg_field.lsb;
894 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
895 rm_field->id_size = reg_field.id_size;
896 rm_field->id_offset = reg_field.id_offset;
900 * devm_regmap_field_alloc(): Allocate and initialise a register field
901 * in a register map.
903 * @dev: Device that will be interacted with
904 * @regmap: regmap bank in which this register field is located.
905 * @reg_field: Register field with in the bank.
907 * The return value will be an ERR_PTR() on error or a valid pointer
908 * to a struct regmap_field. The regmap_field will be automatically freed
909 * by the device management code.
911 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
912 struct regmap *regmap, struct reg_field reg_field)
914 struct regmap_field *rm_field = devm_kzalloc(dev,
915 sizeof(*rm_field), GFP_KERNEL);
916 if (!rm_field)
917 return ERR_PTR(-ENOMEM);
919 regmap_field_init(rm_field, regmap, reg_field);
921 return rm_field;
924 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
927 * devm_regmap_field_free(): Free register field allocated using
928 * devm_regmap_field_alloc. Usally drivers need not call this function,
929 * as the memory allocated via devm will be freed as per device-driver
930 * life-cyle.
932 * @dev: Device that will be interacted with
933 * @field: regmap field which should be freed.
935 void devm_regmap_field_free(struct device *dev,
936 struct regmap_field *field)
938 devm_kfree(dev, field);
940 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
943 * regmap_field_alloc(): Allocate and initialise a register field
944 * in a register map.
946 * @regmap: regmap bank in which this register field is located.
947 * @reg_field: Register field with in the bank.
949 * The return value will be an ERR_PTR() on error or a valid pointer
950 * to a struct regmap_field. The regmap_field should be freed by the
951 * user once its finished working with it using regmap_field_free().
953 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
954 struct reg_field reg_field)
956 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
958 if (!rm_field)
959 return ERR_PTR(-ENOMEM);
961 regmap_field_init(rm_field, regmap, reg_field);
963 return rm_field;
965 EXPORT_SYMBOL_GPL(regmap_field_alloc);
968 * regmap_field_free(): Free register field allocated using regmap_field_alloc
970 * @field: regmap field which should be freed.
972 void regmap_field_free(struct regmap_field *field)
974 kfree(field);
976 EXPORT_SYMBOL_GPL(regmap_field_free);
979 * regmap_reinit_cache(): Reinitialise the current register cache
981 * @map: Register map to operate on.
982 * @config: New configuration. Only the cache data will be used.
984 * Discard any existing register cache for the map and initialize a
985 * new cache. This can be used to restore the cache to defaults or to
986 * update the cache configuration to reflect runtime discovery of the
987 * hardware.
989 * No explicit locking is done here, the user needs to ensure that
990 * this function will not race with other calls to regmap.
992 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
994 regcache_exit(map);
995 regmap_debugfs_exit(map);
997 map->max_register = config->max_register;
998 map->writeable_reg = config->writeable_reg;
999 map->readable_reg = config->readable_reg;
1000 map->volatile_reg = config->volatile_reg;
1001 map->precious_reg = config->precious_reg;
1002 map->cache_type = config->cache_type;
1004 regmap_debugfs_init(map, config->name);
1006 map->cache_bypass = false;
1007 map->cache_only = false;
1009 return regcache_init(map, config);
1011 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1014 * regmap_exit(): Free a previously allocated register map
1016 void regmap_exit(struct regmap *map)
1018 struct regmap_async *async;
1020 regcache_exit(map);
1021 regmap_debugfs_exit(map);
1022 regmap_range_exit(map);
1023 if (map->bus && map->bus->free_context)
1024 map->bus->free_context(map->bus_context);
1025 kfree(map->work_buf);
1026 while (!list_empty(&map->async_free)) {
1027 async = list_first_entry_or_null(&map->async_free,
1028 struct regmap_async,
1029 list);
1030 list_del(&async->list);
1031 kfree(async->work_buf);
1032 kfree(async);
1034 kfree(map);
1036 EXPORT_SYMBOL_GPL(regmap_exit);
1038 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1040 struct regmap **r = res;
1041 if (!r || !*r) {
1042 WARN_ON(!r || !*r);
1043 return 0;
1046 /* If the user didn't specify a name match any */
1047 if (data)
1048 return (*r)->name == data;
1049 else
1050 return 1;
1054 * dev_get_regmap(): Obtain the regmap (if any) for a device
1056 * @dev: Device to retrieve the map for
1057 * @name: Optional name for the register map, usually NULL.
1059 * Returns the regmap for the device if one is present, or NULL. If
1060 * name is specified then it must match the name specified when
1061 * registering the device, if it is NULL then the first regmap found
1062 * will be used. Devices with multiple register maps are very rare,
1063 * generic code should normally not need to specify a name.
1065 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1067 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1068 dev_get_regmap_match, (void *)name);
1070 if (!r)
1071 return NULL;
1072 return *r;
1074 EXPORT_SYMBOL_GPL(dev_get_regmap);
1076 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1077 struct regmap_range_node *range,
1078 unsigned int val_num)
1080 void *orig_work_buf;
1081 unsigned int win_offset;
1082 unsigned int win_page;
1083 bool page_chg;
1084 int ret;
1086 win_offset = (*reg - range->range_min) % range->window_len;
1087 win_page = (*reg - range->range_min) / range->window_len;
1089 if (val_num > 1) {
1090 /* Bulk write shouldn't cross range boundary */
1091 if (*reg + val_num - 1 > range->range_max)
1092 return -EINVAL;
1094 /* ... or single page boundary */
1095 if (val_num > range->window_len - win_offset)
1096 return -EINVAL;
1099 /* It is possible to have selector register inside data window.
1100 In that case, selector register is located on every page and
1101 it needs no page switching, when accessed alone. */
1102 if (val_num > 1 ||
1103 range->window_start + win_offset != range->selector_reg) {
1104 /* Use separate work_buf during page switching */
1105 orig_work_buf = map->work_buf;
1106 map->work_buf = map->selector_work_buf;
1108 ret = _regmap_update_bits(map, range->selector_reg,
1109 range->selector_mask,
1110 win_page << range->selector_shift,
1111 &page_chg);
1113 map->work_buf = orig_work_buf;
1115 if (ret != 0)
1116 return ret;
1119 *reg = range->window_start + win_offset;
1121 return 0;
1124 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1125 const void *val, size_t val_len)
1127 struct regmap_range_node *range;
1128 unsigned long flags;
1129 u8 *u8 = map->work_buf;
1130 void *work_val = map->work_buf + map->format.reg_bytes +
1131 map->format.pad_bytes;
1132 void *buf;
1133 int ret = -ENOTSUPP;
1134 size_t len;
1135 int i;
1137 WARN_ON(!map->bus);
1139 /* Check for unwritable registers before we start */
1140 if (map->writeable_reg)
1141 for (i = 0; i < val_len / map->format.val_bytes; i++)
1142 if (!map->writeable_reg(map->dev,
1143 reg + (i * map->reg_stride)))
1144 return -EINVAL;
1146 if (!map->cache_bypass && map->format.parse_val) {
1147 unsigned int ival;
1148 int val_bytes = map->format.val_bytes;
1149 for (i = 0; i < val_len / val_bytes; i++) {
1150 ival = map->format.parse_val(val + (i * val_bytes));
1151 ret = regcache_write(map, reg + (i * map->reg_stride),
1152 ival);
1153 if (ret) {
1154 dev_err(map->dev,
1155 "Error in caching of register: %x ret: %d\n",
1156 reg + i, ret);
1157 return ret;
1160 if (map->cache_only) {
1161 map->cache_dirty = true;
1162 return 0;
1166 range = _regmap_range_lookup(map, reg);
1167 if (range) {
1168 int val_num = val_len / map->format.val_bytes;
1169 int win_offset = (reg - range->range_min) % range->window_len;
1170 int win_residue = range->window_len - win_offset;
1172 /* If the write goes beyond the end of the window split it */
1173 while (val_num > win_residue) {
1174 dev_dbg(map->dev, "Writing window %d/%zu\n",
1175 win_residue, val_len / map->format.val_bytes);
1176 ret = _regmap_raw_write(map, reg, val, win_residue *
1177 map->format.val_bytes);
1178 if (ret != 0)
1179 return ret;
1181 reg += win_residue;
1182 val_num -= win_residue;
1183 val += win_residue * map->format.val_bytes;
1184 val_len -= win_residue * map->format.val_bytes;
1186 win_offset = (reg - range->range_min) %
1187 range->window_len;
1188 win_residue = range->window_len - win_offset;
1191 ret = _regmap_select_page(map, &reg, range, val_num);
1192 if (ret != 0)
1193 return ret;
1196 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1198 u8[0] |= map->write_flag_mask;
1201 * Essentially all I/O mechanisms will be faster with a single
1202 * buffer to write. Since register syncs often generate raw
1203 * writes of single registers optimise that case.
1205 if (val != work_val && val_len == map->format.val_bytes) {
1206 memcpy(work_val, val, map->format.val_bytes);
1207 val = work_val;
1210 if (map->async && map->bus->async_write) {
1211 struct regmap_async *async;
1213 trace_regmap_async_write_start(map->dev, reg, val_len);
1215 spin_lock_irqsave(&map->async_lock, flags);
1216 async = list_first_entry_or_null(&map->async_free,
1217 struct regmap_async,
1218 list);
1219 if (async)
1220 list_del(&async->list);
1221 spin_unlock_irqrestore(&map->async_lock, flags);
1223 if (!async) {
1224 async = map->bus->async_alloc();
1225 if (!async)
1226 return -ENOMEM;
1228 async->work_buf = kzalloc(map->format.buf_size,
1229 GFP_KERNEL | GFP_DMA);
1230 if (!async->work_buf) {
1231 kfree(async);
1232 return -ENOMEM;
1236 async->map = map;
1238 /* If the caller supplied the value we can use it safely. */
1239 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1240 map->format.reg_bytes + map->format.val_bytes);
1242 spin_lock_irqsave(&map->async_lock, flags);
1243 list_add_tail(&async->list, &map->async_list);
1244 spin_unlock_irqrestore(&map->async_lock, flags);
1246 if (val != work_val)
1247 ret = map->bus->async_write(map->bus_context,
1248 async->work_buf,
1249 map->format.reg_bytes +
1250 map->format.pad_bytes,
1251 val, val_len, async);
1252 else
1253 ret = map->bus->async_write(map->bus_context,
1254 async->work_buf,
1255 map->format.reg_bytes +
1256 map->format.pad_bytes +
1257 val_len, NULL, 0, async);
1259 if (ret != 0) {
1260 dev_err(map->dev, "Failed to schedule write: %d\n",
1261 ret);
1263 spin_lock_irqsave(&map->async_lock, flags);
1264 list_move(&async->list, &map->async_free);
1265 spin_unlock_irqrestore(&map->async_lock, flags);
1268 return ret;
1271 trace_regmap_hw_write_start(map->dev, reg,
1272 val_len / map->format.val_bytes);
1274 /* If we're doing a single register write we can probably just
1275 * send the work_buf directly, otherwise try to do a gather
1276 * write.
1278 if (val == work_val)
1279 ret = map->bus->write(map->bus_context, map->work_buf,
1280 map->format.reg_bytes +
1281 map->format.pad_bytes +
1282 val_len);
1283 else if (map->bus->gather_write)
1284 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1285 map->format.reg_bytes +
1286 map->format.pad_bytes,
1287 val, val_len);
1289 /* If that didn't work fall back on linearising by hand. */
1290 if (ret == -ENOTSUPP) {
1291 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1292 buf = kzalloc(len, GFP_KERNEL);
1293 if (!buf)
1294 return -ENOMEM;
1296 memcpy(buf, map->work_buf, map->format.reg_bytes);
1297 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1298 val, val_len);
1299 ret = map->bus->write(map->bus_context, buf, len);
1301 kfree(buf);
1304 trace_regmap_hw_write_done(map->dev, reg,
1305 val_len / map->format.val_bytes);
1307 return ret;
1311 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1313 * @map: Map to check.
1315 bool regmap_can_raw_write(struct regmap *map)
1317 return map->bus && map->format.format_val && map->format.format_reg;
1319 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1321 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1322 unsigned int val)
1324 int ret;
1325 struct regmap_range_node *range;
1326 struct regmap *map = context;
1328 WARN_ON(!map->bus || !map->format.format_write);
1330 range = _regmap_range_lookup(map, reg);
1331 if (range) {
1332 ret = _regmap_select_page(map, &reg, range, 1);
1333 if (ret != 0)
1334 return ret;
1337 map->format.format_write(map, reg, val);
1339 trace_regmap_hw_write_start(map->dev, reg, 1);
1341 ret = map->bus->write(map->bus_context, map->work_buf,
1342 map->format.buf_size);
1344 trace_regmap_hw_write_done(map->dev, reg, 1);
1346 return ret;
1349 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1350 unsigned int val)
1352 struct regmap *map = context;
1354 return map->bus->reg_write(map->bus_context, reg, val);
1357 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1358 unsigned int val)
1360 struct regmap *map = context;
1362 WARN_ON(!map->bus || !map->format.format_val);
1364 map->format.format_val(map->work_buf + map->format.reg_bytes
1365 + map->format.pad_bytes, val, 0);
1366 return _regmap_raw_write(map, reg,
1367 map->work_buf +
1368 map->format.reg_bytes +
1369 map->format.pad_bytes,
1370 map->format.val_bytes);
1373 static inline void *_regmap_map_get_context(struct regmap *map)
1375 return (map->bus) ? map : map->bus_context;
1378 int _regmap_write(struct regmap *map, unsigned int reg,
1379 unsigned int val)
1381 int ret;
1382 void *context = _regmap_map_get_context(map);
1384 if (!regmap_writeable(map, reg))
1385 return -EIO;
1387 if (!map->cache_bypass && !map->defer_caching) {
1388 ret = regcache_write(map, reg, val);
1389 if (ret != 0)
1390 return ret;
1391 if (map->cache_only) {
1392 map->cache_dirty = true;
1393 return 0;
1397 #ifdef LOG_DEVICE
1398 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1399 dev_info(map->dev, "%x <= %x\n", reg, val);
1400 #endif
1402 trace_regmap_reg_write(map->dev, reg, val);
1404 return map->reg_write(context, reg, val);
1408 * regmap_write(): Write a value to a single register
1410 * @map: Register map to write to
1411 * @reg: Register to write to
1412 * @val: Value to be written
1414 * A value of zero will be returned on success, a negative errno will
1415 * be returned in error cases.
1417 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1419 int ret;
1421 if (reg % map->reg_stride)
1422 return -EINVAL;
1424 map->lock(map->lock_arg);
1426 ret = _regmap_write(map, reg, val);
1428 map->unlock(map->lock_arg);
1430 return ret;
1432 EXPORT_SYMBOL_GPL(regmap_write);
1435 * regmap_write_async(): Write a value to a single register asynchronously
1437 * @map: Register map to write to
1438 * @reg: Register to write to
1439 * @val: Value to be written
1441 * A value of zero will be returned on success, a negative errno will
1442 * be returned in error cases.
1444 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1446 int ret;
1448 if (reg % map->reg_stride)
1449 return -EINVAL;
1451 map->lock(map->lock_arg);
1453 map->async = true;
1455 ret = _regmap_write(map, reg, val);
1457 map->async = false;
1459 map->unlock(map->lock_arg);
1461 return ret;
1463 EXPORT_SYMBOL_GPL(regmap_write_async);
1466 * regmap_raw_write(): Write raw values to one or more registers
1468 * @map: Register map to write to
1469 * @reg: Initial register to write to
1470 * @val: Block of data to be written, laid out for direct transmission to the
1471 * device
1472 * @val_len: Length of data pointed to by val.
1474 * This function is intended to be used for things like firmware
1475 * download where a large block of data needs to be transferred to the
1476 * device. No formatting will be done on the data provided.
1478 * A value of zero will be returned on success, a negative errno will
1479 * be returned in error cases.
1481 int regmap_raw_write(struct regmap *map, unsigned int reg,
1482 const void *val, size_t val_len)
1484 int ret;
1486 if (!regmap_can_raw_write(map))
1487 return -EINVAL;
1488 if (val_len % map->format.val_bytes)
1489 return -EINVAL;
1491 map->lock(map->lock_arg);
1493 ret = _regmap_raw_write(map, reg, val, val_len);
1495 map->unlock(map->lock_arg);
1497 return ret;
1499 EXPORT_SYMBOL_GPL(regmap_raw_write);
1502 * regmap_field_write(): Write a value to a single register field
1504 * @field: Register field to write to
1505 * @val: Value to be written
1507 * A value of zero will be returned on success, a negative errno will
1508 * be returned in error cases.
1510 int regmap_field_write(struct regmap_field *field, unsigned int val)
1512 return regmap_update_bits(field->regmap, field->reg,
1513 field->mask, val << field->shift);
1515 EXPORT_SYMBOL_GPL(regmap_field_write);
1518 * regmap_field_update_bits(): Perform a read/modify/write cycle
1519 * on the register field
1521 * @field: Register field to write to
1522 * @mask: Bitmask to change
1523 * @val: Value to be written
1525 * A value of zero will be returned on success, a negative errno will
1526 * be returned in error cases.
1528 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1530 mask = (mask << field->shift) & field->mask;
1532 return regmap_update_bits(field->regmap, field->reg,
1533 mask, val << field->shift);
1535 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1538 * regmap_fields_write(): Write a value to a single register field with port ID
1540 * @field: Register field to write to
1541 * @id: port ID
1542 * @val: Value to be written
1544 * A value of zero will be returned on success, a negative errno will
1545 * be returned in error cases.
1547 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1548 unsigned int val)
1550 if (id >= field->id_size)
1551 return -EINVAL;
1553 return regmap_update_bits(field->regmap,
1554 field->reg + (field->id_offset * id),
1555 field->mask, val << field->shift);
1557 EXPORT_SYMBOL_GPL(regmap_fields_write);
1560 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1561 * on the register field
1563 * @field: Register field to write to
1564 * @id: port ID
1565 * @mask: Bitmask to change
1566 * @val: Value to be written
1568 * A value of zero will be returned on success, a negative errno will
1569 * be returned in error cases.
1571 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1572 unsigned int mask, unsigned int val)
1574 if (id >= field->id_size)
1575 return -EINVAL;
1577 mask = (mask << field->shift) & field->mask;
1579 return regmap_update_bits(field->regmap,
1580 field->reg + (field->id_offset * id),
1581 mask, val << field->shift);
1583 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1586 * regmap_bulk_write(): Write multiple registers to the device
1588 * @map: Register map to write to
1589 * @reg: First register to be write from
1590 * @val: Block of data to be written, in native register size for device
1591 * @val_count: Number of registers to write
1593 * This function is intended to be used for writing a large block of
1594 * data to the device either in single transfer or multiple transfer.
1596 * A value of zero will be returned on success, a negative errno will
1597 * be returned in error cases.
1599 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1600 size_t val_count)
1602 int ret = 0, i;
1603 size_t val_bytes = map->format.val_bytes;
1605 if (map->bus && !map->format.parse_inplace)
1606 return -EINVAL;
1607 if (reg % map->reg_stride)
1608 return -EINVAL;
1611 * Some devices don't support bulk write, for
1612 * them we have a series of single write operations.
1614 if (!map->bus || map->use_single_rw) {
1615 map->lock(map->lock_arg);
1616 for (i = 0; i < val_count; i++) {
1617 unsigned int ival;
1619 switch (val_bytes) {
1620 case 1:
1621 ival = *(u8 *)(val + (i * val_bytes));
1622 break;
1623 case 2:
1624 ival = *(u16 *)(val + (i * val_bytes));
1625 break;
1626 case 4:
1627 ival = *(u32 *)(val + (i * val_bytes));
1628 break;
1629 #ifdef CONFIG_64BIT
1630 case 8:
1631 ival = *(u64 *)(val + (i * val_bytes));
1632 break;
1633 #endif
1634 default:
1635 ret = -EINVAL;
1636 goto out;
1639 ret = _regmap_write(map, reg + (i * map->reg_stride),
1640 ival);
1641 if (ret != 0)
1642 goto out;
1644 out:
1645 map->unlock(map->lock_arg);
1646 } else {
1647 void *wval;
1649 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1650 if (!wval) {
1651 dev_err(map->dev, "Error in memory allocation\n");
1652 return -ENOMEM;
1654 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1655 map->format.parse_inplace(wval + i);
1657 map->lock(map->lock_arg);
1658 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1659 map->unlock(map->lock_arg);
1661 kfree(wval);
1663 return ret;
1665 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1668 * _regmap_raw_multi_reg_write()
1670 * the (register,newvalue) pairs in regs have not been formatted, but
1671 * they are all in the same page and have been changed to being page
1672 * relative. The page register has been written if that was neccessary.
1674 static int _regmap_raw_multi_reg_write(struct regmap *map,
1675 const struct reg_default *regs,
1676 size_t num_regs)
1678 int ret;
1679 void *buf;
1680 int i;
1681 u8 *u8;
1682 size_t val_bytes = map->format.val_bytes;
1683 size_t reg_bytes = map->format.reg_bytes;
1684 size_t pad_bytes = map->format.pad_bytes;
1685 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1686 size_t len = pair_size * num_regs;
1688 if (!len)
1689 return -EINVAL;
1691 buf = kzalloc(len, GFP_KERNEL);
1692 if (!buf)
1693 return -ENOMEM;
1695 /* We have to linearise by hand. */
1697 u8 = buf;
1699 for (i = 0; i < num_regs; i++) {
1700 int reg = regs[i].reg;
1701 int val = regs[i].def;
1702 trace_regmap_hw_write_start(map->dev, reg, 1);
1703 map->format.format_reg(u8, reg, map->reg_shift);
1704 u8 += reg_bytes + pad_bytes;
1705 map->format.format_val(u8, val, 0);
1706 u8 += val_bytes;
1708 u8 = buf;
1709 *u8 |= map->write_flag_mask;
1711 ret = map->bus->write(map->bus_context, buf, len);
1713 kfree(buf);
1715 for (i = 0; i < num_regs; i++) {
1716 int reg = regs[i].reg;
1717 trace_regmap_hw_write_done(map->dev, reg, 1);
1719 return ret;
1722 static unsigned int _regmap_register_page(struct regmap *map,
1723 unsigned int reg,
1724 struct regmap_range_node *range)
1726 unsigned int win_page = (reg - range->range_min) / range->window_len;
1728 return win_page;
1731 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1732 struct reg_default *regs,
1733 size_t num_regs)
1735 int ret;
1736 int i, n;
1737 struct reg_default *base;
1738 unsigned int this_page = 0;
1740 * the set of registers are not neccessarily in order, but
1741 * since the order of write must be preserved this algorithm
1742 * chops the set each time the page changes
1744 base = regs;
1745 for (i = 0, n = 0; i < num_regs; i++, n++) {
1746 unsigned int reg = regs[i].reg;
1747 struct regmap_range_node *range;
1749 range = _regmap_range_lookup(map, reg);
1750 if (range) {
1751 unsigned int win_page = _regmap_register_page(map, reg,
1752 range);
1754 if (i == 0)
1755 this_page = win_page;
1756 if (win_page != this_page) {
1757 this_page = win_page;
1758 ret = _regmap_raw_multi_reg_write(map, base, n);
1759 if (ret != 0)
1760 return ret;
1761 base += n;
1762 n = 0;
1764 ret = _regmap_select_page(map, &base[n].reg, range, 1);
1765 if (ret != 0)
1766 return ret;
1769 if (n > 0)
1770 return _regmap_raw_multi_reg_write(map, base, n);
1771 return 0;
1774 static int _regmap_multi_reg_write(struct regmap *map,
1775 const struct reg_default *regs,
1776 size_t num_regs)
1778 int i;
1779 int ret;
1781 if (!map->can_multi_write) {
1782 for (i = 0; i < num_regs; i++) {
1783 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1784 if (ret != 0)
1785 return ret;
1787 return 0;
1790 if (!map->format.parse_inplace)
1791 return -EINVAL;
1793 if (map->writeable_reg)
1794 for (i = 0; i < num_regs; i++) {
1795 int reg = regs[i].reg;
1796 if (!map->writeable_reg(map->dev, reg))
1797 return -EINVAL;
1798 if (reg % map->reg_stride)
1799 return -EINVAL;
1802 if (!map->cache_bypass) {
1803 for (i = 0; i < num_regs; i++) {
1804 unsigned int val = regs[i].def;
1805 unsigned int reg = regs[i].reg;
1806 ret = regcache_write(map, reg, val);
1807 if (ret) {
1808 dev_err(map->dev,
1809 "Error in caching of register: %x ret: %d\n",
1810 reg, ret);
1811 return ret;
1814 if (map->cache_only) {
1815 map->cache_dirty = true;
1816 return 0;
1820 WARN_ON(!map->bus);
1822 for (i = 0; i < num_regs; i++) {
1823 unsigned int reg = regs[i].reg;
1824 struct regmap_range_node *range;
1825 range = _regmap_range_lookup(map, reg);
1826 if (range) {
1827 size_t len = sizeof(struct reg_default)*num_regs;
1828 struct reg_default *base = kmemdup(regs, len,
1829 GFP_KERNEL);
1830 if (!base)
1831 return -ENOMEM;
1832 ret = _regmap_range_multi_paged_reg_write(map, base,
1833 num_regs);
1834 kfree(base);
1836 return ret;
1839 return _regmap_raw_multi_reg_write(map, regs, num_regs);
1843 * regmap_multi_reg_write(): Write multiple registers to the device
1845 * where the set of register,value pairs are supplied in any order,
1846 * possibly not all in a single range.
1848 * @map: Register map to write to
1849 * @regs: Array of structures containing register,value to be written
1850 * @num_regs: Number of registers to write
1852 * The 'normal' block write mode will send ultimately send data on the
1853 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
1854 * addressed. However, this alternative block multi write mode will send
1855 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
1856 * must of course support the mode.
1858 * A value of zero will be returned on success, a negative errno will be
1859 * returned in error cases.
1861 int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
1862 int num_regs)
1864 int ret;
1866 map->lock(map->lock_arg);
1868 ret = _regmap_multi_reg_write(map, regs, num_regs);
1870 map->unlock(map->lock_arg);
1872 return ret;
1874 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1877 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
1878 * device but not the cache
1880 * where the set of register are supplied in any order
1882 * @map: Register map to write to
1883 * @regs: Array of structures containing register,value to be written
1884 * @num_regs: Number of registers to write
1886 * This function is intended to be used for writing a large block of data
1887 * atomically to the device in single transfer for those I2C client devices
1888 * that implement this alternative block write mode.
1890 * A value of zero will be returned on success, a negative errno will
1891 * be returned in error cases.
1893 int regmap_multi_reg_write_bypassed(struct regmap *map,
1894 const struct reg_default *regs,
1895 int num_regs)
1897 int ret;
1898 bool bypass;
1900 map->lock(map->lock_arg);
1902 bypass = map->cache_bypass;
1903 map->cache_bypass = true;
1905 ret = _regmap_multi_reg_write(map, regs, num_regs);
1907 map->cache_bypass = bypass;
1909 map->unlock(map->lock_arg);
1911 return ret;
1913 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
1916 * regmap_raw_write_async(): Write raw values to one or more registers
1917 * asynchronously
1919 * @map: Register map to write to
1920 * @reg: Initial register to write to
1921 * @val: Block of data to be written, laid out for direct transmission to the
1922 * device. Must be valid until regmap_async_complete() is called.
1923 * @val_len: Length of data pointed to by val.
1925 * This function is intended to be used for things like firmware
1926 * download where a large block of data needs to be transferred to the
1927 * device. No formatting will be done on the data provided.
1929 * If supported by the underlying bus the write will be scheduled
1930 * asynchronously, helping maximise I/O speed on higher speed buses
1931 * like SPI. regmap_async_complete() can be called to ensure that all
1932 * asynchrnous writes have been completed.
1934 * A value of zero will be returned on success, a negative errno will
1935 * be returned in error cases.
1937 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1938 const void *val, size_t val_len)
1940 int ret;
1942 if (val_len % map->format.val_bytes)
1943 return -EINVAL;
1944 if (reg % map->reg_stride)
1945 return -EINVAL;
1947 map->lock(map->lock_arg);
1949 map->async = true;
1951 ret = _regmap_raw_write(map, reg, val, val_len);
1953 map->async = false;
1955 map->unlock(map->lock_arg);
1957 return ret;
1959 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1961 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1962 unsigned int val_len)
1964 struct regmap_range_node *range;
1965 u8 *u8 = map->work_buf;
1966 int ret;
1968 WARN_ON(!map->bus);
1970 range = _regmap_range_lookup(map, reg);
1971 if (range) {
1972 ret = _regmap_select_page(map, &reg, range,
1973 val_len / map->format.val_bytes);
1974 if (ret != 0)
1975 return ret;
1978 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1981 * Some buses or devices flag reads by setting the high bits in the
1982 * register addresss; since it's always the high bits for all
1983 * current formats we can do this here rather than in
1984 * formatting. This may break if we get interesting formats.
1986 u8[0] |= map->read_flag_mask;
1988 trace_regmap_hw_read_start(map->dev, reg,
1989 val_len / map->format.val_bytes);
1991 ret = map->bus->read(map->bus_context, map->work_buf,
1992 map->format.reg_bytes + map->format.pad_bytes,
1993 val, val_len);
1995 trace_regmap_hw_read_done(map->dev, reg,
1996 val_len / map->format.val_bytes);
1998 return ret;
2001 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2002 unsigned int *val)
2004 struct regmap *map = context;
2006 return map->bus->reg_read(map->bus_context, reg, val);
2009 static int _regmap_bus_read(void *context, unsigned int reg,
2010 unsigned int *val)
2012 int ret;
2013 struct regmap *map = context;
2015 if (!map->format.parse_val)
2016 return -EINVAL;
2018 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2019 if (ret == 0)
2020 *val = map->format.parse_val(map->work_buf);
2022 return ret;
2025 static int _regmap_read(struct regmap *map, unsigned int reg,
2026 unsigned int *val)
2028 int ret;
2029 void *context = _regmap_map_get_context(map);
2031 WARN_ON(!map->reg_read);
2033 if (!map->cache_bypass) {
2034 ret = regcache_read(map, reg, val);
2035 if (ret == 0)
2036 return 0;
2039 if (map->cache_only)
2040 return -EBUSY;
2042 if (!regmap_readable(map, reg))
2043 return -EIO;
2045 ret = map->reg_read(context, reg, val);
2046 if (ret == 0) {
2047 #ifdef LOG_DEVICE
2048 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2049 dev_info(map->dev, "%x => %x\n", reg, *val);
2050 #endif
2052 trace_regmap_reg_read(map->dev, reg, *val);
2054 if (!map->cache_bypass)
2055 regcache_write(map, reg, *val);
2058 return ret;
2062 * regmap_read(): Read a value from a single register
2064 * @map: Register map to read from
2065 * @reg: Register to be read from
2066 * @val: Pointer to store read value
2068 * A value of zero will be returned on success, a negative errno will
2069 * be returned in error cases.
2071 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2073 int ret;
2075 if (reg % map->reg_stride)
2076 return -EINVAL;
2078 map->lock(map->lock_arg);
2080 ret = _regmap_read(map, reg, val);
2082 map->unlock(map->lock_arg);
2084 return ret;
2086 EXPORT_SYMBOL_GPL(regmap_read);
2089 * regmap_raw_read(): Read raw data from the device
2091 * @map: Register map to read from
2092 * @reg: First register to be read from
2093 * @val: Pointer to store read value
2094 * @val_len: Size of data to read
2096 * A value of zero will be returned on success, a negative errno will
2097 * be returned in error cases.
2099 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2100 size_t val_len)
2102 size_t val_bytes = map->format.val_bytes;
2103 size_t val_count = val_len / val_bytes;
2104 unsigned int v;
2105 int ret, i;
2107 if (!map->bus)
2108 return -EINVAL;
2109 if (val_len % map->format.val_bytes)
2110 return -EINVAL;
2111 if (reg % map->reg_stride)
2112 return -EINVAL;
2114 map->lock(map->lock_arg);
2116 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2117 map->cache_type == REGCACHE_NONE) {
2118 /* Physical block read if there's no cache involved */
2119 ret = _regmap_raw_read(map, reg, val, val_len);
2121 } else {
2122 /* Otherwise go word by word for the cache; should be low
2123 * cost as we expect to hit the cache.
2125 for (i = 0; i < val_count; i++) {
2126 ret = _regmap_read(map, reg + (i * map->reg_stride),
2127 &v);
2128 if (ret != 0)
2129 goto out;
2131 map->format.format_val(val + (i * val_bytes), v, 0);
2135 out:
2136 map->unlock(map->lock_arg);
2138 return ret;
2140 EXPORT_SYMBOL_GPL(regmap_raw_read);
2143 * regmap_field_read(): Read a value to a single register field
2145 * @field: Register field to read from
2146 * @val: Pointer to store read value
2148 * A value of zero will be returned on success, a negative errno will
2149 * be returned in error cases.
2151 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2153 int ret;
2154 unsigned int reg_val;
2155 ret = regmap_read(field->regmap, field->reg, &reg_val);
2156 if (ret != 0)
2157 return ret;
2159 reg_val &= field->mask;
2160 reg_val >>= field->shift;
2161 *val = reg_val;
2163 return ret;
2165 EXPORT_SYMBOL_GPL(regmap_field_read);
2168 * regmap_fields_read(): Read a value to a single register field with port ID
2170 * @field: Register field to read from
2171 * @id: port ID
2172 * @val: Pointer to store read value
2174 * A value of zero will be returned on success, a negative errno will
2175 * be returned in error cases.
2177 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2178 unsigned int *val)
2180 int ret;
2181 unsigned int reg_val;
2183 if (id >= field->id_size)
2184 return -EINVAL;
2186 ret = regmap_read(field->regmap,
2187 field->reg + (field->id_offset * id),
2188 &reg_val);
2189 if (ret != 0)
2190 return ret;
2192 reg_val &= field->mask;
2193 reg_val >>= field->shift;
2194 *val = reg_val;
2196 return ret;
2198 EXPORT_SYMBOL_GPL(regmap_fields_read);
2201 * regmap_bulk_read(): Read multiple registers from the device
2203 * @map: Register map to read from
2204 * @reg: First register to be read from
2205 * @val: Pointer to store read value, in native register size for device
2206 * @val_count: Number of registers to read
2208 * A value of zero will be returned on success, a negative errno will
2209 * be returned in error cases.
2211 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2212 size_t val_count)
2214 int ret, i;
2215 size_t val_bytes = map->format.val_bytes;
2216 bool vol = regmap_volatile_range(map, reg, val_count);
2218 if (reg % map->reg_stride)
2219 return -EINVAL;
2221 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2223 * Some devices does not support bulk read, for
2224 * them we have a series of single read operations.
2226 if (map->use_single_rw) {
2227 for (i = 0; i < val_count; i++) {
2228 ret = regmap_raw_read(map,
2229 reg + (i * map->reg_stride),
2230 val + (i * val_bytes),
2231 val_bytes);
2232 if (ret != 0)
2233 return ret;
2235 } else {
2236 ret = regmap_raw_read(map, reg, val,
2237 val_bytes * val_count);
2238 if (ret != 0)
2239 return ret;
2242 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2243 map->format.parse_inplace(val + i);
2244 } else {
2245 for (i = 0; i < val_count; i++) {
2246 unsigned int ival;
2247 ret = regmap_read(map, reg + (i * map->reg_stride),
2248 &ival);
2249 if (ret != 0)
2250 return ret;
2251 memcpy(val + (i * val_bytes), &ival, val_bytes);
2255 return 0;
2257 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2259 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2260 unsigned int mask, unsigned int val,
2261 bool *change)
2263 int ret;
2264 unsigned int tmp, orig;
2266 ret = _regmap_read(map, reg, &orig);
2267 if (ret != 0)
2268 return ret;
2270 tmp = orig & ~mask;
2271 tmp |= val & mask;
2273 if (tmp != orig) {
2274 ret = _regmap_write(map, reg, tmp);
2275 if (change)
2276 *change = true;
2277 } else {
2278 if (change)
2279 *change = false;
2282 return ret;
2286 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2288 * @map: Register map to update
2289 * @reg: Register to update
2290 * @mask: Bitmask to change
2291 * @val: New value for bitmask
2293 * Returns zero for success, a negative number on error.
2295 int regmap_update_bits(struct regmap *map, unsigned int reg,
2296 unsigned int mask, unsigned int val)
2298 int ret;
2300 map->lock(map->lock_arg);
2301 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2302 map->unlock(map->lock_arg);
2304 return ret;
2306 EXPORT_SYMBOL_GPL(regmap_update_bits);
2309 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2310 * map asynchronously
2312 * @map: Register map to update
2313 * @reg: Register to update
2314 * @mask: Bitmask to change
2315 * @val: New value for bitmask
2317 * With most buses the read must be done synchronously so this is most
2318 * useful for devices with a cache which do not need to interact with
2319 * the hardware to determine the current register value.
2321 * Returns zero for success, a negative number on error.
2323 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2324 unsigned int mask, unsigned int val)
2326 int ret;
2328 map->lock(map->lock_arg);
2330 map->async = true;
2332 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2334 map->async = false;
2336 map->unlock(map->lock_arg);
2338 return ret;
2340 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2343 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2344 * register map and report if updated
2346 * @map: Register map to update
2347 * @reg: Register to update
2348 * @mask: Bitmask to change
2349 * @val: New value for bitmask
2350 * @change: Boolean indicating if a write was done
2352 * Returns zero for success, a negative number on error.
2354 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2355 unsigned int mask, unsigned int val,
2356 bool *change)
2358 int ret;
2360 map->lock(map->lock_arg);
2361 ret = _regmap_update_bits(map, reg, mask, val, change);
2362 map->unlock(map->lock_arg);
2363 return ret;
2365 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2368 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2369 * register map asynchronously and report if
2370 * updated
2372 * @map: Register map to update
2373 * @reg: Register to update
2374 * @mask: Bitmask to change
2375 * @val: New value for bitmask
2376 * @change: Boolean indicating if a write was done
2378 * With most buses the read must be done synchronously so this is most
2379 * useful for devices with a cache which do not need to interact with
2380 * the hardware to determine the current register value.
2382 * Returns zero for success, a negative number on error.
2384 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2385 unsigned int mask, unsigned int val,
2386 bool *change)
2388 int ret;
2390 map->lock(map->lock_arg);
2392 map->async = true;
2394 ret = _regmap_update_bits(map, reg, mask, val, change);
2396 map->async = false;
2398 map->unlock(map->lock_arg);
2400 return ret;
2402 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2404 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2406 struct regmap *map = async->map;
2407 bool wake;
2409 trace_regmap_async_io_complete(map->dev);
2411 spin_lock(&map->async_lock);
2412 list_move(&async->list, &map->async_free);
2413 wake = list_empty(&map->async_list);
2415 if (ret != 0)
2416 map->async_ret = ret;
2418 spin_unlock(&map->async_lock);
2420 if (wake)
2421 wake_up(&map->async_waitq);
2423 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2425 static int regmap_async_is_done(struct regmap *map)
2427 unsigned long flags;
2428 int ret;
2430 spin_lock_irqsave(&map->async_lock, flags);
2431 ret = list_empty(&map->async_list);
2432 spin_unlock_irqrestore(&map->async_lock, flags);
2434 return ret;
2438 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2440 * @map: Map to operate on.
2442 * Blocks until any pending asynchronous I/O has completed. Returns
2443 * an error code for any failed I/O operations.
2445 int regmap_async_complete(struct regmap *map)
2447 unsigned long flags;
2448 int ret;
2450 /* Nothing to do with no async support */
2451 if (!map->bus || !map->bus->async_write)
2452 return 0;
2454 trace_regmap_async_complete_start(map->dev);
2456 wait_event(map->async_waitq, regmap_async_is_done(map));
2458 spin_lock_irqsave(&map->async_lock, flags);
2459 ret = map->async_ret;
2460 map->async_ret = 0;
2461 spin_unlock_irqrestore(&map->async_lock, flags);
2463 trace_regmap_async_complete_done(map->dev);
2465 return ret;
2467 EXPORT_SYMBOL_GPL(regmap_async_complete);
2470 * regmap_register_patch: Register and apply register updates to be applied
2471 * on device initialistion
2473 * @map: Register map to apply updates to.
2474 * @regs: Values to update.
2475 * @num_regs: Number of entries in regs.
2477 * Register a set of register updates to be applied to the device
2478 * whenever the device registers are synchronised with the cache and
2479 * apply them immediately. Typically this is used to apply
2480 * corrections to be applied to the device defaults on startup, such
2481 * as the updates some vendors provide to undocumented registers.
2483 * The caller must ensure that this function cannot be called
2484 * concurrently with either itself or regcache_sync().
2486 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2487 int num_regs)
2489 struct reg_default *p;
2490 int ret;
2491 bool bypass;
2493 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2494 num_regs))
2495 return 0;
2497 p = krealloc(map->patch,
2498 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2499 GFP_KERNEL);
2500 if (p) {
2501 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2502 map->patch = p;
2503 map->patch_regs += num_regs;
2504 } else {
2505 return -ENOMEM;
2508 map->lock(map->lock_arg);
2510 bypass = map->cache_bypass;
2512 map->cache_bypass = true;
2513 map->async = true;
2515 ret = _regmap_multi_reg_write(map, regs, num_regs);
2516 if (ret != 0)
2517 goto out;
2519 out:
2520 map->async = false;
2521 map->cache_bypass = bypass;
2523 map->unlock(map->lock_arg);
2525 regmap_async_complete(map);
2527 return ret;
2529 EXPORT_SYMBOL_GPL(regmap_register_patch);
2532 * regmap_get_val_bytes(): Report the size of a register value
2534 * Report the size of a register value, mainly intended to for use by
2535 * generic infrastructure built on top of regmap.
2537 int regmap_get_val_bytes(struct regmap *map)
2539 if (map->format.format_write)
2540 return -EINVAL;
2542 return map->format.val_bytes;
2544 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2546 int regmap_parse_val(struct regmap *map, const void *buf,
2547 unsigned int *val)
2549 if (!map->format.parse_val)
2550 return -EINVAL;
2552 *val = map->format.parse_val(buf);
2554 return 0;
2556 EXPORT_SYMBOL_GPL(regmap_parse_val);
2558 static int __init regmap_initcall(void)
2560 regmap_debugfs_initcall();
2562 return 0;
2564 postcore_initcall(regmap_initcall);