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