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