Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Register cache access API - flat caching support |
| 3 | * |
| 4 | * Copyright 2012 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 | |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/seq_file.h> |
Xiubo Li | e39be3a | 2014-10-09 17:02:52 +0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 16 | |
| 17 | #include "internal.h" |
| 18 | |
Xiubo Li | ce11020 | 2016-01-04 18:00:35 +0800 | [diff] [blame] | 19 | static inline unsigned int regcache_flat_get_index(const struct regmap *map, |
| 20 | unsigned int reg) |
| 21 | { |
| 22 | return regcache_get_index_by_order(map, reg); |
| 23 | } |
| 24 | |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 25 | static int regcache_flat_init(struct regmap *map) |
| 26 | { |
| 27 | int i; |
| 28 | unsigned int *cache; |
| 29 | |
Alexander Stein | 6e4f287 | 2016-03-29 08:53:32 +0200 | [diff] [blame] | 30 | if (!map || map->reg_stride_order < 0 || !map->max_register) |
Xiubo Li | ce11020 | 2016-01-04 18:00:35 +0800 | [diff] [blame] | 31 | return -EINVAL; |
| 32 | |
| 33 | map->cache = kcalloc(regcache_flat_get_index(map, map->max_register) |
| 34 | + 1, sizeof(unsigned int), GFP_KERNEL); |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 35 | if (!map->cache) |
| 36 | return -ENOMEM; |
| 37 | |
| 38 | cache = map->cache; |
| 39 | |
| 40 | for (i = 0; i < map->num_reg_defaults; i++) |
Xiubo Li | ce11020 | 2016-01-04 18:00:35 +0800 | [diff] [blame] | 41 | cache[regcache_flat_get_index(map, map->reg_defaults[i].reg)] = |
| 42 | map->reg_defaults[i].def; |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int regcache_flat_exit(struct regmap *map) |
| 48 | { |
| 49 | kfree(map->cache); |
| 50 | map->cache = NULL; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int regcache_flat_read(struct regmap *map, |
| 56 | unsigned int reg, unsigned int *value) |
| 57 | { |
| 58 | unsigned int *cache = map->cache; |
| 59 | |
Xiubo Li | ce11020 | 2016-01-04 18:00:35 +0800 | [diff] [blame] | 60 | *value = cache[regcache_flat_get_index(map, reg)]; |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int regcache_flat_write(struct regmap *map, unsigned int reg, |
| 66 | unsigned int value) |
| 67 | { |
| 68 | unsigned int *cache = map->cache; |
| 69 | |
Xiubo Li | ce11020 | 2016-01-04 18:00:35 +0800 | [diff] [blame] | 70 | cache[regcache_flat_get_index(map, reg)] = value; |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | struct regcache_ops regcache_flat_ops = { |
| 76 | .type = REGCACHE_FLAT, |
| 77 | .name = "flat", |
| 78 | .init = regcache_flat_init, |
| 79 | .exit = regcache_flat_exit, |
| 80 | .read = regcache_flat_read, |
| 81 | .write = regcache_flat_write, |
| 82 | }; |