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