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