regcache.c 6.4 KB

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