xref: /openbmc/linux/drivers/base/regmap/regmap.c (revision f05643a0)
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->write && map->format.format_val && map->format.format_reg;
1884 }
1885 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1886 
1887 /**
1888  * regmap_get_raw_read_max - Get the maximum size we can read
1889  *
1890  * @map: Map to check.
1891  */
1892 size_t regmap_get_raw_read_max(struct regmap *map)
1893 {
1894 	return map->max_raw_read;
1895 }
1896 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1897 
1898 /**
1899  * regmap_get_raw_write_max - Get the maximum size we can read
1900  *
1901  * @map: Map to check.
1902  */
1903 size_t regmap_get_raw_write_max(struct regmap *map)
1904 {
1905 	return map->max_raw_write;
1906 }
1907 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1908 
1909 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1910 				       unsigned int val)
1911 {
1912 	int ret;
1913 	struct regmap_range_node *range;
1914 	struct regmap *map = context;
1915 
1916 	WARN_ON(!map->format.format_write);
1917 
1918 	range = _regmap_range_lookup(map, reg);
1919 	if (range) {
1920 		ret = _regmap_select_page(map, &reg, range, 1);
1921 		if (ret != 0)
1922 			return ret;
1923 	}
1924 
1925 	reg += map->reg_base;
1926 	reg >>= map->format.reg_downshift;
1927 	map->format.format_write(map, reg, val);
1928 
1929 	trace_regmap_hw_write_start(map, reg, 1);
1930 
1931 	ret = map->write(map->bus_context, map->work_buf, map->format.buf_size);
1932 
1933 	trace_regmap_hw_write_done(map, reg, 1);
1934 
1935 	return ret;
1936 }
1937 
1938 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1939 				 unsigned int val)
1940 {
1941 	struct regmap *map = context;
1942 
1943 	return map->bus->reg_write(map->bus_context, reg, val);
1944 }
1945 
1946 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1947 				 unsigned int val)
1948 {
1949 	struct regmap *map = context;
1950 
1951 	WARN_ON(!map->format.format_val);
1952 
1953 	map->format.format_val(map->work_buf + map->format.reg_bytes
1954 			       + map->format.pad_bytes, val, 0);
1955 	return _regmap_raw_write_impl(map, reg,
1956 				      map->work_buf +
1957 				      map->format.reg_bytes +
1958 				      map->format.pad_bytes,
1959 				      map->format.val_bytes,
1960 				      false);
1961 }
1962 
1963 static inline void *_regmap_map_get_context(struct regmap *map)
1964 {
1965 	return (map->bus || (!map->bus && map->read)) ? map : map->bus_context;
1966 }
1967 
1968 int _regmap_write(struct regmap *map, unsigned int reg,
1969 		  unsigned int val)
1970 {
1971 	int ret;
1972 	void *context = _regmap_map_get_context(map);
1973 
1974 	if (!regmap_writeable(map, reg))
1975 		return -EIO;
1976 
1977 	if (!map->cache_bypass && !map->defer_caching) {
1978 		ret = regcache_write(map, reg, val);
1979 		if (ret != 0)
1980 			return ret;
1981 		if (map->cache_only) {
1982 			map->cache_dirty = true;
1983 			return 0;
1984 		}
1985 	}
1986 
1987 	ret = map->reg_write(context, reg, val);
1988 	if (ret == 0) {
1989 		if (regmap_should_log(map))
1990 			dev_info(map->dev, "%x <= %x\n", reg, val);
1991 
1992 		trace_regmap_reg_write(map, reg, val);
1993 	}
1994 
1995 	return ret;
1996 }
1997 
1998 /**
1999  * regmap_write() - Write a value to a single register
2000  *
2001  * @map: Register map to write to
2002  * @reg: Register to write to
2003  * @val: Value to be written
2004  *
2005  * A value of zero will be returned on success, a negative errno will
2006  * be returned in error cases.
2007  */
2008 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
2009 {
2010 	int ret;
2011 
2012 	if (!IS_ALIGNED(reg, map->reg_stride))
2013 		return -EINVAL;
2014 
2015 	map->lock(map->lock_arg);
2016 
2017 	ret = _regmap_write(map, reg, val);
2018 
2019 	map->unlock(map->lock_arg);
2020 
2021 	return ret;
2022 }
2023 EXPORT_SYMBOL_GPL(regmap_write);
2024 
2025 /**
2026  * regmap_write_async() - Write a value to a single register asynchronously
2027  *
2028  * @map: Register map to write to
2029  * @reg: Register to write to
2030  * @val: Value to be written
2031  *
2032  * A value of zero will be returned on success, a negative errno will
2033  * be returned in error cases.
2034  */
2035 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
2036 {
2037 	int ret;
2038 
2039 	if (!IS_ALIGNED(reg, map->reg_stride))
2040 		return -EINVAL;
2041 
2042 	map->lock(map->lock_arg);
2043 
2044 	map->async = true;
2045 
2046 	ret = _regmap_write(map, reg, val);
2047 
2048 	map->async = false;
2049 
2050 	map->unlock(map->lock_arg);
2051 
2052 	return ret;
2053 }
2054 EXPORT_SYMBOL_GPL(regmap_write_async);
2055 
2056 int _regmap_raw_write(struct regmap *map, unsigned int reg,
2057 		      const void *val, size_t val_len, bool noinc)
2058 {
2059 	size_t val_bytes = map->format.val_bytes;
2060 	size_t val_count = val_len / val_bytes;
2061 	size_t chunk_count, chunk_bytes;
2062 	size_t chunk_regs = val_count;
2063 	int ret, i;
2064 
2065 	if (!val_count)
2066 		return -EINVAL;
2067 
2068 	if (map->use_single_write)
2069 		chunk_regs = 1;
2070 	else if (map->max_raw_write && val_len > map->max_raw_write)
2071 		chunk_regs = map->max_raw_write / val_bytes;
2072 
2073 	chunk_count = val_count / chunk_regs;
2074 	chunk_bytes = chunk_regs * val_bytes;
2075 
2076 	/* Write as many bytes as possible with chunk_size */
2077 	for (i = 0; i < chunk_count; i++) {
2078 		ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc);
2079 		if (ret)
2080 			return ret;
2081 
2082 		reg += regmap_get_offset(map, chunk_regs);
2083 		val += chunk_bytes;
2084 		val_len -= chunk_bytes;
2085 	}
2086 
2087 	/* Write remaining bytes */
2088 	if (val_len)
2089 		ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc);
2090 
2091 	return ret;
2092 }
2093 
2094 /**
2095  * regmap_raw_write() - Write raw values to one or more registers
2096  *
2097  * @map: Register map to write to
2098  * @reg: Initial register to write to
2099  * @val: Block of data to be written, laid out for direct transmission to the
2100  *       device
2101  * @val_len: Length of data pointed to by val.
2102  *
2103  * This function is intended to be used for things like firmware
2104  * download where a large block of data needs to be transferred to the
2105  * device.  No formatting will be done on the data provided.
2106  *
2107  * A value of zero will be returned on success, a negative errno will
2108  * be returned in error cases.
2109  */
2110 int regmap_raw_write(struct regmap *map, unsigned int reg,
2111 		     const void *val, size_t val_len)
2112 {
2113 	int ret;
2114 
2115 	if (!regmap_can_raw_write(map))
2116 		return -EINVAL;
2117 	if (val_len % map->format.val_bytes)
2118 		return -EINVAL;
2119 
2120 	map->lock(map->lock_arg);
2121 
2122 	ret = _regmap_raw_write(map, reg, val, val_len, false);
2123 
2124 	map->unlock(map->lock_arg);
2125 
2126 	return ret;
2127 }
2128 EXPORT_SYMBOL_GPL(regmap_raw_write);
2129 
2130 /**
2131  * regmap_noinc_write(): Write data from a register without incrementing the
2132  *			register number
2133  *
2134  * @map: Register map to write to
2135  * @reg: Register to write to
2136  * @val: Pointer to data buffer
2137  * @val_len: Length of output buffer in bytes.
2138  *
2139  * The regmap API usually assumes that bulk bus write operations will write a
2140  * range of registers. Some devices have certain registers for which a write
2141  * operation can write to an internal FIFO.
2142  *
2143  * The target register must be volatile but registers after it can be
2144  * completely unrelated cacheable registers.
2145  *
2146  * This will attempt multiple writes as required to write val_len bytes.
2147  *
2148  * A value of zero will be returned on success, a negative errno will be
2149  * returned in error cases.
2150  */
2151 int regmap_noinc_write(struct regmap *map, unsigned int reg,
2152 		      const void *val, size_t val_len)
2153 {
2154 	size_t write_len;
2155 	int ret;
2156 
2157 	if (!map->write)
2158 		return -ENOTSUPP;
2159 
2160 	if (val_len % map->format.val_bytes)
2161 		return -EINVAL;
2162 	if (!IS_ALIGNED(reg, map->reg_stride))
2163 		return -EINVAL;
2164 	if (val_len == 0)
2165 		return -EINVAL;
2166 
2167 	map->lock(map->lock_arg);
2168 
2169 	if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) {
2170 		ret = -EINVAL;
2171 		goto out_unlock;
2172 	}
2173 
2174 	while (val_len) {
2175 		if (map->max_raw_write && map->max_raw_write < val_len)
2176 			write_len = map->max_raw_write;
2177 		else
2178 			write_len = val_len;
2179 		ret = _regmap_raw_write(map, reg, val, write_len, true);
2180 		if (ret)
2181 			goto out_unlock;
2182 		val = ((u8 *)val) + write_len;
2183 		val_len -= write_len;
2184 	}
2185 
2186 out_unlock:
2187 	map->unlock(map->lock_arg);
2188 	return ret;
2189 }
2190 EXPORT_SYMBOL_GPL(regmap_noinc_write);
2191 
2192 /**
2193  * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
2194  *                                   register field.
2195  *
2196  * @field: Register field to write to
2197  * @mask: Bitmask to change
2198  * @val: Value to be written
2199  * @change: Boolean indicating if a write was done
2200  * @async: Boolean indicating asynchronously
2201  * @force: Boolean indicating use force update
2202  *
2203  * Perform a read/modify/write cycle on the register field with change,
2204  * async, force option.
2205  *
2206  * A value of zero will be returned on success, a negative errno will
2207  * be returned in error cases.
2208  */
2209 int regmap_field_update_bits_base(struct regmap_field *field,
2210 				  unsigned int mask, unsigned int val,
2211 				  bool *change, bool async, bool force)
2212 {
2213 	mask = (mask << field->shift) & field->mask;
2214 
2215 	return regmap_update_bits_base(field->regmap, field->reg,
2216 				       mask, val << field->shift,
2217 				       change, async, force);
2218 }
2219 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
2220 
2221 /**
2222  * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
2223  *                                    register field with port ID
2224  *
2225  * @field: Register field to write to
2226  * @id: port ID
2227  * @mask: Bitmask to change
2228  * @val: Value to be written
2229  * @change: Boolean indicating if a write was done
2230  * @async: Boolean indicating asynchronously
2231  * @force: Boolean indicating use force update
2232  *
2233  * A value of zero will be returned on success, a negative errno will
2234  * be returned in error cases.
2235  */
2236 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
2237 				   unsigned int mask, unsigned int val,
2238 				   bool *change, bool async, bool force)
2239 {
2240 	if (id >= field->id_size)
2241 		return -EINVAL;
2242 
2243 	mask = (mask << field->shift) & field->mask;
2244 
2245 	return regmap_update_bits_base(field->regmap,
2246 				       field->reg + (field->id_offset * id),
2247 				       mask, val << field->shift,
2248 				       change, async, force);
2249 }
2250 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
2251 
2252 /**
2253  * regmap_bulk_write() - Write multiple registers to the device
2254  *
2255  * @map: Register map to write to
2256  * @reg: First register to be write from
2257  * @val: Block of data to be written, in native register size for device
2258  * @val_count: Number of registers to write
2259  *
2260  * This function is intended to be used for writing a large block of
2261  * data to the device either in single transfer or multiple transfer.
2262  *
2263  * A value of zero will be returned on success, a negative errno will
2264  * be returned in error cases.
2265  */
2266 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
2267 		     size_t val_count)
2268 {
2269 	int ret = 0, i;
2270 	size_t val_bytes = map->format.val_bytes;
2271 
2272 	if (!IS_ALIGNED(reg, map->reg_stride))
2273 		return -EINVAL;
2274 
2275 	/*
2276 	 * Some devices don't support bulk write, for them we have a series of
2277 	 * single write operations.
2278 	 */
2279 	if (!map->write || !map->format.parse_inplace) {
2280 		map->lock(map->lock_arg);
2281 		for (i = 0; i < val_count; i++) {
2282 			unsigned int ival;
2283 
2284 			switch (val_bytes) {
2285 			case 1:
2286 				ival = *(u8 *)(val + (i * val_bytes));
2287 				break;
2288 			case 2:
2289 				ival = *(u16 *)(val + (i * val_bytes));
2290 				break;
2291 			case 4:
2292 				ival = *(u32 *)(val + (i * val_bytes));
2293 				break;
2294 #ifdef CONFIG_64BIT
2295 			case 8:
2296 				ival = *(u64 *)(val + (i * val_bytes));
2297 				break;
2298 #endif
2299 			default:
2300 				ret = -EINVAL;
2301 				goto out;
2302 			}
2303 
2304 			ret = _regmap_write(map,
2305 					    reg + regmap_get_offset(map, i),
2306 					    ival);
2307 			if (ret != 0)
2308 				goto out;
2309 		}
2310 out:
2311 		map->unlock(map->lock_arg);
2312 	} else {
2313 		void *wval;
2314 
2315 		wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2316 		if (!wval)
2317 			return -ENOMEM;
2318 
2319 		for (i = 0; i < val_count * val_bytes; i += val_bytes)
2320 			map->format.parse_inplace(wval + i);
2321 
2322 		ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
2323 
2324 		kfree(wval);
2325 	}
2326 	return ret;
2327 }
2328 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2329 
2330 /*
2331  * _regmap_raw_multi_reg_write()
2332  *
2333  * the (register,newvalue) pairs in regs have not been formatted, but
2334  * they are all in the same page and have been changed to being page
2335  * relative. The page register has been written if that was necessary.
2336  */
2337 static int _regmap_raw_multi_reg_write(struct regmap *map,
2338 				       const struct reg_sequence *regs,
2339 				       size_t num_regs)
2340 {
2341 	int ret;
2342 	void *buf;
2343 	int i;
2344 	u8 *u8;
2345 	size_t val_bytes = map->format.val_bytes;
2346 	size_t reg_bytes = map->format.reg_bytes;
2347 	size_t pad_bytes = map->format.pad_bytes;
2348 	size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2349 	size_t len = pair_size * num_regs;
2350 
2351 	if (!len)
2352 		return -EINVAL;
2353 
2354 	buf = kzalloc(len, GFP_KERNEL);
2355 	if (!buf)
2356 		return -ENOMEM;
2357 
2358 	/* We have to linearise by hand. */
2359 
2360 	u8 = buf;
2361 
2362 	for (i = 0; i < num_regs; i++) {
2363 		unsigned int reg = regs[i].reg;
2364 		unsigned int val = regs[i].def;
2365 		trace_regmap_hw_write_start(map, reg, 1);
2366 		reg += map->reg_base;
2367 		reg >>= map->format.reg_downshift;
2368 		map->format.format_reg(u8, reg, map->reg_shift);
2369 		u8 += reg_bytes + pad_bytes;
2370 		map->format.format_val(u8, val, 0);
2371 		u8 += val_bytes;
2372 	}
2373 	u8 = buf;
2374 	*u8 |= map->write_flag_mask;
2375 
2376 	ret = map->write(map->bus_context, buf, len);
2377 
2378 	kfree(buf);
2379 
2380 	for (i = 0; i < num_regs; i++) {
2381 		int reg = regs[i].reg;
2382 		trace_regmap_hw_write_done(map, reg, 1);
2383 	}
2384 	return ret;
2385 }
2386 
2387 static unsigned int _regmap_register_page(struct regmap *map,
2388 					  unsigned int reg,
2389 					  struct regmap_range_node *range)
2390 {
2391 	unsigned int win_page = (reg - range->range_min) / range->window_len;
2392 
2393 	return win_page;
2394 }
2395 
2396 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2397 					       struct reg_sequence *regs,
2398 					       size_t num_regs)
2399 {
2400 	int ret;
2401 	int i, n;
2402 	struct reg_sequence *base;
2403 	unsigned int this_page = 0;
2404 	unsigned int page_change = 0;
2405 	/*
2406 	 * the set of registers are not neccessarily in order, but
2407 	 * since the order of write must be preserved this algorithm
2408 	 * chops the set each time the page changes. This also applies
2409 	 * if there is a delay required at any point in the sequence.
2410 	 */
2411 	base = regs;
2412 	for (i = 0, n = 0; i < num_regs; i++, n++) {
2413 		unsigned int reg = regs[i].reg;
2414 		struct regmap_range_node *range;
2415 
2416 		range = _regmap_range_lookup(map, reg);
2417 		if (range) {
2418 			unsigned int win_page = _regmap_register_page(map, reg,
2419 								      range);
2420 
2421 			if (i == 0)
2422 				this_page = win_page;
2423 			if (win_page != this_page) {
2424 				this_page = win_page;
2425 				page_change = 1;
2426 			}
2427 		}
2428 
2429 		/* If we have both a page change and a delay make sure to
2430 		 * write the regs and apply the delay before we change the
2431 		 * page.
2432 		 */
2433 
2434 		if (page_change || regs[i].delay_us) {
2435 
2436 				/* For situations where the first write requires
2437 				 * a delay we need to make sure we don't call
2438 				 * raw_multi_reg_write with n=0
2439 				 * This can't occur with page breaks as we
2440 				 * never write on the first iteration
2441 				 */
2442 				if (regs[i].delay_us && i == 0)
2443 					n = 1;
2444 
2445 				ret = _regmap_raw_multi_reg_write(map, base, n);
2446 				if (ret != 0)
2447 					return ret;
2448 
2449 				if (regs[i].delay_us) {
2450 					if (map->can_sleep)
2451 						fsleep(regs[i].delay_us);
2452 					else
2453 						udelay(regs[i].delay_us);
2454 				}
2455 
2456 				base += n;
2457 				n = 0;
2458 
2459 				if (page_change) {
2460 					ret = _regmap_select_page(map,
2461 								  &base[n].reg,
2462 								  range, 1);
2463 					if (ret != 0)
2464 						return ret;
2465 
2466 					page_change = 0;
2467 				}
2468 
2469 		}
2470 
2471 	}
2472 	if (n > 0)
2473 		return _regmap_raw_multi_reg_write(map, base, n);
2474 	return 0;
2475 }
2476 
2477 static int _regmap_multi_reg_write(struct regmap *map,
2478 				   const struct reg_sequence *regs,
2479 				   size_t num_regs)
2480 {
2481 	int i;
2482 	int ret;
2483 
2484 	if (!map->can_multi_write) {
2485 		for (i = 0; i < num_regs; i++) {
2486 			ret = _regmap_write(map, regs[i].reg, regs[i].def);
2487 			if (ret != 0)
2488 				return ret;
2489 
2490 			if (regs[i].delay_us) {
2491 				if (map->can_sleep)
2492 					fsleep(regs[i].delay_us);
2493 				else
2494 					udelay(regs[i].delay_us);
2495 			}
2496 		}
2497 		return 0;
2498 	}
2499 
2500 	if (!map->format.parse_inplace)
2501 		return -EINVAL;
2502 
2503 	if (map->writeable_reg)
2504 		for (i = 0; i < num_regs; i++) {
2505 			int reg = regs[i].reg;
2506 			if (!map->writeable_reg(map->dev, reg))
2507 				return -EINVAL;
2508 			if (!IS_ALIGNED(reg, map->reg_stride))
2509 				return -EINVAL;
2510 		}
2511 
2512 	if (!map->cache_bypass) {
2513 		for (i = 0; i < num_regs; i++) {
2514 			unsigned int val = regs[i].def;
2515 			unsigned int reg = regs[i].reg;
2516 			ret = regcache_write(map, reg, val);
2517 			if (ret) {
2518 				dev_err(map->dev,
2519 				"Error in caching of register: %x ret: %d\n",
2520 								reg, ret);
2521 				return ret;
2522 			}
2523 		}
2524 		if (map->cache_only) {
2525 			map->cache_dirty = true;
2526 			return 0;
2527 		}
2528 	}
2529 
2530 	WARN_ON(!map->bus);
2531 
2532 	for (i = 0; i < num_regs; i++) {
2533 		unsigned int reg = regs[i].reg;
2534 		struct regmap_range_node *range;
2535 
2536 		/* Coalesce all the writes between a page break or a delay
2537 		 * in a sequence
2538 		 */
2539 		range = _regmap_range_lookup(map, reg);
2540 		if (range || regs[i].delay_us) {
2541 			size_t len = sizeof(struct reg_sequence)*num_regs;
2542 			struct reg_sequence *base = kmemdup(regs, len,
2543 							   GFP_KERNEL);
2544 			if (!base)
2545 				return -ENOMEM;
2546 			ret = _regmap_range_multi_paged_reg_write(map, base,
2547 								  num_regs);
2548 			kfree(base);
2549 
2550 			return ret;
2551 		}
2552 	}
2553 	return _regmap_raw_multi_reg_write(map, regs, num_regs);
2554 }
2555 
2556 /**
2557  * regmap_multi_reg_write() - Write multiple registers to the device
2558  *
2559  * @map: Register map to write to
2560  * @regs: Array of structures containing register,value to be written
2561  * @num_regs: Number of registers to write
2562  *
2563  * Write multiple registers to the device where the set of register, value
2564  * pairs are supplied in any order, possibly not all in a single range.
2565  *
2566  * The 'normal' block write mode will send ultimately send data on the
2567  * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2568  * addressed. However, this alternative block multi write mode will send
2569  * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2570  * must of course support the mode.
2571  *
2572  * A value of zero will be returned on success, a negative errno will be
2573  * returned in error cases.
2574  */
2575 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2576 			   int num_regs)
2577 {
2578 	int ret;
2579 
2580 	map->lock(map->lock_arg);
2581 
2582 	ret = _regmap_multi_reg_write(map, regs, num_regs);
2583 
2584 	map->unlock(map->lock_arg);
2585 
2586 	return ret;
2587 }
2588 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2589 
2590 /**
2591  * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2592  *                                     device but not the cache
2593  *
2594  * @map: Register map to write to
2595  * @regs: Array of structures containing register,value to be written
2596  * @num_regs: Number of registers to write
2597  *
2598  * Write multiple registers to the device but not the cache where the set
2599  * of register are supplied in any order.
2600  *
2601  * This function is intended to be used for writing a large block of data
2602  * atomically to the device in single transfer for those I2C client devices
2603  * that implement this alternative block write mode.
2604  *
2605  * A value of zero will be returned on success, a negative errno will
2606  * be returned in error cases.
2607  */
2608 int regmap_multi_reg_write_bypassed(struct regmap *map,
2609 				    const struct reg_sequence *regs,
2610 				    int num_regs)
2611 {
2612 	int ret;
2613 	bool bypass;
2614 
2615 	map->lock(map->lock_arg);
2616 
2617 	bypass = map->cache_bypass;
2618 	map->cache_bypass = true;
2619 
2620 	ret = _regmap_multi_reg_write(map, regs, num_regs);
2621 
2622 	map->cache_bypass = bypass;
2623 
2624 	map->unlock(map->lock_arg);
2625 
2626 	return ret;
2627 }
2628 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2629 
2630 /**
2631  * regmap_raw_write_async() - Write raw values to one or more registers
2632  *                            asynchronously
2633  *
2634  * @map: Register map to write to
2635  * @reg: Initial register to write to
2636  * @val: Block of data to be written, laid out for direct transmission to the
2637  *       device.  Must be valid until regmap_async_complete() is called.
2638  * @val_len: Length of data pointed to by val.
2639  *
2640  * This function is intended to be used for things like firmware
2641  * download where a large block of data needs to be transferred to the
2642  * device.  No formatting will be done on the data provided.
2643  *
2644  * If supported by the underlying bus the write will be scheduled
2645  * asynchronously, helping maximise I/O speed on higher speed buses
2646  * like SPI.  regmap_async_complete() can be called to ensure that all
2647  * asynchrnous writes have been completed.
2648  *
2649  * A value of zero will be returned on success, a negative errno will
2650  * be returned in error cases.
2651  */
2652 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2653 			   const void *val, size_t val_len)
2654 {
2655 	int ret;
2656 
2657 	if (val_len % map->format.val_bytes)
2658 		return -EINVAL;
2659 	if (!IS_ALIGNED(reg, map->reg_stride))
2660 		return -EINVAL;
2661 
2662 	map->lock(map->lock_arg);
2663 
2664 	map->async = true;
2665 
2666 	ret = _regmap_raw_write(map, reg, val, val_len, false);
2667 
2668 	map->async = false;
2669 
2670 	map->unlock(map->lock_arg);
2671 
2672 	return ret;
2673 }
2674 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2675 
2676 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2677 			    unsigned int val_len, bool noinc)
2678 {
2679 	struct regmap_range_node *range;
2680 	int ret;
2681 
2682 	if (!map->read)
2683 		return -EINVAL;
2684 
2685 	range = _regmap_range_lookup(map, reg);
2686 	if (range) {
2687 		ret = _regmap_select_page(map, &reg, range,
2688 					  noinc ? 1 : val_len / map->format.val_bytes);
2689 		if (ret != 0)
2690 			return ret;
2691 	}
2692 
2693 	reg += map->reg_base;
2694 	reg >>= map->format.reg_downshift;
2695 	map->format.format_reg(map->work_buf, reg, map->reg_shift);
2696 	regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2697 				      map->read_flag_mask);
2698 	trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2699 
2700 	ret = map->read(map->bus_context, map->work_buf,
2701 			map->format.reg_bytes + map->format.pad_bytes,
2702 			val, val_len);
2703 
2704 	trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2705 
2706 	return ret;
2707 }
2708 
2709 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2710 				unsigned int *val)
2711 {
2712 	struct regmap *map = context;
2713 
2714 	return map->bus->reg_read(map->bus_context, reg, val);
2715 }
2716 
2717 static int _regmap_bus_read(void *context, unsigned int reg,
2718 			    unsigned int *val)
2719 {
2720 	int ret;
2721 	struct regmap *map = context;
2722 	void *work_val = map->work_buf + map->format.reg_bytes +
2723 		map->format.pad_bytes;
2724 
2725 	if (!map->format.parse_val)
2726 		return -EINVAL;
2727 
2728 	ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
2729 	if (ret == 0)
2730 		*val = map->format.parse_val(work_val);
2731 
2732 	return ret;
2733 }
2734 
2735 static int _regmap_read(struct regmap *map, unsigned int reg,
2736 			unsigned int *val)
2737 {
2738 	int ret;
2739 	void *context = _regmap_map_get_context(map);
2740 
2741 	if (!map->cache_bypass) {
2742 		ret = regcache_read(map, reg, val);
2743 		if (ret == 0)
2744 			return 0;
2745 	}
2746 
2747 	if (map->cache_only)
2748 		return -EBUSY;
2749 
2750 	if (!regmap_readable(map, reg))
2751 		return -EIO;
2752 
2753 	ret = map->reg_read(context, reg, val);
2754 	if (ret == 0) {
2755 		if (regmap_should_log(map))
2756 			dev_info(map->dev, "%x => %x\n", reg, *val);
2757 
2758 		trace_regmap_reg_read(map, reg, *val);
2759 
2760 		if (!map->cache_bypass)
2761 			regcache_write(map, reg, *val);
2762 	}
2763 
2764 	return ret;
2765 }
2766 
2767 /**
2768  * regmap_read() - Read a value from a single register
2769  *
2770  * @map: Register map to read from
2771  * @reg: Register to be read from
2772  * @val: Pointer to store read value
2773  *
2774  * A value of zero will be returned on success, a negative errno will
2775  * be returned in error cases.
2776  */
2777 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2778 {
2779 	int ret;
2780 
2781 	if (!IS_ALIGNED(reg, map->reg_stride))
2782 		return -EINVAL;
2783 
2784 	map->lock(map->lock_arg);
2785 
2786 	ret = _regmap_read(map, reg, val);
2787 
2788 	map->unlock(map->lock_arg);
2789 
2790 	return ret;
2791 }
2792 EXPORT_SYMBOL_GPL(regmap_read);
2793 
2794 /**
2795  * regmap_raw_read() - Read raw data from the device
2796  *
2797  * @map: Register map to read from
2798  * @reg: First register to be read from
2799  * @val: Pointer to store read value
2800  * @val_len: Size of data to read
2801  *
2802  * A value of zero will be returned on success, a negative errno will
2803  * be returned in error cases.
2804  */
2805 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2806 		    size_t val_len)
2807 {
2808 	size_t val_bytes = map->format.val_bytes;
2809 	size_t val_count = val_len / val_bytes;
2810 	unsigned int v;
2811 	int ret, i;
2812 
2813 	if (val_len % map->format.val_bytes)
2814 		return -EINVAL;
2815 	if (!IS_ALIGNED(reg, map->reg_stride))
2816 		return -EINVAL;
2817 	if (val_count == 0)
2818 		return -EINVAL;
2819 
2820 	map->lock(map->lock_arg);
2821 
2822 	if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2823 	    map->cache_type == REGCACHE_NONE) {
2824 		size_t chunk_count, chunk_bytes;
2825 		size_t chunk_regs = val_count;
2826 
2827 		if (!map->read) {
2828 			ret = -ENOTSUPP;
2829 			goto out;
2830 		}
2831 
2832 		if (map->use_single_read)
2833 			chunk_regs = 1;
2834 		else if (map->max_raw_read && val_len > map->max_raw_read)
2835 			chunk_regs = map->max_raw_read / val_bytes;
2836 
2837 		chunk_count = val_count / chunk_regs;
2838 		chunk_bytes = chunk_regs * val_bytes;
2839 
2840 		/* Read bytes that fit into whole chunks */
2841 		for (i = 0; i < chunk_count; i++) {
2842 			ret = _regmap_raw_read(map, reg, val, chunk_bytes, false);
2843 			if (ret != 0)
2844 				goto out;
2845 
2846 			reg += regmap_get_offset(map, chunk_regs);
2847 			val += chunk_bytes;
2848 			val_len -= chunk_bytes;
2849 		}
2850 
2851 		/* Read remaining bytes */
2852 		if (val_len) {
2853 			ret = _regmap_raw_read(map, reg, val, val_len, false);
2854 			if (ret != 0)
2855 				goto out;
2856 		}
2857 	} else {
2858 		/* Otherwise go word by word for the cache; should be low
2859 		 * cost as we expect to hit the cache.
2860 		 */
2861 		for (i = 0; i < val_count; i++) {
2862 			ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2863 					   &v);
2864 			if (ret != 0)
2865 				goto out;
2866 
2867 			map->format.format_val(val + (i * val_bytes), v, 0);
2868 		}
2869 	}
2870 
2871  out:
2872 	map->unlock(map->lock_arg);
2873 
2874 	return ret;
2875 }
2876 EXPORT_SYMBOL_GPL(regmap_raw_read);
2877 
2878 /**
2879  * regmap_noinc_read(): Read data from a register without incrementing the
2880  *			register number
2881  *
2882  * @map: Register map to read from
2883  * @reg: Register to read from
2884  * @val: Pointer to data buffer
2885  * @val_len: Length of output buffer in bytes.
2886  *
2887  * The regmap API usually assumes that bulk read operations will read a
2888  * range of registers. Some devices have certain registers for which a read
2889  * operation read will read from an internal FIFO.
2890  *
2891  * The target register must be volatile but registers after it can be
2892  * completely unrelated cacheable registers.
2893  *
2894  * This will attempt multiple reads as required to read val_len bytes.
2895  *
2896  * A value of zero will be returned on success, a negative errno will be
2897  * returned in error cases.
2898  */
2899 int regmap_noinc_read(struct regmap *map, unsigned int reg,
2900 		      void *val, size_t val_len)
2901 {
2902 	size_t read_len;
2903 	int ret;
2904 
2905 	if (!map->read)
2906 		return -ENOTSUPP;
2907 
2908 	if (val_len % map->format.val_bytes)
2909 		return -EINVAL;
2910 	if (!IS_ALIGNED(reg, map->reg_stride))
2911 		return -EINVAL;
2912 	if (val_len == 0)
2913 		return -EINVAL;
2914 
2915 	map->lock(map->lock_arg);
2916 
2917 	if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
2918 		ret = -EINVAL;
2919 		goto out_unlock;
2920 	}
2921 
2922 	while (val_len) {
2923 		if (map->max_raw_read && map->max_raw_read < val_len)
2924 			read_len = map->max_raw_read;
2925 		else
2926 			read_len = val_len;
2927 		ret = _regmap_raw_read(map, reg, val, read_len, true);
2928 		if (ret)
2929 			goto out_unlock;
2930 		val = ((u8 *)val) + read_len;
2931 		val_len -= read_len;
2932 	}
2933 
2934 out_unlock:
2935 	map->unlock(map->lock_arg);
2936 	return ret;
2937 }
2938 EXPORT_SYMBOL_GPL(regmap_noinc_read);
2939 
2940 /**
2941  * regmap_field_read(): Read a value to a single register field
2942  *
2943  * @field: Register field to read from
2944  * @val: Pointer to store read value
2945  *
2946  * A value of zero will be returned on success, a negative errno will
2947  * be returned in error cases.
2948  */
2949 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2950 {
2951 	int ret;
2952 	unsigned int reg_val;
2953 	ret = regmap_read(field->regmap, field->reg, &reg_val);
2954 	if (ret != 0)
2955 		return ret;
2956 
2957 	reg_val &= field->mask;
2958 	reg_val >>= field->shift;
2959 	*val = reg_val;
2960 
2961 	return ret;
2962 }
2963 EXPORT_SYMBOL_GPL(regmap_field_read);
2964 
2965 /**
2966  * regmap_fields_read() - Read a value to a single register field with port ID
2967  *
2968  * @field: Register field to read from
2969  * @id: port ID
2970  * @val: Pointer to store read value
2971  *
2972  * A value of zero will be returned on success, a negative errno will
2973  * be returned in error cases.
2974  */
2975 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2976 		       unsigned int *val)
2977 {
2978 	int ret;
2979 	unsigned int reg_val;
2980 
2981 	if (id >= field->id_size)
2982 		return -EINVAL;
2983 
2984 	ret = regmap_read(field->regmap,
2985 			  field->reg + (field->id_offset * id),
2986 			  &reg_val);
2987 	if (ret != 0)
2988 		return ret;
2989 
2990 	reg_val &= field->mask;
2991 	reg_val >>= field->shift;
2992 	*val = reg_val;
2993 
2994 	return ret;
2995 }
2996 EXPORT_SYMBOL_GPL(regmap_fields_read);
2997 
2998 /**
2999  * regmap_bulk_read() - Read multiple registers from the device
3000  *
3001  * @map: Register map to read from
3002  * @reg: First register to be read from
3003  * @val: Pointer to store read value, in native register size for device
3004  * @val_count: Number of registers to read
3005  *
3006  * A value of zero will be returned on success, a negative errno will
3007  * be returned in error cases.
3008  */
3009 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
3010 		     size_t val_count)
3011 {
3012 	int ret, i;
3013 	size_t val_bytes = map->format.val_bytes;
3014 	bool vol = regmap_volatile_range(map, reg, val_count);
3015 
3016 	if (!IS_ALIGNED(reg, map->reg_stride))
3017 		return -EINVAL;
3018 	if (val_count == 0)
3019 		return -EINVAL;
3020 
3021 	if (map->read && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
3022 		ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
3023 		if (ret != 0)
3024 			return ret;
3025 
3026 		for (i = 0; i < val_count * val_bytes; i += val_bytes)
3027 			map->format.parse_inplace(val + i);
3028 	} else {
3029 #ifdef CONFIG_64BIT
3030 		u64 *u64 = val;
3031 #endif
3032 		u32 *u32 = val;
3033 		u16 *u16 = val;
3034 		u8 *u8 = val;
3035 
3036 		map->lock(map->lock_arg);
3037 
3038 		for (i = 0; i < val_count; i++) {
3039 			unsigned int ival;
3040 
3041 			ret = _regmap_read(map, reg + regmap_get_offset(map, i),
3042 					   &ival);
3043 			if (ret != 0)
3044 				goto out;
3045 
3046 			switch (map->format.val_bytes) {
3047 #ifdef CONFIG_64BIT
3048 			case 8:
3049 				u64[i] = ival;
3050 				break;
3051 #endif
3052 			case 4:
3053 				u32[i] = ival;
3054 				break;
3055 			case 2:
3056 				u16[i] = ival;
3057 				break;
3058 			case 1:
3059 				u8[i] = ival;
3060 				break;
3061 			default:
3062 				ret = -EINVAL;
3063 				goto out;
3064 			}
3065 		}
3066 
3067 out:
3068 		map->unlock(map->lock_arg);
3069 	}
3070 
3071 	return ret;
3072 }
3073 EXPORT_SYMBOL_GPL(regmap_bulk_read);
3074 
3075 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
3076 			       unsigned int mask, unsigned int val,
3077 			       bool *change, bool force_write)
3078 {
3079 	int ret;
3080 	unsigned int tmp, orig;
3081 
3082 	if (change)
3083 		*change = false;
3084 
3085 	if (regmap_volatile(map, reg) && map->reg_update_bits) {
3086 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
3087 		if (ret == 0 && change)
3088 			*change = true;
3089 	} else {
3090 		ret = _regmap_read(map, reg, &orig);
3091 		if (ret != 0)
3092 			return ret;
3093 
3094 		tmp = orig & ~mask;
3095 		tmp |= val & mask;
3096 
3097 		if (force_write || (tmp != orig)) {
3098 			ret = _regmap_write(map, reg, tmp);
3099 			if (ret == 0 && change)
3100 				*change = true;
3101 		}
3102 	}
3103 
3104 	return ret;
3105 }
3106 
3107 /**
3108  * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
3109  *
3110  * @map: Register map to update
3111  * @reg: Register to update
3112  * @mask: Bitmask to change
3113  * @val: New value for bitmask
3114  * @change: Boolean indicating if a write was done
3115  * @async: Boolean indicating asynchronously
3116  * @force: Boolean indicating use force update
3117  *
3118  * Perform a read/modify/write cycle on a register map with change, async, force
3119  * options.
3120  *
3121  * If async is true:
3122  *
3123  * With most buses the read must be done synchronously so this is most useful
3124  * for devices with a cache which do not need to interact with the hardware to
3125  * determine the current register value.
3126  *
3127  * Returns zero for success, a negative number on error.
3128  */
3129 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
3130 			    unsigned int mask, unsigned int val,
3131 			    bool *change, bool async, bool force)
3132 {
3133 	int ret;
3134 
3135 	map->lock(map->lock_arg);
3136 
3137 	map->async = async;
3138 
3139 	ret = _regmap_update_bits(map, reg, mask, val, change, force);
3140 
3141 	map->async = false;
3142 
3143 	map->unlock(map->lock_arg);
3144 
3145 	return ret;
3146 }
3147 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
3148 
3149 /**
3150  * regmap_test_bits() - Check if all specified bits are set in a register.
3151  *
3152  * @map: Register map to operate on
3153  * @reg: Register to read from
3154  * @bits: Bits to test
3155  *
3156  * Returns 0 if at least one of the tested bits is not set, 1 if all tested
3157  * bits are set and a negative error number if the underlying regmap_read()
3158  * fails.
3159  */
3160 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits)
3161 {
3162 	unsigned int val, ret;
3163 
3164 	ret = regmap_read(map, reg, &val);
3165 	if (ret)
3166 		return ret;
3167 
3168 	return (val & bits) == bits;
3169 }
3170 EXPORT_SYMBOL_GPL(regmap_test_bits);
3171 
3172 void regmap_async_complete_cb(struct regmap_async *async, int ret)
3173 {
3174 	struct regmap *map = async->map;
3175 	bool wake;
3176 
3177 	trace_regmap_async_io_complete(map);
3178 
3179 	spin_lock(&map->async_lock);
3180 	list_move(&async->list, &map->async_free);
3181 	wake = list_empty(&map->async_list);
3182 
3183 	if (ret != 0)
3184 		map->async_ret = ret;
3185 
3186 	spin_unlock(&map->async_lock);
3187 
3188 	if (wake)
3189 		wake_up(&map->async_waitq);
3190 }
3191 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
3192 
3193 static int regmap_async_is_done(struct regmap *map)
3194 {
3195 	unsigned long flags;
3196 	int ret;
3197 
3198 	spin_lock_irqsave(&map->async_lock, flags);
3199 	ret = list_empty(&map->async_list);
3200 	spin_unlock_irqrestore(&map->async_lock, flags);
3201 
3202 	return ret;
3203 }
3204 
3205 /**
3206  * regmap_async_complete - Ensure all asynchronous I/O has completed.
3207  *
3208  * @map: Map to operate on.
3209  *
3210  * Blocks until any pending asynchronous I/O has completed.  Returns
3211  * an error code for any failed I/O operations.
3212  */
3213 int regmap_async_complete(struct regmap *map)
3214 {
3215 	unsigned long flags;
3216 	int ret;
3217 
3218 	/* Nothing to do with no async support */
3219 	if (!map->bus || !map->bus->async_write)
3220 		return 0;
3221 
3222 	trace_regmap_async_complete_start(map);
3223 
3224 	wait_event(map->async_waitq, regmap_async_is_done(map));
3225 
3226 	spin_lock_irqsave(&map->async_lock, flags);
3227 	ret = map->async_ret;
3228 	map->async_ret = 0;
3229 	spin_unlock_irqrestore(&map->async_lock, flags);
3230 
3231 	trace_regmap_async_complete_done(map);
3232 
3233 	return ret;
3234 }
3235 EXPORT_SYMBOL_GPL(regmap_async_complete);
3236 
3237 /**
3238  * regmap_register_patch - Register and apply register updates to be applied
3239  *                         on device initialistion
3240  *
3241  * @map: Register map to apply updates to.
3242  * @regs: Values to update.
3243  * @num_regs: Number of entries in regs.
3244  *
3245  * Register a set of register updates to be applied to the device
3246  * whenever the device registers are synchronised with the cache and
3247  * apply them immediately.  Typically this is used to apply
3248  * corrections to be applied to the device defaults on startup, such
3249  * as the updates some vendors provide to undocumented registers.
3250  *
3251  * The caller must ensure that this function cannot be called
3252  * concurrently with either itself or regcache_sync().
3253  */
3254 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
3255 			  int num_regs)
3256 {
3257 	struct reg_sequence *p;
3258 	int ret;
3259 	bool bypass;
3260 
3261 	if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
3262 	    num_regs))
3263 		return 0;
3264 
3265 	p = krealloc(map->patch,
3266 		     sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
3267 		     GFP_KERNEL);
3268 	if (p) {
3269 		memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
3270 		map->patch = p;
3271 		map->patch_regs += num_regs;
3272 	} else {
3273 		return -ENOMEM;
3274 	}
3275 
3276 	map->lock(map->lock_arg);
3277 
3278 	bypass = map->cache_bypass;
3279 
3280 	map->cache_bypass = true;
3281 	map->async = true;
3282 
3283 	ret = _regmap_multi_reg_write(map, regs, num_regs);
3284 
3285 	map->async = false;
3286 	map->cache_bypass = bypass;
3287 
3288 	map->unlock(map->lock_arg);
3289 
3290 	regmap_async_complete(map);
3291 
3292 	return ret;
3293 }
3294 EXPORT_SYMBOL_GPL(regmap_register_patch);
3295 
3296 /**
3297  * regmap_get_val_bytes() - Report the size of a register value
3298  *
3299  * @map: Register map to operate on.
3300  *
3301  * Report the size of a register value, mainly intended to for use by
3302  * generic infrastructure built on top of regmap.
3303  */
3304 int regmap_get_val_bytes(struct regmap *map)
3305 {
3306 	if (map->format.format_write)
3307 		return -EINVAL;
3308 
3309 	return map->format.val_bytes;
3310 }
3311 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
3312 
3313 /**
3314  * regmap_get_max_register() - Report the max register value
3315  *
3316  * @map: Register map to operate on.
3317  *
3318  * Report the max register value, mainly intended to for use by
3319  * generic infrastructure built on top of regmap.
3320  */
3321 int regmap_get_max_register(struct regmap *map)
3322 {
3323 	return map->max_register ? map->max_register : -EINVAL;
3324 }
3325 EXPORT_SYMBOL_GPL(regmap_get_max_register);
3326 
3327 /**
3328  * regmap_get_reg_stride() - Report the register address stride
3329  *
3330  * @map: Register map to operate on.
3331  *
3332  * Report the register address stride, mainly intended to for use by
3333  * generic infrastructure built on top of regmap.
3334  */
3335 int regmap_get_reg_stride(struct regmap *map)
3336 {
3337 	return map->reg_stride;
3338 }
3339 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
3340 
3341 int regmap_parse_val(struct regmap *map, const void *buf,
3342 			unsigned int *val)
3343 {
3344 	if (!map->format.parse_val)
3345 		return -EINVAL;
3346 
3347 	*val = map->format.parse_val(buf);
3348 
3349 	return 0;
3350 }
3351 EXPORT_SYMBOL_GPL(regmap_parse_val);
3352 
3353 static int __init regmap_initcall(void)
3354 {
3355 	regmap_debugfs_initcall();
3356 
3357 	return 0;
3358 }
3359 postcore_initcall(regmap_initcall);
3360