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