regcache.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Register cache access API
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Dimitris Papastamos <dp@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. #include <linux/slab.h>
  13. #include <trace/events/regmap.h>
  14. #include "internal.h"
  15. static const struct regcache_ops *cache_types[] = {
  16. &regcache_indexed_ops,
  17. &regcache_rbtree_ops,
  18. };
  19. static int regcache_hw_init(struct regmap *map)
  20. {
  21. int i, j;
  22. int ret;
  23. int count;
  24. unsigned int val;
  25. void *tmp_buf;
  26. if (!map->num_reg_defaults_raw)
  27. return -EINVAL;
  28. if (!map->reg_defaults_raw) {
  29. dev_warn(map->dev, "No cache defaults, reading back from HW\n");
  30. tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
  31. if (!tmp_buf)
  32. return -EINVAL;
  33. ret = regmap_bulk_read(map, 0, tmp_buf,
  34. map->num_reg_defaults_raw);
  35. if (ret < 0) {
  36. kfree(tmp_buf);
  37. return ret;
  38. }
  39. map->reg_defaults_raw = tmp_buf;
  40. map->cache_free = 1;
  41. }
  42. /* calculate the size of reg_defaults */
  43. for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
  44. val = regcache_get_val(map->reg_defaults_raw,
  45. i, map->cache_word_size);
  46. if (!val)
  47. continue;
  48. count++;
  49. }
  50. map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
  51. GFP_KERNEL);
  52. if (!map->reg_defaults)
  53. return -ENOMEM;
  54. /* fill the reg_defaults */
  55. map->num_reg_defaults = count;
  56. for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
  57. val = regcache_get_val(map->reg_defaults_raw,
  58. i, map->cache_word_size);
  59. if (!val)
  60. continue;
  61. map->reg_defaults[j].reg = i;
  62. map->reg_defaults[j].def = val;
  63. j++;
  64. }
  65. return 0;
  66. }
  67. int regcache_init(struct regmap *map)
  68. {
  69. int ret;
  70. int i;
  71. void *tmp_buf;
  72. if (map->cache_type == REGCACHE_NONE)
  73. return 0;
  74. for (i = 0; i < ARRAY_SIZE(cache_types); i++)
  75. if (cache_types[i]->type == map->cache_type)
  76. break;
  77. if (i == ARRAY_SIZE(cache_types)) {
  78. dev_err(map->dev, "Could not match compress type: %d\n",
  79. map->cache_type);
  80. return -EINVAL;
  81. }
  82. map->cache = NULL;
  83. map->cache_ops = cache_types[i];
  84. if (!map->cache_ops->read ||
  85. !map->cache_ops->write ||
  86. !map->cache_ops->name)
  87. return -EINVAL;
  88. /* We still need to ensure that the reg_defaults
  89. * won't vanish from under us. We'll need to make
  90. * a copy of it.
  91. */
  92. if (map->reg_defaults) {
  93. if (!map->num_reg_defaults)
  94. return -EINVAL;
  95. tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
  96. sizeof(struct reg_default), GFP_KERNEL);
  97. if (!tmp_buf)
  98. return -ENOMEM;
  99. map->reg_defaults = tmp_buf;
  100. } else {
  101. /* Some devices such as PMIC's don't have cache defaults,
  102. * we cope with this by reading back the HW registers and
  103. * crafting the cache defaults by hand.
  104. */
  105. ret = regcache_hw_init(map);
  106. if (ret < 0)
  107. return ret;
  108. }
  109. if (!map->max_register)
  110. map->max_register = map->num_reg_defaults_raw;
  111. if (map->cache_ops->init) {
  112. dev_dbg(map->dev, "Initializing %s cache\n",
  113. map->cache_ops->name);
  114. return map->cache_ops->init(map);
  115. }
  116. return 0;
  117. }
  118. void regcache_exit(struct regmap *map)
  119. {
  120. if (map->cache_type == REGCACHE_NONE)
  121. return;
  122. BUG_ON(!map->cache_ops);
  123. kfree(map->reg_defaults);
  124. if (map->cache_free)
  125. kfree(map->reg_defaults_raw);
  126. if (map->cache_ops->exit) {
  127. dev_dbg(map->dev, "Destroying %s cache\n",
  128. map->cache_ops->name);
  129. map->cache_ops->exit(map);
  130. }
  131. }
  132. /**
  133. * regcache_read: Fetch the value of a given register from the cache.
  134. *
  135. * @map: map to configure.
  136. * @reg: The register index.
  137. * @value: The value to be returned.
  138. *
  139. * Return a negative value on failure, 0 on success.
  140. */
  141. int regcache_read(struct regmap *map,
  142. unsigned int reg, unsigned int *value)
  143. {
  144. if (map->cache_type == REGCACHE_NONE)
  145. return -ENOSYS;
  146. BUG_ON(!map->cache_ops);
  147. if (!regmap_readable(map, reg))
  148. return -EIO;
  149. if (!regmap_volatile(map, reg))
  150. return map->cache_ops->read(map, reg, value);
  151. return -EINVAL;
  152. }
  153. EXPORT_SYMBOL_GPL(regcache_read);
  154. /**
  155. * regcache_write: Set the value of a given register in the cache.
  156. *
  157. * @map: map to configure.
  158. * @reg: The register index.
  159. * @value: The new register value.
  160. *
  161. * Return a negative value on failure, 0 on success.
  162. */
  163. int regcache_write(struct regmap *map,
  164. unsigned int reg, unsigned int value)
  165. {
  166. if (map->cache_type == REGCACHE_NONE)
  167. return 0;
  168. BUG_ON(!map->cache_ops);
  169. if (!regmap_writeable(map, reg))
  170. return -EIO;
  171. if (!regmap_volatile(map, reg))
  172. return map->cache_ops->write(map, reg, value);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(regcache_write);
  176. /**
  177. * regcache_sync: Sync the register cache with the hardware.
  178. *
  179. * @map: map to configure.
  180. *
  181. * Any registers that should not be synced should be marked as
  182. * volatile. In general drivers can choose not to use the provided
  183. * syncing functionality if they so require.
  184. *
  185. * Return a negative value on failure, 0 on success.
  186. */
  187. int regcache_sync(struct regmap *map)
  188. {
  189. BUG_ON(!map->cache_ops);
  190. if (map->cache_ops->sync) {
  191. dev_dbg(map->dev, "Syncing %s cache\n",
  192. map->cache_ops->name);
  193. return map->cache_ops->sync(map);
  194. }
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(regcache_sync);
  198. bool regcache_set_val(void *base, unsigned int idx,
  199. unsigned int val, unsigned int word_size)
  200. {
  201. switch (word_size) {
  202. case 1: {
  203. u8 *cache = base;
  204. if (cache[idx] == val)
  205. return true;
  206. cache[idx] = val;
  207. break;
  208. }
  209. case 2: {
  210. u16 *cache = base;
  211. if (cache[idx] == val)
  212. return true;
  213. cache[idx] = val;
  214. break;
  215. }
  216. default:
  217. BUG();
  218. }
  219. /* unreachable */
  220. return false;
  221. }
  222. unsigned int regcache_get_val(const void *base, unsigned int idx,
  223. unsigned int word_size)
  224. {
  225. if (!base)
  226. return -EINVAL;
  227. switch (word_size) {
  228. case 1: {
  229. const u8 *cache = base;
  230. return cache[idx];
  231. }
  232. case 2: {
  233. const u16 *cache = base;
  234. return cache[idx];
  235. }
  236. default:
  237. BUG();
  238. }
  239. /* unreachable */
  240. return -1;
  241. }
  242. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  243. {
  244. unsigned int i;
  245. for (i = 0; i < map->num_reg_defaults; i++)
  246. if (map->reg_defaults[i].reg == reg)
  247. return i;
  248. return -1;
  249. }
  250. int regcache_insert_reg(struct regmap *map, unsigned int reg,
  251. unsigned int val)
  252. {
  253. void *tmp;
  254. tmp = krealloc(map->reg_defaults,
  255. (map->num_reg_defaults + 1) * sizeof(struct reg_default),
  256. GFP_KERNEL);
  257. if (!tmp)
  258. return -ENOMEM;
  259. map->reg_defaults = tmp;
  260. map->num_reg_defaults++;
  261. map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
  262. map->reg_defaults[map->num_reg_defaults - 1].def = val;
  263. return 0;
  264. }