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