regmap: mmio: Staticize regmap_mmio_gen_context()
[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_be(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_16_native(void *buf, unsigned int val,
130 unsigned int shift)
131 {
132 *(u16 *)buf = val << shift;
133 }
134
135 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
136 {
137 u8 *b = buf;
138
139 val <<= shift;
140
141 b[0] = val >> 16;
142 b[1] = val >> 8;
143 b[2] = val;
144 }
145
146 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
147 {
148 __be32 *b = buf;
149
150 b[0] = cpu_to_be32(val << shift);
151 }
152
153 static void regmap_format_32_native(void *buf, unsigned int val,
154 unsigned int shift)
155 {
156 *(u32 *)buf = val << shift;
157 }
158
159 static unsigned int regmap_parse_8(void *buf)
160 {
161 u8 *b = buf;
162
163 return b[0];
164 }
165
166 static unsigned int regmap_parse_16_be(void *buf)
167 {
168 __be16 *b = buf;
169
170 b[0] = be16_to_cpu(b[0]);
171
172 return b[0];
173 }
174
175 static unsigned int regmap_parse_16_native(void *buf)
176 {
177 return *(u16 *)buf;
178 }
179
180 static unsigned int regmap_parse_24(void *buf)
181 {
182 u8 *b = buf;
183 unsigned int ret = b[2];
184 ret |= ((unsigned int)b[1]) << 8;
185 ret |= ((unsigned int)b[0]) << 16;
186
187 return ret;
188 }
189
190 static unsigned int regmap_parse_32_be(void *buf)
191 {
192 __be32 *b = buf;
193
194 b[0] = be32_to_cpu(b[0]);
195
196 return b[0];
197 }
198
199 static unsigned int regmap_parse_32_native(void *buf)
200 {
201 return *(u32 *)buf;
202 }
203
204 static void regmap_lock_mutex(struct regmap *map)
205 {
206 mutex_lock(&map->mutex);
207 }
208
209 static void regmap_unlock_mutex(struct regmap *map)
210 {
211 mutex_unlock(&map->mutex);
212 }
213
214 static void regmap_lock_spinlock(struct regmap *map)
215 {
216 spin_lock(&map->spinlock);
217 }
218
219 static void regmap_unlock_spinlock(struct regmap *map)
220 {
221 spin_unlock(&map->spinlock);
222 }
223
224 static void dev_get_regmap_release(struct device *dev, void *res)
225 {
226 /*
227 * We don't actually have anything to do here; the goal here
228 * is not to manage the regmap but to provide a simple way to
229 * get the regmap back given a struct device.
230 */
231 }
232
233 /**
234 * regmap_init(): Initialise register map
235 *
236 * @dev: Device that will be interacted with
237 * @bus: Bus-specific callbacks to use with device
238 * @bus_context: Data passed to bus-specific callbacks
239 * @config: Configuration for register map
240 *
241 * The return value will be an ERR_PTR() on error or a valid pointer to
242 * a struct regmap. This function should generally not be called
243 * directly, it should be called by bus-specific init functions.
244 */
245 struct regmap *regmap_init(struct device *dev,
246 const struct regmap_bus *bus,
247 void *bus_context,
248 const struct regmap_config *config)
249 {
250 struct regmap *map, **m;
251 int ret = -EINVAL;
252 enum regmap_endian reg_endian, val_endian;
253
254 if (!bus || !config)
255 goto err;
256
257 map = kzalloc(sizeof(*map), GFP_KERNEL);
258 if (map == NULL) {
259 ret = -ENOMEM;
260 goto err;
261 }
262
263 if (bus->fast_io) {
264 spin_lock_init(&map->spinlock);
265 map->lock = regmap_lock_spinlock;
266 map->unlock = regmap_unlock_spinlock;
267 } else {
268 mutex_init(&map->mutex);
269 map->lock = regmap_lock_mutex;
270 map->unlock = regmap_unlock_mutex;
271 }
272 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
273 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
274 map->format.pad_bytes = config->pad_bits / 8;
275 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
276 map->format.buf_size += map->format.pad_bytes;
277 map->reg_shift = config->pad_bits % 8;
278 if (config->reg_stride)
279 map->reg_stride = config->reg_stride;
280 else
281 map->reg_stride = 1;
282 map->use_single_rw = config->use_single_rw;
283 map->dev = dev;
284 map->bus = bus;
285 map->bus_context = bus_context;
286 map->max_register = config->max_register;
287 map->writeable_reg = config->writeable_reg;
288 map->readable_reg = config->readable_reg;
289 map->volatile_reg = config->volatile_reg;
290 map->precious_reg = config->precious_reg;
291 map->cache_type = config->cache_type;
292 map->name = config->name;
293
294 if (config->read_flag_mask || config->write_flag_mask) {
295 map->read_flag_mask = config->read_flag_mask;
296 map->write_flag_mask = config->write_flag_mask;
297 } else {
298 map->read_flag_mask = bus->read_flag_mask;
299 }
300
301 reg_endian = config->reg_format_endian;
302 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
303 reg_endian = bus->reg_format_endian_default;
304 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
305 reg_endian = REGMAP_ENDIAN_BIG;
306
307 val_endian = config->val_format_endian;
308 if (val_endian == REGMAP_ENDIAN_DEFAULT)
309 val_endian = bus->val_format_endian_default;
310 if (val_endian == REGMAP_ENDIAN_DEFAULT)
311 val_endian = REGMAP_ENDIAN_BIG;
312
313 switch (config->reg_bits + map->reg_shift) {
314 case 2:
315 switch (config->val_bits) {
316 case 6:
317 map->format.format_write = regmap_format_2_6_write;
318 break;
319 default:
320 goto err_map;
321 }
322 break;
323
324 case 4:
325 switch (config->val_bits) {
326 case 12:
327 map->format.format_write = regmap_format_4_12_write;
328 break;
329 default:
330 goto err_map;
331 }
332 break;
333
334 case 7:
335 switch (config->val_bits) {
336 case 9:
337 map->format.format_write = regmap_format_7_9_write;
338 break;
339 default:
340 goto err_map;
341 }
342 break;
343
344 case 10:
345 switch (config->val_bits) {
346 case 14:
347 map->format.format_write = regmap_format_10_14_write;
348 break;
349 default:
350 goto err_map;
351 }
352 break;
353
354 case 8:
355 map->format.format_reg = regmap_format_8;
356 break;
357
358 case 16:
359 switch (reg_endian) {
360 case REGMAP_ENDIAN_BIG:
361 map->format.format_reg = regmap_format_16_be;
362 break;
363 case REGMAP_ENDIAN_NATIVE:
364 map->format.format_reg = regmap_format_16_native;
365 break;
366 default:
367 goto err_map;
368 }
369 break;
370
371 case 32:
372 switch (reg_endian) {
373 case REGMAP_ENDIAN_BIG:
374 map->format.format_reg = regmap_format_32_be;
375 break;
376 case REGMAP_ENDIAN_NATIVE:
377 map->format.format_reg = regmap_format_32_native;
378 break;
379 default:
380 goto err_map;
381 }
382 break;
383
384 default:
385 goto err_map;
386 }
387
388 switch (config->val_bits) {
389 case 8:
390 map->format.format_val = regmap_format_8;
391 map->format.parse_val = regmap_parse_8;
392 break;
393 case 16:
394 switch (val_endian) {
395 case REGMAP_ENDIAN_BIG:
396 map->format.format_val = regmap_format_16_be;
397 map->format.parse_val = regmap_parse_16_be;
398 break;
399 case REGMAP_ENDIAN_NATIVE:
400 map->format.format_val = regmap_format_16_native;
401 map->format.parse_val = regmap_parse_16_native;
402 break;
403 default:
404 goto err_map;
405 }
406 break;
407 case 24:
408 if (val_endian != REGMAP_ENDIAN_BIG)
409 goto err_map;
410 map->format.format_val = regmap_format_24;
411 map->format.parse_val = regmap_parse_24;
412 break;
413 case 32:
414 switch (val_endian) {
415 case REGMAP_ENDIAN_BIG:
416 map->format.format_val = regmap_format_32_be;
417 map->format.parse_val = regmap_parse_32_be;
418 break;
419 case REGMAP_ENDIAN_NATIVE:
420 map->format.format_val = regmap_format_32_native;
421 map->format.parse_val = regmap_parse_32_native;
422 break;
423 default:
424 goto err_map;
425 }
426 break;
427 }
428
429 if (map->format.format_write) {
430 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
431 (val_endian != REGMAP_ENDIAN_BIG))
432 goto err_map;
433 map->use_single_rw = true;
434 }
435
436 if (!map->format.format_write &&
437 !(map->format.format_reg && map->format.format_val))
438 goto err_map;
439
440 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
441 if (map->work_buf == NULL) {
442 ret = -ENOMEM;
443 goto err_map;
444 }
445
446 regmap_debugfs_init(map, config->name);
447
448 ret = regcache_init(map, config);
449 if (ret < 0)
450 goto err_free_workbuf;
451
452 /* Add a devres resource for dev_get_regmap() */
453 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
454 if (!m) {
455 ret = -ENOMEM;
456 goto err_cache;
457 }
458 *m = map;
459 devres_add(dev, m);
460
461 return map;
462
463 err_cache:
464 regcache_exit(map);
465 err_free_workbuf:
466 kfree(map->work_buf);
467 err_map:
468 kfree(map);
469 err:
470 return ERR_PTR(ret);
471 }
472 EXPORT_SYMBOL_GPL(regmap_init);
473
474 static void devm_regmap_release(struct device *dev, void *res)
475 {
476 regmap_exit(*(struct regmap **)res);
477 }
478
479 /**
480 * devm_regmap_init(): Initialise managed register map
481 *
482 * @dev: Device that will be interacted with
483 * @bus: Bus-specific callbacks to use with device
484 * @bus_context: Data passed to bus-specific callbacks
485 * @config: Configuration for register map
486 *
487 * The return value will be an ERR_PTR() on error or a valid pointer
488 * to a struct regmap. This function should generally not be called
489 * directly, it should be called by bus-specific init functions. The
490 * map will be automatically freed by the device management code.
491 */
492 struct regmap *devm_regmap_init(struct device *dev,
493 const struct regmap_bus *bus,
494 void *bus_context,
495 const struct regmap_config *config)
496 {
497 struct regmap **ptr, *regmap;
498
499 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
500 if (!ptr)
501 return ERR_PTR(-ENOMEM);
502
503 regmap = regmap_init(dev, bus, bus_context, config);
504 if (!IS_ERR(regmap)) {
505 *ptr = regmap;
506 devres_add(dev, ptr);
507 } else {
508 devres_free(ptr);
509 }
510
511 return regmap;
512 }
513 EXPORT_SYMBOL_GPL(devm_regmap_init);
514
515 /**
516 * regmap_reinit_cache(): Reinitialise the current register cache
517 *
518 * @map: Register map to operate on.
519 * @config: New configuration. Only the cache data will be used.
520 *
521 * Discard any existing register cache for the map and initialize a
522 * new cache. This can be used to restore the cache to defaults or to
523 * update the cache configuration to reflect runtime discovery of the
524 * hardware.
525 */
526 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
527 {
528 int ret;
529
530 map->lock(map);
531
532 regcache_exit(map);
533 regmap_debugfs_exit(map);
534
535 map->max_register = config->max_register;
536 map->writeable_reg = config->writeable_reg;
537 map->readable_reg = config->readable_reg;
538 map->volatile_reg = config->volatile_reg;
539 map->precious_reg = config->precious_reg;
540 map->cache_type = config->cache_type;
541
542 regmap_debugfs_init(map, config->name);
543
544 map->cache_bypass = false;
545 map->cache_only = false;
546
547 ret = regcache_init(map, config);
548
549 map->unlock(map);
550
551 return ret;
552 }
553
554 /**
555 * regmap_exit(): Free a previously allocated register map
556 */
557 void regmap_exit(struct regmap *map)
558 {
559 regcache_exit(map);
560 regmap_debugfs_exit(map);
561 if (map->bus->free_context)
562 map->bus->free_context(map->bus_context);
563 kfree(map->work_buf);
564 kfree(map);
565 }
566 EXPORT_SYMBOL_GPL(regmap_exit);
567
568 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
569 {
570 struct regmap **r = res;
571 if (!r || !*r) {
572 WARN_ON(!r || !*r);
573 return 0;
574 }
575
576 /* If the user didn't specify a name match any */
577 if (data)
578 return (*r)->name == data;
579 else
580 return 1;
581 }
582
583 /**
584 * dev_get_regmap(): Obtain the regmap (if any) for a device
585 *
586 * @dev: Device to retrieve the map for
587 * @name: Optional name for the register map, usually NULL.
588 *
589 * Returns the regmap for the device if one is present, or NULL. If
590 * name is specified then it must match the name specified when
591 * registering the device, if it is NULL then the first regmap found
592 * will be used. Devices with multiple register maps are very rare,
593 * generic code should normally not need to specify a name.
594 */
595 struct regmap *dev_get_regmap(struct device *dev, const char *name)
596 {
597 struct regmap **r = devres_find(dev, dev_get_regmap_release,
598 dev_get_regmap_match, (void *)name);
599
600 if (!r)
601 return NULL;
602 return *r;
603 }
604 EXPORT_SYMBOL_GPL(dev_get_regmap);
605
606 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
607 const void *val, size_t val_len)
608 {
609 u8 *u8 = map->work_buf;
610 void *buf;
611 int ret = -ENOTSUPP;
612 size_t len;
613 int i;
614
615 /* Check for unwritable registers before we start */
616 if (map->writeable_reg)
617 for (i = 0; i < val_len / map->format.val_bytes; i++)
618 if (!map->writeable_reg(map->dev,
619 reg + (i * map->reg_stride)))
620 return -EINVAL;
621
622 if (!map->cache_bypass && map->format.parse_val) {
623 unsigned int ival;
624 int val_bytes = map->format.val_bytes;
625 for (i = 0; i < val_len / val_bytes; i++) {
626 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
627 ival = map->format.parse_val(map->work_buf);
628 ret = regcache_write(map, reg + (i * map->reg_stride),
629 ival);
630 if (ret) {
631 dev_err(map->dev,
632 "Error in caching of register: %u ret: %d\n",
633 reg + i, ret);
634 return ret;
635 }
636 }
637 if (map->cache_only) {
638 map->cache_dirty = true;
639 return 0;
640 }
641 }
642
643 map->format.format_reg(map->work_buf, reg, map->reg_shift);
644
645 u8[0] |= map->write_flag_mask;
646
647 trace_regmap_hw_write_start(map->dev, reg,
648 val_len / map->format.val_bytes);
649
650 /* If we're doing a single register write we can probably just
651 * send the work_buf directly, otherwise try to do a gather
652 * write.
653 */
654 if (val == (map->work_buf + map->format.pad_bytes +
655 map->format.reg_bytes))
656 ret = map->bus->write(map->bus_context, map->work_buf,
657 map->format.reg_bytes +
658 map->format.pad_bytes +
659 val_len);
660 else if (map->bus->gather_write)
661 ret = map->bus->gather_write(map->bus_context, map->work_buf,
662 map->format.reg_bytes +
663 map->format.pad_bytes,
664 val, val_len);
665
666 /* If that didn't work fall back on linearising by hand. */
667 if (ret == -ENOTSUPP) {
668 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
669 buf = kzalloc(len, GFP_KERNEL);
670 if (!buf)
671 return -ENOMEM;
672
673 memcpy(buf, map->work_buf, map->format.reg_bytes);
674 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
675 val, val_len);
676 ret = map->bus->write(map->bus_context, buf, len);
677
678 kfree(buf);
679 }
680
681 trace_regmap_hw_write_done(map->dev, reg,
682 val_len / map->format.val_bytes);
683
684 return ret;
685 }
686
687 int _regmap_write(struct regmap *map, unsigned int reg,
688 unsigned int val)
689 {
690 int ret;
691 BUG_ON(!map->format.format_write && !map->format.format_val);
692
693 if (!map->cache_bypass && map->format.format_write) {
694 ret = regcache_write(map, reg, val);
695 if (ret != 0)
696 return ret;
697 if (map->cache_only) {
698 map->cache_dirty = true;
699 return 0;
700 }
701 }
702
703 trace_regmap_reg_write(map->dev, reg, val);
704
705 if (map->format.format_write) {
706 map->format.format_write(map, reg, val);
707
708 trace_regmap_hw_write_start(map->dev, reg, 1);
709
710 ret = map->bus->write(map->bus_context, map->work_buf,
711 map->format.buf_size);
712
713 trace_regmap_hw_write_done(map->dev, reg, 1);
714
715 return ret;
716 } else {
717 map->format.format_val(map->work_buf + map->format.reg_bytes
718 + map->format.pad_bytes, val, 0);
719 return _regmap_raw_write(map, reg,
720 map->work_buf +
721 map->format.reg_bytes +
722 map->format.pad_bytes,
723 map->format.val_bytes);
724 }
725 }
726
727 /**
728 * regmap_write(): Write a value to a single register
729 *
730 * @map: Register map to write to
731 * @reg: Register to write to
732 * @val: Value to be written
733 *
734 * A value of zero will be returned on success, a negative errno will
735 * be returned in error cases.
736 */
737 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
738 {
739 int ret;
740
741 if (reg % map->reg_stride)
742 return -EINVAL;
743
744 map->lock(map);
745
746 ret = _regmap_write(map, reg, val);
747
748 map->unlock(map);
749
750 return ret;
751 }
752 EXPORT_SYMBOL_GPL(regmap_write);
753
754 /**
755 * regmap_raw_write(): Write raw values to one or more registers
756 *
757 * @map: Register map to write to
758 * @reg: Initial register to write to
759 * @val: Block of data to be written, laid out for direct transmission to the
760 * device
761 * @val_len: Length of data pointed to by val.
762 *
763 * This function is intended to be used for things like firmware
764 * download where a large block of data needs to be transferred to the
765 * device. No formatting will be done on the data provided.
766 *
767 * A value of zero will be returned on success, a negative errno will
768 * be returned in error cases.
769 */
770 int regmap_raw_write(struct regmap *map, unsigned int reg,
771 const void *val, size_t val_len)
772 {
773 int ret;
774
775 if (val_len % map->format.val_bytes)
776 return -EINVAL;
777 if (reg % map->reg_stride)
778 return -EINVAL;
779
780 map->lock(map);
781
782 ret = _regmap_raw_write(map, reg, val, val_len);
783
784 map->unlock(map);
785
786 return ret;
787 }
788 EXPORT_SYMBOL_GPL(regmap_raw_write);
789
790 /*
791 * regmap_bulk_write(): Write multiple registers to the device
792 *
793 * @map: Register map to write to
794 * @reg: First register to be write from
795 * @val: Block of data to be written, in native register size for device
796 * @val_count: Number of registers to write
797 *
798 * This function is intended to be used for writing a large block of
799 * data to be device either in single transfer or multiple transfer.
800 *
801 * A value of zero will be returned on success, a negative errno will
802 * be returned in error cases.
803 */
804 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
805 size_t val_count)
806 {
807 int ret = 0, i;
808 size_t val_bytes = map->format.val_bytes;
809 void *wval;
810
811 if (!map->format.parse_val)
812 return -EINVAL;
813 if (reg % map->reg_stride)
814 return -EINVAL;
815
816 map->lock(map);
817
818 /* No formatting is require if val_byte is 1 */
819 if (val_bytes == 1) {
820 wval = (void *)val;
821 } else {
822 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
823 if (!wval) {
824 ret = -ENOMEM;
825 dev_err(map->dev, "Error in memory allocation\n");
826 goto out;
827 }
828 for (i = 0; i < val_count * val_bytes; i += val_bytes)
829 map->format.parse_val(wval + i);
830 }
831 /*
832 * Some devices does not support bulk write, for
833 * them we have a series of single write operations.
834 */
835 if (map->use_single_rw) {
836 for (i = 0; i < val_count; i++) {
837 ret = regmap_raw_write(map,
838 reg + (i * map->reg_stride),
839 val + (i * val_bytes),
840 val_bytes);
841 if (ret != 0)
842 return ret;
843 }
844 } else {
845 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
846 }
847
848 if (val_bytes != 1)
849 kfree(wval);
850
851 out:
852 map->unlock(map);
853 return ret;
854 }
855 EXPORT_SYMBOL_GPL(regmap_bulk_write);
856
857 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
858 unsigned int val_len)
859 {
860 u8 *u8 = map->work_buf;
861 int ret;
862
863 map->format.format_reg(map->work_buf, reg, map->reg_shift);
864
865 /*
866 * Some buses or devices flag reads by setting the high bits in the
867 * register addresss; since it's always the high bits for all
868 * current formats we can do this here rather than in
869 * formatting. This may break if we get interesting formats.
870 */
871 u8[0] |= map->read_flag_mask;
872
873 trace_regmap_hw_read_start(map->dev, reg,
874 val_len / map->format.val_bytes);
875
876 ret = map->bus->read(map->bus_context, map->work_buf,
877 map->format.reg_bytes + map->format.pad_bytes,
878 val, val_len);
879
880 trace_regmap_hw_read_done(map->dev, reg,
881 val_len / map->format.val_bytes);
882
883 return ret;
884 }
885
886 static int _regmap_read(struct regmap *map, unsigned int reg,
887 unsigned int *val)
888 {
889 int ret;
890
891 if (!map->cache_bypass) {
892 ret = regcache_read(map, reg, val);
893 if (ret == 0)
894 return 0;
895 }
896
897 if (!map->format.parse_val)
898 return -EINVAL;
899
900 if (map->cache_only)
901 return -EBUSY;
902
903 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
904 if (ret == 0) {
905 *val = map->format.parse_val(map->work_buf);
906 trace_regmap_reg_read(map->dev, reg, *val);
907 }
908
909 if (ret == 0 && !map->cache_bypass)
910 regcache_write(map, reg, *val);
911
912 return ret;
913 }
914
915 /**
916 * regmap_read(): Read a value from a single register
917 *
918 * @map: Register map to write to
919 * @reg: Register to be read from
920 * @val: Pointer to store read value
921 *
922 * A value of zero will be returned on success, a negative errno will
923 * be returned in error cases.
924 */
925 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
926 {
927 int ret;
928
929 if (reg % map->reg_stride)
930 return -EINVAL;
931
932 map->lock(map);
933
934 ret = _regmap_read(map, reg, val);
935
936 map->unlock(map);
937
938 return ret;
939 }
940 EXPORT_SYMBOL_GPL(regmap_read);
941
942 /**
943 * regmap_raw_read(): Read raw data from the device
944 *
945 * @map: Register map to write to
946 * @reg: First register to be read from
947 * @val: Pointer to store read value
948 * @val_len: Size of data to read
949 *
950 * A value of zero will be returned on success, a negative errno will
951 * be returned in error cases.
952 */
953 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
954 size_t val_len)
955 {
956 size_t val_bytes = map->format.val_bytes;
957 size_t val_count = val_len / val_bytes;
958 unsigned int v;
959 int ret, i;
960
961 if (val_len % map->format.val_bytes)
962 return -EINVAL;
963 if (reg % map->reg_stride)
964 return -EINVAL;
965
966 map->lock(map);
967
968 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
969 map->cache_type == REGCACHE_NONE) {
970 /* Physical block read if there's no cache involved */
971 ret = _regmap_raw_read(map, reg, val, val_len);
972
973 } else {
974 /* Otherwise go word by word for the cache; should be low
975 * cost as we expect to hit the cache.
976 */
977 for (i = 0; i < val_count; i++) {
978 ret = _regmap_read(map, reg + (i * map->reg_stride),
979 &v);
980 if (ret != 0)
981 goto out;
982
983 map->format.format_val(val + (i * val_bytes), v, 0);
984 }
985 }
986
987 out:
988 map->unlock(map);
989
990 return ret;
991 }
992 EXPORT_SYMBOL_GPL(regmap_raw_read);
993
994 /**
995 * regmap_bulk_read(): Read multiple registers from the device
996 *
997 * @map: Register map to write to
998 * @reg: First register to be read from
999 * @val: Pointer to store read value, in native register size for device
1000 * @val_count: Number of registers to read
1001 *
1002 * A value of zero will be returned on success, a negative errno will
1003 * be returned in error cases.
1004 */
1005 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1006 size_t val_count)
1007 {
1008 int ret, i;
1009 size_t val_bytes = map->format.val_bytes;
1010 bool vol = regmap_volatile_range(map, reg, val_count);
1011
1012 if (!map->format.parse_val)
1013 return -EINVAL;
1014 if (reg % map->reg_stride)
1015 return -EINVAL;
1016
1017 if (vol || map->cache_type == REGCACHE_NONE) {
1018 /*
1019 * Some devices does not support bulk read, for
1020 * them we have a series of single read operations.
1021 */
1022 if (map->use_single_rw) {
1023 for (i = 0; i < val_count; i++) {
1024 ret = regmap_raw_read(map,
1025 reg + (i * map->reg_stride),
1026 val + (i * val_bytes),
1027 val_bytes);
1028 if (ret != 0)
1029 return ret;
1030 }
1031 } else {
1032 ret = regmap_raw_read(map, reg, val,
1033 val_bytes * val_count);
1034 if (ret != 0)
1035 return ret;
1036 }
1037
1038 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1039 map->format.parse_val(val + i);
1040 } else {
1041 for (i = 0; i < val_count; i++) {
1042 unsigned int ival;
1043 ret = regmap_read(map, reg + (i * map->reg_stride),
1044 &ival);
1045 if (ret != 0)
1046 return ret;
1047 memcpy(val + (i * val_bytes), &ival, val_bytes);
1048 }
1049 }
1050
1051 return 0;
1052 }
1053 EXPORT_SYMBOL_GPL(regmap_bulk_read);
1054
1055 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1056 unsigned int mask, unsigned int val,
1057 bool *change)
1058 {
1059 int ret;
1060 unsigned int tmp, orig;
1061
1062 map->lock(map);
1063
1064 ret = _regmap_read(map, reg, &orig);
1065 if (ret != 0)
1066 goto out;
1067
1068 tmp = orig & ~mask;
1069 tmp |= val & mask;
1070
1071 if (tmp != orig) {
1072 ret = _regmap_write(map, reg, tmp);
1073 *change = true;
1074 } else {
1075 *change = false;
1076 }
1077
1078 out:
1079 map->unlock(map);
1080
1081 return ret;
1082 }
1083
1084 /**
1085 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1086 *
1087 * @map: Register map to update
1088 * @reg: Register to update
1089 * @mask: Bitmask to change
1090 * @val: New value for bitmask
1091 *
1092 * Returns zero for success, a negative number on error.
1093 */
1094 int regmap_update_bits(struct regmap *map, unsigned int reg,
1095 unsigned int mask, unsigned int val)
1096 {
1097 bool change;
1098 return _regmap_update_bits(map, reg, mask, val, &change);
1099 }
1100 EXPORT_SYMBOL_GPL(regmap_update_bits);
1101
1102 /**
1103 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1104 * register map and report if updated
1105 *
1106 * @map: Register map to update
1107 * @reg: Register to update
1108 * @mask: Bitmask to change
1109 * @val: New value for bitmask
1110 * @change: Boolean indicating if a write was done
1111 *
1112 * Returns zero for success, a negative number on error.
1113 */
1114 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1115 unsigned int mask, unsigned int val,
1116 bool *change)
1117 {
1118 return _regmap_update_bits(map, reg, mask, val, change);
1119 }
1120 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1121
1122 /**
1123 * regmap_register_patch: Register and apply register updates to be applied
1124 * on device initialistion
1125 *
1126 * @map: Register map to apply updates to.
1127 * @regs: Values to update.
1128 * @num_regs: Number of entries in regs.
1129 *
1130 * Register a set of register updates to be applied to the device
1131 * whenever the device registers are synchronised with the cache and
1132 * apply them immediately. Typically this is used to apply
1133 * corrections to be applied to the device defaults on startup, such
1134 * as the updates some vendors provide to undocumented registers.
1135 */
1136 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1137 int num_regs)
1138 {
1139 int i, ret;
1140 bool bypass;
1141
1142 /* If needed the implementation can be extended to support this */
1143 if (map->patch)
1144 return -EBUSY;
1145
1146 map->lock(map);
1147
1148 bypass = map->cache_bypass;
1149
1150 map->cache_bypass = true;
1151
1152 /* Write out first; it's useful to apply even if we fail later. */
1153 for (i = 0; i < num_regs; i++) {
1154 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1155 if (ret != 0) {
1156 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1157 regs[i].reg, regs[i].def, ret);
1158 goto out;
1159 }
1160 }
1161
1162 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
1163 if (map->patch != NULL) {
1164 memcpy(map->patch, regs,
1165 num_regs * sizeof(struct reg_default));
1166 map->patch_regs = num_regs;
1167 } else {
1168 ret = -ENOMEM;
1169 }
1170
1171 out:
1172 map->cache_bypass = bypass;
1173
1174 map->unlock(map);
1175
1176 return ret;
1177 }
1178 EXPORT_SYMBOL_GPL(regmap_register_patch);
1179
1180 /*
1181 * regmap_get_val_bytes(): Report the size of a register value
1182 *
1183 * Report the size of a register value, mainly intended to for use by
1184 * generic infrastructure built on top of regmap.
1185 */
1186 int regmap_get_val_bytes(struct regmap *map)
1187 {
1188 if (map->format.format_write)
1189 return -EINVAL;
1190
1191 return map->format.val_bytes;
1192 }
1193 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1194
1195 static int __init regmap_initcall(void)
1196 {
1197 regmap_debugfs_initcall();
1198
1199 return 0;
1200 }
1201 postcore_initcall(regmap_initcall);
This page took 0.057336 seconds and 6 git commands to generate.