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