xref: /openbmc/linux/drivers/base/regmap/regmap.c (revision c0cc6fe1d09e3f1baecbdf8922473c8e7d3a5317)
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 
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
21 
22 #include "internal.h"
23 
24 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 {
26 	if (map->max_register && reg > map->max_register)
27 		return false;
28 
29 	if (map->writeable_reg)
30 		return map->writeable_reg(map->dev, reg);
31 
32 	return true;
33 }
34 
35 bool regmap_readable(struct regmap *map, unsigned int reg)
36 {
37 	if (map->max_register && reg > map->max_register)
38 		return false;
39 
40 	if (map->format.format_write)
41 		return false;
42 
43 	if (map->readable_reg)
44 		return map->readable_reg(map->dev, reg);
45 
46 	return true;
47 }
48 
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
50 {
51 	if (!regmap_readable(map, reg))
52 		return false;
53 
54 	if (map->volatile_reg)
55 		return map->volatile_reg(map->dev, reg);
56 
57 	return true;
58 }
59 
60 bool regmap_precious(struct regmap *map, unsigned int reg)
61 {
62 	if (!regmap_readable(map, reg))
63 		return false;
64 
65 	if (map->precious_reg)
66 		return map->precious_reg(map->dev, reg);
67 
68 	return false;
69 }
70 
71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 	unsigned int num)
73 {
74 	unsigned int i;
75 
76 	for (i = 0; i < num; i++)
77 		if (!regmap_volatile(map, reg + i))
78 			return false;
79 
80 	return true;
81 }
82 
83 static void regmap_format_2_6_write(struct regmap *map,
84 				     unsigned int reg, unsigned int val)
85 {
86 	u8 *out = map->work_buf;
87 
88 	*out = (reg << 6) | val;
89 }
90 
91 static void regmap_format_4_12_write(struct regmap *map,
92 				     unsigned int reg, unsigned int val)
93 {
94 	__be16 *out = map->work_buf;
95 	*out = cpu_to_be16((reg << 12) | val);
96 }
97 
98 static void regmap_format_7_9_write(struct regmap *map,
99 				    unsigned int reg, unsigned int val)
100 {
101 	__be16 *out = map->work_buf;
102 	*out = cpu_to_be16((reg << 9) | val);
103 }
104 
105 static void regmap_format_10_14_write(struct regmap *map,
106 				    unsigned int reg, unsigned int val)
107 {
108 	u8 *out = map->work_buf;
109 
110 	out[2] = val;
111 	out[1] = (val >> 8) | (reg << 6);
112 	out[0] = reg >> 2;
113 }
114 
115 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
116 {
117 	u8 *b = buf;
118 
119 	b[0] = val << shift;
120 }
121 
122 static void regmap_format_16(void *buf, unsigned int val, unsigned int shift)
123 {
124 	__be16 *b = buf;
125 
126 	b[0] = cpu_to_be16(val << shift);
127 }
128 
129 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
130 {
131 	u8 *b = buf;
132 
133 	val <<= shift;
134 
135 	b[0] = val >> 16;
136 	b[1] = val >> 8;
137 	b[2] = val;
138 }
139 
140 static void regmap_format_32(void *buf, unsigned int val, unsigned int shift)
141 {
142 	__be32 *b = buf;
143 
144 	b[0] = cpu_to_be32(val << shift);
145 }
146 
147 static unsigned int regmap_parse_8(void *buf)
148 {
149 	u8 *b = buf;
150 
151 	return b[0];
152 }
153 
154 static unsigned int regmap_parse_16(void *buf)
155 {
156 	__be16 *b = buf;
157 
158 	b[0] = be16_to_cpu(b[0]);
159 
160 	return b[0];
161 }
162 
163 static unsigned int regmap_parse_24(void *buf)
164 {
165 	u8 *b = buf;
166 	unsigned int ret = b[2];
167 	ret |= ((unsigned int)b[1]) << 8;
168 	ret |= ((unsigned int)b[0]) << 16;
169 
170 	return ret;
171 }
172 
173 static unsigned int regmap_parse_32(void *buf)
174 {
175 	__be32 *b = buf;
176 
177 	b[0] = be32_to_cpu(b[0]);
178 
179 	return b[0];
180 }
181 
182 static void regmap_lock_mutex(struct regmap *map)
183 {
184 	mutex_lock(&map->mutex);
185 }
186 
187 static void regmap_unlock_mutex(struct regmap *map)
188 {
189 	mutex_unlock(&map->mutex);
190 }
191 
192 static void regmap_lock_spinlock(struct regmap *map)
193 {
194 	spin_lock(&map->spinlock);
195 }
196 
197 static void regmap_unlock_spinlock(struct regmap *map)
198 {
199 	spin_unlock(&map->spinlock);
200 }
201 
202 /**
203  * regmap_init(): Initialise register map
204  *
205  * @dev: Device that will be interacted with
206  * @bus: Bus-specific callbacks to use with device
207  * @bus_context: Data passed to bus-specific callbacks
208  * @config: Configuration for register map
209  *
210  * The return value will be an ERR_PTR() on error or a valid pointer to
211  * a struct regmap.  This function should generally not be called
212  * directly, it should be called by bus-specific init functions.
213  */
214 struct regmap *regmap_init(struct device *dev,
215 			   const struct regmap_bus *bus,
216 			   void *bus_context,
217 			   const struct regmap_config *config)
218 {
219 	struct regmap *map;
220 	int ret = -EINVAL;
221 
222 	if (!bus || !config)
223 		goto err;
224 
225 	map = kzalloc(sizeof(*map), GFP_KERNEL);
226 	if (map == NULL) {
227 		ret = -ENOMEM;
228 		goto err;
229 	}
230 
231 	if (bus->fast_io) {
232 		spin_lock_init(&map->spinlock);
233 		map->lock = regmap_lock_spinlock;
234 		map->unlock = regmap_unlock_spinlock;
235 	} else {
236 		mutex_init(&map->mutex);
237 		map->lock = regmap_lock_mutex;
238 		map->unlock = regmap_unlock_mutex;
239 	}
240 	map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
241 	map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
242 	map->format.pad_bytes = config->pad_bits / 8;
243 	map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
244 	map->format.buf_size += map->format.pad_bytes;
245 	map->reg_shift = config->pad_bits % 8;
246 	map->dev = dev;
247 	map->bus = bus;
248 	map->bus_context = bus_context;
249 	map->max_register = config->max_register;
250 	map->writeable_reg = config->writeable_reg;
251 	map->readable_reg = config->readable_reg;
252 	map->volatile_reg = config->volatile_reg;
253 	map->precious_reg = config->precious_reg;
254 	map->cache_type = config->cache_type;
255 
256 	if (config->read_flag_mask || config->write_flag_mask) {
257 		map->read_flag_mask = config->read_flag_mask;
258 		map->write_flag_mask = config->write_flag_mask;
259 	} else {
260 		map->read_flag_mask = bus->read_flag_mask;
261 	}
262 
263 	switch (config->reg_bits + map->reg_shift) {
264 	case 2:
265 		switch (config->val_bits) {
266 		case 6:
267 			map->format.format_write = regmap_format_2_6_write;
268 			break;
269 		default:
270 			goto err_map;
271 		}
272 		break;
273 
274 	case 4:
275 		switch (config->val_bits) {
276 		case 12:
277 			map->format.format_write = regmap_format_4_12_write;
278 			break;
279 		default:
280 			goto err_map;
281 		}
282 		break;
283 
284 	case 7:
285 		switch (config->val_bits) {
286 		case 9:
287 			map->format.format_write = regmap_format_7_9_write;
288 			break;
289 		default:
290 			goto err_map;
291 		}
292 		break;
293 
294 	case 10:
295 		switch (config->val_bits) {
296 		case 14:
297 			map->format.format_write = regmap_format_10_14_write;
298 			break;
299 		default:
300 			goto err_map;
301 		}
302 		break;
303 
304 	case 8:
305 		map->format.format_reg = regmap_format_8;
306 		break;
307 
308 	case 16:
309 		map->format.format_reg = regmap_format_16;
310 		break;
311 
312 	case 32:
313 		map->format.format_reg = regmap_format_32;
314 		break;
315 
316 	default:
317 		goto err_map;
318 	}
319 
320 	switch (config->val_bits) {
321 	case 8:
322 		map->format.format_val = regmap_format_8;
323 		map->format.parse_val = regmap_parse_8;
324 		break;
325 	case 16:
326 		map->format.format_val = regmap_format_16;
327 		map->format.parse_val = regmap_parse_16;
328 		break;
329 	case 24:
330 		map->format.format_val = regmap_format_24;
331 		map->format.parse_val = regmap_parse_24;
332 		break;
333 	case 32:
334 		map->format.format_val = regmap_format_32;
335 		map->format.parse_val = regmap_parse_32;
336 		break;
337 	}
338 
339 	if (!map->format.format_write &&
340 	    !(map->format.format_reg && map->format.format_val))
341 		goto err_map;
342 
343 	map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
344 	if (map->work_buf == NULL) {
345 		ret = -ENOMEM;
346 		goto err_map;
347 	}
348 
349 	regmap_debugfs_init(map, config->name);
350 
351 	ret = regcache_init(map, config);
352 	if (ret < 0)
353 		goto err_free_workbuf;
354 
355 	return map;
356 
357 err_free_workbuf:
358 	kfree(map->work_buf);
359 err_map:
360 	kfree(map);
361 err:
362 	return ERR_PTR(ret);
363 }
364 EXPORT_SYMBOL_GPL(regmap_init);
365 
366 static void devm_regmap_release(struct device *dev, void *res)
367 {
368 	regmap_exit(*(struct regmap **)res);
369 }
370 
371 /**
372  * devm_regmap_init(): Initialise managed register map
373  *
374  * @dev: Device that will be interacted with
375  * @bus: Bus-specific callbacks to use with device
376  * @bus_context: Data passed to bus-specific callbacks
377  * @config: Configuration for register map
378  *
379  * The return value will be an ERR_PTR() on error or a valid pointer
380  * to a struct regmap.  This function should generally not be called
381  * directly, it should be called by bus-specific init functions.  The
382  * map will be automatically freed by the device management code.
383  */
384 struct regmap *devm_regmap_init(struct device *dev,
385 				const struct regmap_bus *bus,
386 				void *bus_context,
387 				const struct regmap_config *config)
388 {
389 	struct regmap **ptr, *regmap;
390 
391 	ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
392 	if (!ptr)
393 		return ERR_PTR(-ENOMEM);
394 
395 	regmap = regmap_init(dev, bus, bus_context, config);
396 	if (!IS_ERR(regmap)) {
397 		*ptr = regmap;
398 		devres_add(dev, ptr);
399 	} else {
400 		devres_free(ptr);
401 	}
402 
403 	return regmap;
404 }
405 EXPORT_SYMBOL_GPL(devm_regmap_init);
406 
407 /**
408  * regmap_reinit_cache(): Reinitialise the current register cache
409  *
410  * @map: Register map to operate on.
411  * @config: New configuration.  Only the cache data will be used.
412  *
413  * Discard any existing register cache for the map and initialize a
414  * new cache.  This can be used to restore the cache to defaults or to
415  * update the cache configuration to reflect runtime discovery of the
416  * hardware.
417  */
418 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
419 {
420 	int ret;
421 
422 	map->lock(map);
423 
424 	regcache_exit(map);
425 	regmap_debugfs_exit(map);
426 
427 	map->max_register = config->max_register;
428 	map->writeable_reg = config->writeable_reg;
429 	map->readable_reg = config->readable_reg;
430 	map->volatile_reg = config->volatile_reg;
431 	map->precious_reg = config->precious_reg;
432 	map->cache_type = config->cache_type;
433 
434 	regmap_debugfs_init(map, config->name);
435 
436 	map->cache_bypass = false;
437 	map->cache_only = false;
438 
439 	ret = regcache_init(map, config);
440 
441 	map->unlock(map);
442 
443 	return ret;
444 }
445 
446 /**
447  * regmap_exit(): Free a previously allocated register map
448  */
449 void regmap_exit(struct regmap *map)
450 {
451 	regcache_exit(map);
452 	regmap_debugfs_exit(map);
453 	if (map->bus->free_context)
454 		map->bus->free_context(map->bus_context);
455 	kfree(map->work_buf);
456 	kfree(map);
457 }
458 EXPORT_SYMBOL_GPL(regmap_exit);
459 
460 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
461 			     const void *val, size_t val_len)
462 {
463 	u8 *u8 = map->work_buf;
464 	void *buf;
465 	int ret = -ENOTSUPP;
466 	size_t len;
467 	int i;
468 
469 	/* Check for unwritable registers before we start */
470 	if (map->writeable_reg)
471 		for (i = 0; i < val_len / map->format.val_bytes; i++)
472 			if (!map->writeable_reg(map->dev, reg + i))
473 				return -EINVAL;
474 
475 	if (!map->cache_bypass && map->format.parse_val) {
476 		unsigned int ival;
477 		int val_bytes = map->format.val_bytes;
478 		for (i = 0; i < val_len / val_bytes; i++) {
479 			memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
480 			ival = map->format.parse_val(map->work_buf);
481 			ret = regcache_write(map, reg + i, ival);
482 			if (ret) {
483 				dev_err(map->dev,
484 				   "Error in caching of register: %u ret: %d\n",
485 					reg + i, ret);
486 				return ret;
487 			}
488 		}
489 		if (map->cache_only) {
490 			map->cache_dirty = true;
491 			return 0;
492 		}
493 	}
494 
495 	map->format.format_reg(map->work_buf, reg, map->reg_shift);
496 
497 	u8[0] |= map->write_flag_mask;
498 
499 	trace_regmap_hw_write_start(map->dev, reg,
500 				    val_len / map->format.val_bytes);
501 
502 	/* If we're doing a single register write we can probably just
503 	 * send the work_buf directly, otherwise try to do a gather
504 	 * write.
505 	 */
506 	if (val == (map->work_buf + map->format.pad_bytes +
507 		    map->format.reg_bytes))
508 		ret = map->bus->write(map->bus_context, map->work_buf,
509 				      map->format.reg_bytes +
510 				      map->format.pad_bytes +
511 				      val_len);
512 	else if (map->bus->gather_write)
513 		ret = map->bus->gather_write(map->bus_context, map->work_buf,
514 					     map->format.reg_bytes +
515 					     map->format.pad_bytes,
516 					     val, val_len);
517 
518 	/* If that didn't work fall back on linearising by hand. */
519 	if (ret == -ENOTSUPP) {
520 		len = map->format.reg_bytes + map->format.pad_bytes + val_len;
521 		buf = kzalloc(len, GFP_KERNEL);
522 		if (!buf)
523 			return -ENOMEM;
524 
525 		memcpy(buf, map->work_buf, map->format.reg_bytes);
526 		memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
527 		       val, val_len);
528 		ret = map->bus->write(map->bus_context, buf, len);
529 
530 		kfree(buf);
531 	}
532 
533 	trace_regmap_hw_write_done(map->dev, reg,
534 				   val_len / map->format.val_bytes);
535 
536 	return ret;
537 }
538 
539 int _regmap_write(struct regmap *map, unsigned int reg,
540 		  unsigned int val)
541 {
542 	int ret;
543 	BUG_ON(!map->format.format_write && !map->format.format_val);
544 
545 	if (!map->cache_bypass && map->format.format_write) {
546 		ret = regcache_write(map, reg, val);
547 		if (ret != 0)
548 			return ret;
549 		if (map->cache_only) {
550 			map->cache_dirty = true;
551 			return 0;
552 		}
553 	}
554 
555 	trace_regmap_reg_write(map->dev, reg, val);
556 
557 	if (map->format.format_write) {
558 		map->format.format_write(map, reg, val);
559 
560 		trace_regmap_hw_write_start(map->dev, reg, 1);
561 
562 		ret = map->bus->write(map->bus_context, map->work_buf,
563 				      map->format.buf_size);
564 
565 		trace_regmap_hw_write_done(map->dev, reg, 1);
566 
567 		return ret;
568 	} else {
569 		map->format.format_val(map->work_buf + map->format.reg_bytes
570 				       + map->format.pad_bytes, val, 0);
571 		return _regmap_raw_write(map, reg,
572 					 map->work_buf +
573 					 map->format.reg_bytes +
574 					 map->format.pad_bytes,
575 					 map->format.val_bytes);
576 	}
577 }
578 
579 /**
580  * regmap_write(): Write a value to a single register
581  *
582  * @map: Register map to write to
583  * @reg: Register to write to
584  * @val: Value to be written
585  *
586  * A value of zero will be returned on success, a negative errno will
587  * be returned in error cases.
588  */
589 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
590 {
591 	int ret;
592 
593 	map->lock(map);
594 
595 	ret = _regmap_write(map, reg, val);
596 
597 	map->unlock(map);
598 
599 	return ret;
600 }
601 EXPORT_SYMBOL_GPL(regmap_write);
602 
603 /**
604  * regmap_raw_write(): Write raw values to one or more registers
605  *
606  * @map: Register map to write to
607  * @reg: Initial register to write to
608  * @val: Block of data to be written, laid out for direct transmission to the
609  *       device
610  * @val_len: Length of data pointed to by val.
611  *
612  * This function is intended to be used for things like firmware
613  * download where a large block of data needs to be transferred to the
614  * device.  No formatting will be done on the data provided.
615  *
616  * A value of zero will be returned on success, a negative errno will
617  * be returned in error cases.
618  */
619 int regmap_raw_write(struct regmap *map, unsigned int reg,
620 		     const void *val, size_t val_len)
621 {
622 	int ret;
623 
624 	if (val_len % map->format.val_bytes)
625 		return -EINVAL;
626 
627 	map->lock(map);
628 
629 	ret = _regmap_raw_write(map, reg, val, val_len);
630 
631 	map->unlock(map);
632 
633 	return ret;
634 }
635 EXPORT_SYMBOL_GPL(regmap_raw_write);
636 
637 /*
638  * regmap_bulk_write(): Write multiple registers to the device
639  *
640  * @map: Register map to write to
641  * @reg: First register to be write from
642  * @val: Block of data to be written, in native register size for device
643  * @val_count: Number of registers to write
644  *
645  * This function is intended to be used for writing a large block of
646  * data to be device either in single transfer or multiple transfer.
647  *
648  * A value of zero will be returned on success, a negative errno will
649  * be returned in error cases.
650  */
651 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
652 		     size_t val_count)
653 {
654 	int ret = 0, i;
655 	size_t val_bytes = map->format.val_bytes;
656 	void *wval;
657 
658 	if (!map->format.parse_val)
659 		return -EINVAL;
660 
661 	map->lock(map);
662 
663 	/* No formatting is require if val_byte is 1 */
664 	if (val_bytes == 1) {
665 		wval = (void *)val;
666 	} else {
667 		wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
668 		if (!wval) {
669 			ret = -ENOMEM;
670 			dev_err(map->dev, "Error in memory allocation\n");
671 			goto out;
672 		}
673 		for (i = 0; i < val_count * val_bytes; i += val_bytes)
674 			map->format.parse_val(wval + i);
675 	}
676 	ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
677 
678 	if (val_bytes != 1)
679 		kfree(wval);
680 
681 out:
682 	map->unlock(map);
683 	return ret;
684 }
685 EXPORT_SYMBOL_GPL(regmap_bulk_write);
686 
687 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
688 			    unsigned int val_len)
689 {
690 	u8 *u8 = map->work_buf;
691 	int ret;
692 
693 	map->format.format_reg(map->work_buf, reg, map->reg_shift);
694 
695 	/*
696 	 * Some buses or devices flag reads by setting the high bits in the
697 	 * register addresss; since it's always the high bits for all
698 	 * current formats we can do this here rather than in
699 	 * formatting.  This may break if we get interesting formats.
700 	 */
701 	u8[0] |= map->read_flag_mask;
702 
703 	trace_regmap_hw_read_start(map->dev, reg,
704 				   val_len / map->format.val_bytes);
705 
706 	ret = map->bus->read(map->bus_context, map->work_buf,
707 			     map->format.reg_bytes + map->format.pad_bytes,
708 			     val, val_len);
709 
710 	trace_regmap_hw_read_done(map->dev, reg,
711 				  val_len / map->format.val_bytes);
712 
713 	return ret;
714 }
715 
716 static int _regmap_read(struct regmap *map, unsigned int reg,
717 			unsigned int *val)
718 {
719 	int ret;
720 
721 	if (!map->cache_bypass) {
722 		ret = regcache_read(map, reg, val);
723 		if (ret == 0)
724 			return 0;
725 	}
726 
727 	if (!map->format.parse_val)
728 		return -EINVAL;
729 
730 	if (map->cache_only)
731 		return -EBUSY;
732 
733 	ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
734 	if (ret == 0) {
735 		*val = map->format.parse_val(map->work_buf);
736 		trace_regmap_reg_read(map->dev, reg, *val);
737 	}
738 
739 	return ret;
740 }
741 
742 /**
743  * regmap_read(): Read a value from a single register
744  *
745  * @map: Register map to write to
746  * @reg: Register to be read from
747  * @val: Pointer to store read value
748  *
749  * A value of zero will be returned on success, a negative errno will
750  * be returned in error cases.
751  */
752 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
753 {
754 	int ret;
755 
756 	map->lock(map);
757 
758 	ret = _regmap_read(map, reg, val);
759 
760 	map->unlock(map);
761 
762 	return ret;
763 }
764 EXPORT_SYMBOL_GPL(regmap_read);
765 
766 /**
767  * regmap_raw_read(): Read raw data from the device
768  *
769  * @map: Register map to write to
770  * @reg: First register to be read from
771  * @val: Pointer to store read value
772  * @val_len: Size of data to read
773  *
774  * A value of zero will be returned on success, a negative errno will
775  * be returned in error cases.
776  */
777 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
778 		    size_t val_len)
779 {
780 	size_t val_bytes = map->format.val_bytes;
781 	size_t val_count = val_len / val_bytes;
782 	unsigned int v;
783 	int ret, i;
784 
785 	if (val_len % map->format.val_bytes)
786 		return -EINVAL;
787 
788 	map->lock(map);
789 
790 	if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
791 	    map->cache_type == REGCACHE_NONE) {
792 		/* Physical block read if there's no cache involved */
793 		ret = _regmap_raw_read(map, reg, val, val_len);
794 
795 	} else {
796 		/* Otherwise go word by word for the cache; should be low
797 		 * cost as we expect to hit the cache.
798 		 */
799 		for (i = 0; i < val_count; i++) {
800 			ret = _regmap_read(map, reg + i, &v);
801 			if (ret != 0)
802 				goto out;
803 
804 			map->format.format_val(val + (i * val_bytes), v, 0);
805 		}
806 	}
807 
808  out:
809 	map->unlock(map);
810 
811 	return ret;
812 }
813 EXPORT_SYMBOL_GPL(regmap_raw_read);
814 
815 /**
816  * regmap_bulk_read(): Read multiple registers from the device
817  *
818  * @map: Register map to write to
819  * @reg: First register to be read from
820  * @val: Pointer to store read value, in native register size for device
821  * @val_count: Number of registers to read
822  *
823  * A value of zero will be returned on success, a negative errno will
824  * be returned in error cases.
825  */
826 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
827 		     size_t val_count)
828 {
829 	int ret, i;
830 	size_t val_bytes = map->format.val_bytes;
831 	bool vol = regmap_volatile_range(map, reg, val_count);
832 
833 	if (!map->format.parse_val)
834 		return -EINVAL;
835 
836 	if (vol || map->cache_type == REGCACHE_NONE) {
837 		ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
838 		if (ret != 0)
839 			return ret;
840 
841 		for (i = 0; i < val_count * val_bytes; i += val_bytes)
842 			map->format.parse_val(val + i);
843 	} else {
844 		for (i = 0; i < val_count; i++) {
845 			ret = regmap_read(map, reg + i, val + (i * val_bytes));
846 			if (ret != 0)
847 				return ret;
848 		}
849 	}
850 
851 	return 0;
852 }
853 EXPORT_SYMBOL_GPL(regmap_bulk_read);
854 
855 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
856 			       unsigned int mask, unsigned int val,
857 			       bool *change)
858 {
859 	int ret;
860 	unsigned int tmp, orig;
861 
862 	map->lock(map);
863 
864 	ret = _regmap_read(map, reg, &orig);
865 	if (ret != 0)
866 		goto out;
867 
868 	tmp = orig & ~mask;
869 	tmp |= val & mask;
870 
871 	if (tmp != orig) {
872 		ret = _regmap_write(map, reg, tmp);
873 		*change = true;
874 	} else {
875 		*change = false;
876 	}
877 
878 out:
879 	map->unlock(map);
880 
881 	return ret;
882 }
883 
884 /**
885  * regmap_update_bits: Perform a read/modify/write cycle on the register map
886  *
887  * @map: Register map to update
888  * @reg: Register to update
889  * @mask: Bitmask to change
890  * @val: New value for bitmask
891  *
892  * Returns zero for success, a negative number on error.
893  */
894 int regmap_update_bits(struct regmap *map, unsigned int reg,
895 		       unsigned int mask, unsigned int val)
896 {
897 	bool change;
898 	return _regmap_update_bits(map, reg, mask, val, &change);
899 }
900 EXPORT_SYMBOL_GPL(regmap_update_bits);
901 
902 /**
903  * regmap_update_bits_check: Perform a read/modify/write cycle on the
904  *                           register map and report if updated
905  *
906  * @map: Register map to update
907  * @reg: Register to update
908  * @mask: Bitmask to change
909  * @val: New value for bitmask
910  * @change: Boolean indicating if a write was done
911  *
912  * Returns zero for success, a negative number on error.
913  */
914 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
915 			     unsigned int mask, unsigned int val,
916 			     bool *change)
917 {
918 	return _regmap_update_bits(map, reg, mask, val, change);
919 }
920 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
921 
922 /**
923  * regmap_register_patch: Register and apply register updates to be applied
924  *                        on device initialistion
925  *
926  * @map: Register map to apply updates to.
927  * @regs: Values to update.
928  * @num_regs: Number of entries in regs.
929  *
930  * Register a set of register updates to be applied to the device
931  * whenever the device registers are synchronised with the cache and
932  * apply them immediately.  Typically this is used to apply
933  * corrections to be applied to the device defaults on startup, such
934  * as the updates some vendors provide to undocumented registers.
935  */
936 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
937 			  int num_regs)
938 {
939 	int i, ret;
940 	bool bypass;
941 
942 	/* If needed the implementation can be extended to support this */
943 	if (map->patch)
944 		return -EBUSY;
945 
946 	map->lock(map);
947 
948 	bypass = map->cache_bypass;
949 
950 	map->cache_bypass = true;
951 
952 	/* Write out first; it's useful to apply even if we fail later. */
953 	for (i = 0; i < num_regs; i++) {
954 		ret = _regmap_write(map, regs[i].reg, regs[i].def);
955 		if (ret != 0) {
956 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
957 				regs[i].reg, regs[i].def, ret);
958 			goto out;
959 		}
960 	}
961 
962 	map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
963 	if (map->patch != NULL) {
964 		memcpy(map->patch, regs,
965 		       num_regs * sizeof(struct reg_default));
966 		map->patch_regs = num_regs;
967 	} else {
968 		ret = -ENOMEM;
969 	}
970 
971 out:
972 	map->cache_bypass = bypass;
973 
974 	map->unlock(map);
975 
976 	return ret;
977 }
978 EXPORT_SYMBOL_GPL(regmap_register_patch);
979 
980 /*
981  * regmap_get_val_bytes(): Report the size of a register value
982  *
983  * Report the size of a register value, mainly intended to for use by
984  * generic infrastructure built on top of regmap.
985  */
986 int regmap_get_val_bytes(struct regmap *map)
987 {
988 	if (map->format.format_write)
989 		return -EINVAL;
990 
991 	return map->format.val_bytes;
992 }
993 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
994 
995 static int __init regmap_initcall(void)
996 {
997 	regmap_debugfs_initcall();
998 
999 	return 0;
1000 }
1001 postcore_initcall(regmap_initcall);
1002