regcache.c 6.4 KB

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